forked from juju/testing
/
cmd_test.go
64 lines (50 loc) · 1.68 KB
/
cmd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing_test
import (
"os/exec"
gc "launchpad.net/gocheck"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
)
type cmdSuite struct {
testing.CleanupSuite
}
var _ = gc.Suite(&cmdSuite{})
func (s *cmdSuite) TestHookCommandOutput(c *gc.C) {
var CommandOutput = (*exec.Cmd).CombinedOutput
cmdChan, cleanup := testing.HookCommandOutput(&CommandOutput, []byte{1, 2, 3, 4}, nil)
defer cleanup()
testCmd := exec.Command("fake-command", "arg1", "arg2")
out, err := CommandOutput(testCmd)
c.Assert(err, gc.IsNil)
cmd := <-cmdChan
c.Assert(out, gc.DeepEquals, []byte{1, 2, 3, 4})
c.Assert(cmd.Args, gc.DeepEquals, []string{"fake-command", "arg1", "arg2"})
}
func (s *cmdSuite) EnsureArgFileRemoved(name string) {
s.AddCleanup(func(c *gc.C) {
c.Assert(name+".out", jc.DoesNotExist)
})
}
const testFunc = "test-ouput"
func (s *cmdSuite) TestPatchExecutableNoArgs(c *gc.C) {
s.EnsureArgFileRemoved(testFunc)
testing.PatchExecutableAsEchoArgs(c, s, testFunc)
output := runCommand(c, testFunc)
c.Assert(output, gc.Equals, testFunc+"\n")
testing.AssertEchoArgs(c, testFunc)
}
func (s *cmdSuite) TestPatchExecutableWithArgs(c *gc.C) {
s.EnsureArgFileRemoved(testFunc)
testing.PatchExecutableAsEchoArgs(c, s, testFunc)
output := runCommand(c, testFunc, "foo", "bar baz")
c.Assert(output, gc.Equals, testFunc+" \"foo\" \"bar baz\"\n")
testing.AssertEchoArgs(c, testFunc, "foo", "bar baz")
}
func runCommand(c *gc.C, command string, args ...string) string {
cmd := exec.Command(command, args...)
out, err := cmd.CombinedOutput()
c.Assert(err, gc.IsNil)
return string(out)
}