Skip to content

Commit

Permalink
Merge pull request #66 from k1LoW/run-match
Browse files Browse the repository at this point in the history
Add option RunMatch
  • Loading branch information
k1LoW authored Jun 20, 2022
2 parents a6f8f75 + aae9c4e commit 49e1d65
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -34,6 +35,7 @@ type book struct {
included bool
failFast bool
skipIncluded bool
runMatch *regexp.Regexp
runnerErrs map[string]error
beforeFuncs []func() error
afterFuncs []func() error
Expand Down
8 changes: 3 additions & 5 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,13 @@ type operators struct {

func Load(pathp string, opts ...Option) (*operators, error) {
bk := newBook()
opts = append([]Option{RunMatch(os.Getenv("RUNN_RUN"))}, opts...)
for _, opt := range opts {
if err := opt(bk); err != nil {
return nil, err
}
}

ops := &operators{}
books, err := Books(pathp)
if err != nil {
Expand All @@ -777,12 +779,8 @@ func Load(pathp string, opts ...Option) (*operators, error) {
om[o.bookPath] = o
}

re, err := regexp.Compile(os.Getenv("RUNN_RUN"))
if err != nil {
return nil, err
}
for p, o := range om {
if !re.MatchString(p) {
if !bk.runMatch.MatchString(p) {
o.Debugf(yellow("Skip %s because it does not match RUNN_RUN\n"), p)
continue
}
Expand Down
13 changes: 13 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -263,6 +264,18 @@ func AfterFunc(fn func() error) Option {
}
}

// RunMatch - Run only runbooks with matching paths.
func RunMatch(m string) Option {
return func(bk *book) error {
re, err := regexp.Compile(m)
if err != nil {
return err
}
bk.runMatch = re
return nil
}
}

func included(included bool) Option {
return func(bk *book) error {
bk.included = included
Expand Down
19 changes: 19 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,22 @@ func TestOptionIntarval(t *testing.T) {
}
}
}

func TestRunMatch(t *testing.T) {
tests := []struct {
match string
}{
{""},
{"regexp"},
}
for _, tt := range tests {
bk := newBook()
opt := RunMatch(tt.match)
if err := opt(bk); err != nil {
t.Fatal(err)
}
if bk.runMatch == nil {
t.Error("bk.runMatch should not be nil")
}
}
}

0 comments on commit 49e1d65

Please sign in to comment.