~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/testing/testing.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package testing
 
5
 
 
6
import (
 
7
        "bytes"
 
8
        "flag"
 
9
        "fmt"
 
10
        "io"
 
11
        "io/ioutil"
 
12
        "os"
 
13
        "os/exec"
 
14
 
 
15
        "github.com/juju/cmd"
 
16
        jc "github.com/juju/testing/checkers"
 
17
        gc "gopkg.in/check.v1"
 
18
        "launchpad.net/gnuflag"
 
19
 
 
20
        "github.com/juju/juju/juju/osenv"
 
21
        "github.com/juju/juju/provider/dummy"
 
22
        coretesting "github.com/juju/juju/testing"
 
23
)
 
24
 
 
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")
 
27
 
 
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)
 
35
        if exit != 0 {
 
36
                c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit))
 
37
        }
 
38
        return string(output)
 
39
}
 
40
 
 
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()
 
45
        info.Name = name
 
46
        f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError)
 
47
        command.SetFlags(f)
 
48
        buff.Write(info.Help(f))
 
49
        return buff.String()
 
50
}
 
51
 
 
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
 
59
        return ctx
 
60
}
 
61
 
 
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) {
 
65
        if ctx == nil {
 
66
                panic("ctx == nil")
 
67
        }
 
68
        errc = make(chan error, 1)
 
69
        opc = make(chan dummy.Operation, 200)
 
70
        dummy.Listen(opc)
 
71
        go func() {
 
72
                defer func() {
 
73
                        // signal that we're done with this ops channel.
 
74
                        dummy.Listen(nil)
 
75
                        // now that dummy is no longer going to send ops on
 
76
                        // this channel, close it to signal to test cases
 
77
                        // that we are done.
 
78
                        close(opc)
 
79
                }()
 
80
 
 
81
                if err := coretesting.InitCommand(com, args); err != nil {
 
82
                        errc <- err
 
83
                        return
 
84
                }
 
85
 
 
86
                errc <- com.Run(ctx)
 
87
        }()
 
88
        return
 
89
}