Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #772 from gobuffalo/test-individual-methods
Browse files Browse the repository at this point in the history
Running a single test fixes #769
  • Loading branch information
markbates authored Nov 27, 2017
2 parents 5e03e0a + ad053c7 commit 6bb737f
Showing 1 changed file with 73 additions and 18 deletions.
91 changes: 73 additions & 18 deletions buffalo/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strings"

"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/envy"
"github.com/pkg/errors"

Expand Down Expand Up @@ -81,32 +82,86 @@ func findSchema() io.Reader {
}

func testRunner(args []string) error {
cmd := exec.Command(envy.Get("GO_BIN", "go"), "test", "-p", "1")
if _, err := exec.LookPath("gotest"); err == nil {
cmd = exec.Command("gotest", "-p", "1")
}
cmd.Args = append(cmd.Args, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
runFlag := false
for _, a := range cmd.Args {
cmd := newTestCmd(args)
var runFlag bool
var mFlag bool
for i, a := range args {
if a == "-run" {
runFlag = true
}
if a == "-m" {
mFlag = true
args[i] = "-testify.m"
}
}

if mFlag {
return mFlagRunner(args)
}

if !runFlag {
out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
pkgs, err := testPackages()
if err != nil {
return err
}
pkgs := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
for _, p := range pkgs {
if !vendorRegex.Match(p) {
cmd.Args = append(cmd.Args, string(p))
}
return errors.WithStack(err)
}
cmd.Args = append(cmd.Args, pkgs...)
}
fmt.Println(strings.Join(cmd.Args, " "))
return cmd.Run()
}

func mFlagRunner(args []string) error {
app := meta.New(".")
pwd, _ := os.Getwd()
defer os.Chdir(pwd)

pkgs, err := testPackages()
if err != nil {
return errors.WithStack(err)
}
var errs bool
for _, p := range pkgs {
os.Chdir(pwd)
if p == app.PackagePkg {
continue
}
cmd := newTestCmd(args)
p = strings.TrimPrefix(p, app.PackagePkg+string(filepath.Separator))
fmt.Println(strings.Join(cmd.Args, " "))
os.Chdir(p)
if err := cmd.Run(); err != nil {
errs = true
}
}
if errs {
return errors.New("errors running tests")
}
return nil
}

func testPackages() ([]string, error) {
args := []string{}
out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
if err != nil {
return args, err
}
pkgs := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
for _, p := range pkgs {
if !vendorRegex.Match(p) {
args = append(args, string(p))
}
}
return args, nil
}

func newTestCmd(args []string) *exec.Cmd {
cmd := exec.Command(envy.Get("GO_BIN", "go"), "test", "-p", "1")
if _, err := exec.LookPath("gotest"); err == nil {
cmd = exec.Command("gotest", "-p", "1")
}
cmd.Args = append(cmd.Args, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}

0 comments on commit 6bb737f

Please sign in to comment.