Skip to content

Commit d1dc7cf

Browse files
committed
Add --dir flag, add output messages to describe input errors
1 parent b826bb5 commit d1dc7cf

8 files changed

Lines changed: 79 additions & 39 deletions

File tree

cmd/commander/commander.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ func createTestCommand() cli.Command {
6060
EnvVar: "COMMANDER_VERBOSE",
6161
},
6262
cli.BoolFlag{
63-
Name: "file-order",
64-
Usage: "Preserve file order",
65-
EnvVar: "COMMANDER_FILE_ORDER",
63+
Name: "dir",
64+
Usage: "Execute all test files in directory - e.g. /path/to/test_files/",
6665
},
6766
},
6867
Action: func(c *cli.Context) error {

commander_linux.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tests:
1717
exit-code: 0
1818

1919
test directory:
20-
command: ./commander test integration/linux/
20+
command: ./commander test --dir integration/linux/
2121
stdout:
2222
contains:
2323
- ✓ [nodes.yaml] [ssh-host] it should test ssh host
@@ -27,4 +27,11 @@ tests:
2727
- ✓ [nodes.yaml] [ssh-host-default] it should test multiple hosts
2828
- ✓ [nodes.yaml] [local] it should test multiple hosts
2929
- ✓ [docker.yaml] [docker-host] cat /etc/os-release
30-
exit-code: 0
30+
exit-code: 0
31+
32+
test file with directory:
33+
command: ./commander test integration/linux/
34+
stdout:
35+
contains:
36+
- Use --dir to test directories with multiple test files
37+
exit-code: 1

commander_unix.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ tests:
6767
exit-code: 1
6868

6969
test directory order:
70-
command: ./commander test --file-order integration/unix/directory_test/
70+
command: ./commander test --dir integration/unix/directory_test/
7171
stdout:
7272
lines:
7373
3: ✓ [alpha_test.yaml] [local] sleep test
74-
4: ✓ [beta_test.yaml] [local] ehco hello
74+
4: ✓ [beta_test.yaml] [local] ehco hello
75+
76+
test missing dir flag:
77+
command: ./commander test integration/unix/directory_test/
78+
stdout:
79+
contains:
80+
- 'integration/unix/directory_test/: is a directory'
81+
exit-code: 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello, I am file in a directory. Don't delete me
2+
as I am a place hold for commander's integration tests.
3+
Please see missing test 'test missing dir flag' in
4+
commander_unix.yaml for why (:

pkg/app/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
type AddCommandContext struct {
1414
Verbose bool
1515
NoColor bool
16+
Dir bool
1617
Concurrent int
1718
}
1819

@@ -21,6 +22,7 @@ func NewAddContextFromCli(c *cli.Context) AddCommandContext {
2122
return AddCommandContext{
2223
Verbose: c.Bool("verbose"),
2324
NoColor: c.Bool("no-color"),
25+
Dir: c.Bool("dir"),
2426
Concurrent: c.Int("concurrent"),
2527
}
2628
}

pkg/app/test_command.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"log"
77
"os"
8+
"strings"
89
"sync"
910

1011
"github.com/SimonBaeumer/commander/pkg/output"
@@ -13,32 +14,32 @@ import (
1314
)
1415

1516
// TestCommand executes the test argument
16-
// file is the path to the configuration file
17-
// title ist the title of test which should be executed, if empty it will execute all tests
17+
// testPath is the path to the configuration object, an object can be a dir or file
18+
// titleFilterTitle is the title of test which should be executed, if empty it will execute all tests
1819
// ctx holds the command flags
19-
func TestCommand(input string, title string, ctx AddCommandContext) error {
20+
// when --dir is enabled testFilterPath must be a zero value
21+
func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext) error {
2022
if ctx.Verbose == true {
2123
log.SetOutput(os.Stdout)
2224
}
2325

24-
if input == "" {
25-
input = CommanderFile
26-
}
27-
inputType, err := os.Stat(input)
28-
if err != nil {
29-
return fmt.Errorf("Error " + err.Error())
26+
if testPath == "" {
27+
testPath = CommanderFile
3028
}
3129

3230
var results <-chan runtime.TestResult
33-
if inputType.IsDir() {
34-
fmt.Println("Starting test against directory: " + input + "...")
31+
var err error
32+
if ctx.Dir {
33+
if testFilterTitle != "" {
34+
return fmt.Errorf("Test may not be filtered when --dir is enabled")
35+
}
36+
fmt.Println("Starting test against directory: " + testPath + "...")
3537
fmt.Println("")
36-
// since no handling of file errors occur should we not allow title
37-
results, err = testDir(input, title)
38+
results, err = testDir(testPath, testFilterTitle)
3839
} else {
39-
fmt.Println("Starting test file " + input + "...")
40+
fmt.Println("Starting test file " + testPath + "...")
4041
fmt.Println("")
41-
results, err = testFile(input, title)
42+
results, err = testFile(testPath, testFilterTitle)
4243
}
4344

4445
if err != nil {
@@ -56,7 +57,7 @@ func TestCommand(input string, title string, ctx AddCommandContext) error {
5657
func testDir(directory string, title string) (<-chan runtime.TestResult, error) {
5758
files, err := ioutil.ReadDir(directory)
5859
if err != nil {
59-
return nil, fmt.Errorf("Error " + err.Error())
60+
return nil, fmt.Errorf(err.Error())
6061
}
6162

6263
results := make(chan runtime.TestResult)
@@ -94,6 +95,9 @@ func testDir(directory string, title string) (<-chan runtime.TestResult, error)
9495
func testFile(input string, title string) (<-chan runtime.TestResult, error) {
9596
content, err := ioutil.ReadFile(input)
9697
if err != nil {
98+
if strings.Contains(err.Error(), "is a directory") {
99+
return nil, fmt.Errorf("Error %s: is a directory\nUse --dir to test directories with multiple test files", input)
100+
}
97101
return nil, fmt.Errorf("Error " + err.Error())
98102
}
99103

pkg/app/test_command_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,52 @@ func Test_TestCommand(t *testing.T) {
3030
err := TestCommand("commander.yaml", "", AddCommandContext{})
3131

3232
if runtime.GOOS == "windows" {
33-
assert.Contains(t, err.Error(), "Error CreateFile commander.yaml:")
33+
assert.Contains(t, err.Error(), "Error open commander.yaml:")
3434
} else {
35-
assert.Equal(t, "Error stat commander.yaml: no such file or directory", err.Error())
35+
assert.Equal(t, "Error open commander.yaml: no such file or directory", err.Error())
3636
}
3737
}
3838

3939
func Test_TestCommand_ShouldUseCustomFile(t *testing.T) {
4040
err := TestCommand("my-test.yaml", "", AddCommandContext{})
4141

4242
if runtime.GOOS == "windows" {
43-
assert.Contains(t, err.Error(), "Error CreateFile my-test.yaml: ")
43+
assert.Contains(t, err.Error(), "Error open my-test.yaml:")
4444
} else {
45-
assert.Equal(t, "Error stat my-test.yaml: no such file or directory", err.Error())
45+
assert.Equal(t, "Error open my-test.yaml: no such file or directory", err.Error())
4646
}
4747
}
4848

49-
func Test_TestCommand_Dir(t *testing.T) {
49+
func Test_TestCommand_File_WithDir(t *testing.T) {
5050
err := TestCommand("../../examples", "", AddCommandContext{})
5151

52+
if runtime.GOOS == "windows" {
53+
assert.Contains(t, err.Error(), "is a directory")
54+
} else {
55+
assert.Equal(t, "Error ../../examples: is a directory\nUse --dir to test directories with multiple test files", err.Error())
56+
}
57+
}
58+
59+
func Test_TestCommand_Dir(t *testing.T) {
60+
err := TestCommand("../../examples", "", AddCommandContext{Dir: true})
61+
5262
if runtime.GOOS == "windows" {
5363
assert.Contains(t, err.Error(), "Test suite failed, use --verbose for more detailed output")
5464
} else {
5565
assert.Equal(t, "Test suite failed, use --verbose for more detailed output", err.Error())
5666
}
5767
}
5868

69+
func Test_TestCommand_Dir_FilterTitle(t *testing.T) {
70+
err := TestCommand("/fake", "hello", AddCommandContext{Dir: true})
71+
72+
if runtime.GOOS == "windows" {
73+
assert.Contains(t, err.Error(), "Test may not be filtered when --dir is enabled")
74+
} else {
75+
assert.Equal(t, "Test may not be filtered when --dir is enabled", err.Error())
76+
}
77+
}
78+
5979
func captureOutput(f func()) string {
6080
reader, writer, err := os.Pipe()
6181
if err != nil {

pkg/output/cli.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
4141

4242
for r := range results {
4343
testResults = append(testResults, r)
44-
failed += w.printResult(r)
44+
if r.ValidationResult.Success {
45+
w.printResult(r, "✓")
46+
} else {
47+
w.printResult(r, "✗")
48+
failed++
49+
}
4550
}
4651

4752
duration := time.Since(start)
@@ -62,19 +67,11 @@ func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
6267
return failed == 0
6368
}
6469

65-
func (w *OutputWriter) printResult(r runtime.TestResult) int {
66-
if !r.ValidationResult.Success {
67-
str := fmt.Sprintf("✗ [%s] %s", r.Node, r.TestCase.Title)
68-
str = w.addFile(str, r)
69-
s := w.addTries(str, r)
70-
w.fprintf(au.Red(s))
71-
return 1
72-
}
73-
str := fmt.Sprintf("✓ [%s] %s", r.Node, r.TestCase.Title)
70+
func (w *OutputWriter) printResult(r runtime.TestResult, mark string) {
71+
str := fmt.Sprintf("%s [%s] %s", mark, r.Node, r.TestCase.Title)
7472
str = w.addFile(str, r)
7573
s := w.addTries(str, r)
7674
w.fprintf(s)
77-
return 0
7875
}
7976

8077
func (w *OutputWriter) printFailures(results []runtime.TestResult) {

0 commit comments

Comments
 (0)