Skip to content

Commit

Permalink
Add filter regex support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Jul 6, 2020
1 parent cffe4b6 commit f900fbc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# v2.2.0

- Remove `filter` as an argument, instead use `--filter` with regex support

# v2.1.0

- Add registry authentication for `docker` nodes
Expand Down
2 changes: 1 addition & 1 deletion cmd/commander/commander.go
Expand Up @@ -65,7 +65,7 @@ func createTestCommand() cli.Command {
},
cli.StringFlag{
Name: "filter",
Usage: "Filter for specific tests to execute",
Usage: "Filter tests by a given regex pattern. Tests are filtered by its title.",
},
},
Action: func(c *cli.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/test_command.go
Expand Up @@ -104,11 +104,11 @@ func testFile(filePath string, filters runtime.Filters) (<-chan runtime.TestResu

// Filter tests if test filters was given
for _, f := range filters {
t, err := s.GetTestByTitle(f)
t, err := s.FindTests(f)
if err != nil {
return nil, err
}
tests = append(tests, t)
tests = append(tests, t...)
}

r := runtime.NewRuntime(s.Nodes...)
Expand Down
10 changes: 0 additions & 10 deletions pkg/app/test_command_test.go
Expand Up @@ -66,16 +66,6 @@ func Test_TestCommand_Dir(t *testing.T) {
}
}

func Test_TestCommand_Dir_FilterTitle(t *testing.T) {
err := TestCommand("/fake", AddCommandContext{Dir: true})

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

func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/suite/suite.go
Expand Up @@ -2,6 +2,7 @@ package suite

import (
"fmt"
"regexp"

"github.com/SimonBaeumer/commander/pkg/runtime"
)
Expand Down Expand Up @@ -50,11 +51,36 @@ func (s Suite) GetTestByTitle(title string) (runtime.TestCase, error) {
if t.Title == title {
return t, nil
}
matched, _ := regexp.Match(title, []byte(t.Title))
if matched {
return t, nil
}
}

return runtime.TestCase{}, fmt.Errorf("could not find test %s", title)
}

// GetTestByTitle returns a test by title, if the test was not found an error is returned
func (s Suite) FindTests(pattern string) ([]runtime.TestCase, error) {
var r []runtime.TestCase
for _, t := range s.GetTests() {
matched, err := regexp.Match(pattern, []byte(t.Title))
if err != nil {
panic(fmt.Sprintf("Regex error %s: %s", pattern, err.Error()))
}

if matched {
r = append(r, t)
}
}

if len(r) == 0 {
return []runtime.TestCase{}, fmt.Errorf("could not find test with pattern: %s", pattern)
}

return r, nil
}

// GetGlobalConfig returns the global configuration which applies to the complete suite
func (s Suite) GetGlobalConfig() runtime.GlobalTestConfig {
return s.Config
Expand Down
25 changes: 25 additions & 0 deletions pkg/suite/suite_test.go
Expand Up @@ -46,3 +46,28 @@ func Test_GetGlobalConfig(t *testing.T) {
s := Suite{Config: runtime.GlobalTestConfig{Dir: "/tmp"}}
assert.Equal(t, "/tmp", s.GetGlobalConfig().Dir)
}

func Test_FindTests(t *testing.T) {
s := Suite{TestCases: []runtime.TestCase{
{Title: "exists"},
{Title: "another"},
{Title: "another one"},
}}

test, _ := s.FindTests("exists")
assert.Len(t, test, 1)
}

func Test_FindMultipleTests(t *testing.T) {
s := Suite{TestCases: []runtime.TestCase{
{Title: "exists"},
{Title: "another"},
{Title: "another one"},
}}

test, _ := s.FindTests("another")
assert.Len(t, test, 2)

test, _ = s.FindTests("another$")
assert.Len(t, test, 1)
}

0 comments on commit f900fbc

Please sign in to comment.