From e1b59f5cd0acc4814d0b2d801f384a22c1f3c6dc Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 5 Mar 2019 23:03:25 -0500 Subject: [PATCH] Fix tests & run them all in parallel --- fix_test.go | 28 ++++++++++++++++------------ goose_test.go | 33 ++++++++++++++++++++++----------- helpers_test.go | 2 ++ migrate_test.go | 10 ++++++---- sql_parser_test.go | 5 +++++ 5 files changed, 51 insertions(+), 27 deletions(-) diff --git a/fix_test.go b/fix_test.go index 14672d2..3a4777e 100644 --- a/fix_test.go +++ b/fix_test.go @@ -11,27 +11,31 @@ import ( ) func TestFix(t *testing.T) { + t.Parallel() + dir, err := ioutil.TempDir("", "tmptest") if err != nil { t.Fatal(err) } - defer os.RemoveAll(dir) // clean up - defer os.Remove("goose") // clean up + defer os.RemoveAll(dir) // clean up + defer os.Remove("./bin/fix-goose") // clean up commands := []string{ - "go build -i -o ./bin/goose ./cmd/goose", - fmt.Sprintf("./bin/goose -dir=%s create create_table", dir), - fmt.Sprintf("./bin/goose -dir=%s create add_users", dir), - fmt.Sprintf("./bin/goose -dir=%s create add_indices", dir), - fmt.Sprintf("./bin/goose -dir=%s create update_users", dir), - fmt.Sprintf("./bin/goose -dir=%s fix", dir), + "go build -i -o ./bin/fix-goose ./cmd/goose", + fmt.Sprintf("./bin/fix-goose -dir=%s create create_table", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s create add_users", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s create add_indices", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s create update_users", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s fix", dir), } for _, cmd := range commands { args := strings.Split(cmd, " ") time.Sleep(1 * time.Second) - out, err := exec.Command(args[0], args[1:]...).CombinedOutput() + cmd := exec.Command(args[0], args[1:]...) + cmd.Env = os.Environ() + out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) } @@ -52,9 +56,9 @@ func TestFix(t *testing.T) { // add more migrations and then fix it commands = []string{ - fmt.Sprintf("./bin/goose -dir=%s create remove_column", dir), - fmt.Sprintf("./bin/goose -dir=%s create create_books_table", dir), - fmt.Sprintf("./bin/goose -dir=%s fix", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s create remove_column", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s create create_books_table", dir), + fmt.Sprintf("./bin/fix-goose -dir=%s fix", dir), } for _, cmd := range commands { diff --git a/goose_test.go b/goose_test.go index 9af0de4..ae5ec93 100644 --- a/goose_test.go +++ b/goose_test.go @@ -10,6 +10,8 @@ import ( ) func TestDefaultBinary(t *testing.T) { + t.Parallel() + commands := []string{ "go build -i -o ./bin/goose ./cmd/goose", "./bin/goose -dir=examples/sql-migrations sqlite3 sql.db up", @@ -18,6 +20,7 @@ func TestDefaultBinary(t *testing.T) { "./bin/goose -dir=examples/sql-migrations sqlite3 sql.db status", "./bin/goose", } + defer os.Remove("./bin/goose") // clean up for _, cmd := range commands { args := strings.Split(cmd, " ") @@ -27,7 +30,9 @@ func TestDefaultBinary(t *testing.T) { params = args[1:] } - out, err := exec.Command(command, params...).CombinedOutput() + cmd := exec.Command(command, params...) + cmd.Env = os.Environ() + out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) } @@ -35,30 +40,34 @@ func TestDefaultBinary(t *testing.T) { } func TestLiteBinary(t *testing.T) { + t.Parallel() + dir, err := ioutil.TempDir("", "tmptest") if err != nil { t.Fatal(err) } - defer os.RemoveAll(dir) // clean up - defer os.Remove("./bin/goose") // clean up - - commands := []string{ - fmt.Sprintf("./bin/goose -dir=%s create user_indices sql", dir), - fmt.Sprintf("./bin/goose -dir=%s fix", dir), - } + defer os.RemoveAll(dir) // clean up + defer os.Remove("./bin/light-goose") // clean up // this has to be done outside of the loop // since go only supports space separated tags list. - cmd := "go build -tags='no_mysql no_sqlite no_psql' -i -o ./bin/goose ./cmd/goose" - out, err := exec.Command("go", "build", "-tags='no_mysql no_sqlite no_psql'", "-i", "-o", "./bin/goose", "./cmd/goose").CombinedOutput() + cmd := exec.Command("go", "build", "-tags='no_mysql no_sqlite no_psql'", "-i", "-o", "./bin/light-goose", "./cmd/goose") + out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) } + commands := []string{ + fmt.Sprintf("./bin/light-goose -dir=%s create user_indices sql", dir), + fmt.Sprintf("./bin/light-goose -dir=%s fix", dir), + } + for _, cmd := range commands { args := strings.Split(cmd, " ") - out, err := exec.Command(args[0], args[1:]...).CombinedOutput() + cmd := exec.Command(args[0], args[1:]...) + cmd.Env = os.Environ() + out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) } @@ -66,6 +75,8 @@ func TestLiteBinary(t *testing.T) { } func TestCustomBinary(t *testing.T) { + t.Parallel() + commands := []string{ "go build -i -o ./bin/custom-goose ./examples/go-migrations", "./bin/custom-goose -dir=examples/go-migrations sqlite3 go.db up", diff --git a/helpers_test.go b/helpers_test.go index f461338..413c991 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -5,6 +5,8 @@ import ( ) func TestCamelSnake(t *testing.T) { + t.Parallel() + tt := []struct { in string camel string diff --git a/migrate_test.go b/migrate_test.go index e1b5802..ac30b48 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -4,11 +4,9 @@ import ( "testing" ) -func newMigration(v int64, src string) *Migration { - return &Migration{Version: v, Previous: -1, Next: -1, Source: src} -} - func TestMigrationSort(t *testing.T) { + t.Parallel() + ms := Migrations{} // insert in any order @@ -24,6 +22,10 @@ func TestMigrationSort(t *testing.T) { validateMigrationSort(t, ms, sorted) } +func newMigration(v int64, src string) *Migration { + return &Migration{Version: v, Previous: -1, Next: -1, Source: src} +} + func validateMigrationSort(t *testing.T, ms Migrations, sorted []int64) { for i, m := range ms { if sorted[i] != m.Version { diff --git a/sql_parser_test.go b/sql_parser_test.go index d7adafa..594596b 100644 --- a/sql_parser_test.go +++ b/sql_parser_test.go @@ -9,6 +9,8 @@ import ( ) func TestSemicolons(t *testing.T) { + t.Parallel() + type testData struct { line string result bool @@ -32,6 +34,7 @@ func TestSemicolons(t *testing.T) { } func TestSplitStatements(t *testing.T) { + t.Parallel() // SetVerbose(true) type testData struct { @@ -72,6 +75,8 @@ func TestSplitStatements(t *testing.T) { } func TestUseTransactions(t *testing.T) { + t.Parallel() + type testData struct { fileName string useTransactions bool