Skip to content

Commit 9900ff9

Browse files
authored
Merge pull request #16 from roots/tests
Add command tests
2 parents 20ac1f4 + 696208f commit 9900ff9

17 files changed

+607
-33
lines changed

cmd/cmd_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
type MockProject struct {
12+
detected bool
13+
}
14+
15+
func (p *MockProject) Detect(path string) (projectPath string, ok bool) {
16+
return "trellis", p.detected
17+
}
18+
19+
func mockExecCommand(command string, args ...string) *exec.Cmd {
20+
cs := []string{"-test.run=TestHelperProcess", "--", command}
21+
cs = append(cs, args...)
22+
cmd := exec.Command(os.Args[0], cs...)
23+
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
24+
return cmd
25+
}
26+
27+
func TestHelperProcess(t *testing.T) {
28+
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
29+
return
30+
}
31+
32+
fmt.Fprintf(os.Stdout, strings.Join(os.Args[3:], " "))
33+
os.Exit(0)
34+
}

cmd/deploy.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"log"
6-
"os/exec"
76
"strings"
87

98
"github.com/mitchellh/cli"
@@ -30,7 +29,7 @@ func (c *DeployCommand) Run(args []string) int {
3029
c.UI.Output(c.Help())
3130
return 1
3231
case 1:
33-
c.UI.Error("Missing SITE argument\n")
32+
c.UI.Error("Error: missing SITE argument\n")
3433
c.UI.Output(c.Help())
3534
return 1
3635
case 2:
@@ -42,8 +41,8 @@ func (c *DeployCommand) Run(args []string) int {
4241
return 1
4342
}
4443

45-
deploy := exec.Command("./bin/deploy.sh", environment, siteName)
46-
logCmd(deploy, true)
44+
deploy := execCommand("./bin/deploy.sh", environment, siteName)
45+
logCmd(deploy, c.UI, true)
4746
err := deploy.Run()
4847

4948
if err != nil {

cmd/deploy_test.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package cmd
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
8+
"github.com/mitchellh/cli"
9+
"trellis-cli/trellis"
10+
)
11+
12+
func TestDeployRunValidations(t *testing.T) {
13+
ui := cli.NewMockUi()
14+
15+
cases := []struct {
16+
name string
17+
projectDetected bool
18+
args []string
19+
out string
20+
code int
21+
}{
22+
{
23+
"no_project",
24+
false,
25+
nil,
26+
"No Trellis project detected",
27+
1,
28+
},
29+
{
30+
"no_args",
31+
true,
32+
nil,
33+
"Usage: trellis",
34+
1,
35+
},
36+
{
37+
"missing_site_arg",
38+
true,
39+
[]string{"development"},
40+
"Error: missing SITE argument",
41+
1,
42+
},
43+
{
44+
"too_many_args",
45+
true,
46+
[]string{"development", "site", "foo"},
47+
"Error: too many arguments",
48+
1,
49+
},
50+
}
51+
52+
for _, tc := range cases {
53+
mockProject := &MockProject{tc.projectDetected}
54+
trellis := trellis.NewTrellis(mockProject)
55+
deployCommand := &DeployCommand{ui, trellis}
56+
57+
code := deployCommand.Run(tc.args)
58+
59+
if code != tc.code {
60+
t.Errorf("expected code %d to be %d", code, tc.code)
61+
}
62+
63+
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
64+
65+
if !strings.Contains(combined, tc.out) {
66+
t.Errorf("expected output %q to contain %q", combined, tc.out)
67+
}
68+
}
69+
}
70+
71+
func TestDeployRun(t *testing.T) {
72+
ui := cli.NewMockUi()
73+
mockProject := &MockProject{true}
74+
trellis := trellis.NewTrellis(mockProject)
75+
deployCommand := &DeployCommand{ui, trellis}
76+
77+
execCommand = mockExecCommand
78+
defer func() { execCommand = exec.Command }()
79+
80+
cases := []struct {
81+
name string
82+
args []string
83+
out string
84+
code int
85+
}{
86+
{
87+
"default",
88+
[]string{"development", "example.com"},
89+
"./bin/deploy.sh development example.com",
90+
0,
91+
},
92+
}
93+
94+
for _, tc := range cases {
95+
code := deployCommand.Run(tc.args)
96+
97+
if code != tc.code {
98+
t.Errorf("expected code %d to be %d", code, tc.code)
99+
}
100+
101+
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
102+
103+
if !strings.Contains(combined, tc.out) {
104+
t.Errorf("expected output %q to contain %q", combined, tc.out)
105+
}
106+
}
107+
}

cmd/exec.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package cmd
22

33
import (
44
"fmt"
5+
"github.com/mitchellh/cli"
56
"os"
67
"os/exec"
78
"strings"
89
)
910

10-
func logCmd(cmd *exec.Cmd, output bool) {
11+
func logCmd(cmd *exec.Cmd, ui cli.Ui, output bool) {
1112
cmd.Stderr = os.Stderr
1213

1314
if output {
14-
cmd.Stdout = os.Stdout
15+
cmd.Stdout = &cli.UiWriter{ui}
1516
}
1617

1718
fmt.Println("Running command =>", strings.Join(cmd.Args, " "))

cmd/galaxy_install.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cmd
22

33
import (
4-
"github.com/mitchellh/cli"
4+
"fmt"
55
"log"
6-
"os/exec"
76
"strings"
7+
8+
"github.com/mitchellh/cli"
89
"trellis-cli/trellis"
910
)
1011

@@ -19,8 +20,14 @@ func (c *GalaxyInstallCommand) Run(args []string) int {
1920
return 1
2021
}
2122

22-
galaxyInstall := exec.Command("ansible-galaxy", "install", "-r", "requirements.yml")
23-
logCmd(galaxyInstall, true)
23+
if len(args) > 0 {
24+
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
25+
c.UI.Output(c.Help())
26+
return 1
27+
}
28+
29+
galaxyInstall := execCommand("ansible-galaxy", "install", "-r", "requirements.yml")
30+
logCmd(galaxyInstall, c.UI, true)
2431
err := galaxyInstall.Run()
2532

2633
if err != nil {

cmd/galaxy_install_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package cmd
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
8+
"github.com/mitchellh/cli"
9+
"trellis-cli/trellis"
10+
)
11+
12+
func TestGalaxyInstallRunValidations(t *testing.T) {
13+
ui := cli.NewMockUi()
14+
15+
cases := []struct {
16+
name string
17+
projectDetected bool
18+
args []string
19+
out string
20+
code int
21+
}{
22+
{
23+
"no_project",
24+
false,
25+
nil,
26+
"No Trellis project detected",
27+
1,
28+
},
29+
{
30+
"too_many_args",
31+
true,
32+
[]string{"foo"},
33+
"Error: too many arguments",
34+
1,
35+
},
36+
}
37+
38+
for _, tc := range cases {
39+
mockProject := &MockProject{tc.projectDetected}
40+
trellis := trellis.NewTrellis(mockProject)
41+
galaxyInstallCommand := GalaxyInstallCommand{ui, trellis}
42+
43+
code := galaxyInstallCommand.Run(tc.args)
44+
45+
if code != tc.code {
46+
t.Errorf("expected code %d to be %d", code, tc.code)
47+
}
48+
49+
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
50+
51+
if !strings.Contains(combined, tc.out) {
52+
t.Errorf("expected output %q to contain %q", combined, tc.out)
53+
}
54+
}
55+
}
56+
57+
func TestGalaxyInstallRun(t *testing.T) {
58+
ui := cli.NewMockUi()
59+
mockProject := &MockProject{true}
60+
trellis := trellis.NewTrellis(mockProject)
61+
galaxyInstallCommand := GalaxyInstallCommand{ui, trellis}
62+
63+
execCommand = mockExecCommand
64+
defer func() { execCommand = exec.Command }()
65+
66+
cases := []struct {
67+
name string
68+
args []string
69+
out string
70+
code int
71+
}{
72+
{
73+
"default",
74+
[]string{},
75+
"ansible-galaxy install -r requirements.yml",
76+
0,
77+
},
78+
}
79+
80+
for _, tc := range cases {
81+
code := galaxyInstallCommand.Run(tc.args)
82+
83+
if code != tc.code {
84+
t.Errorf("expected code %d to be %d", code, tc.code)
85+
}
86+
87+
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
88+
89+
if !strings.Contains(combined, tc.out) {
90+
t.Errorf("expected output %q to contain %q", combined, tc.out)
91+
}
92+
}
93+
}

cmd/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cmd
2+
3+
import (
4+
"os/exec"
5+
)
6+
7+
var execCommand = exec.Command

cmd/provision.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"fmt"
66
"log"
7-
"os/exec"
87
"strings"
98

109
"github.com/mitchellh/cli"
@@ -49,7 +48,7 @@ func (c *ProvisionCommand) Run(args []string) int {
4948

5049
switch len(args) {
5150
case 0:
52-
c.UI.Error("Missing ENVIRONMENT argument\n")
51+
c.UI.Error("Error: missing ENVIRONMENT argument\n")
5352
c.UI.Output(c.Help())
5453
return 1
5554
case 1:
@@ -73,8 +72,8 @@ func (c *ProvisionCommand) Run(args []string) int {
7372
playbookArgs = append(playbookArgs, "--tags", c.tags)
7473
}
7574

76-
playbook := exec.Command("ansible-playbook", playbookArgs...)
77-
logCmd(playbook, true)
75+
playbook := execCommand("ansible-playbook", playbookArgs...)
76+
logCmd(playbook, c.UI, true)
7877
err := playbook.Run()
7978

8079
if err != nil {

0 commit comments

Comments
 (0)