func (S) TestLineIsolatesEnv(c *C) { p := pipe.Line( pipe.SetEnvVar("PIPE_VAR", "outer"), pipe.Line( pipe.SetEnvVar("PIPE_VAR", "inner"), ), pipe.System("echo $PIPE_VAR"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "outer\n") }
func (S) TestLineNesting(c *C) { b := &bytes.Buffer{} p := pipe.Line( pipe.Print("hello"), pipe.Line( pipe.Filter(func(line []byte) bool { return true }), pipe.Exec("sed", "s/l/k/g"), ), pipe.Write(b), ) err := pipe.Run(p) c.Assert(err, IsNil) c.Assert(b.String(), Equals, "hekko") }
func (S) TestLineIsolatesDir(c *C) { dir1 := c.MkDir() dir2 := c.MkDir() p := pipe.Line( pipe.ChDir(dir1), pipe.Line( pipe.ChDir(dir2), ), pipe.System("echo $PWD"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, dir1+"\n") }
// Pipe connects the output of one Executable to the input of the next // Executable in the list. The result is an Executable that, when run, returns // the output of the last Executable run, and any error it might have had. // // If any of the Executables fails, no further Executables are run, and the // failing Executable's stderr and error are returned. func Pipe(cmds ...Executable) Executable { ps := make([]pipe.Pipe, len(cmds)) for i, c := range cmds { ps[i] = c.Pipe } return Executable{pipe.Line(ps...)} }
// PipeWith functions like Pipe, but runs the first command with stdin as the // input. func PipeWith(stdin string, cmds ...Executable) Executable { ps := make([]pipe.Pipe, len(cmds)+1) ps[0] = pipe.Read(strings.NewReader(stdin)) for i, c := range cmds { ps[i+1] = c.Pipe } return Executable{pipe.Line(ps...)} }
func (S) TestFilter(c *C) { p := pipe.Line( pipe.System("echo out1; echo err1 1>&2; echo out2; echo err2 1>&2; echo out3"), pipe.Filter(func(line []byte) bool { return string(line) != "out2" }), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "out1\nout3\n") }
func (S) TestRead(c *C) { p := pipe.Line( pipe.Read(bytes.NewBufferString("hello")), pipe.Exec("sed", "s/l/k/g"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko") }
func (S) TestPrintf(c *C) { p := pipe.Line( pipe.Printf("hello:%d", 42), pipe.Exec("sed", "s/l/k/g"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko:42") }
func (S) TestFilterNoNewLine(c *C) { p := pipe.Line( pipe.Print("out1\nout2\nout3"), pipe.Filter(func(line []byte) bool { return string(line) != "out2" }), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "out1\nout3") }
func (S) TestLine(c *C) { p := pipe.Line( pipe.Exec("/bin/sh", "-c", "echo out1; echo err1 1>&2; echo out2; echo err2 1>&2"), pipe.Exec("sed", `s/\(...\)\([12]\)/\1-\2/`), ) output, err := pipe.CombinedOutput(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "err1\nerr2\nout-1\nout-2\n") }
func (S) TestReadFileNonExistent(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Line( pipe.ReadFile(path), pipe.Exec("cat"), ) output, err := pipe.Output(p) c.Assert(err, ErrorMatches, "open .*/file: no such file or directory") c.Assert(output, IsNil) }
func (S) TestDiscard(c *C) { p := pipe.Line( pipe.Print("hello"), pipe.Discard(), pipe.Print("world"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "world") }
func (S) TestAppendFileAbsolute(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Script( pipe.Line( pipe.Print("hello "), pipe.AppendFile(path, 0600), ), pipe.Line( pipe.Print("world!"), pipe.AppendFile(path, 0600), ), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hello world!") }
func (S) TestScriptPreservesStreams(c *C) { p := pipe.Script( pipe.Line( pipe.Print("hello\n"), pipe.Discard(), ), pipe.Exec("echo", "world"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "world\n") }
func (S) TestTee(c *C) { var b bytes.Buffer p := pipe.Line( pipe.Print("hello"), pipe.Exec("sed", "s/l/k/g"), pipe.Tee(&b), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko") c.Assert(b.String(), Equals, "hekko") }
func (S) TestTeeAppendFileMode(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Line( pipe.Print("hello"), pipe.TeeAppendFile(path, 0600), ) err := pipe.Run(p) c.Assert(err, IsNil) stat, err := os.Stat(path) c.Assert(err, IsNil) c.Assert(stat.Mode()&os.ModePerm, Equals, os.FileMode(0600)) }
func (S) TestTeeAppendFileRelative(c *C) { dir := c.MkDir() path := filepath.Join(dir, "file") p := pipe.Script( pipe.ChDir(dir), pipe.Line( pipe.Print("hello "), pipe.TeeAppendFile("file", 0600), ), pipe.Line( pipe.Print("world!"), pipe.TeeAppendFile("file", 0600), ), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hello world!") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hello world!") }
func (S) TestLineTermination(c *C) { // Shouldn't block waiting for a reader that won't read. var b []byte for i := 0; i < 256*1024/8; i++ { b = append(b, "xxxxxxxx"...) } p := pipe.Line( pipe.Print(string(b)), pipe.Exec("true"), ) output, err := pipe.Output(p) c.Assert(err, ErrorMatches, `command "true": write \|1: broken pipe`) c.Assert(string(output), Equals, "") }
func (S) TestScriptNesting(c *C) { b := &bytes.Buffer{} p := pipe.Line( pipe.Print("hello"), pipe.Script( pipe.Print("world"), pipe.Exec("sed", "s/l/k/g"), ), pipe.Write(b), ) err := pipe.Run(p) c.Assert(err, IsNil) c.Assert(b.String(), Equals, "worldhekko") }
func (S) TestReadFileRelative(c *C) { dir := c.MkDir() path := filepath.Join(dir, "file") err := ioutil.WriteFile(path, []byte("hello"), 0644) c.Assert(err, IsNil) p := pipe.Line( pipe.ReadFile(path), pipe.Exec("sed", "s/l/k/g"), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko") }
func (S) TestTeeAppendFileAbsolute(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Script( pipe.Line( pipe.Print("hello "), pipe.TeeAppendFile(path, 0600), ), pipe.Line( pipe.Print("world!"), pipe.TeeAppendFile(path, 0600), ), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hello world!") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hello world!") stat, err := os.Stat(path) c.Assert(err, IsNil) c.Assert(stat.Mode()&os.ModePerm, Equals, os.FileMode(0600)) }
func (S) TestWriteFileAbsolute(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Line( pipe.Print("hello"), pipe.Exec("sed", "s/l/k/g"), pipe.WriteFile(path, 0600), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hekko") }
func findCommandOutput(cmd []string, c chan []Searchresult) { results := make([]Searchresult, 0) line := pipe.Line( pipe.Exec("find", cmd...), pipe.Exec("head", "-10"), ) output, err := pipe.Output(line) if err != nil { panic(err) } split := strings.Split(string(output), "\n") for _, i := range split { results = append(results, *NewSearchResult(i)) } c <- results }
func (S) TestReplaceNoNewLine(c *C) { p := pipe.Line( pipe.Print("out1\nout2\nout3"), pipe.Replace(func(line []byte) []byte { if bytes.HasPrefix(line, []byte("out")) { if line[3] == '2' { return nil } return []byte{'l', line[3], ','} } return line }), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "l1,l3,") }
func (S) TestReplace(c *C) { p := pipe.Line( pipe.System("echo out1; echo err1 1>&2; echo out2; echo err2 1>&2; echo out3"), pipe.Replace(func(line []byte) []byte { if bytes.HasPrefix(line, []byte("out")) { if line[3] == '3' { return nil } return []byte{'l', line[3], ','} } return line }), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "l1,l2,") }
func (S) TestTeeWriteFileRelative(c *C) { dir := c.MkDir() path := filepath.Join(dir, "file") p := pipe.Line( pipe.ChDir(dir), pipe.Print("hello"), pipe.Exec("sed", "s/l/k/g"), pipe.TeeWriteFile("file", 0600), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hekko") }
func (S) TestTeeWriteFileAbsolute(c *C) { path := filepath.Join(c.MkDir(), "file") p := pipe.Line( pipe.Print("hello"), pipe.Exec("sed", "s/l/k/g"), pipe.TeeWriteFile(path, 0600), ) output, err := pipe.Output(p) c.Assert(err, IsNil) c.Assert(string(output), Equals, "hekko") data, err := ioutil.ReadFile(path) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hekko") stat, err := os.Stat(path) c.Assert(err, IsNil) c.Assert(stat.Mode()&os.ModePerm, Equals, os.FileMode(0600)) }
func (S) TestErrorHandling(c *C) { sync := make(chan bool) p := pipe.Script( pipe.Line( pipe.TaskFunc(func(*pipe.State) error { sync <- true return fmt.Errorf("err1") }), pipe.TaskFunc(func(*pipe.State) error { <-sync return fmt.Errorf("err2") }), ), pipe.Print("never happened"), ) output, err := pipe.Output(p) if err.Error() != "err1; err2" && err.Error() != "err2; err1" { c.Fatalf(`want "err1; err2" or "err2; err1"; got %q`, err.Error()) } c.Assert(string(output), Equals, "") }
// RunWith executes the command with the given string as standard input, and // returns stdout and a nil error on success, or stderr and a non-nil error on // failure. func (c Executable) RunWith(stdin string) (string, error) { out, err := pipe.CombinedOutput( pipe.Line(pipe.Read(strings.NewReader(stdin)), c.Pipe), ) return string(out), err }