/
test_command.go
214 lines (173 loc) · 4.8 KB
/
test_command.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package app
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/commander-cli/commander/v2/pkg/output"
"github.com/commander-cli/commander/v2/pkg/runtime"
"github.com/commander-cli/commander/v2/pkg/suite"
)
var (
out output.OutputWriter
overwriteConfigPath string
)
// TestCommand executes the test argument
// testPath is the path to the test suite config, it can be a dir or file
// ctx holds the command flags. If directory scanning is enabled with --dir,
// test filtering is not supported
func TestCommand(testPath string, ctx TestCommandContext) error {
if ctx.Workdir != "" {
err := os.Chdir(ctx.Workdir)
if err != nil {
return fmt.Errorf(err.Error())
}
}
if ctx.Verbose {
log.SetOutput(os.Stdout)
}
overwriteConfigPath = ctx.Config
out = output.NewCliOutput(!ctx.NoColor)
if testPath == "" {
testPath = CommanderFile
}
var result runtime.Result
var err error
switch {
case ctx.Dir:
fmt.Println("Starting test against directory: " + testPath + "...")
fmt.Println("")
result, err = testDir(testPath, ctx.Filters)
case testPath == "-":
fmt.Println("Starting test from stdin...")
fmt.Println("")
result, err = testStdin(ctx.Filters)
case isURL(testPath):
fmt.Println("Starting test from " + testPath + "...")
fmt.Println("")
result, err = testURL(testPath, ctx.Filters)
default:
fmt.Println("Starting test file " + testPath + "...")
fmt.Println("")
result, err = testFile(testPath, "", ctx.Filters)
}
if err != nil {
return fmt.Errorf(err.Error())
}
if !out.PrintSummary(result) && !ctx.Verbose {
return fmt.Errorf("Test suite failed, use --verbose for more detailed output")
}
return nil
}
func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
s, err := getSuite(filePath, fileName)
if err != nil {
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
}
return execute(s, filters)
}
func testDir(directory string, filters runtime.Filters) (runtime.Result, error) {
result := runtime.Result{}
files, err := ioutil.ReadDir(directory)
if err != nil {
return result, fmt.Errorf("Error: Input is not a directory")
}
for _, f := range files {
if f.IsDir() {
continue // skip dirs
}
p := path.Join(directory, f.Name())
newResult, err := testFile(p, f.Name(), filters)
if err != nil {
return result, err
}
result = convergeResults(result, newResult)
}
return result, nil
}
func testURL(url string, filters runtime.Filters) (runtime.Result, error) {
resp, err := http.Get(url)
if err != nil {
return runtime.Result{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return runtime.Result{}, err
}
s := suite.ParseYAML(body, "")
return execute(s, filters)
}
func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
result.TestResults = append(result.TestResults, new.TestResults...)
result.Failed += new.Failed
result.Duration += new.Duration
return result
}
func testStdin(filters runtime.Filters) (runtime.Result, error) {
f, err := os.Stdin.Stat()
if err != nil {
return runtime.Result{}, err
}
if (f.Mode() & os.ModeCharDevice) != 0 {
return runtime.Result{}, fmt.Errorf("Error: when testing from stdin the command is intended to work with pipes")
}
r := bufio.NewReader(os.Stdin)
content, err := ioutil.ReadAll(r)
s := suite.ParseYAML(content, "")
return execute(s, filters)
}
func execute(s suite.Suite, filters runtime.Filters) (runtime.Result, error) {
tests := s.GetTests()
if len(filters) != 0 {
tests = []runtime.TestCase{}
}
// Filter tests if test filters was given
for _, f := range filters {
t, err := s.FindTests(f)
if err != nil {
return runtime.Result{}, err
}
tests = append(tests, t...)
}
r := runtime.NewRuntime(out.GetEventHandler(), s.Nodes...)
result := r.Start(tests)
return result, nil
}
func getSuite(filePath string, fileName string) (suite.Suite, error) {
s := suite.Suite{}
content, err := readFile(filePath)
if err != nil {
return suite.Suite{}, err
}
overwriteContent := []byte("")
if overwriteConfigPath != "" {
overwriteContent, err = readFile(overwriteConfigPath)
if err != nil {
return suite.Suite{}, err
}
}
s = suite.NewSuite(content, overwriteContent, fileName)
return s, nil
}
func readFile(filePath string) ([]byte, error) {
f, err := os.Stat(filePath)
if err != nil {
return nil, fmt.Errorf("open %s: no such file or directory", filePath)
}
if f.IsDir() {
return nil, fmt.Errorf("%s: is a directory\nUse --dir to test directories with multiple test files", filePath)
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return content, nil
}