-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathscripts_test.go
More file actions
154 lines (124 loc) · 3.88 KB
/
Copy pathscripts_test.go
File metadata and controls
154 lines (124 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package commands
import (
"encoding/json"
"errors"
"fmt"
"kool-dev/kool/core/environment"
"kool-dev/kool/core/parser"
"kool-dev/kool/core/shell"
"strings"
"testing"
)
func newFakeKoolScripts(mockScripts []string, mockParseErr error) *KoolScripts {
var details []parser.ScriptDetail
for _, script := range mockScripts {
details = append(details, parser.ScriptDetail{Name: script, Comments: []string{}, Commands: []string{}})
}
return &KoolScripts{
*(newDefaultKoolService().Fake()),
&KoolScriptsFlags{},
&parser.FakeParser{
MockScripts: mockScripts,
MockScriptDetails: details,
MockParseAvailableScriptsError: mockParseErr,
},
environment.NewFakeEnvStorage(),
}
}
func TestScriptsCommandListsScripts(t *testing.T) {
f := newFakeKoolScripts([]string{"setup", "lint"}, nil)
cmd := NewScriptsCommand(f)
cmd.SetArgs([]string{})
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing scripts command; error: %v", err)
}
if !f.parser.(*parser.FakeParser).CalledParseAvailableScripts {
t.Errorf("did not call ParseAvailableScripts")
}
fakeShell := f.shell.(*shell.FakeShell)
if !containsLine(fakeShell.OutLines, "setup") || !containsLine(fakeShell.OutLines, "lint") {
t.Errorf("missing scripts on output: %v", fakeShell.OutLines)
}
}
func TestScriptsCommandFiltersScripts(t *testing.T) {
f := newFakeKoolScripts([]string{"setup", "lint"}, nil)
cmd := NewScriptsCommand(f)
cmd.SetArgs([]string{"se"})
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing scripts command; error: %v", err)
}
fakeShell := f.shell.(*shell.FakeShell)
if containsLine(fakeShell.OutLines, "lint") {
t.Errorf("unexpected script on output: %v", fakeShell.OutLines)
}
if !containsLine(fakeShell.OutLines, "setup") {
t.Errorf("missing filtered script on output: %v", fakeShell.OutLines)
}
}
func TestScriptsCommandNoScripts(t *testing.T) {
f := newFakeKoolScripts([]string{}, nil)
cmd := NewScriptsCommand(f)
cmd.SetArgs([]string{})
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing scripts command; error: %v", err)
}
fakeShell := f.shell.(*shell.FakeShell)
if !fakeShell.CalledWarning {
t.Errorf("did not warn about missing scripts")
}
if !strings.Contains(fmt.Sprint(fakeShell.WarningOutput...), "No scripts found") {
t.Errorf("unexpected warning output: %v", fakeShell.WarningOutput)
}
}
func TestScriptsCommandParseError(t *testing.T) {
f := newFakeKoolScripts([]string{"setup"}, errors.New("parse error"))
cmd := NewScriptsCommand(f)
cmd.SetArgs([]string{})
assertExecGotError(t, cmd, "parse error")
}
func TestScriptsCommandJsonOutput(t *testing.T) {
parserMock := &parser.FakeParser{
MockScriptDetails: []parser.ScriptDetail{
{
Name: "setup",
Comments: []string{"Sets up dependencies"},
Commands: []string{"kool run composer install"},
},
{
Name: "lint",
Comments: []string{},
Commands: []string{"kool run go:linux fmt ./..."},
},
},
}
f := newFakeKoolScripts([]string{}, nil)
f.parser = parserMock
cmd := NewScriptsCommand(f)
cmd.SetArgs([]string{"--json"})
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing scripts command; error: %v", err)
}
fakeShell := f.shell.(*shell.FakeShell)
if len(fakeShell.OutLines) == 0 {
t.Errorf("expected JSON output")
return
}
var output []parser.ScriptDetail
if err := json.Unmarshal([]byte(fakeShell.OutLines[0]), &output); err != nil {
t.Fatalf("failed to parse json output: %v", err)
}
if len(output) != 2 {
t.Fatalf("expected 2 script entries, got %d", len(output))
}
if output[0].Name != "lint" || output[1].Name != "setup" {
t.Errorf("unexpected scripts order or names: %v", output)
}
}
func containsLine(lines []string, match string) bool {
for _, line := range lines {
if strings.Contains(line, match) {
return true
}
}
return false
}