Skip to content

Commit cd3b80d

Browse files
committed
test: cover root and main error paths
1 parent ebbc276 commit cd3b80d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cmd/root_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmd
33
import (
44
"strings"
55
"testing"
6+
7+
"github.com/spf13/viper"
68
)
79

810
func TestRootCmdUsesRunE(t *testing.T) {
@@ -32,3 +34,71 @@ func TestExecuteReturnsErrorForMissingHosts(t *testing.T) {
3234
t.Fatalf("unexpected error: %v", err)
3335
}
3436
}
37+
38+
func TestExecuteReturnsErrorForInvalidHostSpec(t *testing.T) {
39+
prevHostGroup := hostGroup
40+
prevHostsFile := hostsFile
41+
t.Cleanup(func() {
42+
hostGroup = prevHostGroup
43+
hostsFile = prevHostsFile
44+
RootCmd.SetArgs(nil)
45+
})
46+
47+
hostGroup = ""
48+
hostsFile = ""
49+
RootCmd.SetArgs([]string{":"})
50+
51+
err := Execute()
52+
if err == nil {
53+
t.Fatalf("expected error")
54+
}
55+
if !strings.Contains(err.Error(), "invalid host") {
56+
t.Fatalf("unexpected error: %v", err)
57+
}
58+
}
59+
60+
func TestExecuteReturnsErrorWhenHostsFileCannotBeRead(t *testing.T) {
61+
prevHostGroup := hostGroup
62+
prevHostsFile := hostsFile
63+
t.Cleanup(func() {
64+
hostGroup = prevHostGroup
65+
hostsFile = prevHostsFile
66+
RootCmd.SetArgs(nil)
67+
})
68+
69+
hostGroup = ""
70+
hostsFile = "/path/that/does/not/exist"
71+
RootCmd.SetArgs([]string{"host1"})
72+
73+
err := Execute()
74+
if err == nil {
75+
t.Fatalf("expected error")
76+
}
77+
if !strings.Contains(err.Error(), "unable to read hostsFile") {
78+
t.Fatalf("unexpected error: %v", err)
79+
}
80+
}
81+
82+
func TestExecuteReturnsErrorForInvalidGroupSpec(t *testing.T) {
83+
prevHostGroup := hostGroup
84+
prevHostsFile := hostsFile
85+
t.Cleanup(func() {
86+
hostGroup = prevHostGroup
87+
hostsFile = prevHostsFile
88+
viper.Set("groups.bad", nil)
89+
RootCmd.SetArgs(nil)
90+
})
91+
92+
viper.Set("groups.bad", map[string]interface{}{"user": "deploy"})
93+
hostGroup = "bad"
94+
hostsFile = ""
95+
RootCmd.SetArgs([]string{"host1"})
96+
97+
err := Execute()
98+
if err == nil {
99+
t.Fatalf("expected error")
100+
}
101+
if !strings.Contains(err.Error(), "missing hosts") {
102+
t.Fatalf("unexpected error: %v", err)
103+
}
104+
}

main_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestMainExitsOnExecuteError(t *testing.T) {
12+
if os.Getenv("PRETTY_MAIN_CHILD") == "1" {
13+
os.Args = []string{"pretty"}
14+
main()
15+
return
16+
}
17+
18+
cmd := exec.Command(os.Args[0], "-test.run=TestMainExitsOnExecuteError")
19+
cmd.Env = append(os.Environ(), "PRETTY_MAIN_CHILD=1")
20+
out, err := cmd.CombinedOutput()
21+
22+
var exitErr *exec.ExitError
23+
if !errors.As(err, &exitErr) {
24+
t.Fatalf("expected process exit error, got %v", err)
25+
}
26+
if exitErr.ExitCode() != 1 {
27+
t.Fatalf("expected exit code 1, got %d", exitErr.ExitCode())
28+
}
29+
if !strings.Contains(string(out), "requires at least one host") {
30+
t.Fatalf("expected error output, got %q", string(out))
31+
}
32+
}

0 commit comments

Comments
 (0)