~hduran-8/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/envcmd/environmentcommand_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:
12
12
        "launchpad.net/gnuflag"
13
13
        gc "launchpad.net/gocheck"
14
14
 
 
15
        "launchpad.net/juju-core/cmd"
15
16
        "launchpad.net/juju-core/cmd/envcmd"
16
17
        "launchpad.net/juju-core/environs"
17
18
        "launchpad.net/juju-core/juju/osenv"
47
48
}
48
49
 
49
50
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentNothingSet(c *gc.C) {
50
 
        env := envcmd.GetDefaultEnvironment()
 
51
        env, err := envcmd.GetDefaultEnvironment()
51
52
        c.Assert(env, gc.Equals, "")
 
53
        c.Assert(err, jc.Satisfies, environs.IsNoEnv)
52
54
}
53
55
 
54
56
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentCurrentEnvironmentSet(c *gc.C) {
55
57
        err := envcmd.WriteCurrentEnvironment("fubar")
56
58
        c.Assert(err, gc.IsNil)
57
 
        env := envcmd.GetDefaultEnvironment()
 
59
        env, err := envcmd.GetDefaultEnvironment()
58
60
        c.Assert(env, gc.Equals, "fubar")
 
61
        c.Assert(err, gc.IsNil)
59
62
}
60
63
 
61
64
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentJujuEnvSet(c *gc.C) {
62
65
        os.Setenv(osenv.JujuEnvEnvKey, "magic")
63
 
        env := envcmd.GetDefaultEnvironment()
 
66
        env, err := envcmd.GetDefaultEnvironment()
64
67
        c.Assert(env, gc.Equals, "magic")
 
68
        c.Assert(err, gc.IsNil)
65
69
}
66
70
 
67
71
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentBothSet(c *gc.C) {
68
72
        os.Setenv(osenv.JujuEnvEnvKey, "magic")
69
73
        err := envcmd.WriteCurrentEnvironment("fubar")
70
74
        c.Assert(err, gc.IsNil)
71
 
        env := envcmd.GetDefaultEnvironment()
 
75
        env, err := envcmd.GetDefaultEnvironment()
72
76
        c.Assert(env, gc.Equals, "magic")
 
77
        c.Assert(err, gc.IsNil)
73
78
}
74
79
 
75
80
func (s *EnvironmentCommandSuite) TestWriteAddsNewline(c *gc.C) {
87
92
        c.Assert(err, gc.ErrorMatches, "unable to write to the environment file: .*")
88
93
}
89
94
 
90
 
func (s *EnvironmentCommandSuite) TestEnsureEnvName(c *gc.C) {
 
95
func (s *EnvironmentCommandSuite) TestEnvironCommandInit(c *gc.C) {
91
96
        // Take environment name from command line arg.
92
 
        cmd := initEnvCommandBase(c, "explicit")
93
 
        err := cmd.EnsureEnvName()
 
97
        cmd, envName := prepareEnvCommand(c, "explicit")
 
98
        err := cmd.Init(nil)
94
99
        c.Assert(err, gc.IsNil)
95
 
        c.Assert(cmd.EnvName, gc.Equals, "explicit")
 
100
        c.Assert(*envName, gc.Equals, "explicit")
96
101
 
97
102
        // Take environment name from the default.
98
103
        defer coretesting.MakeFakeHome(c, coretesting.MultipleEnvConfig).Restore()
108
113
        testEnsureEnvName(c, "fubar")
109
114
}
110
115
 
111
 
func (s *EnvironmentCommandSuite) TestEnsureEnvNameErrors(c *gc.C) {
112
 
        err := initEnvCommandBase(c, "").EnsureEnvName()
 
116
func (s *EnvironmentCommandSuite) TestEnvironCommandInitErrors(c *gc.C) {
 
117
        cmd, _ := prepareEnvCommand(c, "")
 
118
        err := cmd.Init(nil)
113
119
        c.Assert(err, jc.Satisfies, environs.IsNoEnv)
114
120
 
115
121
        // If there are multiple environments but no default,
116
122
        // an error should be returned.
117
123
        defer coretesting.MakeFakeHome(c, coretesting.MultipleEnvConfigNoDefault).Restore()
118
 
        err = initEnvCommandBase(c, "").EnsureEnvName()
 
124
        cmd, _ = prepareEnvCommand(c, "")
 
125
        err = cmd.Init(nil)
119
126
        c.Assert(err, gc.Equals, envcmd.ErrNoEnvironmentSpecified)
120
127
}
121
128
 
122
 
func initEnvCommandBase(c *gc.C, name string) *envcmd.EnvCommandBase {
 
129
type testCommand struct {
 
130
        envcmd.EnvCommandBase
 
131
}
 
132
 
 
133
func (c *testCommand) Info() *cmd.Info {
 
134
        panic("should not be called")
 
135
}
 
136
 
 
137
func (c *testCommand) Run(ctx *cmd.Context) error {
 
138
        panic("should not be called")
 
139
}
 
140
 
 
141
// prepareEnvCommand prepares a Command for a call to Init,
 
142
// returning the Command and a pointer to a string that will
 
143
// contain the environment name after the Command's Init method
 
144
// has been called.
 
145
func prepareEnvCommand(c *gc.C, name string) (cmd.Command, *string) {
123
146
        var flags gnuflag.FlagSet
124
 
        var cmd envcmd.EnvCommandBase
125
 
        cmd.SetFlags(&flags)
 
147
        var cmd testCommand
 
148
        wrapped := envcmd.Wrap(&cmd)
 
149
        wrapped.SetFlags(&flags)
126
150
        var args []string
127
151
        if name != "" {
128
152
                args = []string{"-e", name}
129
153
        }
130
154
        err := flags.Parse(false, args)
131
155
        c.Assert(err, gc.IsNil)
132
 
        return &cmd
 
156
        return wrapped, &cmd.EnvName
133
157
}
134
158
 
135
159
func testEnsureEnvName(c *gc.C, expect string) {
136
 
        cmd := initEnvCommandBase(c, "")
137
 
        err := cmd.EnsureEnvName()
 
160
        cmd, envName := prepareEnvCommand(c, "")
 
161
        err := cmd.Init(nil)
138
162
        c.Assert(err, gc.IsNil)
139
 
        c.Assert(cmd.EnvName, gc.Equals, expect)
 
163
        c.Assert(*envName, gc.Equals, expect)
140
164
}