示例#1
0
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")
}
示例#2
0
func (S) TestExecDividedOutput(c *C) {
	p := pipe.Exec("/bin/sh", "-c", "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")
}
示例#3
0
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
}
示例#4
0
func (S) TestKillAbortedExecTask(c *C) {
	p := pipe.Script(
		pipe.TaskFunc(func(*pipe.State) error { return fmt.Errorf("boom") }),
		pipe.Exec("will-not-run"),
	)
	_, err := pipe.Output(p)
	c.Assert(err, ErrorMatches, "boom")
}
示例#5
0
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")
}
示例#6
0
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")
}
示例#7
0
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)
}
示例#8
0
func (S) TestExecRun(c *C) {
	path := filepath.Join(c.MkDir(), "file")
	p := pipe.Exec("/bin/sh", "-c", "echo hello > "+path)
	err := pipe.Run(p)
	c.Assert(err, IsNil)

	data, err := ioutil.ReadFile(path)
	c.Assert(err, IsNil)
	c.Assert(string(data), Equals, "hello\n")
}
示例#9
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")
}
示例#10
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")
}
示例#11
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")
}
示例#12
0
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")
}
示例#13
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")
}
示例#14
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, "")
}
示例#15
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")
}
示例#16
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")
}
示例#17
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))
}
示例#18
0
文件: sh.go 项目: simudream/sh-2
// Runner returns a function that will run the given shell command with
// specified arguments. This is a convenience for creating one-off commands that
// aren't going to be put into Pipe, but instead just run standalone.  The
// return values are the combined output and an error if any.
//
// The args that are passed to Runner are passed to the shell command when the
// returned function is run, allowing you to pre-set some common arguments.
func Runner(name string, args0 ...string) func(args ...string) (string, error) {
	return func(args1 ...string) (string, error) {
		return Executable{pipe.Exec(name, append(args0, args1...)...)}.Run()
	}
}
示例#19
0
文件: sh.go 项目: simudream/sh-2
// Cmd returns a function that will return an Executable for the given command
// with the given args.  This is a convenience for defining functions for
// commonly reused Executables, such as grep, ls, mkdir, etc.
//
// The args that are passed to Cmd are passed to the Executable when the
// returned function is run, allowing you to pre-set some common arguments.
func Cmd(name string, args0 ...string) func(args ...string) Executable {
	return func(args1 ...string) Executable {
		return Executable{pipe.Exec(name, append(args0, args1...)...)}
	}
}
示例#20
0
func (S) TestExecCombinedOutput(c *C) {
	p := pipe.Exec("/bin/sh", "-c", "echo out1; echo err1 1>&2; echo out2; echo err2 1>&2")
	output, err := pipe.CombinedOutput(p)
	c.Assert(err, IsNil)
	c.Assert(string(output), Equals, "out1\nerr1\nout2\nerr2\n")
}