Skip to content

Commit 8ea8b1e

Browse files
committed
Add --filter flag
1 parent bde1096 commit 8ea8b1e

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

cmd/commander/commander.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func createTestCommand() cli.Command {
6363
Name: "dir",
6464
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
6565
},
66+
cli.StringFlag{
67+
Name: "filter",
68+
Usage: "Filter for specific tests to execute",
69+
},
6670
},
6771
Action: func(c *cli.Context) error {
6872
return app.TestCommand(c.Args().First(), c.Args().Get(1), app.NewAddContextFromCli(c))

examples/minimal_test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
tests:
22
echo hello:
33
exit-code: 0
4+
echo hello123:
5+
exit-code: 0

pkg/app/app.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package app
22

3-
import "github.com/urfave/cli"
3+
import (
4+
"github.com/urfave/cli"
5+
"strings"
6+
)
47

58
const (
69
//AppName defines the app name
@@ -15,14 +18,21 @@ type AddCommandContext struct {
1518
NoColor bool
1619
Dir bool
1720
Concurrent int
21+
Filters []string
1822
}
1923

2024
//NewAddContextFromCli is a constructor which creates the context
2125
func NewAddContextFromCli(c *cli.Context) AddCommandContext {
26+
filters := strings.Split(c.String("filter"), ",")
27+
if filters[0] == "" {
28+
filters = make([]string, 0)
29+
}
30+
2231
return AddCommandContext{
2332
Verbose: c.Bool("verbose"),
2433
NoColor: c.Bool("no-color"),
2534
Dir: c.Bool("dir"),
2635
Concurrent: c.Int("concurrent"),
36+
Filters: filters,
2737
}
2838
}

pkg/app/test_command.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ var out output.OutputWriter
1616

1717
// TestCommand executes the test argument
1818
// testPath is the path to the test suite config, it can be a dir or file
19-
// titleFilterTitle is the title of test which should be executed, if empty it will execute all tests
2019
// ctx holds the command flags. If directory scanning is enabled with --dir it is
2120
// not supported to filter tests, therefore testFilterTitle is an empty string
22-
func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext) error {
21+
func TestCommand(testPath string, ctx AddCommandContext) error {
2322
if ctx.Verbose {
2423
log.SetOutput(os.Stdout)
2524
}
@@ -34,16 +33,13 @@ func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext)
3433
var err error
3534
switch {
3635
case ctx.Dir:
37-
if testFilterTitle != "" {
38-
return fmt.Errorf("Test may not be filtered when --dir is enabled")
39-
}
4036
fmt.Println("Starting test against directory: " + testPath + "...")
4137
fmt.Println("")
42-
result, err = testDir(testPath)
38+
result, err = testDir(testPath, ctx.Filters)
4339
default:
4440
fmt.Println("Starting test file " + testPath + "...")
4541
fmt.Println("")
46-
result, err = testFile(testPath, "", testFilterTitle)
42+
result, err = testFile(testPath, ctx.Filters)
4743
}
4844

4945
if err != nil {
@@ -57,9 +53,8 @@ func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext)
5753
return nil
5854
}
5955

60-
func testDir(directory string) (runtime.Result, error) {
56+
func testDir(directory string, filters runtime.Filters) (runtime.Result, error) {
6157
result := runtime.Result{}
62-
6358
files, err := ioutil.ReadDir(directory)
6459
if err != nil {
6560
return result, fmt.Errorf(err.Error())
@@ -71,7 +66,7 @@ func testDir(directory string) (runtime.Result, error) {
7166
}
7267

7368
path := path.Join(directory, f.Name())
74-
newResult, err := testFile(path, f.Name(), "")
69+
newResult, err := testFile(path, f.Name(), filters)
7570
if err != nil {
7671
return result, err
7772
}
@@ -90,25 +85,29 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
9085
return result
9186
}
9287

93-
func testFile(filePath string, fileName string, title string) (runtime.Result, error) {
94-
s, err := readFile(filePath, fileName)
88+
func testFile(filePath string, fileName string, filters runtime.Filters) (<-chan runtime.TestResult, error) {
89+
content, err := readFile(filePath, fileName)
9590
if err != nil {
9691
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
9792
}
9893

99-
return execute(s, title)
94+
return execute(content, filters)
10095
}
10196

102-
func execute(s suite.Suite, title string) (runtime.Result, error) {
97+
func execute(s suite.Suite, title string) (runtime.Result, error)
98+
{
10399
tests := s.GetTests()
100+
if len(filters) != 0 {
101+
tests = []runtime.TestCase{}
102+
}
104103

105-
// Filter tests if test title was given
106-
if title != "" {
107-
test, err := s.GetTestByTitle(title)
104+
// Filter tests if test filters was given
105+
for _, f := range filters {
106+
t, err := s.GetTestByTitle(f)
108107
if err != nil {
109108
return runtime.Result{}, err
110109
}
111-
tests = []runtime.TestCase{test}
110+
tests = append(tests, t)
112111
}
113112

114113
r := runtime.NewRuntime(out.GetEventHandler(), s.Nodes...)

pkg/app/test_command_linux_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"github.com/SimonBaeumer/commander/pkg/runtime"
45
"github.com/stretchr/testify/assert"
56
"io/ioutil"
67
"testing"
@@ -18,7 +19,7 @@ tests:
1819

1920
assert.Nil(t, err)
2021

21-
got := TestCommand(TestSuiteFile, "", AddCommandContext{})
22+
got := TestCommand(TestSuiteFile, AddCommandContext{})
2223
assert.Nil(t, got)
2324
}
2425

@@ -32,7 +33,7 @@ tests:
3233

3334
assert.Nil(t, err)
3435

35-
got := TestCommand(TestSuiteFile, "", AddCommandContext{})
36+
got := TestCommand(TestSuiteFile, AddCommandContext{})
3637
assert.Equal(t, "Test suite failed, use --verbose for more detailed output", got.Error())
3738

3839
}
@@ -51,6 +52,8 @@ tests:
5152

5253
assert.Nil(t, err)
5354

54-
got := TestCommand(TestSuiteFile, "my title", AddCommandContext{})
55+
context := AddCommandContext{}
56+
context.Filters = runtime.Filters{"my title"}
57+
got := TestCommand(TestSuiteFile, context)
5558
assert.Nil(t, got)
5659
}

pkg/app/test_command_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ import (
1616

1717
func Test_TestCommand_Verbose(t *testing.T) {
1818
out := captureOutput(func() {
19-
TestCommand("commander.yaml", "", AddCommandContext{Verbose: true})
19+
TestCommand("commander.yaml", AddCommandContext{Verbose: true})
2020
log.Println("test test test")
2121
})
2222

2323
assert.Contains(t, out, "test test test")
2424
}
2525

2626
func Test_TestCommand_DefaultFile(t *testing.T) {
27-
err := TestCommand("", "", AddCommandContext{Verbose: true})
27+
err := TestCommand("", AddCommandContext{Verbose: true})
2828
assert.Contains(t, err.Error(), "commander.yaml")
2929
}
3030

3131
func Test_TestCommand(t *testing.T) {
32-
err := TestCommand("commander.yaml", "", AddCommandContext{})
32+
err := TestCommand("commander.yaml", AddCommandContext{})
3333

3434
if runtime.GOOS == "windows" {
3535
assert.Contains(t, err.Error(), "Error open commander.yaml:")
@@ -39,7 +39,7 @@ func Test_TestCommand(t *testing.T) {
3939
}
4040

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

4444
if runtime.GOOS == "windows" {
4545
assert.Contains(t, err.Error(), "Error open my-test.yaml:")
@@ -49,7 +49,7 @@ func Test_TestCommand_ShouldUseCustomFile(t *testing.T) {
4949
}
5050

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

5454
if runtime.GOOS == "windows" {
5555
assert.Contains(t, err.Error(), "is a directory")
@@ -59,7 +59,7 @@ func Test_TestCommand_File_WithDir(t *testing.T) {
5959
}
6060

6161
func Test_TestCommand_Dir(t *testing.T) {
62-
err := TestCommand("../../examples", "", AddCommandContext{Dir: true})
62+
err := TestCommand("../../examples", AddCommandContext{Dir: true})
6363

6464
if runtime.GOOS == "windows" {
6565
assert.Contains(t, err.Error(), "Test suite failed, use --verbose for more detailed output")
@@ -69,7 +69,7 @@ func Test_TestCommand_Dir(t *testing.T) {
6969
}
7070

7171
func Test_TestCommand_Dir_FilterTitle(t *testing.T) {
72-
err := TestCommand("/fake", "hello", AddCommandContext{Dir: true})
72+
err := TestCommand("/fake", AddCommandContext{Dir: true})
7373

7474
if runtime.GOOS == "windows" {
7575
assert.Contains(t, err.Error(), "Test may not be filtered when --dir is enabled")

pkg/runtime/runtime.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
Skipped
2323
)
2424

25+
type Filters []string
26+
2527
// NewRuntime creates a new runtime and inits default nodes
2628
func NewRuntime(eh *EventHandler, nodes ...Node) Runtime {
2729
local := Node{

0 commit comments

Comments
 (0)