@@ -11,103 +11,74 @@ import (
1111)
1212
1313func Test_main (t * testing.T ) {
14- t .Run ("main sanity check" , func (t * testing.T ) {
15- os .Args = []string {"eirctl" , "run" , "unknown" }
16-
17- eirctlRootCmd := eirctlcmd .NewEirCtlCmd (context .TODO (), os .Stdout , os .Stderr )
18-
19- if err := eirctlRootCmd .InitCommand (eirctlcmd .WithSubCommands ()... ); err != nil {
20- logrus .Fatal (err )
21- }
22-
23- setDefaultCommandIfNonePresent (eirctlRootCmd .Cmd )
24-
25- if err := eirctlRootCmd .Execute (); err == nil {
26- t .Error ("got nil wanted error" )
27- }
28- })
29-
30- t .Run ("main sanity check (explicit debug)" , func (t * testing.T ) {
31- os .Args = []string {"eirctl" , "run" , "unknown" , "--debug" }
32-
33- eirctlRootCmd := eirctlcmd .NewEirCtlCmd (context .TODO (), os .Stdout , os .Stderr )
34-
35- if err := eirctlRootCmd .InitCommand (eirctlcmd .WithSubCommands ()... ); err != nil {
36- logrus .Fatal (err )
37- }
38-
39- setDefaultCommandIfNonePresent (eirctlRootCmd .Cmd )
40-
41- if err := eirctlRootCmd .Execute (); err == nil {
42- t .Error ("got nil wanted error" )
43- }
44-
45- logLevel := logrus .GetLevel ()
46- if logLevel != logrus .DebugLevel {
47- t .Errorf ("Expected Log Level to be '%s', got: '%s'" , logrus .DebugLevel , logLevel )
48- }
49- })
50-
51- t .Run ("main sanity check (explicit verbose)" , func (t * testing.T ) {
52- os .Args = []string {"eirctl" , "run" , "unknown" , "--verbose" }
53-
54- eirctlRootCmd := eirctlcmd .NewEirCtlCmd (context .TODO (), os .Stdout , os .Stderr )
55-
56- if err := eirctlRootCmd .InitCommand (eirctlcmd .WithSubCommands ()... ); err != nil {
57- logrus .Fatal (err )
58- }
59-
60- setDefaultCommandIfNonePresent (eirctlRootCmd .Cmd )
61-
62- if err := eirctlRootCmd .Execute (); err == nil {
63- t .Error ("got nil wanted error" )
64- }
65-
66- logLevel := logrus .GetLevel ()
67- if logLevel != logrus .TraceLevel {
68- t .Errorf ("Expected Log Level to be '%s', got: '%s'" , logrus .TraceLevel , logLevel )
69- }
70- })
71- t .Run ("exit code correctly bubbled up" , func (t * testing.T ) {
72- os .Args = []string {"eirctl" , "run" , "task" , "fail_125" , "-c" , "testdata/eirctl.yaml" }
73- moutW := & bytes.Buffer {}
74- merrW := & bytes.Buffer {}
75- ec := runMain (moutW , merrW )
76-
77- if ec != 125 {
78- t .Fatalf ("process ran wihout error" )
79- }
80-
81- if len (moutW .String ()) < 1 {
82- t .Errorf ("got empty error, expected a message" )
83- }
84- })
85- t .Run ("exited at eirctl command not found" , func (t * testing.T ) {
86- os .Args = []string {"eirctl" , "run" , "task" , "not-found" , "-c" , "testdata/eirctl.yaml" }
87- moutW := & bytes.Buffer {}
88- merrW := & bytes.Buffer {}
89- ec := runMain (moutW , merrW )
14+ for _ , testCase := range []struct {
15+ name string
16+ args []string
17+ expectedLogLevel * logrus.Level
18+ }{
19+ {name : "main sanity check" , args : []string {"eirctl" , "run" , "unknown" }},
20+ {name : "main sanity check (explicit debug)" , args : []string {"eirctl" , "run" , "unknown" , "--debug" }, expectedLogLevel : logLevel (logrus .DebugLevel )},
21+ {name : "main sanity check (explicit verbose)" , args : []string {"eirctl" , "run" , "unknown" , "--verbose" }, expectedLogLevel : logLevel (logrus .TraceLevel )},
22+ } {
23+ t .Run (testCase .name , func (t * testing.T ) {
24+ assertRootCommandFailure (t , testCase .args , testCase .expectedLogLevel )
25+ })
26+ }
27+
28+ for _ , testCase := range []struct {
29+ name string
30+ args []string
31+ expectedCode int
32+ }{
33+ {name : "exit code correctly bubbled up" , args : []string {"eirctl" , "run" , "task" , "fail_125" , "-c" , "testdata/eirctl.yaml" }, expectedCode : 125 },
34+ {name : "exited at eirctl command not found" , args : []string {"eirctl" , "run" , "task" , "not-found" , "-c" , "testdata/eirctl.yaml" }, expectedCode : 1 },
35+ {name : "exited at eirctl with help" , args : []string {"eirctl" , "--help" }, expectedCode : 0 },
36+ } {
37+ t .Run (testCase .name , func (t * testing.T ) {
38+ assertMainExit (t , testCase .args , testCase .expectedCode )
39+ })
40+ }
41+ }
9042
91- if ec != 1 {
92- t . Fatalf ( "process ran wihout error" )
93- }
43+ func logLevel ( level logrus. Level ) * logrus. Level {
44+ return & level
45+ }
9446
95- if len (moutW .String ()) < 1 {
96- t .Errorf ("got empty error, expected a message" )
97- }
98- })
99- t .Run ("exited at eirctl with help" , func (t * testing.T ) {
100- os .Args = []string {"eirctl" , "--help" }
101- moutW := & bytes.Buffer {}
102- merrW := & bytes.Buffer {}
103- ec := runMain (moutW , merrW )
47+ func assertRootCommandFailure (t * testing.T , args []string , expectedLogLevel * logrus.Level ) {
48+ t .Helper ()
49+ withTestArgs (t , args )
50+ previousLogLevel := logrus .GetLevel ()
51+ t .Cleanup (func () { logrus .SetLevel (previousLogLevel ) })
52+
53+ eirctlRootCmd := eirctlcmd .NewEirCtlCmd (context .TODO (), os .Stdout , os .Stderr )
54+ if err := eirctlRootCmd .InitCommand (eirctlcmd .WithSubCommands ()... ); err != nil {
55+ t .Fatal (err )
56+ }
57+
58+ setDefaultCommandIfNonePresent (eirctlRootCmd .Cmd )
59+ if err := eirctlRootCmd .Execute (); err == nil {
60+ t .Error ("got nil wanted error" )
61+ }
62+ if expectedLogLevel != nil && logrus .GetLevel () != * expectedLogLevel {
63+ t .Errorf ("Expected Log Level to be '%s', got: '%s'" , * expectedLogLevel , logrus .GetLevel ())
64+ }
65+ }
10466
105- if ec != 0 {
106- t .Fatalf ("process ran wih error" )
107- }
67+ func assertMainExit (t * testing.T , args []string , expectedCode int ) {
68+ t .Helper ()
69+ withTestArgs (t , args )
70+ stdout := & bytes.Buffer {}
71+ if code := runMain (stdout , & bytes.Buffer {}); code != expectedCode {
72+ t .Fatalf ("got exit code %d, wanted %d" , code , expectedCode )
73+ }
74+ if stdout .Len () < 1 {
75+ t .Error ("got empty error, expected a message" )
76+ }
77+ }
10878
109- if len (moutW .String ()) < 1 {
110- t .Errorf ("got empty error, expected a message" )
111- }
112- })
79+ func withTestArgs (t * testing.T , args []string ) {
80+ t .Helper ()
81+ previousArgs := os .Args
82+ os .Args = args
83+ t .Cleanup (func () { os .Args = previousArgs })
11384}
0 commit comments