Beispiel #1
0
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")
}
Beispiel #2
0
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")
}
Beispiel #3
0
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")
}
Beispiel #4
0
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")
}
Beispiel #5
0
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!")
}
Beispiel #6
0
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")
}
Beispiel #7
0
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")
}
Beispiel #8
0
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))
}
Beispiel #9
0
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!")
}
Beispiel #10
0
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")
}
Beispiel #11
0
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, "")
}
Beispiel #12
0
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")
}
Beispiel #13
0
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))
}
Beispiel #14
0
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,")
}
Beispiel #15
0
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")
}
Beispiel #16
0
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))
}
Beispiel #17
0
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, "")
}