Skip to content

Commit

Permalink
Add --filter flag
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Jul 15, 2020
1 parent bde1096 commit 8ea8b1e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 29 deletions.
4 changes: 4 additions & 0 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ 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.StringFlag{
Name: "filter",
Usage: "Filter for specific tests to execute",
},
},
Action: func(c *cli.Context) error {
return app.TestCommand(c.Args().First(), c.Args().Get(1), app.NewAddContextFromCli(c))
Expand Down
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
12 changes: 11 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app

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

const (
//AppName defines the app name
Expand All @@ -15,14 +18,21 @@ type AddCommandContext struct {
NoColor bool
Dir bool
Concurrent int
Filters []string
}

//NewAddContextFromCli is a constructor which creates the context
func NewAddContextFromCli(c *cli.Context) AddCommandContext {
filters := strings.Split(c.String("filter"), ",")
if filters[0] == "" {
filters = make([]string, 0)
}

return AddCommandContext{
Verbose: c.Bool("verbose"),
NoColor: c.Bool("no-color"),
Dir: c.Bool("dir"),
Concurrent: c.Int("concurrent"),
Filters: filters,
}
}
35 changes: 17 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 AddCommandContext) 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 @@ -71,7 +66,7 @@ func testDir(directory string) (runtime.Result, error) {
}

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

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

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

func execute(s suite.Suite, title string) (runtime.Result, error) {
func execute(s suite.Suite, title string) (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.GetTestByTitle(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
9 changes: 6 additions & 3 deletions pkg/app/test_command_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"github.com/SimonBaeumer/commander/pkg/runtime"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
Expand All @@ -18,7 +19,7 @@ tests:

assert.Nil(t, err)

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

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

assert.Nil(t, err)

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

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

assert.Nil(t, err)

got := TestCommand(TestSuiteFile, "my title", AddCommandContext{})
context := AddCommandContext{}
context.Filters = runtime.Filters{"my title"}
got := TestCommand(TestSuiteFile, context)
assert.Nil(t, got)
}
14 changes: 7 additions & 7 deletions pkg/app/test_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import (

func Test_TestCommand_Verbose(t *testing.T) {
out := captureOutput(func() {
TestCommand("commander.yaml", "", AddCommandContext{Verbose: true})
TestCommand("commander.yaml", AddCommandContext{Verbose: true})
log.Println("test test test")
})

assert.Contains(t, out, "test test test")
}

func Test_TestCommand_DefaultFile(t *testing.T) {
err := TestCommand("", "", AddCommandContext{Verbose: true})
err := TestCommand("", AddCommandContext{Verbose: true})
assert.Contains(t, err.Error(), "commander.yaml")
}

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

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

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

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

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

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

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

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

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

if runtime.GOOS == "windows" {
assert.Contains(t, err.Error(), "Test may not be filtered when --dir is enabled")
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
Skipped
)

type Filters []string

// NewRuntime creates a new runtime and inits default nodes
func NewRuntime(eh *EventHandler, nodes ...Node) Runtime {
local := Node{
Expand Down

0 comments on commit 8ea8b1e

Please sign in to comment.