Skip to content

Commit

Permalink
Implement filtering based on Test Pattern
Browse files Browse the repository at this point in the history
This allows a pattern like foo.bar.baz to be used with the list and run
commands. This pattern filters the tests that are being run to allow for
only one or a subset of the tests to be run

Signed-off-by: Dave Tucker <dt@docker.com>
  • Loading branch information
Dave Tucker committed May 2, 2017
1 parent e9f44ce commit 06358c6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
6 changes: 3 additions & 3 deletions local/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (g *Group) Name() string {
func (g *Group) List(config RunConfig) []Result {
result := []Result{}

if !WillRun(g.Name(), g.Labels, g.NotLabels, config) {
if !WillRun(g.Labels, g.NotLabels, config) {
return []Result{{
TestResult: Skip,
Name: g.Name(),
Expand All @@ -138,7 +138,7 @@ func (g *Group) List(config RunConfig) []Result {
}

for _, t := range g.Tests {
if WillRun(t.Name(), t.Labels, t.NotLabels, config) {
if WillRun(t.Labels, t.NotLabels, config) && CheckPattern(t.Name(), config.TestPattern) {
result = append(result, Result{
Name: t.Name(),
Summary: t.Tags.Summary,
Expand All @@ -159,7 +159,7 @@ func (g *Group) List(config RunConfig) []Result {
func (g *Group) Run(config RunConfig) ([]Result, error) {
var results []Result

if !WillRun(g.Name(), g.Labels, g.NotLabels, config) {
if !WillRun(g.Labels, g.NotLabels, config) {
return []Result{{TestResult: Skip,
Name: g.Name(),
StartTime: time.Now(),
Expand Down
15 changes: 9 additions & 6 deletions local/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ func ParseLabels(labels string) (map[string]bool, map[string]bool) {
return set, unSet
}

func WillRun(name string, labels, notLabels map[string]bool, config RunConfig) bool {
// 1. Check that name begins with the TestPattern
if !strings.HasPrefix(name, config.TestPattern) {
return false
}

func WillRun(labels, notLabels map[string]bool, config RunConfig) bool {
// 2. Check every test label is in the hostLabels
for l := range labels {
if _, ok := config.Labels[l]; !ok {
Expand All @@ -52,6 +47,14 @@ func WillRun(name string, labels, notLabels map[string]bool, config RunConfig) b
return true
}

func CheckPattern(name, pattern string) bool {
// 1. Check that name begins with the TestPattern
if !strings.HasPrefix(name, pattern) {
return false
}
return true
}

func makeLabelString(labels map[string]bool, notLabels map[string]bool) string {
var l []string
for s := range notLabels {
Expand Down
3 changes: 2 additions & 1 deletion local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (t *Test) Run(config RunConfig) ([]Result, error) {
var results []Result
appendIteration := false

if !WillRun(t.Name(), t.Labels, t.NotLabels, config) {
// NAND WillRun CheckPattern
if !(WillRun(t.Labels, t.NotLabels, config) && CheckPattern(t.Name(), config.TestPattern)) {
config.Logger.Log(logger.LevelSkip, fmt.Sprintf("%s %.2fs", t.Name(), 0.0))
return []Result{{TestResult: Skip,
Name: t.Name(),
Expand Down
28 changes: 25 additions & 3 deletions local/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,30 @@ func TestFindingTests(t *testing.T) {
if err := p.Init(); err != nil {
t.Fatal(err)
}
l := p.List()
for _, t := range l {
fmt.Println("Name: " + t.Name() + " Summary: " + t.Summary)
config := RunConfig{}
l := p.List(config)
for _, tst := range l {
fmt.Printf("Name: %s Summary: %s WillRun: %d\n", tst.Name, tst.Summary, tst.TestResult)
}
}

func TestTestPattern(t *testing.T) {
p, err := NewProject("testdata/cases")
if err != nil {
t.Fatal(err)
}
if err := p.Init(); err != nil {
t.Fatal(err)
}
config := RunConfig{TestPattern: "test.apps.basic"}
l := p.List(config)
for _, tst := range l {
if tst.Name == "test.apps.advanced.test" && tst.TestResult != Skip {
t.Fatal("test.apps.advanced.test does not match the TestPattern test.apps.basic")
}
if tst.Name == "test.apps.basic.test" && tst.TestResult != Pass {
t.Fatal("test.apps.basic.test matches the TestPattern but is not going to run")
}
fmt.Printf("Name: %s Summary: %s WillRun: %d\n", tst.Name, tst.Summary, tst.TestResult)
}
}

0 comments on commit 06358c6

Please sign in to comment.