Skip to content

Instantly share code, notes, and snippets.

@sio4
Last active December 3, 2022 07:20
Show Gist options
  • Save sio4/0d892ca8413e994c3ccb99af1ca744bb to your computer and use it in GitHub Desktop.
Save sio4/0d892ca8413e994c3ccb99af1ca744bb to your computer and use it in GitHub Desktop.
patch for checking `buffalo test` flag supporting issue
diff --git a/internal/cmd/test/cmd.go b/internal/cmd/test/cmd.go
index fbadaaf..fdcd852 100644
--- a/internal/cmd/test/cmd.go
+++ b/internal/cmd/test/cmd.go
@@ -4,11 +4,20 @@ import "github.com/spf13/cobra"
func Cmd() *cobra.Command {
cmd := &cobra.Command{
- Use: "test",
- Short: "Run the tests for the Buffalo app. Use --force-migrations to skip schema load.",
- DisableFlagParsing: true,
- RunE: runE,
+ Use: "test",
+ Short: "Run the tests for the Buffalo app. Use --force-migrations to skip schema load.",
+ //DisableFlagParsing: true,
+ RunE: runE,
}
+ // the only buffalo specific flag
+ cmd.Flags().BoolVar(&testOptions.forceMigrations, "force-migrations", false, "force migration instead schema loading")
+
+ // just for backward compatiblity
+ cmd.Flags().BoolVarP(&testOptions.verbose, "verbose", "v", false, "the same as standard -v")
+ // alias flag for the stadnard `-run` flag
+ cmd.Flags().StringVarP(&testOptions.run, "run", "m", "", "alias for the standard -run flag")
+ // alias flag for `-timeout` that is not compatible
+ cmd.Flags().StringVar(&testOptions.timeout, "timeout", "", "alias for the standard -timeout flag")
return cmd
}
diff --git a/internal/cmd/test/test.go b/internal/cmd/test/test.go
index 91aac5b..40e91fe 100644
--- a/internal/cmd/test/test.go
+++ b/internal/cmd/test/test.go
@@ -2,7 +2,6 @@ package test
import (
"bytes"
- "fmt"
"io"
"os"
"os/exec"
@@ -16,6 +15,13 @@ import (
"github.com/spf13/cobra"
)
+var testOptions = struct {
+ forceMigrations bool
+ verbose bool
+ run string
+ timeout string
+}{}
+
func runE(c *cobra.Command, args []string) error {
os.Setenv("GO_ENV", "test")
if _, err := os.Stat("database.yml"); err == nil {
@@ -37,10 +43,7 @@ func runE(c *cobra.Command, args []string) error {
return err
}
- // Read and remove --force-migrations flag from args:
- forceMigrations := strings.Contains(strings.Join(args, ""), "--force-migrations")
- args = cutArg("--force-migrations", args)
- if forceMigrations {
+ if testOptions.forceMigrations {
fm, err := pop.NewFileMigrator("./migrations", test)
if err != nil {
return err
@@ -49,17 +52,16 @@ func runE(c *cobra.Command, args []string) error {
if err := fm.Up(); err != nil {
return err
}
-
- return testRunner(args)
- }
-
- if schema := findSchema(); schema != nil {
- err = test.Dialect.LoadSchema(schema)
- if err != nil {
- return err
+ } else {
+ if schema := findSchema(); schema != nil {
+ err = test.Dialect.LoadSchema(schema)
+ if err != nil {
+ return err
+ }
}
}
}
+
return testRunner(args)
}
@@ -67,6 +69,7 @@ func findSchema() io.Reader {
if f, err := os.Open(filepath.Join("migrations", "schema.sql")); err == nil {
return f
}
+
if dev, err := pop.Connect("development"); err == nil {
schema := &bytes.Buffer{}
if err = dev.Dialect.DumpSchema(schema); err == nil {
@@ -84,149 +87,52 @@ func findSchema() io.Reader {
return nil
}
}
+
return nil
}
func testRunner(args []string) error {
- var mFlag bool
- var query string
-
commandArgs := []string{}
- packageArgs := []string{}
-
- var lastArg string
- for index, arg := range args {
- switch arg {
- case "-run", "-m":
- query = args[index+1]
- mFlag = true
- case "-v", "-timeout":
- commandArgs = append(commandArgs, arg)
- default:
- if lastArg == "-timeout" {
- commandArgs = append(commandArgs, arg)
- } else if lastArg != "-run" && lastArg != "-m" {
- packageArgs = append(packageArgs, arg)
- }
- }
-
- lastArg = arg
- }
- cmd := newTestCmd(commandArgs)
- if mFlag {
- return mFlagRunner{
- query: query,
- args: commandArgs,
- pargs: packageArgs,
- }.Run()
+ // just for backward compatiblity
+ if testOptions.verbose {
+ commandArgs = append(commandArgs, "-v")
}
- pkgs, err := testPackages(packageArgs)
- if err != nil {
- return err
+ // alias flags for common flags for convenient
+ if testOptions.timeout != "" {
+ commandArgs = append(commandArgs, "-timeout", testOptions.timeout)
}
- cmd.Args = append(cmd.Args, pkgs...)
- logrus.Info(strings.Join(cmd.Args, " "))
- return cmd.Run()
-}
-
-type mFlagRunner struct {
- query string
- args []string
- pargs []string
-}
-
-func (m mFlagRunner) Run() error {
- app := meta.New(".")
- pwd, _ := os.Getwd()
- defer os.Chdir(pwd)
-
- pkgs, err := testPackages(m.pargs)
- if err != nil {
- return err
+ if testOptions.run != "" {
+ commandArgs = append(commandArgs, "-run", testOptions.run)
}
- var errs bool
- for _, p := range pkgs {
- os.Chdir(pwd)
-
- if p == app.PackagePkg {
- continue
- }
-
- cmd := newTestCmd(m.args)
-
- p = strings.TrimPrefix(p, app.PackagePkg+string(filepath.Separator))
- os.Chdir(p)
-
- if hasTestify(cmd.Args) {
- cmd.Args = append(cmd.Args, "-testify.m", m.query)
- } else {
- cmd.Args = append(cmd.Args, "-run", m.query)
- }
-
- logrus.Info(strings.Join(cmd.Args, " "))
-
- if err := cmd.Run(); err != nil {
- errs = true
- }
- }
- if errs {
- return fmt.Errorf("errors running tests")
+ if len(args) != 0 {
+ // when the user specified test argument explicitly, just run it
+ commandArgs = append(commandArgs, args...)
+ } else {
+ // when there is no user defined args, no need to seek packages
+ commandArgs = append(commandArgs, "./...")
}
- return nil
-}
-
-func hasTestify(args []string) bool {
- cmd := exec.Command(args[0], args[1:]...)
- cmd.Args = append(cmd.Args, "-unknownflag")
- b, _ := cmd.Output()
- return bytes.Contains(b, []byte("-testify.m"))
-}
-func testPackages(givenArgs []string) ([]string, error) {
- // If there are args, then assume these are the packages to test.
- //
- // Instead of always returning all packages from 'go list ./...', just
- // return the given packages in this case
- if len(givenArgs) > 0 {
- return givenArgs, nil
- }
+ cmd := newTestCmd(commandArgs)
+ logrus.Info(strings.Join(cmd.Args, " "))
- 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 !strings.Contains(string(p), "/vendor/") {
- args = append(args, string(p))
- }
- }
- return args, nil
+ return cmd.Run()
}
func newTestCmd(args []string) *exec.Cmd {
- cargs := []string{"test", "-p", "1"}
app := meta.New(".")
+
+ cargs := []string{"test", "-p", "1"}
cargs = append(cargs, "-tags", app.BuildTags("development").String())
cargs = append(cargs, args...)
+
cmd := exec.Command(envy.Get("GO_BIN", "go"), cargs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
- return cmd
-}
-func cutArg(arg string, args []string) []string {
- for i, v := range args {
- if v == arg {
- return append(args[:i], args[i+1:]...)
- }
- }
-
- return args
+ return cmd
}
diff --git a/internal/cmd/test/test_test.go b/internal/cmd/test/test_test.go
index 0185692..56e5404 100644
--- a/internal/cmd/test/test_test.go
+++ b/internal/cmd/test/test_test.go
@@ -1,27 +1 @@
package test
-
-import (
- "reflect"
- "testing"
-)
-
-func Test_CutArg(t *testing.T) {
- tests := []struct {
- arg string
- args []string
- expected []string
- }{
- {"b", []string{"a", "b", "c"}, []string{"a", "c"}},
- {"--is-not-in-args", []string{"a", "b", "c"}, []string{"a", "b", "c"}},
- {"--foo", []string{"--foo", "--bar", "--baz"}, []string{"--bar", "--baz"}},
- {"--force-migrations", []string{"./actions/", "--force-migrations"}, []string{"./actions/"}},
- {"--force-migrations", []string{"./actions/", "--force-migrations", "-m", "Test_HomeHandler"}, []string{"./actions/", "-m", "Test_HomeHandler"}},
- }
-
- for _, tt := range tests {
- result := cutArg(tt.arg, tt.args)
- if !reflect.DeepEqual(result, tt.expected) {
- t.Errorf("got %s, want %s when cutting %s from %s", result, tt.expected, tt.arg, tt.args)
- }
- }
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment