Skip to content

Commit

Permalink
Merge pull request #138 from commander-cli/add-filter-flag
Browse files Browse the repository at this point in the history
Add --filter flag
  • Loading branch information
SimonBaeumer authored Jul 17, 2020
2 parents bde1096 + cb91de1 commit d896521
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 59 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:

before_install:
- go get -u golang.org/x/lint/golint
- curl -L https://github.com/commander-cli/commander/releases/download/v1.2.1/commander-linux-amd64 -o ~/bin/commander
- curl -L https://github.com/commander-cli/commander/releases/download/v2.1.0/commander-linux-amd64 -o ~/bin/commander
- chmod +x ~/bin/commander

jobs:
Expand All @@ -37,7 +37,7 @@ jobs:
- name: macOS integration
os: osx
script:
- curl -L https://github.com/commander-cli/commander/releases/download/v0.3.0/commander-darwin-amd64 -o ~/bin/commander
- curl -L https://github.com/commander-cli/commander/releases/download/v2.1.0/commander-darwin-amd64 -o ~/bin/commander
- chmod +x ~/bin/commander
- make integration-unix

Expand All @@ -53,7 +53,7 @@ jobs:
before_install:
- choco install make
- choco install curl
- curl -L https://github.com/commander-cli/commander/releases/download/v1.2.1/commander-windows-amd64 -o C:\Windows\system32\commander.exe
- curl -L https://github.com/commander-cli/commander/releases/download/v2.1.0/commander-windows-amd64 -o C:\Windows\system32\commander.exe
script:
- make integration-windows

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v2.2.0

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

# v2.1.0

