forked from juju/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_test.go
52 lines (42 loc) · 1.21 KB
/
command_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"io/ioutil"
"path/filepath"
"github.com/juju/testing"
gc "launchpad.net/gocheck"
"github.com/juju/utils"
)
type EnvironmentPatcher interface {
PatchEnvironment(name, value string)
}
func patchExecutable(patcher EnvironmentPatcher, dir, execName, script string) {
patcher.PatchEnvironment("PATH", dir)
filename := filepath.Join(dir, execName)
ioutil.WriteFile(filename, []byte(script), 0755)
}
type commandSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&commandSuite{})
func (s *commandSuite) TestRunCommandCombinesOutput(c *gc.C) {
content := `#!/bin/bash --norc
echo stdout
echo stderr 1>&2
`
patchExecutable(s, c.MkDir(), "test-output", content)
output, err := utils.RunCommand("test-output")
c.Assert(err, gc.IsNil)
c.Assert(output, gc.Equals, "stdout\nstderr\n")
}
func (s *commandSuite) TestRunCommandNonZeroExit(c *gc.C) {
content := `#!/bin/bash --norc
echo stdout
exit 42
`
patchExecutable(s, c.MkDir(), "test-output", content)
output, err := utils.RunCommand("test-output")
c.Assert(err, gc.ErrorMatches, `exit status 42`)
c.Assert(output, gc.Equals, "stdout\n")
}