github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/help_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_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 HelpStorageSuite struct {
    17  	testing.FakeJujuHomeSuite
    18  
    19  	command cmd.Command
    20  }
    21  
    22  func (s *HelpStorageSuite) SetUpTest(c *gc.C) {
    23  	s.FakeJujuHomeSuite.SetUpTest(c)
    24  }
    25  
    26  func (s *HelpStorageSuite) TearDownTest(c *gc.C) {
    27  	s.FakeJujuHomeSuite.TearDownTest(c)
    28  }
    29  
    30  func (s *HelpStorageSuite) assertHelp(c *gc.C, expectedNames []string) {
    31  	ctx, err := testing.RunCommand(c, s.command, "--help")
    32  	c.Assert(err, jc.ErrorIsNil)
    33  
    34  	super, ok := s.command.(*cmd.SuperCommand)
    35  	c.Assert(ok, jc.IsTrue)
    36  	expected := "(?sm).*^purpose: " + super.Purpose + "$.*"
    37  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    38  	expected = "(?sm).*^" + super.Doc + "$.*"
    39  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    40  
    41  	s.checkHelpCommands(c, ctx, expectedNames)
    42  }
    43  
    44  func (s *HelpStorageSuite) checkHelpCommands(c *gc.C, ctx *cmd.Context, expectedNames []string) {
    45  	// Check that we have registered all the sub commands by
    46  	// inspecting the help output.
    47  	var namesFound []string
    48  	commandHelp := strings.SplitAfter(testing.Stdout(ctx), "commands:")[1]
    49  	commandHelp = strings.TrimSpace(commandHelp)
    50  	for _, line := range strings.Split(commandHelp, "\n") {
    51  		name := strings.TrimSpace(strings.Split(line, " - ")[0])
    52  		namesFound = append(namesFound, name)
    53  	}
    54  	c.Check(namesFound, gc.DeepEquals, expectedNames)
    55  }