From 67b3ca2a38835dfc9d38f2a2e27fd7830c8f64ec Mon Sep 17 00:00:00 2001 From: Andrew Burns Date: Mon, 15 Jun 2015 08:33:34 -0600 Subject: [PATCH 1/3] Add ability to pass arbitrary arguments to `go test` --- gat/run.go | 8 ++++++++ looper.go | 11 ++++++++++- print.go | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/gat/run.go b/gat/run.go index 7f80a75..afbbc72 100644 --- a/gat/run.go +++ b/gat/run.go @@ -9,6 +9,9 @@ import ( type Run struct { Tags string + + // Additional args to pass to `go test` + Args []string } func (run Run) RunAll() { @@ -28,6 +31,11 @@ func (run Run) goTest(test_files string) { if len(run.Tags) > 0 { args = append(args, []string{"-tags", run.Tags}...) } + + for _, arg := range run.Args { + args = append(args, arg) + } + args = append(args, test_files) command := "go" diff --git a/looper.go b/looper.go index 45b084e..c295f3e 100644 --- a/looper.go +++ b/looper.go @@ -3,7 +3,9 @@ package main import ( "flag" + "fmt" "log" + "os" "github.com/nathany/looper/gat" ) @@ -47,9 +49,16 @@ func main() { var debug bool flag.StringVar(&tags, "tags", "", "a list of build tags for testing.") flag.BoolVar(&debug, "debug", false, "adds additional logging") + flag.Usage = func() { + fmt.Printf("Usage: %s [options] [-- [go test options]]\n", os.Args[0]) + flag.PrintDefaults() + fmt.Println(` + EXAMPLE: Specify a -run option to go test, run looper in debug mode: + looper -debug -- -run MyTest`) + } flag.Parse() - runner := gat.Run{Tags: tags} + runner := gat.Run{Tags: tags, Args: flag.Args()} Header() if debug { diff --git a/print.go b/print.go index 7920cdc..a557b06 100644 --- a/print.go +++ b/print.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "github.com/koyachi/go-term-ansicolor/ansicolor" @@ -8,6 +9,15 @@ import ( func Header() { fmt.Println(ansicolor.Cyan("Looper 0.3.2 is watching your files")) + + testArgLen := len(flag.Args()) + if testArgLen > 0 { + fmt.Printf(ansicolor.Green("Passing %d addition argument(s) to go test:\n"), testArgLen) + for _, arg := range flag.Args() { + fmt.Printf(ansicolor.Green(" %s\n"), arg) + } + } + fmt.Println("Type " + ansicolor.Magenta("help") + " for help.\n") } From 8c964fd38ba657d2a08f0dc042c66fbe1b7c5086 Mon Sep 17 00:00:00 2001 From: Andrew Burns Date: Mon, 15 Jun 2015 09:00:05 -0600 Subject: [PATCH 2/3] Remove flag options from looper command, pass all command line options directly to `go test`. After the ability to pass arbitrary flags to `go test` [#26](https://github.com/nathany/looper/pull/26), there was no reason to keep the `-tags` flag. Removing `-tags` leaves only `-debug` and that makes sense to move to an environment variable (`$LOOPER_DEBUG`). Removing looper flags allows us to drop the need for `--` to pass options to `go test`, so now we can call looper just like go test. --- gat/run.go | 28 +++++++++++++++------------- looper.go | 23 +++++++---------------- print.go | 13 ++++++------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/gat/run.go b/gat/run.go index afbbc72..56a990a 100644 --- a/gat/run.go +++ b/gat/run.go @@ -8,8 +8,6 @@ import ( ) type Run struct { - Tags string - // Additional args to pass to `go test` Args []string } @@ -27,17 +25,7 @@ func (run Run) RunOnChange(file string) { } func (run Run) goTest(test_files string) { - args := []string{"test"} - if len(run.Tags) > 0 { - args = append(args, []string{"-tags", run.Tags}...) - } - - for _, arg := range run.Args { - args = append(args, arg) - } - - args = append(args, test_files) - + args := run.buildCmdArgs(test_files) command := "go" if _, err := os.Stat("Godeps/Godeps.json"); err == nil { @@ -63,3 +51,17 @@ func (run Run) goTest(test_files string) { func isGoFile(file string) bool { return filepath.Ext(file) == ".go" } + +func (run Run) buildCmdArgs(test_files string) []string { + // go test command: test + args := []string{"test"} + + // additional args passed in on looper cmd line + for _, arg := range run.Args { + args = append(args, arg) + } + + args = append(args, test_files) + + return args +} diff --git a/looper.go b/looper.go index c295f3e..49ea106 100644 --- a/looper.go +++ b/looper.go @@ -2,10 +2,9 @@ package main import ( - "flag" - "fmt" "log" "os" + "strconv" "github.com/nathany/looper/gat" ) @@ -45,22 +44,14 @@ out: } func main() { - var tags string - var debug bool - flag.StringVar(&tags, "tags", "", "a list of build tags for testing.") - flag.BoolVar(&debug, "debug", false, "adds additional logging") - flag.Usage = func() { - fmt.Printf("Usage: %s [options] [-- [go test options]]\n", os.Args[0]) - flag.PrintDefaults() - fmt.Println(` - EXAMPLE: Specify a -run option to go test, run looper in debug mode: - looper -debug -- -run MyTest`) - } - flag.Parse() + // Get debug status from env var, if error ignore and debug is off + debug, _ := strconv.ParseBool(os.Getenv("LOOPER_DEBUG")) - runner := gat.Run{Tags: tags, Args: flag.Args()} + // Pass all args to go test, except the name of the looper command + gtargs := os.Args[1:len(os.Args)] + runner := gat.Run{Args: gtargs} - Header() + Header(gtargs) if debug { DebugEnabled() } diff --git a/print.go b/print.go index a557b06..fbf6442 100644 --- a/print.go +++ b/print.go @@ -1,21 +1,20 @@ package main import ( - "flag" "fmt" "github.com/koyachi/go-term-ansicolor/ansicolor" ) -func Header() { - fmt.Println(ansicolor.Cyan("Looper 0.3.2 is watching your files")) +func Header(gtargs []string) { + fmt.Println(ansicolor.Cyan("Looper 0.3.4 is watching your files")) - testArgLen := len(flag.Args()) - if testArgLen > 0 { - fmt.Printf(ansicolor.Green("Passing %d addition argument(s) to go test:\n"), testArgLen) - for _, arg := range flag.Args() { + if len(gtargs) > 0 { + fmt.Printf(ansicolor.Green("Passing %d addition argument(s) to go test:\n"), len(gtargs)) + for _, arg := range gtargs { fmt.Printf(ansicolor.Green(" %s\n"), arg) } + fmt.Println(ansicolor.Green(" ")) } fmt.Println("Type " + ansicolor.Magenta("help") + " for help.\n") From 442a937ad51a8d92e732606578c0df2bcd9461bd Mon Sep 17 00:00:00 2001 From: Andrew Burns Date: Mon, 15 Jun 2015 10:13:07 -0600 Subject: [PATCH 3/3] Add ability to specify where to inject files to `go test` --- gat/run.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gat/run.go b/gat/run.go index 56a990a..2c38447 100644 --- a/gat/run.go +++ b/gat/run.go @@ -53,15 +53,27 @@ func isGoFile(file string) bool { } func (run Run) buildCmdArgs(test_files string) []string { + var haveAddedFiles bool + // go test command: test args := []string{"test"} // additional args passed in on looper cmd line + // if the arg is {} then the files will be places there + // if {} is not specifed then they will be appened + // to the end of the go test call for _, arg := range run.Args { - args = append(args, arg) + if arg == "{}" { + args = append(args, test_files) + haveAddedFiles = true + } else { + args = append(args, arg) + } } - args = append(args, test_files) + if !haveAddedFiles { + args = append(args, test_files) + } return args }