Skip to content

Commit

Permalink
Add test cases for last-mile cases like version, expected errors, etc (
Browse files Browse the repository at this point in the history
…#93)

* Add test cases for built-in commands
* Add concurrent pipe capture
* parseQueries has no error to return
* Test template errors
* Add before/after hook tests
  • Loading branch information
rdsubhas authored Jun 25, 2017
1 parent eca3bae commit 2da9794
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 26 deletions.
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ func Exec(_args []string) error {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
fmt.Fprintln(mykeOpts.Writer, flagsErr.Message)
return nil
} else {
return err
}
return err
} else {
return Action(&mykeOpts, tasks)
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import (

// Run runs the given tasks
func Run(opts *mykeOpts, tasks []string) error {
queries, err := core.ParseQueries(tasks)
if err != nil {
return errors.Wrap(err, "error parsing command")
}

queries := core.ParseQueries(tasks)
w, err := loadWorkspace(opts.File)
if err != nil {
return err
Expand Down
13 changes: 5 additions & 8 deletions core/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ type queryMatch struct {
}

// ParseQueries parses a query from the command line
func ParseQueries(qs []string) ([]Query, error) {
func ParseQueries(qs []string) []Query {
queries := make([]Query, 0)
for _, q := range splitQueries(qs) {
query, err := parseQuery(q)
if err != nil {
return nil, err
}
query := parseQuery(q)
queries = append(queries, query)
}
return queries, nil
return queries
}

func splitQueries(qs []string) [][]string {
Expand All @@ -49,7 +46,7 @@ func splitQueries(qs []string) [][]string {
return res
}

func parseQuery(tokens []string) (Query, error) {
func parseQuery(tokens []string) Query {
tasks := strings.Split(strings.Trim(tokens[0], " /"), "/")
task, tags := tasks[len(tasks)-1], tasks[:len(tasks)-1]

Expand All @@ -63,7 +60,7 @@ func parseQuery(tokens []string) (Query, error) {
}
}

return Query{Raw: strings.Join(tokens, " "), Task: task, Tags: tags, Params: params}, nil
return Query{Raw: strings.Join(tokens, " "), Task: task, Tags: tags, Params: params}
}

func (q *Query) search(w *Workspace) []queryMatch {
Expand Down
3 changes: 1 addition & 2 deletions core/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ func ParseWorkspace(cwd string) (Workspace, error) {
for p := range in {
if p.err != nil {
return w, p.err
} else {
w.Projects = append(w.Projects, p.p)
}
w.Projects = append(w.Projects, p.p)
}

return w, nil
Expand Down
6 changes: 4 additions & 2 deletions examples/hooks/myke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ desc: |-
these before/after dependencies get passed the full arguments of this task.
tasks:
before:
cmd: echo running before
before: echo running before
cmd: echo
after:
cmd: echo running after
cmd: echo
after: echo running after
error:
cmd: foobar
error: echo there was an error
3 changes: 3 additions & 0 deletions examples/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var tests = []TestTable{
{Arg: ``, Out: `(?m)^\s*tags1\s*\|\s*tagA, tagB\s*\|\s*tag\s*$`},
{Arg: ``, Out: `(?m)^\s*tags2\s*\|\s*tagB, tagC\s*\|\s*tag\s*$`},
{Arg: ``, Out: `(?m)^\s*template\s*\|\s*\|\s*args, envs, file\s*$`},
{Arg: `--help`, Out: `(?s).*Usage.*myke options.*`},
{Arg: `--version`, Out: `.*myke version.*`},
{Arg: `--license`, Out: `.*OPEN SOURCE LICENSES.*`},
{Arg: `-f myke-error.yml`, Err: true, Out: `.*open.*foobar.*`},
}

Expand Down
1 change: 0 additions & 1 deletion examples/template/myke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ project: template
desc: demonstrates use of templates and parameters in command
env:
PARAM1: value1
PARAM2: value2
tasks:
args:
cmd: echo from={{.from|required}} to={{.to|default "something_to"}}
Expand Down
4 changes: 3 additions & 1 deletion examples/template/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ var tests = []TestTable{
{Arg: `args --from=a`, Out: `from=a to=something_to`},
{Arg: `args --from=a --to=b`, Out: `from=a to=b`},
{Arg: `args --from=a args --from=b`, Out: `(?s).*from=a to=something_to.*from=b to=something_to`},
{Arg: `envs`, Out: `(?s).*PARAM1=value2 PARAM2=value2`},
{Arg: `envs`, Err: true, Out: `variable not provided to template`},
{Arg: `envs PARAM2=value2`, Out: `(?s).*PARAM1=value2 PARAM2=value2`},
{Arg: `--template template.tpl`, Out: `(?s)^I am a template.*TEST=TEST.*`},
{Arg: `--template foobar.tpl`, Err: true, Out: `error rendering template`},
}

func Test(t *testing.T) {
Expand Down
16 changes: 11 additions & 5 deletions examples/util/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ func captureChdir(dir string, f func()) {
}

func captureStdout(f func() error) (string, error) {
// Initialize
var err error
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, _ := os.Pipe()

os.Stdout = w
os.Stderr = w
err := f()
os.Stdout = oldStdout
os.Stderr = oldStderr

w.Close()
// Execute command
go func() {
err = f()
w.Close()
}()

// Capture stdout concurrently
var buf bytes.Buffer
io.Copy(&buf, r)
os.Stdout = oldStdout
os.Stderr = oldStderr
return buf.String(), err
}

0 comments on commit 2da9794

Please sign in to comment.