github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/testing/testing.go (about)

     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  		// signal that we're done with this ops channel.
    73  		defer dummy.Listen(nil)
    74  
    75  		err := coretesting.InitCommand(com, args)
    76  		if err != nil {
    77  			errc <- err
    78  			return
    79  		}
    80  
    81  		err = com.Run(ctx)
    82  		errc <- err
    83  	}()
    84  	return
    85  }