Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Add ability to pass arbitrary arguments to go test #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions gat/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type Run struct {
Tags string
// Additional args to pass to `go test`
Args []string
}

func (run Run) RunAll() {
Expand All @@ -24,12 +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}...)
}
args = append(args, test_files)

args := run.buildCmdArgs(test_files)
command := "go"

if _, err := os.Stat("Godeps/Godeps.json"); err == nil {
Expand All @@ -55,3 +51,29 @@ 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 {
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious. Any specific reason to use {} as a placeholder?

// if {} is not specifed then they will be appened
// to the end of the go test call
for _, arg := range run.Args {
if arg == "{}" {
args = append(args, test_files)
haveAddedFiles = true
} else {
args = append(args, arg)
}
}

if !haveAddedFiles {
args = append(args, test_files)
}

return args
}
16 changes: 8 additions & 8 deletions looper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
package main

import (
"flag"
"log"
"os"
"strconv"

"github.com/nathany/looper/gat"
)
Expand Down Expand Up @@ -43,15 +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.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}
// Pass all args to go test, except the name of the looper command
gtargs := os.Args[1:len(os.Args)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(os.Args) is the default [1:]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is "gt"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have already fixed [1:] on my end, I will push it.

gt == GoTest. I just needed a prefix, I can be more verbose, or now that tags are gone it could just be args.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args sounds good. thanks

runner := gat.Run{Args: gtargs}

Header()
Header(gtargs)
if debug {
DebugEnabled()
}
Expand Down
13 changes: 11 additions & 2 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import (
"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"))

if len(gtargs) > 0 {
fmt.Printf(ansicolor.Green("Passing %d addition argument(s) to go test:\n"), len(gtargs))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. It seems pretty verbose (multiple lines).

Also, don't we already show the full command when it actually executes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be removed. It was mostly a leftover from the first commit that still had differing looper/gotest arguments so it was clear that your arguments were indeed getting picked up (ie: a debugging artifact).

Now that they are all being passed to go test I see no reason to keep this output, which would also revert the method signature to be the same as it was before I tinkered with it.

for _, arg := range gtargs {
fmt.Printf(ansicolor.Green(" %s\n"), arg)
}
fmt.Println(ansicolor.Green(" <files>"))
}

fmt.Println("Type " + ansicolor.Magenta("help") + " for help.\n")
}

Expand Down