~hduran-8/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/juju/main_test.go

  • Committer: Tarmac
  • Author(s): Andrew Wilkins
  • Date: 2014-05-13 01:48:02 UTC
  • mfrom: (2713.1.5 envcmd-inversion)
  • Revision ID: tarmac-20140513014802-d1n958os00gw27zf
[r=axwalk] Invert envcmd relationship

Previously we embedded EnvCommandBase in all commands
requring an environment, and EnvCommandBase included
everything (SetFlags, and Init). The problem with this
is that it was easy to miss initialisation of the
EnvCommandBase type (this happened a few times), which
leads to bad things happening like sync-tools destroying
environments.

I have inverted the relationship so that we now have
envcmd.EnvironCommand, an interface that extends Command
with a SetEnvName method, and EnvCommandBase, which
implements EnvironCommand. A new method, envcmd.Wrap takes
an EnvironCommand and creates a Command that calls
SetEnvName prior to the wrapped method's Init method. If
the environment name cannot be determined, the wrapping
command will error out early.

https://codereview.appspot.com/94350045/

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
        gc "launchpad.net/gocheck"
20
20
 
21
21
        "launchpad.net/juju-core/cmd"
 
22
        "launchpad.net/juju-core/cmd/envcmd"
22
23
        "launchpad.net/juju-core/juju/osenv"
23
24
        _ "launchpad.net/juju-core/provider/dummy"
24
25
        "launchpad.net/juju-core/testing"
70
71
}
71
72
 
72
73
func deployHelpText() string {
73
 
        return helpText(&DeployCommand{}, "juju deploy")
 
74
        return helpText(envcmd.Wrap(&DeployCommand{}), "juju deploy")
74
75
}
75
76
 
76
77
func syncToolsHelpText() string {
77
 
        return helpText(&SyncToolsCommand{}, "juju sync-tools")
 
78
        return helpText(envcmd.Wrap(&SyncToolsCommand{}), "juju sync-tools")
78
79
}
79
80
 
80
81
func (s *MainSuite) TestRunMain(c *gc.C) {
350
351
                c.Assert(line, gc.Matches, globalFlags[i])
351
352
        }
352
353
}
 
354
 
 
355
type commands []cmd.Command
 
356
 
 
357
func (r *commands) Register(c cmd.Command) {
 
358
        *r = append(*r, c)
 
359
}
 
360
 
 
361
func (s *MainSuite) TestEnvironCommands(c *gc.C) {
 
362
        var commands commands
 
363
        registerCommands(&commands, testing.Context(c))
 
364
        // There should not be any EnvironCommands registered.
 
365
        // EnvironCommands must be wrapped using envcmd.Wrap.
 
366
        for _, cmd := range commands {
 
367
                c.Logf("%v", cmd.Info().Name)
 
368
                c.Check(cmd, gc.Not(gc.FitsTypeOf), envcmd.EnvironCommand(&BootstrapCommand{}))
 
369
        }
 
370
}