File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,15 +25,7 @@ const (
2525 OutputPrefix = "o:"
2626)
2727
28- func init () {
29- Ui = & cli.PrefixedUi {
30- AskPrefix : OutputPrefix ,
31- OutputPrefix : OutputPrefix ,
32- InfoPrefix : OutputPrefix ,
33- ErrorPrefix : ErrorPrefix ,
34- Ui : & cli.BasicUi {Writer : os .Stdout },
35- }
36-
28+ func initCommands (config * Config ) {
3729 var inAutomation bool
3830 if v := os .Getenv (runningInAutomationEnvName ); v != "" {
3931 inAutomation = true
Original file line number Diff line number Diff line change @@ -96,6 +96,16 @@ func realMain() int {
9696 return wrappedMain ()
9797}
9898
99+ func init () {
100+ Ui = & cli.PrefixedUi {
101+ AskPrefix : OutputPrefix ,
102+ OutputPrefix : OutputPrefix ,
103+ InfoPrefix : OutputPrefix ,
104+ ErrorPrefix : ErrorPrefix ,
105+ Ui : & cli.BasicUi {Writer : os .Stdout },
106+ }
107+ }
108+
99109func wrappedMain () int {
100110 // We always need to close the DebugInfo before we exit.
101111 defer terraform .CloseDebugInfo ()
@@ -128,6 +138,11 @@ func wrappedMain() int {
128138 config = * config .Merge (usrcfg )
129139 }
130140
141+ // In tests, Commands may already be set to provide mock commands
142+ if Commands == nil {
143+ initCommands (& config )
144+ }
145+
131146 // Run checkpoint
132147 go runCheckpoint (& config )
133148
Original file line number Diff line number Diff line change @@ -18,9 +18,12 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
1818 defer func () { os .Args = oldArgs }()
1919
2020 // Setup test command and restore that
21+ Commands = make (map [string ]cli.CommandFactory )
22+ defer func () {
23+ Commands = nil
24+ }()
2125 testCommandName := "unit-test-cli-args"
2226 testCommand := & testCommandCLI {}
23- defer func () { delete (Commands , testCommandName ) }()
2427 Commands [testCommandName ] = func () (cli.Command , error ) {
2528 return testCommand , nil
2629 }
@@ -150,6 +153,12 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
150153 oldArgs := os .Args
151154 defer func () { os .Args = oldArgs }()
152155
156+ // Setup test command and restore that
157+ Commands = make (map [string ]cli.CommandFactory )
158+ defer func () {
159+ Commands = nil
160+ }()
161+
153162 cases := []struct {
154163 Name string
155164 Command string
@@ -230,7 +239,7 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
230239 testCommand .Args = nil
231240 exit := wrappedMain ()
232241 if (exit != 0 ) != tc .Err {
233- t .Fatalf ("bad: %d " , exit )
242+ t .Fatalf ("unexpected exit status %d; want 0 " , exit )
234243 }
235244 if tc .Err {
236245 return
You can’t perform that action at this time.
0 commit comments