Skip to content

Commit 14a067e

Browse files
committed
Add filter regex support
1 parent 8ea8b1e commit 14a067e

File tree

6 files changed

+51
-13
lines changed

6 files changed

+51
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# v2.2.0
22

33
- Move from `github.com/SimonBaeumer` to `github.com/commander-cli`
4+
- Remove `filter` as an argument, instead use `--filter` with regex support
45

56
# v2.1.0
67

cmd/commander/commander.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func createTestCommand() cli.Command {
6565
},
6666
cli.StringFlag{
6767
Name: "filter",
68-
Usage: "Filter for specific tests to execute",
68+
Usage: "Filter tests by a given regex pattern. Tests are filtered by its title.",
6969
},
7070
},
7171
Action: func(c *cli.Context) error {

pkg/app/test_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func execute(s suite.Suite, title string) (runtime.Result, error)
103103

104104
// Filter tests if test filters was given
105105
for _, f := range filters {
106-
t, err := s.GetTestByTitle(f)
106+
t, err := s.FindTests(f)
107107
if err != nil {
108108
return runtime.Result{}, err
109109
}
110-
tests = append(tests, t)
110+
tests = append(tests, t...)
111111
}
112112

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

pkg/app/test_command_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ func Test_TestCommand_Dir(t *testing.T) {
6868
}
6969
}
7070

71-
func Test_TestCommand_Dir_FilterTitle(t *testing.T) {
72-
err := TestCommand("/fake", AddCommandContext{Dir: true})
73-
74-
if runtime.GOOS == "windows" {
75-
assert.Contains(t, err.Error(), "Test may not be filtered when --dir is enabled")
76-
} else {
77-
assert.Equal(t, "Test may not be filtered when --dir is enabled", err.Error())
78-
}
79-
}
80-
8171
func Test_ConvergeResults(t *testing.T) {
8272
duration, _ := time.ParseDuration("5s")
8373

pkg/suite/suite.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package suite
22

33
import (
44
"fmt"
5+
"regexp"
56

67
"github.com/commander-cli/commander/pkg/runtime"
78
)
@@ -55,6 +56,27 @@ func (s Suite) GetTestByTitle(title string) (runtime.TestCase, error) {
5556
return runtime.TestCase{}, fmt.Errorf("could not find test %s", title)
5657
}
5758

59+
// GetTestByTitle returns a test by title, if the test was not found an error is returned
60+
func (s Suite) FindTests(pattern string) ([]runtime.TestCase, error) {
61+
var r []runtime.TestCase
62+
for _, t := range s.GetTests() {
63+
matched, err := regexp.Match(pattern, []byte(t.Title))
64+
if err != nil {
65+
panic(fmt.Sprintf("Regex error %s: %s", pattern, err.Error()))
66+
}
67+
68+
if matched {
69+
r = append(r, t)
70+
}
71+
}
72+
73+
if len(r) == 0 {
74+
return []runtime.TestCase{}, fmt.Errorf("could not find test with pattern: %s", pattern)
75+
}
76+
77+
return r, nil
78+
}
79+
5880
// GetGlobalConfig returns the global configuration which applies to the complete suite
5981
func (s Suite) GetGlobalConfig() runtime.GlobalTestConfig {
6082
return s.Config

pkg/suite/suite_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,28 @@ func Test_GetGlobalConfig(t *testing.T) {
4646
s := Suite{Config: runtime.GlobalTestConfig{Dir: "/tmp"}}
4747
assert.Equal(t, "/tmp", s.GetGlobalConfig().Dir)
4848
}
49+
50+
func Test_FindTests(t *testing.T) {
51+
s := Suite{TestCases: []runtime.TestCase{
52+
{Title: "exists"},
53+
{Title: "another"},
54+
{Title: "another one"},
55+
}}
56+
57+
test, _ := s.FindTests("exists")
58+
assert.Len(t, test, 1)
59+
}
60+
61+
func Test_FindMultipleTests(t *testing.T) {
62+
s := Suite{TestCases: []runtime.TestCase{
63+
{Title: "exists"},
64+
{Title: "another"},
65+
{Title: "another one"},
66+
}}
67+
68+
test, _ := s.FindTests("another")
69+
assert.Len(t, test, 2)
70+
71+
test, _ = s.FindTests("another$")
72+
assert.Len(t, test, 1)
73+
}

0 commit comments

Comments
 (0)