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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"io/ioutil"
"path/filepath"
"runtime"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"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) {
var content string
var cmdName string
var expect string
if runtime.GOOS != "windows" {
content = `#!/bin/bash --norc
echo stdout
echo stderr 1>&2
`
cmdName = "test-output"
expect = "stdout\nstderr\n"
} else {
content = `@echo off
echo stdout
echo stderr 1>&2
`
cmdName = "test-output.bat"
expect = "stdout\r\nstderr \r\n"
}
patchExecutable(s, c.MkDir(), cmdName, content)
output, err := utils.RunCommand("test-output")
c.Assert(err, gc.IsNil)
c.Assert(output, gc.Equals, expect)
}
func (s *commandSuite) TestRunCommandNonZeroExit(c *gc.C) {
var content string
var cmdName string
var expect string
if runtime.GOOS != "windows" {
content = `#!/bin/bash --norc
echo stdout
exit 42
`
cmdName = "test-output"
expect = "stdout\n"
} else {
content = `@echo off
echo stdout
exit 42
`
cmdName = "test-output.bat"
expect = "stdout\r\n"
}
patchExecutable(s, c.MkDir(), cmdName, content)
output, err := utils.RunCommand("test-output")
c.Assert(err, gc.ErrorMatches, `exit status 42`)
c.Assert(output, gc.Equals, expect)
}
|