Exemplo n.º 1
0
func (S) TestScriptCombinedOutput(c *C) {
	p := pipe.Script(
		pipe.System("echo out1; echo err1 1>&2; echo out2; echo err2 1>&2"),
		pipe.System("echo out3; echo err3 1>&2; echo out4; echo err4 1>&2"),
	)
	output, err := pipe.CombinedOutput(p)
	c.Assert(err, IsNil)
	c.Assert(string(output), Equals, "out1\nerr1\nout2\nerr2\nout3\nerr3\nout4\nerr4\n")
}
Exemplo n.º 2
0
func (S) TestSystem(c *C) {
	p := pipe.System("echo out1; echo err1 1>&2; echo out2; echo err2 1>&2")
	stdout, stderr, err := pipe.DividedOutput(p)
	c.Assert(err, IsNil)
	c.Assert(string(stdout), Equals, "out1\nout2\n")
	c.Assert(string(stderr), Equals, "err1\nerr2\n")
}
Exemplo n.º 3
0
func (S) TestSetEnvVar(c *C) {
	os.Setenv("PIPE_NEW_VAR", "")
	os.Setenv("PIPE_OLD_VAR", "old")
	defer os.Setenv("PIPE_OLD_VAR", "")
	p := pipe.Script(
		pipe.SetEnvVar("PIPE_NEW_VAR", "new"),
		pipe.System("echo $PIPE_OLD_VAR $PIPE_NEW_VAR"),
		pipe.SetEnvVar("PIPE_NEW_VAR", "after"),
		func(s *pipe.State) error {
			count := 0
			prefix := "PIPE_NEW_VAR="
			for _, kv := range s.Env {
				if strings.HasPrefix(kv, prefix) {
					count++
				}
			}
			if count != 1 {
				return fmt.Errorf("found %d environment variables", count)
			}
			return nil
		},
	)
	output, err := pipe.Output(p)
	c.Assert(err, IsNil)
	c.Assert(string(output), Equals, "old new\n")
	c.Assert(os.Getenv("PIPE_NEW_VAR"), Equals, "")
}
Exemplo n.º 4
0
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")
}
Exemplo n.º 5
0
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")
}
Exemplo n.º 6
0
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")
}
Exemplo n.º 7
0
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,")
}
Exemplo n.º 8
0
func (S) TestMkDir(c *C) {
	dir := c.MkDir()
	subdir := filepath.Join(dir, "subdir")
	subsubdir := filepath.Join(subdir, "subsubdir")
	p := pipe.Script(
		pipe.MkDir(subdir, 0755), // Absolute
		pipe.ChDir(subdir),
		pipe.MkDir("subsubdir", 0700), // Relative
		pipe.ChDir("subsubdir"),
		pipe.System("echo $PWD"),
	)
	output, err := pipe.Output(p)
	c.Assert(err, IsNil)
	c.Assert(string(output), Equals, subsubdir+"\n")

	stat, err := os.Stat(subsubdir)
	c.Assert(err, IsNil)
	c.Assert(stat.Mode()&os.ModePerm, Equals, os.FileMode(0700))
}
Exemplo n.º 9
0
func (S) TestChDir(c *C) {
	wd1, err := os.Getwd()
	c.Assert(err, IsNil)

	dir := c.MkDir()
	subdir := filepath.Join(dir, "subdir")
	err = os.Mkdir(subdir, 0755)
	p := pipe.Script(
		pipe.ChDir(dir),
		pipe.ChDir("subdir"),
		pipe.System("echo $PWD"),
	)
	output, err := pipe.Output(p)
	c.Assert(err, IsNil)
	c.Assert(string(output), Equals, subdir+"\n")

	wd2, err := os.Getwd()
	c.Assert(err, IsNil)
	c.Assert(wd2, Equals, wd1)
}