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