Skip to content

Commit c9a3e57

Browse files
authored
test: add success and error tests to cmd.ExecuteContext (#61)
* fix: cancelled context now exits the process with error code * test: add success and error tests to root.ExecuteContext * test: rewrite root.ExecuteContext tests as table tests * test: add TODO to assert that the event tracker was called
1 parent fee6a2f commit c9a3e57

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func ExecuteContext(ctx context.Context, rootCmd *cobra.Command, clients *shared
361361
clients.IO.PrintDebug(ctx, "... root cleanup waitgroup done.")
362362
cleanup(ctx, clients)
363363
clients.IO.PrintDebug(ctx, "Exiting with cancel exit code.")
364-
os.Exit(int(iostreams.ExitCancel))
364+
clients.Os.Exit(int(iostreams.ExitCancel))
365365
}()
366366
// Received completed execution, so exit the process successfully
367367
case <-completedChan:
@@ -371,7 +371,7 @@ func ExecuteContext(ctx context.Context, rootCmd *cobra.Command, clients *shared
371371
// If we get a second interrupt, no matter what exit the process
372372
<-interruptChan
373373
clients.IO.PrintDebug(ctx, "Got second process interrupt signal, exiting the process")
374-
os.Exit(int(iostreams.ExitCancel))
374+
clients.Os.Exit(int(iostreams.ExitCancel))
375375
}()
376376

377377
// The cleanup() method in the root command will invoke via `defer` from within Execute.
@@ -391,7 +391,7 @@ func ExecuteContext(ctx context.Context, rootCmd *cobra.Command, clients *shared
391391
}
392392
clients.IO.PrintError(ctx, errMsg)
393393
}
394-
defer os.Exit(int(clients.IO.GetExitCode()))
394+
defer clients.Os.Exit(int(clients.IO.GetExitCode()))
395395
completedChan <- true
396396
} else {
397397
completedChan <- true

cmd/root_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"strings"
2222
"testing"
2323

24+
"github.com/slackapi/slack-cli/internal/iostreams"
2425
"github.com/slackapi/slack-cli/internal/shared"
2526
"github.com/slackapi/slack-cli/internal/slackcontext"
2627
"github.com/slackapi/slack-cli/test/testutil"
28+
"github.com/spf13/cobra"
2729
"github.com/stretchr/testify/assert"
2830
"github.com/stretchr/testify/require"
2931
)
@@ -76,6 +78,56 @@ func TestRootCommand(t *testing.T) {
7678
}
7779
}
7880

81+
func TestExecuteContext(t *testing.T) {
82+
tests := map[string]struct {
83+
expectedErr error
84+
expectedExitCode iostreams.ExitCode
85+
expectedOutputs []string
86+
}{
87+
"Command successfully executes": {
88+
expectedErr: nil,
89+
expectedExitCode: iostreams.ExitOK,
90+
},
91+
"Command fails execution and returns an error": {
92+
expectedErr: fmt.Errorf("command failed"),
93+
expectedExitCode: iostreams.ExitError,
94+
expectedOutputs: []string{
95+
"command failed",
96+
},
97+
},
98+
}
99+
for name, tt := range tests {
100+
t.Run(name, func(t *testing.T) {
101+
ctx := slackcontext.MockContext(t.Context())
102+
103+
// Mock clients
104+
clientsMock := shared.NewClientsMock()
105+
clientsMock.AddDefaultMocks()
106+
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
107+
108+
// Mock command
109+
cmd := &cobra.Command{
110+
Use: "mock [flags]",
111+
RunE: func(cmd *cobra.Command, args []string) error {
112+
return tt.expectedErr
113+
},
114+
}
115+
116+
// Execute the command
117+
ExecuteContext(ctx, cmd, clients)
118+
output := clientsMock.GetCombinedOutput()
119+
120+
// Assertions
121+
// TODO: Assert that the event tracker was called with the correct exit code
122+
require.Equal(t, tt.expectedExitCode, clients.IO.GetExitCode())
123+
124+
for _, expectedOutput := range tt.expectedOutputs {
125+
require.Contains(t, output, expectedOutput)
126+
}
127+
})
128+
}
129+
}
130+
79131
// FYI: do not try to run this test in vscode using the run/debug test inline test helper; as the assertions in this test will fail in that context
80132
func TestVersionFlags(t *testing.T) {
81133
ctx := slackcontext.MockContext(t.Context())

0 commit comments

Comments
 (0)