github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/commands/commands_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  var _ = gc.Suite(&commandsSuite{})
    16  
    17  type commandsSuite struct {
    18  	stub    *testing.Stub
    19  	command *stubCommand
    20  }
    21  
    22  func (s *commandsSuite) SetUpTest(c *gc.C) {
    23  	s.stub = &testing.Stub{}
    24  	s.command = &stubCommand{stub: s.stub}
    25  }
    26  
    27  func (s *commandsSuite) TearDownTest(c *gc.C) {
    28  	registeredCommands = nil
    29  	registeredEnvCommands = nil
    30  }
    31  
    32  func (s *commandsSuite) TestRegisterCommand(c *gc.C) {
    33  	RegisterCommand(func() cmd.Command {
    34  		return s.command
    35  	})
    36  
    37  	// We can't compare functions directly, so...
    38  	c.Check(registeredEnvCommands, gc.HasLen, 0)
    39  	c.Assert(registeredCommands, gc.HasLen, 1)
    40  	command := registeredCommands[0]()
    41  	c.Check(command, gc.Equals, s.command)
    42  }
    43  
    44  func (s *commandsSuite) TestRegisterEnvCommand(c *gc.C) {
    45  	RegisterEnvCommand(func() modelcmd.ModelCommand {
    46  		return s.command
    47  	})
    48  
    49  	// We can't compare functions directly, so...
    50  	c.Assert(registeredCommands, gc.HasLen, 0)
    51  	c.Assert(registeredEnvCommands, gc.HasLen, 1)
    52  	command := registeredEnvCommands[0]()
    53  	c.Check(command, gc.Equals, s.command)
    54  }
    55  
    56  type stubCommand struct {
    57  	modelcmd.ModelCommandBase
    58  	stub    *testing.Stub
    59  	info    *cmd.Info
    60  	envName string
    61  }
    62  
    63  func (c *stubCommand) Info() *cmd.Info {
    64  	c.stub.AddCall("Info")
    65  	c.stub.NextErr() // pop one off
    66  
    67  	if c.info == nil {
    68  		return &cmd.Info{
    69  			Name: "some-command",
    70  		}
    71  	}
    72  	return c.info
    73  }
    74  
    75  func (c *stubCommand) Run(ctx *cmd.Context) error {
    76  	c.stub.AddCall("Run", ctx)
    77  	if err := c.stub.NextErr(); err != nil {
    78  		return errors.Trace(err)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func (c *stubCommand) SetModelName(name string) error {
    85  	c.stub.AddCall("SetModelName", name)
    86  	c.envName = name
    87  	return c.stub.NextErr()
    88  }
    89  
    90  func (c *stubCommand) ModelName() string {
    91  	c.stub.AddCall("ModelName")
    92  	c.stub.NextErr() // pop one off
    93  
    94  	return c.envName
    95  }
    96  
    97  type stubRegistry struct {
    98  	stub *testing.Stub
    99  
   100  	names []string
   101  }
   102  
   103  func (r *stubRegistry) Register(subcmd cmd.Command) {
   104  	r.stub.AddCall("Register", subcmd)
   105  	r.stub.NextErr() // pop one off
   106  
   107  	r.names = append(r.names, subcmd.Info().Name)
   108  	for _, name := range subcmd.Info().Aliases {
   109  		r.names = append(r.names, name)
   110  	}
   111  }
   112  
   113  func (r *stubRegistry) RegisterSuperAlias(name, super, forName string, check cmd.DeprecationCheck) {
   114  	r.stub.AddCall("RegisterSuperAlias", name, super, forName)
   115  	r.stub.NextErr() // pop one off
   116  
   117  	r.names = append(r.names, name)
   118  }
   119  
   120  func (r *stubRegistry) RegisterDeprecated(subcmd cmd.Command, check cmd.DeprecationCheck) {
   121  	r.stub.AddCall("RegisterDeprecated", subcmd, check)
   122  	r.stub.NextErr() // pop one off
   123  
   124  	r.names = append(r.names, subcmd.Info().Name)
   125  	for _, name := range subcmd.Info().Aliases {
   126  		r.names = append(r.names, name)
   127  	}
   128  }