From 4ad4272da8ca75e1c9dd0888108df0d22cc7c85a Mon Sep 17 00:00:00 2001 From: Matias Ylipelto <3822534+ypjama@users.noreply.github.com> Date: Wed, 30 Oct 2019 16:32:28 +0200 Subject: [PATCH] Fix test --force-migrations flag bug (#1812) * Fix test --force-migrations flag bug * This fixes a bug that occurs when --force-migrations flag is used while also specifying the packages to be tested. e.g. `buffalo test ./actions/ --force-migrations` * Remove whitespaces from empty row Suggested by fixmie Co-Authored-By: fixmie[bot] <44270338+fixmie[bot]@users.noreply.github.com> --- buffalo/cmd/test.go | 12 ++++++++++++ buffalo/cmd/test_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 buffalo/cmd/test_test.go diff --git a/buffalo/cmd/test.go b/buffalo/cmd/test.go index 4aaf7e36b..fead3e989 100644 --- a/buffalo/cmd/test.go +++ b/buffalo/cmd/test.go @@ -46,7 +46,9 @@ var testCmd = &cobra.Command{ 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 { fm, err := pop.NewFileMigrator("./migrations", test) @@ -216,3 +218,13 @@ func newTestCmd(args []string) *exec.Cmd { 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 +} diff --git a/buffalo/cmd/test_test.go b/buffalo/cmd/test_test.go new file mode 100644 index 000000000..3e23b29dd --- /dev/null +++ b/buffalo/cmd/test_test.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "reflect" + "testing" +) + +func Test_CutArg(t *testing.T) { + var 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) + } + } +} \ No newline at end of file