github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/action/action_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type ActionCommandSuite struct {
    17  	BaseActionSuite
    18  }
    19  
    20  var _ = gc.Suite(&ActionCommandSuite{})
    21  
    22  func (s *ActionCommandSuite) SetUpTest(c *gc.C) {
    23  	s.BaseActionSuite.SetUpTest(c)
    24  }
    25  
    26  func (s *ActionCommandSuite) TestHelp(c *gc.C) {
    27  	// Check the normal help for any command
    28  	ctx, err := testing.RunCommand(c, s.command, "--help")
    29  	c.Assert(err, gc.IsNil)
    30  
    31  	expected := "(?s).*^usage: juju action \\[options\\] <command> .+"
    32  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    33  
    34  	supercommand, ok := s.command.(*cmd.SuperCommand)
    35  	c.Check(ok, jc.IsTrue)
    36  	expected = "(?sm).*^purpose: " + supercommand.Purpose + "$.*"
    37  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    38  	expected = "(?sm).*^" + supercommand.Doc + "$.*"
    39  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    40  
    41  	// Check that we've properly registered all subcommands
    42  	s.checkHelpSubCommands(c, ctx)
    43  }
    44  
    45  func (s *ActionCommandSuite) checkHelpSubCommands(c *gc.C, ctx *cmd.Context) {
    46  	var expectedSubCommmands = [][]string{
    47  		{"defined", "show actions defined for a service"},
    48  		{"do", "queue an action for execution"},
    49  		{"fetch", "show results of an action by ID"},
    50  		{"help", "show help on a command or other topic"},
    51  		{"status", "show results of all actions filtered by optional ID prefix"},
    52  	}
    53  
    54  	// Check that we have registered all the sub commands by
    55  	// inspecting the help output.
    56  	var subsFound [][]string
    57  	commandHelp := strings.SplitAfter(testing.Stdout(ctx), "commands:")[1]
    58  	commandHelp = strings.TrimSpace(commandHelp)
    59  	for _, line := range strings.Split(commandHelp, "\n") {
    60  		subcommand := strings.Split(line, " - ")
    61  		c.Assert(len(subcommand), gc.Equals, 2)
    62  		name := strings.TrimSpace(subcommand[0])
    63  		desc := strings.TrimSpace(subcommand[1])
    64  		subsFound = append(subsFound, []string{name, desc})
    65  	}
    66  
    67  	c.Check(subsFound, jc.DeepEquals, expectedSubCommmands)
    68  }