1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
16
jc "github.com/juju/testing/checkers"
17
gc "gopkg.in/check.v1"
18
"launchpad.net/gnuflag"
20
"github.com/juju/juju/juju/osenv"
21
"github.com/juju/juju/provider/dummy"
22
coretesting "github.com/juju/juju/testing"
25
// FlagRunMain is used to indicate that the -run-main flag was used.
26
var FlagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing")
28
// BadRun is used to run a command, check the exit code, and return the output.
29
func BadRun(c *gc.C, exit int, args ...string) string {
30
localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--"}, args...)
31
ps := exec.Command(os.Args[0], localArgs...)
32
ps.Env = append(os.Environ(), osenv.JujuXDGDataHomeEnvKey+"="+osenv.JujuXDGDataHome())
33
output, err := ps.CombinedOutput()
34
c.Logf("command output: %q", output)
36
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit))
41
// HelpText returns a command's formatted help text.
42
func HelpText(command cmd.Command, name string) string {
43
buff := &bytes.Buffer{}
44
info := command.Info()
46
f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError)
48
buff.Write(info.Help(f))
52
// NullContext returns a no-op command context.
53
func NullContext(c *gc.C) *cmd.Context {
54
ctx, err := cmd.DefaultContext()
55
c.Assert(err, jc.ErrorIsNil)
56
ctx.Stdin = io.LimitReader(nil, 0)
57
ctx.Stdout = ioutil.Discard
58
ctx.Stderr = ioutil.Discard
62
// RunCommand runs the command and returns channels holding the
63
// command's operations and errors.
64
func RunCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
68
errc = make(chan error, 1)
69
opc = make(chan dummy.Operation, 200)
73
// signal that we're done with this ops channel.
75
// now that dummy is no longer going to send ops on
76
// this channel, close it to signal to test cases
81
if err := coretesting.InitCommand(com, args); err != nil {