Skip to content

Commit cb80fd5

Browse files
committed
Add main command tests and dispatch version tests
1 parent ea4a980 commit cb80fd5

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

cli/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ var (
1010
DispatchCmdLong = `Welcome to Dispatch!
1111
1212
To get started, use the login command to authenticate with Dispatch or create an account.
13-
13+
1414
Documentation: https://docs.dispatch.run
1515
Discord: https://dispatch.run/discord
1616
1717
`
1818
)
1919

20-
// Main is the entry point of the command line.
21-
func Main() error {
20+
func createMainCommand() *cobra.Command {
2221
cmd := &cobra.Command{
2322
Version: version(),
2423
Use: "dispatch",
@@ -50,5 +49,10 @@ func Main() error {
5049
cmd.AddCommand(runCommand())
5150
cmd.AddCommand(versionCommand())
5251

53-
return cmd.ExecuteContext(context.Background())
52+
return cmd
53+
}
54+
55+
// Main is the entry point of the command line.
56+
func Main() error {
57+
return createMainCommand().ExecuteContext(context.Background())
5458
}

cli/main_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cli
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
var expectedCommands = []string{"login", "switch [organization]", "verification", "run", "version"}
11+
12+
func TestMainCommand(t *testing.T) {
13+
t.Run("Main command", func(t *testing.T) {
14+
t.Parallel()
15+
16+
cmd := createMainCommand()
17+
assert.NotNil(t, cmd, "Expected main command to be created")
18+
19+
groups := cmd.Groups()
20+
assert.Len(t, groups, 2, "Expected 2 groups")
21+
assert.Equal(t, "management", groups[0].ID, "Expected first group to be 'management'")
22+
assert.Equal(t, "dispatch", groups[1].ID, "Expected second group to be 'dispatch'")
23+
24+
commands := cmd.Commands()
25+
assert.Len(t, commands, 5, "Expected 5 commands")
26+
27+
// Extract the command IDs
28+
commandIDs := make([]string, 0, len(commands))
29+
for _, command := range commands {
30+
commandIDs = append(commandIDs, command.Use)
31+
}
32+
33+
// Sort slices alphabetically
34+
sort.Strings(expectedCommands)
35+
assert.Equal(t, expectedCommands, commandIDs, "All commands should be present in the main command")
36+
})
37+
}

cli/version.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli
22

33
import (
4-
"fmt"
54
"runtime/debug"
65

76
"github.com/spf13/cobra"
@@ -13,7 +12,7 @@ func versionCommand() *cobra.Command {
1312
Short: "Print the version",
1413
RunE: func(cmd *cobra.Command, args []string) error {
1514
// Match dispatch -v,--version output:
16-
fmt.Println("dispatch version " + version())
15+
cmd.Println("dispatch version " + version())
1716
return nil
1817
},
1918
}

cli/version_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var versionText = "dispatch version devel"
12+
13+
func TestVersionCommand(t *testing.T) {
14+
t.Run("Print the version (test runtime)", func(t *testing.T) {
15+
t.Parallel()
16+
17+
cmd := versionCommand()
18+
stdout := &bytes.Buffer{}
19+
cmd.SetOut(stdout)
20+
21+
if err := cmd.Execute(); err != nil {
22+
t.Fatalf("Received unexpected error: %v", err)
23+
}
24+
25+
assert.Equal(t, versionText+"\n", stdout.String())
26+
})
27+
28+
t.Run("Print the version (binary)", func(t *testing.T) {
29+
t.Parallel()
30+
31+
cmd := exec.Command(dispatchBinary, "version")
32+
stderr := &bytes.Buffer{}
33+
cmd.Stderr = stderr
34+
35+
if err := cmd.Run(); err != nil {
36+
t.Fatalf("Received unexpected error: %v", err)
37+
}
38+
39+
// get git commit hash
40+
cmdGitHash := exec.Command("git", "rev-parse", "HEAD")
41+
stdout := &bytes.Buffer{}
42+
cmdGitHash.Stdout = stdout
43+
44+
if err := cmdGitHash.Run(); err != nil {
45+
t.Fatalf("Received unexpected error: %v", err)
46+
}
47+
48+
version := stdout.String()
49+
50+
assert.Equal(t, versionText+" "+version, stderr.String())
51+
})
52+
}

0 commit comments

Comments
 (0)