Expand Down
15 changes: 13 additions & 2 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func createTestCommand() cli.Command {
return cli.Command{
Name: "test",
Usage: "Execute the test suite, by default it will use the commander.yaml from your current directory",
ArgsUsage: "[file] [title]",
ArgsUsage: "[file] [--filter]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "no-color",
Expand All @@ -63,9 +63,20 @@ func createTestCommand() cli.Command {
Name: "dir",
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
},
cli.StringSliceFlag{
Name: "filter",
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.
Example:
test commander.yaml --filter="my test"
Apply multiple filters:
test commander.yaml --filter=filter1 --filter=filter2
`,
},
},
Action: func(c *cli.Context) error {
return app.TestCommand(c.Args().First(), c.Args().Get(1), app.NewAddContextFromCli(c))
return app.TestCommand(c.Args().First(), app.NewTestContextFromCli(c))
},
}
}
Expand Down
34 changes: 33 additions & 1 deletion commander_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,36 @@ tests:
stdout:
contains:
- 'integration/unix/directory_test/: is a directory'
exit-code: 1
exit-code: 1

test filter flag works:
command: ./commander test integration/unix/filter_test.yaml --filter=executed
stdout:
contains:
- ✓ [local] should be executed
- ✓ [local] should also be executed
not-contains:
- should be filtered
exit-code: 0

test filter flag with regex:
command: ./commander test integration/unix/filter_test.yaml --filter="executed$"
stdout:
contains:
- ✓ [local] should be executed
- ✓ [local] should also be executed
not-contains:
- should be ignored
- executed at the beginning is ignored
exit-code: 0

test mulitple filters:
command: ./commander test integration/unix/filter_test.yaml --filter="executed$" --filter="should be ignored"
stdout:
contains:
- ✓ [local] should be executed
- ✓ [local] should also be executed
- ✓ [local] should be ignored
not-contains:
- executed at the beginning is ignored
exit-code: 0
2 changes: 2 additions & 0 deletions examples/minimal_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tests:
echo hello:
exit-code: 0
echo hello123:
exit-code: 0
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module github.com/commander-cli/commander

require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/SimonBaeumer/commander v1.3.0 // indirect
github.com/antchfx/xmlquery v1.1.0
github.com/antchfx/xpath v1.1.0 // indirect
github.com/commander-cli/cmd v1.3.0
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/SimonBaeumer/cmd v1.1.0 h1:tr5dUMlly/8bLiC5B0J1AcE4ISru8POEfzAirWnUJnY=
github.com/SimonBaeumer/cmd v1.1.0/go.mod h1:4mc/LDXDWNbkeooqHP83yx3JXtInPHjJkF8zhzqqmZE=
github.com/SimonBaeumer/commander v1.3.0 h1:QwZ7sTYFrmkOAccZwu8LZltDC08kIEcFjBBOkpjdZIg=
github.com/SimonBaeumer/commander v1.3.0/go.mod h1:sNvvvwudBa2kQcSO7G1PttZHcBUnS5LmtJAjDS1Yh+U=
github.com/antchfx/xmlquery v1.1.0 h1:vj0kZ1y3Q6my4AV+a9xbWrMYzubw+84zuiKgvfV8vb8=
github.com/antchfx/xmlquery v1.1.0/go.mod h1:/+CnyD/DzHRnv2eRxrVbieRU/FIF6N0C+7oTtyUtCKk=
github.com/antchfx/xpath v1.1.0 h1:mJTvYpiHvxNQRD4Lbfin/FodHVCHh2a5KrOFr4ZxMOI=
Expand Down Expand Up @@ -40,9 +36,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI=
Expand Down
16 changes: 16 additions & 0 deletions integration/unix/filter_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests:
should be executed:
command: echo executed
exit-code: 0

should also be executed:
command: echo executed
exit-code: 0

executed at the beginning is ignored:
command: echo ignored
exit-code: 0

should be ignored:
command: echo filtered
exit-code: 0
16 changes: 10 additions & 6 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import "github.com/urfave/cli"
import (
"github.com/urfave/cli"
)

const (
//AppName defines the app name
Expand All @@ -9,20 +11,22 @@ const (
CommanderFile = "commander.yaml"
)

//AddCommandContext holds all flags for the add command
type AddCommandContext struct {
//TestCommandContext holds all flags for the add command
type TestCommandContext struct {
Verbose bool
NoColor bool
Dir bool
Concurrent int
Filters []string
}

//NewAddContextFromCli is a constructor which creates the context
func NewAddContextFromCli(c *cli.Context) AddCommandContext {
return AddCommandContext{
//NewTestContextFromCli is a constructor which creates the context
func NewTestContextFromCli(c *cli.Context) TestCommandContext {
return TestCommandContext{
Verbose: c.Bool("verbose"),
NoColor: c.Bool("no-color"),
Dir: c.Bool("dir"),
Concurrent: c.Int("concurrent"),
Filters: c.StringSlice("filter"),
}
}
4 changes: 2 additions & 2 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
)

func TestNewAddCommandContextFromCli(t *testing.T) {
func TestNewTestCommandContextFromCli(t *testing.T) {
set := flag.NewFlagSet("verbose", 0)
set.Bool("verbose", true, "")
set.Bool("no-color", true, "")
Expand All @@ -16,7 +16,7 @@ func TestNewAddCommandContextFromCli(t *testing.T) {
context := &cli.Context{}
ctx := cli.NewContext(nil, set, context)

r := NewAddContextFromCli(ctx)
r := NewTestContextFromCli(ctx)

assert.True(t, r.Verbose)
assert.True(t, r.NoColor)
Expand Down
34 changes: 16 additions & 18 deletions pkg/app/test_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ var out output.OutputWriter

// TestCommand executes the test argument
// testPath is the path to the test suite config, it can be a dir or file
// titleFilterTitle is the title of test which should be executed, if empty it will execute all tests
// ctx holds the command flags. If directory scanning is enabled with --dir it is
// not supported to filter tests, therefore testFilterTitle is an empty string
func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext) error {
func TestCommand(testPath string, ctx TestCommandContext) error {
if ctx.Verbose {
log.SetOutput(os.Stdout)
}
Expand All @@ -34,16 +33,13 @@ func TestCommand(testPath string, testFilterTitle string, ctx AddCommandContext)
var err error
switch {
case ctx.Dir:
if testFilterTitle != "" {
return fmt.Errorf("Test may not be filtered when --dir is enabled")
}
fmt.Println("Starting test against directory: " + testPath + "...")
fmt.Println("")
result, err = testDir(testPath)
result, err = testDir(testPath, ctx.Filters)
default:
fmt.Println("Starting test file " + testPath + "...")
fmt.Println("")
result, err = testFile(testPath, "", testFilterTitle)
result, err = testFile(testPath, "", ctx.Filters)
}

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

func testDir(directory string) (runtime.Result, error) {
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(err.Error())
Expand All @@ -70,8 +65,8 @@ func testDir(directory string) (runtime.Result, error) {
continue // skip dirs
}

path := path.Join(directory, f.Name())
newResult, err := testFile(path, f.Name(), "")
p := path.Join(directory, f.Name())
newResult, err := testFile(p, f.Name(), filters)
if err != nil {
return result, err
}
Expand All @@ -90,25 +85,28 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
return result
}

func testFile(filePath string, fileName string, title string) (runtime.Result, error) {
func testFile(filePath string, fileName string, filters runtime.Filters) (runtime.Result, error) {
s, err := readFile(filePath, fileName)
if err != nil {
return runtime.Result{}, fmt.Errorf("Error " + err.Error())
}

return execute(s, title)
return execute(s, filters)
}

func execute(s suite.Suite, title string) (runtime.Result, error) {
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 title was given
if title != "" {
test, err := s.GetTestByTitle(title)
// Filter tests if test filters was given
for _, f := range filters {
t, err := s.FindTests(f)
if err != nil {
return runtime.Result{}, err
}
tests = []runtime.TestCase{test}
tests = append(tests, t...)
}

r := runtime.NewRuntime(out.GetEventHandler(), s.Nodes...)
Expand Down
12 changes: 8 additions & 4 deletions pkg/app/test_command_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package app

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"

"github.com/commander-cli/commander/pkg/runtime"
"github.com/stretchr/testify/assert"
)

const TestSuiteFile = "/tmp/commander_test.yaml"
Expand All @@ -18,7 +20,7 @@ tests:

assert.Nil(t, err)

got := TestCommand(TestSuiteFile, "", AddCommandContext{})
got := TestCommand(TestSuiteFile, TestCommandContext{})
assert.Nil(t, got)
}

Expand All @@ -32,7 +34,7 @@ tests:

assert.Nil(t, err)

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

}
Expand All @@ -51,6 +53,8 @@ tests:

assert.Nil(t, err)

got := TestCommand(TestSuiteFile, "my title", AddCommandContext{})
context := TestCommandContext{}
context.Filters = runtime.Filters{"my title"}
got := TestCommand(TestSuiteFile, context)
assert.Nil(t, got)
}
Loading

0 comments on commit d896521

Please sign in to comment.