Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --filter flag #138

Merged
merged 8 commits into from Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
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
@@ -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
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
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
@@ -1,3 +1,5 @@
tests:
echo hello:
exit-code: 0
echo hello123:
exit-code: 0
1 change: 0 additions & 1 deletion go.mod
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
@@ -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
@@ -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
@@ -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
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
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
@@ -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)
}