1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
11
jc "github.com/juju/testing/checkers"
12
gc "gopkg.in/check.v1"
14
"github.com/juju/utils/exec"
17
// 1 is thrown by powershell after the a command is cancelled
18
const cancelErrCode = 1
20
// longPath is copied over from the symlink package. This should be removed
21
// if we add it to gc or in some other convenience package
22
func longPath(path string) ([]uint16, error) {
23
pathp, err := syscall.UTF16FromString(path)
29
n, err := syscall.GetLongPathName(&pathp[0], &longp[0], uint32(len(longp)))
33
if n > uint32(len(longp)) {
34
longp = make([]uint16, n)
35
n, err = syscall.GetLongPathName(&pathp[0], &longp[0], uint32(len(longp)))
45
func longPathAsString(path string) (string, error) {
46
longp, err := longPath(path)
50
return syscall.UTF16ToString(longp), nil
53
func (*execSuite) TestRunCommands(c *gc.C) {
54
newDir, err := longPathAsString(c.MkDir())
55
c.Assert(err, gc.IsNil)
56
for i, test := range []struct {
66
message: "test stdout capture",
67
commands: "echo 'testing stdout'",
68
stdout: "testing stdout\r\n",
70
message: "test stderr capture",
71
commands: "Write-Error 'testing stderr'",
72
stderr: "testing stderr\r\n",
74
message: "test return code",
78
message: "test working dir",
79
commands: "(pwd).Path",
81
stdout: filepath.FromSlash(newDir) + "\r\n",
83
message: "test environment",
84
commands: "echo $env:OMG_IT_WORKS",
85
environment: []string{"OMG_IT_WORKS=like magic"},
86
stdout: "like magic\r\n",
89
c.Logf("%v: %s", i, test.message)
91
params := exec.RunParams{
92
Commands: test.commands,
93
WorkingDir: test.workingDir,
94
Environment: test.environment,
97
result, err := exec.RunCommands(params)
98
c.Assert(err, gc.IsNil)
99
c.Assert(string(result.Stdout), gc.Equals, test.stdout)
100
c.Assert(string(result.Stderr), jc.Contains, test.stderr)
101
c.Assert(result.Code, gc.Equals, test.code)
104
c.Assert(err, gc.IsNil)
105
c.Assert(params.Process(), gc.Not(gc.IsNil))
106
result, err = params.Wait()
107
c.Assert(err, gc.IsNil)
108
c.Assert(string(result.Stdout), gc.Equals, test.stdout)
109
c.Assert(string(result.Stderr), jc.Contains, test.stderr)
110
c.Assert(result.Code, gc.Equals, test.code)
113
c.Assert(err, gc.IsNil)
114
c.Assert(params.Process(), gc.Not(gc.IsNil))
115
result, err = params.WaitWithCancel(nil)
116
c.Assert(err, gc.IsNil)
117
c.Assert(string(result.Stdout), gc.Equals, test.stdout)
118
c.Assert(string(result.Stderr), jc.Contains, test.stderr)
119
c.Assert(result.Code, gc.Equals, test.code)
123
func (*execSuite) TestExecUnknownCommand(c *gc.C) {
124
result, err := exec.RunCommands(
126
Commands: "unknown-command",
129
c.Assert(err, gc.IsNil)
130
c.Assert(result.Stdout, gc.HasLen, 0)
131
stderr := strings.Replace(string(result.Stderr), "\r\n", "", -1)
132
c.Assert(stderr, jc.Contains, "is not recognized as the name of a cmdlet")
133
// 1 is returned by RunCommands when powershell commands throw exceptions
134
c.Assert(result.Code, gc.Equals, 1)