diff options
author | Joshua MacDonald <josh.macdonald@gmail.com> | 2015-01-24 22:55:24 -0800 |
---|---|---|
committer | Joshua MacDonald <josh.macdonald@gmail.com> | 2015-01-24 22:55:24 -0800 |
commit | 57c7df665788bbb524c544ce5dea66b1aade63d3 (patch) | |
tree | 100bc8a09b96319aaac7c2367c0209989eac5a4c /xdelta3/go/src/regtest.go | |
parent | b45bf520df94a641133ced43aac8d3829553cd72 (diff) |
Golang regtest framework for testing program execution using pipes, including a named pipe for the source file
Diffstat (limited to 'xdelta3/go/src/regtest.go')
-rw-r--r-- | xdelta3/go/src/regtest.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/xdelta3/go/src/regtest.go b/xdelta3/go/src/regtest.go new file mode 100644 index 0000000..aee419d --- /dev/null +++ b/xdelta3/go/src/regtest.go | |||
@@ -0,0 +1,83 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "io" | ||
5 | "io/ioutil" | ||
6 | "xdelta" | ||
7 | ) | ||
8 | |||
9 | const ( | ||
10 | prog = "/Users/jmacd/src/xdelta/xdelta3/xdelta3" | ||
11 | ) | ||
12 | |||
13 | func drain(f io.ReadCloser) <-chan []byte { | ||
14 | c := make(chan []byte) | ||
15 | go func() { | ||
16 | if b, err := ioutil.ReadAll(f); err != nil { | ||
17 | panic(err) | ||
18 | } else { | ||
19 | c <- b | ||
20 | } | ||
21 | }() | ||
22 | return c | ||
23 | } | ||
24 | |||
25 | func empty(f io.ReadCloser) { | ||
26 | go func() { | ||
27 | if _, err := ioutil.ReadAll(f); err != nil { | ||
28 | panic(err) | ||
29 | } | ||
30 | }() | ||
31 | } | ||
32 | |||
33 | func write(f io.WriteCloser, b []byte) { | ||
34 | if _, err := f.Write(b); err != nil { | ||
35 | panic(err) | ||
36 | } | ||
37 | if err := f.Close(); err != nil { | ||
38 | panic(err) | ||
39 | } | ||
40 | } | ||
41 | |||
42 | func main() { | ||
43 | x := xdelta.Program{prog} | ||
44 | smokeTest(x) | ||
45 | } | ||
46 | |||
47 | func smokeTest(p xdelta.Program) { | ||
48 | target := "Hello world!" | ||
49 | source := "Hello world, nice to meet you!" | ||
50 | |||
51 | run, err := p.Exec(true, []string{"-e"}) | ||
52 | if err != nil { | ||
53 | panic(err) | ||
54 | } | ||
55 | encodeout := drain(run.Stdout) | ||
56 | empty(run.Stderr) | ||
57 | |||
58 | write(run.Stdin, []byte(target)) | ||
59 | write(run.Srcin, []byte(source)) | ||
60 | |||
61 | if err := run.Cmd.Wait(); err != nil { | ||
62 | panic(err) | ||
63 | } | ||
64 | |||
65 | run, err = p.Exec(true, []string{"-d"}) | ||
66 | if err != nil { | ||
67 | panic(err) | ||
68 | } | ||
69 | |||
70 | decodeout := drain(run.Stdout) | ||
71 | empty(run.Stderr) | ||
72 | |||
73 | write(run.Stdin, <-encodeout) | ||
74 | write(run.Srcin, []byte(source)) | ||
75 | |||
76 | if err := run.Cmd.Wait(); err != nil { | ||
77 | panic(err) | ||
78 | } | ||
79 | |||
80 | if string(<-decodeout) != target { | ||
81 | panic("It's not working!!!") | ||
82 | } | ||
83 | } | ||