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) 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) 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) TestPrint(c *C) { p := pipe.Line( pipe.Print("hello:", 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) 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) 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) 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) 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) 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) 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) 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 (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) 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) 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, "") }