summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta
diff options
context:
space:
mode:
authorJoshua MacDonald <josh.macdonald@gmail.com>2015-01-24 22:55:24 -0800
committerJoshua MacDonald <josh.macdonald@gmail.com>2015-01-24 22:55:24 -0800
commit57c7df665788bbb524c544ce5dea66b1aade63d3 (patch)
tree100bc8a09b96319aaac7c2367c0209989eac5a4c /xdelta3/go/src/xdelta
parentb45bf520df94a641133ced43aac8d3829553cd72 (diff)
Golang regtest framework for testing program execution using pipes, including a named pipe for the source file
Diffstat (limited to 'xdelta3/go/src/xdelta')
-rw-r--r--xdelta3/go/src/xdelta/test.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go
new file mode 100644
index 0000000..d586538
--- /dev/null
+++ b/xdelta3/go/src/xdelta/test.go
@@ -0,0 +1,78 @@
1package xdelta
2
3import (
4 "io"
5 "io/ioutil"
6 "os"
7 "os/exec"
8 "path"
9 "golang.org/x/sys/unix"
10)
11
12var (
13 tmpDir = "/tmp"
14)
15
16type Program struct {
17 Path string
18}
19
20type Run struct {
21 Cmd exec.Cmd
22 Testdir string
23 Srcfile string
24 Stdin io.WriteCloser
25 Srcin io.WriteCloser
26 Stdout io.ReadCloser
27 Stderr io.ReadCloser
28}
29
30func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) {
31 var err error
32 run := &Run{}
33 if run.Testdir, err = ioutil.TempDir(tmpDir, "xrt"); err != nil {
34 return nil, err
35 }
36 args := []string{b.Path}
37 if srcfifo {
38 run.Srcfile = path.Join(run.Testdir, "source")
39 if err = unix.Mkfifo(run.Srcfile, 0600); err != nil {
40 return nil, err
41 }
42 // Because OpenFile blocks on the Fifo until the reader
43 // arrives, a pipe to defer open
44 read, write := io.Pipe()
45 run.Srcin = write
46
47 go func() {
48 fifo, err := os.OpenFile(run.Srcfile, os.O_WRONLY, 0600)
49 if err != nil {
50 panic(err)
51 }
52 if _, err := io.Copy(fifo, read); err != nil {
53 panic(err)
54 }
55 if err := fifo.Close(); err != nil {
56 panic(err)
57 }
58 }()
59 args = append(args, "-s")
60 args = append(args, run.Srcfile)
61 }
62 if run.Stdin, err = run.Cmd.StdinPipe(); err != nil {
63 return nil, err
64 }
65 if run.Stdout, err = run.Cmd.StdoutPipe(); err != nil {
66 return nil, err
67 }
68 if run.Stderr, err = run.Cmd.StderrPipe(); err != nil {
69 return nil, err
70 }
71
72 run.Cmd.Path = b.Path
73 run.Cmd.Args = append(args, flags...)
74 run.Cmd.Dir = run.Testdir
75 run.Cmd.Start()
76
77 return run, nil
78}