|
| 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 | +} |
0 commit comments