diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 000000000..180783634 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,14 @@ +linters: + disable-all: true + enable: + # Default + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - unused + - gofmt + # Added + - testifylint + - misspell diff --git a/database/store_test.go b/database/store_test.go index 8e9d6e89f..7dd6c1b02 100644 --- a/database/store_test.go +++ b/database/store_test.go @@ -35,10 +35,11 @@ func TestDialectStore(t *testing.T) { db, err := sql.Open("sqlite", ":memory:") require.NoError(t, err) testStore(context.Background(), t, database.DialectSQLite3, db, func(t *testing.T, err error) { + t.Helper() var sqliteErr *sqlite.Error ok := errors.As(err, &sqliteErr) require.True(t, ok) - require.Equal(t, sqliteErr.Code(), 1) // Generic error (SQLITE_ERROR) + require.Equal(t, 1, sqliteErr.Code()) // Generic error (SQLITE_ERROR) require.Contains(t, sqliteErr.Error(), "table test_goose_db_version already exists") }) }) @@ -58,11 +59,11 @@ func TestDialectStore(t *testing.T) { require.NoError(t, insert(db, 2)) res, err := store.ListMigrations(context.Background(), db) require.NoError(t, err) - require.Equal(t, len(res), 3) + require.Len(t, res, 3) // Check versions are in descending order: [2, 3, 1] - require.EqualValues(t, res[0].Version, 2) - require.EqualValues(t, res[1].Version, 3) - require.EqualValues(t, res[2].Version, 1) + require.EqualValues(t, 2, res[0].Version) + require.EqualValues(t, 3, res[1].Version) + require.EqualValues(t, 1, res[2].Version) }) } @@ -103,7 +104,7 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { res, err := store.ListMigrations(ctx, conn) require.NoError(t, err) - require.Equal(t, len(res), 0) + require.Empty(t, res, 0) return nil }) require.NoError(t, err) @@ -125,7 +126,7 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { res, err := store.ListMigrations(ctx, conn) require.NoError(t, err) - require.Equal(t, len(res), 6) + require.Len(t, res, 6) // Check versions are in descending order. for i := 0; i < 6; i++ { require.EqualValues(t, res[i].Version, 5-i) @@ -151,7 +152,7 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { res, err := store.ListMigrations(ctx, conn) require.NoError(t, err) - require.Equal(t, len(res), 3) + require.Len(t, res, 3) // Check that the remaining versions are in descending order. for i := 0; i < 3; i++ { require.EqualValues(t, res[i].Version, 2-i) @@ -165,8 +166,8 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { res, err := store.GetMigration(ctx, conn, int64(i)) require.NoError(t, err) - require.Equal(t, res.IsApplied, true) - require.Equal(t, res.Timestamp.IsZero(), false) + require.True(t, res.IsApplied) + require.False(t, res.Timestamp.IsZero()) return nil }) require.NoError(t, err) @@ -180,7 +181,7 @@ func testStore( require.NoError(t, err) latest, err := store.GetLatestVersion(ctx, tx) require.NoError(t, err) - require.EqualValues(t, latest, 1) + require.EqualValues(t, 1, latest) return nil }) require.NoError(t, err) @@ -190,7 +191,7 @@ func testStore( require.NoError(t, err) latest, err := store.GetLatestVersion(ctx, conn) require.NoError(t, err) - require.EqualValues(t, latest, 0) + require.EqualValues(t, 0, latest) return nil }) require.NoError(t, err) @@ -204,7 +205,7 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { res, err := store.ListMigrations(ctx, conn) require.NoError(t, err) - require.Equal(t, len(res), 0) + require.Empty(t, res) return nil }) require.NoError(t, err) @@ -213,7 +214,7 @@ func testStore( err = runConn(ctx, db, func(conn *sql.Conn) error { _, err := store.GetMigration(ctx, conn, 0) require.Error(t, err) - require.True(t, errors.Is(err, database.ErrVersionNotFound)) + require.ErrorIs(t, err, database.ErrVersionNotFound) return nil }) require.NoError(t, err) diff --git a/globals_test.go b/globals_test.go index 638e830ba..230708ae0 100644 --- a/globals_test.go +++ b/globals_test.go @@ -12,12 +12,12 @@ func TestNewGoMigration(t *testing.T) { t.Run("valid_both_nil", func(t *testing.T) { m := NewGoMigration(1, nil, nil) // roundtrip - require.Equal(t, m.Version, int64(1)) - require.Equal(t, m.Type, TypeGo) - require.Equal(t, m.Registered, true) - require.Equal(t, m.Next, int64(-1)) - require.Equal(t, m.Previous, int64(-1)) - require.Equal(t, m.Source, "") + require.EqualValues(t, 1, m.Version) + require.Equal(t, TypeGo, m.Type) + require.True(t, m.Registered) + require.EqualValues(t, -1, m.Next) + require.EqualValues(t, -1, m.Previous) + require.Equal(t, "", m.Source) require.Nil(t, m.UpFnNoTxContext) require.Nil(t, m.DownFnNoTxContext) require.Nil(t, m.UpFnContext) @@ -26,10 +26,10 @@ func TestNewGoMigration(t *testing.T) { require.Nil(t, m.DownFn) require.Nil(t, m.UpFnNoTx) require.Nil(t, m.DownFnNoTx) - require.True(t, m.goUp != nil) - require.True(t, m.goDown != nil) - require.Equal(t, m.goUp.Mode, TransactionEnabled) - require.Equal(t, m.goDown.Mode, TransactionEnabled) + require.NotNil(t, m.goUp) + require.NotNil(t, m.goDown) + require.Equal(t, TransactionEnabled, m.goUp.Mode) + require.Equal(t, TransactionEnabled, m.goDown.Mode) }) t.Run("all_set", func(t *testing.T) { // This will eventually be an error when registering migrations. @@ -39,14 +39,14 @@ func TestNewGoMigration(t *testing.T) { &GoFunc{RunTx: func(context.Context, *sql.Tx) error { return nil }, RunDB: func(context.Context, *sql.DB) error { return nil }}, ) // check only functions - require.True(t, m.UpFn != nil) - require.True(t, m.UpFnContext != nil) - require.True(t, m.UpFnNoTx != nil) - require.True(t, m.UpFnNoTxContext != nil) - require.True(t, m.DownFn != nil) - require.True(t, m.DownFnContext != nil) - require.True(t, m.DownFnNoTx != nil) - require.True(t, m.DownFnNoTxContext != nil) + require.NotNil(t, m.UpFn) + require.NotNil(t, m.UpFnContext) + require.NotNil(t, m.UpFnNoTx) + require.NotNil(t, m.UpFnNoTxContext) + require.NotNil(t, m.DownFn) + require.NotNil(t, m.DownFnContext) + require.NotNil(t, m.DownFnNoTx) + require.NotNil(t, m.DownFnNoTxContext) }) } @@ -93,12 +93,12 @@ func TestTransactionMode(t *testing.T) { m := NewGoMigration(1, nil, nil) err = SetGlobalMigrations(m) require.NoError(t, err) - require.Equal(t, len(registeredGoMigrations), 1) + require.Len(t, registeredGoMigrations, 1) registered := registeredGoMigrations[1] - require.True(t, registered.goUp != nil) - require.True(t, registered.goDown != nil) - require.Equal(t, registered.goUp.Mode, TransactionEnabled) - require.Equal(t, registered.goDown.Mode, TransactionEnabled) + require.NotNil(t, registered.goUp) + require.NotNil(t, registered.goDown) + require.Equal(t, TransactionEnabled, registered.goUp.Mode) + require.Equal(t, TransactionEnabled, registered.goDown.Mode) migration2 := NewGoMigration(2, nil, nil) // reset so we can check the default is set @@ -131,12 +131,12 @@ func TestLegacyFunctions(t *testing.T) { assertMigration := func(t *testing.T, m *Migration, version int64) { t.Helper() - require.Equal(t, m.Version, version) - require.Equal(t, m.Type, TypeGo) - require.Equal(t, m.Registered, true) - require.Equal(t, m.Next, int64(-1)) - require.Equal(t, m.Previous, int64(-1)) - require.Equal(t, m.Source, "") + require.Equal(t, version, m.Version) + require.Equal(t, TypeGo, m.Type) + require.True(t, m.Registered) + require.EqualValues(t, -1, m.Next) + require.EqualValues(t, -1, m.Previous) + require.Equal(t, "", m.Source) } t.Run("all_tx", func(t *testing.T) { @@ -145,7 +145,7 @@ func TestLegacyFunctions(t *testing.T) { NewGoMigration(1, &GoFunc{RunTx: runTx}, &GoFunc{RunTx: runTx}), ) require.NoError(t, err) - require.Equal(t, len(registeredGoMigrations), 1) + require.Len(t, registeredGoMigrations, 1) m := registeredGoMigrations[1] assertMigration(t, m, 1) // Legacy functions. @@ -168,7 +168,7 @@ func TestLegacyFunctions(t *testing.T) { NewGoMigration(2, &GoFunc{RunDB: runDB}, &GoFunc{RunDB: runDB}), ) require.NoError(t, err) - require.Equal(t, len(registeredGoMigrations), 1) + require.Len(t, registeredGoMigrations, 1) m := registeredGoMigrations[2] assertMigration(t, m, 2) // Legacy functions. diff --git a/goose_cli_test.go b/goose_cli_test.go index 8b22b4a09..1f4185ac5 100644 --- a/goose_cli_test.go +++ b/goose_cli_test.go @@ -24,7 +24,7 @@ func TestFullBinary(t *testing.T) { cli := buildGooseCLI(t, false) out, err := cli.run("--version") require.NoError(t, err) - require.Equal(t, out, "goose version: "+gooseTestBinaryVersion+"\n") + require.Equal(t, "goose version: "+gooseTestBinaryVersion+"\n", out) } func TestLiteBinary(t *testing.T) { @@ -35,7 +35,7 @@ func TestLiteBinary(t *testing.T) { t.Parallel() out, err := cli.run("--version") require.NoError(t, err) - require.Equal(t, out, "goose version: "+gooseTestBinaryVersion+"\n") + require.Equal(t, "goose version: "+gooseTestBinaryVersion+"\n", out) }) t.Run("default_binary", func(t *testing.T) { t.Parallel() @@ -113,7 +113,7 @@ func TestLiteBinary(t *testing.T) { createEmptyFile(t, dir, "20230826163151_delta.go") total, err := os.ReadDir(dir) require.NoError(t, err) - require.Equal(t, len(total), 4) + require.Len(t, total, 4) migrationFiles := []struct { name string fileType string @@ -137,13 +137,13 @@ func TestLiteBinary(t *testing.T) { } total, err = os.ReadDir(dir) require.NoError(t, err) - require.Equal(t, len(total), 7) + require.Len(t, total, 7) out, err := cli.run("-dir="+dir, "fix") require.NoError(t, err) require.Contains(t, out, "RENAMED") files, err := os.ReadDir(dir) require.NoError(t, err) - require.Equal(t, len(files), 7) + require.Len(t, files, 7) expected := []string{ "00001_alpha.sql", "00003_bravo.sql", @@ -175,6 +175,7 @@ func (g gooseBinary) run(params ...string) (string, error) { // buildGooseCLI builds goose test binary, which is used for testing goose CLI. It is built with all // drivers enabled, unless lite is true, in which case all drivers are disabled except sqlite3 func buildGooseCLI(t *testing.T, lite bool) gooseBinary { + t.Helper() binName := "goose-test" dir := t.TempDir() output := filepath.Join(dir, binName) diff --git a/goose_embed_test.go b/goose_embed_test.go index f5055a7bd..bb39694ba 100644 --- a/goose_embed_test.go +++ b/goose_embed_test.go @@ -46,14 +46,14 @@ func TestEmbeddedMigrations(t *testing.T) { require.NoError(t, err) ver, err = goose.GetDBVersion(db) require.NoError(t, err) - require.EqualValues(t, ver, 0) + require.EqualValues(t, 0, ver) }) t.Run("create_uses_os_fs", func(t *testing.T) { dir := t.TempDir() err := goose.Create(db, dir, "test", "sql") require.NoError(t, err) paths, _ := filepath.Glob(filepath.Join(dir, "*test.sql")) - require.NotZero(t, len(paths)) + require.NotEmpty(t, paths) err = goose.Fix(dir) require.NoError(t, err) _, err = os.Stat(filepath.Join(dir, "00001_test.sql")) diff --git a/internal/dialect/store.go b/internal/dialect/store.go index 5179cabaf..e9b768f91 100644 --- a/internal/dialect/store.go +++ b/internal/dialect/store.go @@ -123,7 +123,12 @@ func (s *store) DeleteVersionNoTx(ctx context.Context, db *sql.DB, tableName str return err } -func (s *store) GetMigration(ctx context.Context, db *sql.DB, tableName string, version int64) (*GetMigrationResult, error) { +func (s *store) GetMigration( + ctx context.Context, + db *sql.DB, + tableName string, + version int64, +) (*GetMigrationResult, error) { q := s.querier.GetMigrationByVersion(tableName) var timestamp time.Time var isApplied bool diff --git a/internal/gooseutil/resolve_test.go b/internal/gooseutil/resolve_test.go index c148a19be..6d33899b2 100644 --- a/internal/gooseutil/resolve_test.go +++ b/internal/gooseutil/resolve_test.go @@ -8,25 +8,26 @@ import ( ) func TestResolveVersions(t *testing.T) { + t.Parallel() t.Run("not_allow_missing", func(t *testing.T) { // Nothing to apply nil got, err := UpVersions(nil, nil, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Nothing to apply empty got, err = UpVersions([]int64{}, []int64{}, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Nothing new got, err = UpVersions([]int64{1, 2, 3}, []int64{1, 2, 3}, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // All new got, err = UpVersions([]int64{1, 2, 3}, []int64{}, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 3, len(got)) + require.Len(t, got, 3) require.Equal(t, int64(1), got[0]) require.Equal(t, int64(2), got[1]) require.Equal(t, int64(3), got[2]) @@ -34,23 +35,23 @@ func TestResolveVersions(t *testing.T) { // Squashed, no new got, err = UpVersions([]int64{3}, []int64{3}, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Squashed, 1 new got, err = UpVersions([]int64{3, 4}, []int64{3}, math.MaxInt64, false) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(4), got[0]) // Some new with target got, err = UpVersions([]int64{1, 2, 3, 4, 5}, []int64{1, 2}, 4, false) require.NoError(t, err) - require.Equal(t, 2, len(got)) + require.Len(t, got, 2) require.Equal(t, int64(3), got[0]) require.Equal(t, int64(4), got[1]) // up to and including target // Some new with zero target got, err = UpVersions([]int64{1, 2, 3, 4, 5}, []int64{1, 2}, 0, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Error: one missing migrations with max target _, err = UpVersions([]int64{1, 2, 3, 4}, []int64{1 /* 2*/, 3}, math.MaxInt64, false) @@ -84,7 +85,7 @@ func TestResolveVersions(t *testing.T) { got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2 */, 3}, 1, false) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2 */, 3}, 2, false) require.Error(t, err) require.Equal(t, @@ -117,21 +118,21 @@ func TestResolveVersions(t *testing.T) { // Nothing to apply nil got, err := UpVersions(nil, nil, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Nothing to apply empty got, err = UpVersions([]int64{}, []int64{}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Nothing new got, err = UpVersions([]int64{1, 2, 3}, []int64{1, 2, 3}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // All new got, err = UpVersions([]int64{1, 2, 3}, []int64{}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 3, len(got)) + require.Len(t, got, 3) require.Equal(t, int64(1), got[0]) require.Equal(t, int64(2), got[1]) require.Equal(t, int64(3), got[2]) @@ -139,34 +140,34 @@ func TestResolveVersions(t *testing.T) { // Squashed, no new got, err = UpVersions([]int64{3}, []int64{3}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // Squashed, 1 new got, err = UpVersions([]int64{3, 4}, []int64{3}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(4), got[0]) // Some new with target got, err = UpVersions([]int64{1, 2, 3, 4, 5}, []int64{1, 2}, 4, true) require.NoError(t, err) - require.Equal(t, 2, len(got)) + require.Len(t, got, 2) require.Equal(t, int64(3), got[0]) require.Equal(t, int64(4), got[1]) // up to and including target // Some new with zero target got, err = UpVersions([]int64{1, 2, 3, 4, 5}, []int64{1, 2}, 0, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) // No error: one missing got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2*/, 3}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(2), got[0]) // missing // No error: multiple missing and new with max target got, err = UpVersions([]int64{1, 2, 3, 4, 5}, []int64{ /* 1 */ 2 /* 3 */, 4}, math.MaxInt64, true) require.NoError(t, err) - require.Equal(t, 3, len(got)) + require.Len(t, got, 3) require.Equal(t, int64(1), got[0]) // missing require.Equal(t, int64(3), got[1]) // missing require.Equal(t, int64(5), got[2]) @@ -174,23 +175,23 @@ func TestResolveVersions(t *testing.T) { t.Run("target_lower_than_max", func(t *testing.T) { got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2 */, 3}, 1, true) require.NoError(t, err) - require.Equal(t, 0, len(got)) + require.Empty(t, got) got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2 */, 3}, 2, true) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(2), got[0]) // missing got, err = UpVersions([]int64{1, 2, 3}, []int64{1 /* 2 */, 3}, 3, true) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(2), got[0]) // missing got, err = UpVersions([]int64{1, 2, 3, 4, 5, 6}, []int64{1 /* 2 */, 3, 4 /* 5*/, 6}, 4, true) require.NoError(t, err) - require.Equal(t, 1, len(got)) + require.Len(t, got, 1) require.Equal(t, int64(2), got[0]) // missing got, err = UpVersions([]int64{1, 2, 3, 4, 5, 6}, []int64{1 /* 2 */, 3, 4 /* 5*/, 6}, 6, true) require.NoError(t, err) - require.Equal(t, 2, len(got)) + require.Len(t, got, 2) require.Equal(t, int64(2), got[0]) // missing require.Equal(t, int64(5), got[1]) // missing }) diff --git a/internal/migrationstats/migrationstats_test.go b/internal/migrationstats/migrationstats_test.go index c24c83e06..1530d3a91 100644 --- a/internal/migrationstats/migrationstats_test.go +++ b/internal/migrationstats/migrationstats_test.go @@ -32,10 +32,10 @@ func TestParsingGoMigrations(t *testing.T) { t.Run(tc.name, func(t *testing.T) { g, err := parseGoFile(strings.NewReader(tc.input)) require.NoError(t, err) - require.Equal(t, g.useTx != nil, true) - require.Equal(t, *g.useTx, tc.wantTx) - require.Equal(t, g.downFuncName, tc.wantDownName) - require.Equal(t, g.upFuncName, tc.wantUpName) + require.NotNil(t, g.useTx) + require.Equal(t, tc.wantTx, *g.useTx) + require.Equal(t, tc.wantDownName, g.downFuncName) + require.Equal(t, tc.wantUpName, g.upFuncName) }) } } @@ -46,14 +46,14 @@ func TestGoMigrationStats(t *testing.T) { base := "../../tests/gomigrations/success/testdata" all, err := os.ReadDir(base) require.NoError(t, err) - require.Equal(t, len(all), 16) + require.Len(t, all, 16) files := make([]string, 0, len(all)) for _, f := range all { files = append(files, filepath.Join(base, f.Name())) } stats, err := GatherStats(NewFileWalker(files...), false) require.NoError(t, err) - require.Equal(t, len(stats), 16) + require.Len(t, stats, 16) checkGoStats(t, stats[0], "001_up_down.go", 1, 1, 1, true) checkGoStats(t, stats[1], "002_up_only.go", 2, 1, 0, true) checkGoStats(t, stats[2], "003_down_only.go", 3, 0, 1, true) diff --git a/internal/sqlparser/parser_test.go b/internal/sqlparser/parser_test.go index a8f24f2ac..1a3560ffc 100644 --- a/internal/sqlparser/parser_test.go +++ b/internal/sqlparser/parser_test.go @@ -92,7 +92,7 @@ func TestInvalidUp(t *testing.T) { testdataDir := filepath.Join("testdata", "invalid", "up") entries, err := os.ReadDir(testdataDir) require.NoError(t, err) - require.NotZero(t, len(entries)) + require.NotEmpty(t, entries) for _, entry := range entries { by, err := os.ReadFile(filepath.Join(testdataDir, entry.Name())) @@ -585,7 +585,7 @@ func Test_extractAnnotation(t *testing.T) { } else { require.NoError(t, err) } - require.Equal(t, got, tt.want) + require.Equal(t, tt.want, got) }) } } diff --git a/migrate_test.go b/migrate_test.go index b73b5187b..a11949f32 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -33,6 +33,7 @@ func newMigration(v int64, src string) *Migration { } func validateMigrationSort(t *testing.T, ms Migrations, sorted []int64) { + t.Helper() for i, m := range ms { if sorted[i] != m.Version { t.Error("incorrect sorted version") @@ -79,7 +80,7 @@ func TestCollectMigrations(t *testing.T) { file3, file4 := "19081_a.go", "19082_b.go" AddNamedMigrationContext(file1, nil, nil) AddNamedMigrationContext(file2, nil, nil) - require.Equal(t, len(registeredGoMigrations), 2) + require.Len(t, registeredGoMigrations, 2) tmp := t.TempDir() dir := filepath.Join(tmp, "migrations", "dir1") err := os.MkdirAll(dir, 0755) @@ -91,14 +92,14 @@ func TestCollectMigrations(t *testing.T) { fsys := os.DirFS(tmp) files, err := fs.ReadDir(fsys, "migrations/dir1") require.NoError(t, err) - require.Equal(t, len(files), 4) + require.Len(t, files, 4) all, err := collectMigrationsFS(fsys, "migrations/dir1", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 4) - require.EqualValues(t, all[0].Version, 9081) - require.EqualValues(t, all[1].Version, 9082) - require.EqualValues(t, all[2].Version, 19081) - require.EqualValues(t, all[3].Version, 19082) + require.Len(t, all, 4) + require.EqualValues(t, 9081, all[0].Version) + require.EqualValues(t, 9082, all[1].Version) + require.EqualValues(t, 19081, all[2].Version) + require.EqualValues(t, 19082, all[3].Version) }) t.Run("filesystem_registered_with_multiple_dirpath", func(t *testing.T) { t.Cleanup(func() { clearMap(registeredGoMigrations) }) @@ -106,7 +107,7 @@ func TestCollectMigrations(t *testing.T) { AddNamedMigrationContext(file1, nil, nil) AddNamedMigrationContext(file2, nil, nil) AddNamedMigrationContext(file3, nil, nil) - require.Equal(t, len(registeredGoMigrations), 3) + require.Len(t, registeredGoMigrations, 3) tmp := t.TempDir() dir1 := filepath.Join(tmp, "migrations", "dir1") dir2 := filepath.Join(tmp, "migrations", "dir2") @@ -123,32 +124,32 @@ func TestCollectMigrations(t *testing.T) { { all, err := collectMigrationsFS(fsys, "migrations/dir1", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 2) - require.EqualValues(t, all[0].Version, 1) - require.EqualValues(t, all[1].Version, 2) + require.Len(t, all, 2) + require.EqualValues(t, 1, all[0].Version) + require.EqualValues(t, 2, all[1].Version) } // Validate if dirpath 2 is specified we only get the one Go migration in migrations/dir2 folder // even though 3 Go migrations have been registered. { all, err := collectMigrationsFS(fsys, "migrations/dir2", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 1) - require.EqualValues(t, all[0].Version, 1111) + require.Len(t, all, 1) + require.EqualValues(t, 1111, all[0].Version) } }) t.Run("empty_filesystem_registered_manually", func(t *testing.T) { t.Cleanup(func() { clearMap(registeredGoMigrations) }) AddNamedMigrationContext("00101_a.go", nil, nil) AddNamedMigrationContext("00102_b.go", nil, nil) - require.Equal(t, len(registeredGoMigrations), 2) + require.Len(t, registeredGoMigrations, 2) tmp := t.TempDir() err := os.MkdirAll(filepath.Join(tmp, "migrations"), 0755) require.NoError(t, err) all, err := collectMigrationsFS(os.DirFS(tmp), "migrations", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 2) - require.EqualValues(t, all[0].Version, 101) - require.EqualValues(t, all[1].Version, 102) + require.Len(t, all, 2) + require.EqualValues(t, 101, all[0].Version) + require.EqualValues(t, 102, all[1].Version) }) t.Run("unregistered_go_migrations", func(t *testing.T) { t.Cleanup(func() { clearMap(registeredGoMigrations) }) @@ -157,7 +158,7 @@ func TestCollectMigrations(t *testing.T) { // valid looking file2 Go migration AddNamedMigrationContext(file1, nil, nil) AddNamedMigrationContext(file3, nil, nil) - require.Equal(t, len(registeredGoMigrations), 2) + require.Len(t, registeredGoMigrations, 2) tmp := t.TempDir() dir1 := filepath.Join(tmp, "migrations", "dir1") err := os.MkdirAll(dir1, 0755) @@ -169,21 +170,21 @@ func TestCollectMigrations(t *testing.T) { createEmptyFile(t, dir1, file3) all, err := collectMigrationsFS(os.DirFS(tmp), "migrations/dir1", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 3) - require.EqualValues(t, all[0].Version, 1) + require.Len(t, all, 3) + require.EqualValues(t, 1, all[0].Version) require.True(t, all[0].Registered) - require.EqualValues(t, all[1].Version, 998) + require.EqualValues(t, 998, all[1].Version) // This migrations is marked unregistered and will lazily raise an error if/when this // migration is run require.False(t, all[1].Registered) - require.EqualValues(t, all[2].Version, 999) + require.EqualValues(t, 999, all[2].Version) require.True(t, all[2].Registered) }) t.Run("with_skipped_go_files", func(t *testing.T) { t.Cleanup(func() { clearMap(registeredGoMigrations) }) file1, file2, file3, file4 := "00001_a.go", "00002_b.sql", "00999_c_test.go", "embed.go" AddNamedMigrationContext(file1, nil, nil) - require.Equal(t, len(registeredGoMigrations), 1) + require.Len(t, registeredGoMigrations, 1) tmp := t.TempDir() dir1 := filepath.Join(tmp, "migrations", "dir1") err := os.MkdirAll(dir1, 0755) @@ -194,10 +195,10 @@ func TestCollectMigrations(t *testing.T) { createEmptyFile(t, dir1, file4) all, err := collectMigrationsFS(os.DirFS(tmp), "migrations/dir1", 0, math.MaxInt64, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 2) - require.EqualValues(t, all[0].Version, 1) + require.Len(t, all, 2) + require.EqualValues(t, 1, all[0].Version) require.True(t, all[0].Registered) - require.EqualValues(t, all[1].Version, 2) + require.EqualValues(t, 2, all[1].Version) require.False(t, all[1].Registered) }) t.Run("current_and_target", func(t *testing.T) { @@ -205,7 +206,7 @@ func TestCollectMigrations(t *testing.T) { file1, file2, file3 := "01001_a.go", "01002_b.sql", "01003_c.go" AddNamedMigrationContext(file1, nil, nil) AddNamedMigrationContext(file3, nil, nil) - require.Equal(t, len(registeredGoMigrations), 2) + require.Len(t, registeredGoMigrations, 2) tmp := t.TempDir() dir1 := filepath.Join(tmp, "migrations", "dir1") err := os.MkdirAll(dir1, 0755) @@ -215,9 +216,9 @@ func TestCollectMigrations(t *testing.T) { createEmptyFile(t, dir1, file3) all, err := collectMigrationsFS(os.DirFS(tmp), "migrations/dir1", 1001, 1003, registeredGoMigrations) require.NoError(t, err) - require.Equal(t, len(all), 2) - require.EqualValues(t, all[0].Version, 1002) - require.EqualValues(t, all[1].Version, 1003) + require.Len(t, all, 2) + require.EqualValues(t, 1002, all[0].Version) + require.EqualValues(t, 1003, all[1].Version) }) } @@ -252,6 +253,7 @@ func TestVersionFilter(t *testing.T) { } } func createEmptyFile(t *testing.T, dir, name string) { + t.Helper() path := filepath.Join(dir, name) f, err := os.Create(path) require.NoError(t, err) diff --git a/provider_collect.go b/provider_collect.go index 6e230926c..936377294 100644 --- a/provider_collect.go +++ b/provider_collect.go @@ -113,7 +113,7 @@ func newSQLMigration(source Source) *Migration { } } -func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration, error) { +func merge(sources *fileSources, registered map[int64]*Migration) ([]*Migration, error) { var migrations []*Migration migrationLookup := make(map[int64]*Migration) // Add all SQL migrations to the list of migrations. @@ -123,7 +123,7 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration, migrationLookup[source.Version] = m } // If there are no Go files in the filesystem and no registered Go migrations, return early. - if len(sources.goSources) == 0 && len(registerd) == 0 { + if len(sources.goSources) == 0 && len(registered) == 0 { return migrations, nil } // Return an error if the given sources contain a versioned Go migration that has not been @@ -133,7 +133,7 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration, // This is almost always a user error. var unregistered []string for _, s := range sources.goSources { - m, ok := registerd[s.Version] + m, ok := registered[s.Version] if !ok { unregistered = append(unregistered, s.Path) } else { @@ -151,7 +151,7 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration, // migrations may not have a corresponding file on disk. Which is fine! We include them // wholesale as part of migrations. This allows users to build a custom binary that only embeds // the SQL migration files. - for version, r := range registerd { + for version, r := range registered { // Ensure there are no duplicate versions. if existing, ok := migrationLookup[version]; ok { fullpath := r.Source diff --git a/provider_collect_test.go b/provider_collect_test.go index 17b574444..46dcbd413 100644 --- a/provider_collect_test.go +++ b/provider_collect_test.go @@ -13,23 +13,23 @@ func TestCollectFileSources(t *testing.T) { t.Run("nil_fsys", func(t *testing.T) { sources, err := collectFilesystemSources(nil, false, nil, nil) require.NoError(t, err) - require.True(t, sources != nil) - require.Equal(t, len(sources.goSources), 0) - require.Equal(t, len(sources.sqlSources), 0) + require.NotNil(t, sources) + require.Empty(t, sources.goSources) + require.Empty(t, sources.sqlSources) }) t.Run("noop_fsys", func(t *testing.T) { sources, err := collectFilesystemSources(noopFS{}, false, nil, nil) require.NoError(t, err) - require.True(t, sources != nil) - require.Equal(t, len(sources.goSources), 0) - require.Equal(t, len(sources.sqlSources), 0) + require.NotNil(t, sources) + require.Empty(t, sources.goSources) + require.Empty(t, sources.sqlSources) }) t.Run("empty_fsys", func(t *testing.T) { sources, err := collectFilesystemSources(fstest.MapFS{}, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.goSources), 0) - require.Equal(t, len(sources.sqlSources), 0) - require.True(t, sources != nil) + require.Empty(t, sources.goSources) + require.Empty(t, sources.sqlSources) + require.NotNil(t, sources) }) t.Run("incorrect_fsys", func(t *testing.T) { mapFS := fstest.MapFS{ @@ -38,8 +38,8 @@ func TestCollectFileSources(t *testing.T) { // strict disable - should not error sources, err := collectFilesystemSources(mapFS, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.goSources), 0) - require.Equal(t, len(sources.sqlSources), 0) + require.Empty(t, sources.goSources) + require.Empty(t, sources.sqlSources) // strict enabled - should error _, err = collectFilesystemSources(mapFS, true, nil, nil) require.Error(t, err) @@ -50,8 +50,8 @@ func TestCollectFileSources(t *testing.T) { require.NoError(t, err) sources, err := collectFilesystemSources(fsys, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.sqlSources), 4) - require.Equal(t, len(sources.goSources), 0) + require.Len(t, sources.sqlSources, 4) + require.Empty(t, sources.goSources) expected := fileSources{ sqlSources: []Source{ newSource(TypeSQL, "00001_foo.sql", 1), @@ -78,8 +78,8 @@ func TestCollectFileSources(t *testing.T) { nil, ) require.NoError(t, err) - require.Equal(t, len(sources.sqlSources), 2) - require.Equal(t, len(sources.goSources), 0) + require.Len(t, sources.sqlSources, 2) + require.Empty(t, sources.goSources) expected := fileSources{ sqlSources: []Source{ newSource(TypeSQL, "00001_foo.sql", 1), @@ -110,8 +110,8 @@ func TestCollectFileSources(t *testing.T) { } sources, err := collectFilesystemSources(mapFS, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.sqlSources), 4) - require.Equal(t, len(sources.goSources), 0) + require.Len(t, sources.sqlSources, 4) + require.Empty(t, sources.goSources) }) t.Run("skip_random_files", func(t *testing.T) { mapFS := fstest.MapFS{ @@ -125,17 +125,17 @@ func TestCollectFileSources(t *testing.T) { } sources, err := collectFilesystemSources(mapFS, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.sqlSources), 2) - require.Equal(t, len(sources.goSources), 1) + require.Len(t, sources.sqlSources, 2) + require.Len(t, sources.goSources, 1) // 1 - require.Equal(t, sources.sqlSources[0].Path, "1_foo.sql") - require.Equal(t, sources.sqlSources[0].Version, int64(1)) + require.Equal(t, "1_foo.sql", sources.sqlSources[0].Path) + require.EqualValues(t, 1, sources.sqlSources[0].Version) // 2 - require.Equal(t, sources.sqlSources[1].Path, "5_qux.sql") - require.Equal(t, sources.sqlSources[1].Version, int64(5)) + require.Equal(t, "5_qux.sql", sources.sqlSources[1].Path) + require.EqualValues(t, 5, sources.sqlSources[1].Version) // 3 - require.Equal(t, sources.goSources[0].Path, "4_something.go") - require.Equal(t, sources.goSources[0].Version, int64(4)) + require.Equal(t, "4_something.go", sources.goSources[0].Path) + require.EqualValues(t, 4, sources.goSources[0].Version) }) t.Run("duplicate_versions", func(t *testing.T) { mapFS := fstest.MapFS{ @@ -161,7 +161,7 @@ func TestCollectFileSources(t *testing.T) { got, err := collectFilesystemSources(f, false, nil, nil) require.NoError(t, err) require.Equal(t, len(got.sqlSources), len(sqlSources)) - require.Equal(t, len(got.goSources), 0) + require.Empty(t, got.goSources) for i := 0; i < len(got.sqlSources); i++ { require.Equal(t, got.sqlSources[i], sqlSources[i]) } @@ -196,8 +196,8 @@ func TestMerge(t *testing.T) { require.NoError(t, err) sources, err := collectFilesystemSources(fsys, false, nil, nil) require.NoError(t, err) - require.Equal(t, len(sources.sqlSources), 1) - require.Equal(t, len(sources.goSources), 2) + require.Len(t, sources.sqlSources, 1) + require.Len(t, sources.goSources, 2) t.Run("valid", func(t *testing.T) { registered := map[int64]*Migration{ 2: NewGoMigration(2, nil, nil), @@ -205,7 +205,7 @@ func TestMerge(t *testing.T) { } migrations, err := merge(sources, registered) require.NoError(t, err) - require.Equal(t, len(migrations), 3) + require.Len(t, migrations, 3) assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1)) assertMigration(t, migrations[1], newSource(TypeGo, "00002_bar.go", 2)) assertMigration(t, migrations[2], newSource(TypeGo, "00003_baz.go", 3)) @@ -251,7 +251,7 @@ func TestMerge(t *testing.T) { 6: NewGoMigration(6, nil, nil), }) require.NoError(t, err) - require.Equal(t, len(migrations), 5) + require.Len(t, migrations, 5) assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1)) assertMigration(t, migrations[1], newSource(TypeSQL, "00002_bar.sql", 2)) assertMigration(t, migrations[2], newSource(TypeGo, "", 3)) @@ -277,7 +277,7 @@ func TestMerge(t *testing.T) { 6: NewGoMigration(6, nil, nil), }) require.NoError(t, err) - require.Equal(t, len(migrations), 4) + require.Len(t, migrations, 4) assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1)) assertMigration(t, migrations[1], newSource(TypeGo, "00002_bar.go", 2)) assertMigration(t, migrations[2], newSource(TypeGo, "", 3)) @@ -288,13 +288,13 @@ func TestMerge(t *testing.T) { func assertMigration(t *testing.T, got *Migration, want Source) { t.Helper() - require.Equal(t, got.Type, want.Type) - require.Equal(t, got.Version, want.Version) - require.Equal(t, got.Source, want.Path) + require.Equal(t, want.Type, got.Type) + require.Equal(t, want.Version, got.Version) + require.Equal(t, want.Path, got.Source) switch got.Type { case TypeGo: - require.True(t, got.goUp != nil) - require.True(t, got.goDown != nil) + require.NotNil(t, got.goUp) + require.NotNil(t, got.goDown) case TypeSQL: require.False(t, got.sql.Parsed) default: diff --git a/provider_run_test.go b/provider_run_test.go index 793072ba4..4598e9510 100644 --- a/provider_run_test.go +++ b/provider_run_test.go @@ -27,7 +27,7 @@ func TestProviderRun(t *testing.T) { require.NoError(t, db.Close()) _, err := p.Up(context.Background()) require.Error(t, err) - require.Equal(t, err.Error(), "failed to initialize: sql: database is closed") + require.Equal(t, "failed to initialize: sql: database is closed", err.Error()) }) t.Run("ping_and_close", func(t *testing.T) { p, _ := newProviderWithDB(t) @@ -40,22 +40,22 @@ func TestProviderRun(t *testing.T) { p, _ := newProviderWithDB(t) _, err := p.ApplyVersion(context.Background(), 999, true) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrVersionNotFound)) + require.ErrorIs(t, err, goose.ErrVersionNotFound) _, err = p.ApplyVersion(context.Background(), 999, false) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrVersionNotFound)) + require.ErrorIs(t, err, goose.ErrVersionNotFound) }) t.Run("run_zero", func(t *testing.T) { p, _ := newProviderWithDB(t) _, err := p.UpTo(context.Background(), 0) require.Error(t, err) - require.Equal(t, err.Error(), "version must be greater than 0") + require.Equal(t, "version must be greater than 0", err.Error()) _, err = p.DownTo(context.Background(), -1) require.Error(t, err) - require.Equal(t, err.Error(), "invalid version: must be a valid number or zero: -1") + require.Equal(t, "invalid version: must be a valid number or zero: -1", err.Error()) _, err = p.ApplyVersion(context.Background(), 0, true) require.Error(t, err) - require.Equal(t, err.Error(), "version must be greater than 0") + require.Equal(t, "version must be greater than 0", err.Error()) }) t.Run("up_and_down_all", func(t *testing.T) { ctx := context.Background() @@ -64,15 +64,15 @@ func TestProviderRun(t *testing.T) { numCount = 7 ) sources := p.ListSources() - require.Equal(t, len(sources), numCount) + require.Len(t, sources, numCount) // Ensure only SQL migrations are returned for _, s := range sources { - require.Equal(t, s.Type, goose.TypeSQL) + require.Equal(t, goose.TypeSQL, s.Type) } // Test Up res, err := p.Up(ctx) require.NoError(t, err) - require.Equal(t, len(res), numCount) + require.Len(t, res, numCount) assertResult(t, res[0], newSource(goose.TypeSQL, "00001_users_table.sql", 1), "up", false) assertResult(t, res[1], newSource(goose.TypeSQL, "00002_posts_table.sql", 2), "up", false) assertResult(t, res[2], newSource(goose.TypeSQL, "00003_comments_table.sql", 3), "up", false) @@ -83,7 +83,7 @@ func TestProviderRun(t *testing.T) { // Test Down res, err = p.DownTo(ctx, 0) require.NoError(t, err) - require.Equal(t, len(res), numCount) + require.Len(t, res, numCount) assertResult(t, res[0], newSource(goose.TypeSQL, "00007_empty_up_down.sql", 7), "down", true) assertResult(t, res[1], newSource(goose.TypeSQL, "00006_empty_up.sql", 6), "down", true) assertResult(t, res[2], newSource(goose.TypeSQL, "00005_posts_view.sql", 5), "down", false) @@ -108,7 +108,7 @@ func TestProviderRun(t *testing.T) { break } require.NoError(t, err) - require.True(t, res != nil) + require.NotNil(t, res) require.Equal(t, res.Source.Version, int64(counter)) } currentVersion, err := p.GetDBVersion(ctx) @@ -127,13 +127,13 @@ func TestProviderRun(t *testing.T) { break } require.NoError(t, err) - require.True(t, res != nil) + require.NotNil(t, res) require.Equal(t, res.Source.Version, int64(maxVersion-counter+1)) } // Once everything is tested the version should match the highest testdata version currentVersion, err = p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, currentVersion, 0) + require.EqualValues(t, 0, currentVersion) }) t.Run("up_to", func(t *testing.T) { ctx := context.Background() @@ -143,17 +143,17 @@ func TestProviderRun(t *testing.T) { ) results, err := p.UpTo(ctx, upToVersion) require.NoError(t, err) - require.EqualValues(t, len(results), upToVersion) + require.Len(t, results, int(upToVersion)) assertResult(t, results[0], newSource(goose.TypeSQL, "00001_users_table.sql", 1), "up", false) assertResult(t, results[1], newSource(goose.TypeSQL, "00002_posts_table.sql", 2), "up", false) // Fetch the goose version from DB currentVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.Equal(t, currentVersion, upToVersion) + require.Equal(t, upToVersion, currentVersion) // Validate the version actually matches what goose claims it is gotVersion, err := getMaxVersionID(db, goose.DefaultTablename) require.NoError(t, err) - require.Equal(t, gotVersion, upToVersion) + require.Equal(t, upToVersion, gotVersion) }) t.Run("sql_connections", func(t *testing.T) { tt := []struct { @@ -177,11 +177,11 @@ func TestProviderRun(t *testing.T) { db.SetMaxIdleConns(tc.maxIdleConns) } sources := p.ListSources() - require.NotZero(t, len(sources)) + require.NotEmpty(t, sources) currentVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, currentVersion, 0) + require.EqualValues(t, 0, currentVersion) { // Apply all up migrations @@ -210,7 +210,7 @@ func TestProviderRun(t *testing.T) { require.Equal(t, len(downResult), len(sources)) gotVersion, err := getMaxVersionID(db, goose.DefaultTablename) require.NoError(t, err) - require.EqualValues(t, gotVersion, 0) + require.EqualValues(t, 0, gotVersion) // Should only be left with a single table, the default goose table tables, err := getTableNames(db) require.NoError(t, err) @@ -256,7 +256,7 @@ func TestProviderRun(t *testing.T) { require.NoError(t, err) _, err = p.ApplyVersion(ctx, 1, true) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrAlreadyApplied)) + require.ErrorIs(t, err, goose.ErrAlreadyApplied) require.Contains(t, err.Error(), "version 1: migration already applied") }) t.Run("status", func(t *testing.T) { @@ -321,31 +321,30 @@ INSERT INTO owners (owner_name) VALUES ('seed-user-3'); _, err = p.Up(ctx) require.Error(t, err) require.Contains(t, err.Error(), "partial migration error (type:sql,version:2)") - var expected *goose.PartialError - require.True(t, errors.As(err, &expected)) + expected := new(goose.PartialError) + require.ErrorAs(t, err, &expected) // Check Err field - require.True(t, expected.Err != nil) require.Contains(t, expected.Err.Error(), "SQL logic error: no such table: invalid_table (1)") // Check Results field - require.Equal(t, len(expected.Applied), 1) + require.Len(t, expected.Applied, 1) assertResult(t, expected.Applied[0], newSource(goose.TypeSQL, "00001_users_table.sql", 1), "up", false) // Check Failed field - require.True(t, expected.Failed != nil) + require.NotNil(t, expected.Failed) assertSource(t, expected.Failed.Source, goose.TypeSQL, "00002_partial_error.sql", 2) require.False(t, expected.Failed.Empty) - require.True(t, expected.Failed.Error != nil) + require.Error(t, expected.Failed.Error) require.Contains(t, expected.Failed.Error.Error(), "SQL logic error: no such table: invalid_table (1)") - require.Equal(t, expected.Failed.Direction, "up") - require.True(t, expected.Failed.Duration > 0) + require.Equal(t, "up", expected.Failed.Direction) + require.Positive(t, expected.Failed.Duration) // Ensure the partial error did not affect the database. count, err := countOwners(db) require.NoError(t, err) - require.Equal(t, count, 0) + require.Equal(t, 0, count) status, err := p.Status(ctx) require.NoError(t, err) - require.Equal(t, len(status), 3) + require.Len(t, status, 3) assertStatus(t, status[0], goose.StateApplied, newSource(goose.TypeSQL, "00001_users_table.sql", 1), false) assertStatus(t, status[1], goose.StatePending, newSource(goose.TypeSQL, "00002_partial_error.sql", 2), true) assertStatus(t, status[2], goose.StatePending, newSource(goose.TypeSQL, "00003_insert_data.sql", 3), true) @@ -444,7 +443,7 @@ func TestConcurrentProvider(t *testing.T) { if t.Failed() { return } - require.Equal(t, len(valid), 1) + require.Len(t, valid, 1) require.Equal(t, len(empty), maxVersion-1) // Ensure the valid result is correct. require.Equal(t, len(valid[0]), maxVersion) @@ -485,13 +484,13 @@ func TestNoVersioning(t *testing.T) { goose.WithVerbose(testing.Verbose()), goose.WithDisableVersioning(false), // This is the default. ) - require.Equal(t, len(p.ListSources()), 3) + require.Len(t, p.ListSources(), 3) require.NoError(t, err) _, err = p.Up(ctx) require.NoError(t, err) baseVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, baseVersion, 3) + require.EqualValues(t, 3, baseVersion) t.Run("seed-up-down-to-zero", func(t *testing.T) { fsys := os.DirFS(filepath.Join("testdata", "no-versioning", "seed")) p, err := goose.NewProvider(goose.DialectSQLite3, db, fsys, @@ -499,36 +498,36 @@ func TestNoVersioning(t *testing.T) { goose.WithDisableVersioning(true), // Provider with no versioning. ) require.NoError(t, err) - require.Equal(t, len(p.ListSources()), 2) + require.Len(t, p.ListSources(), 2) // Run (all) up migrations from the seed dir { upResult, err := p.Up(ctx) require.NoError(t, err) - require.Equal(t, len(upResult), 2) + require.Len(t, upResult, 2) // When versioning is disabled, we cannot track the version of the seed files. _, err = p.GetDBVersion(ctx) require.Error(t, err) seedOwnerCount, err := countSeedOwners(db) require.NoError(t, err) - require.Equal(t, seedOwnerCount, wantSeedOwnerCount) + require.Equal(t, wantSeedOwnerCount, seedOwnerCount) } // Run (all) down migrations from the seed dir { downResult, err := p.DownTo(ctx, 0) require.NoError(t, err) - require.Equal(t, len(downResult), 2) + require.Len(t, downResult, 2) // When versioning is disabled, we cannot track the version of the seed files. _, err = p.GetDBVersion(ctx) require.Error(t, err) seedOwnerCount, err := countSeedOwners(db) require.NoError(t, err) - require.Equal(t, seedOwnerCount, 0) + require.Equal(t, 0, seedOwnerCount) } // The migrations added 4 non-seed owners, they must remain in the database afterwards ownerCount, err := countOwners(db) require.NoError(t, err) - require.Equal(t, ownerCount, wantOwnerCount) + require.Equal(t, wantOwnerCount, ownerCount) }) } @@ -555,15 +554,15 @@ func TestAllowMissing(t *testing.T) { require.NoError(t, err) currentVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, currentVersion, 3) + require.EqualValues(t, 3, currentVersion) // Developer A - migration 5 (mistakenly applied) result, err := p.ApplyVersion(ctx, 5, true) require.NoError(t, err) - require.EqualValues(t, result.Source.Version, 5) + require.EqualValues(t, 5, result.Source.Version) current, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, current, 5) + require.EqualValues(t, 5, current) // The database has migrations 1,2,3,5 applied. @@ -577,7 +576,7 @@ func TestAllowMissing(t *testing.T) { // Confirm db version is unchanged. current, err = p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, current, 5) + require.EqualValues(t, 5, current) _, err = p.UpByOne(ctx) require.Error(t, err) @@ -586,7 +585,7 @@ func TestAllowMissing(t *testing.T) { // Confirm db version is unchanged. current, err = p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, current, 5) + require.EqualValues(t, 5, current) _, err = p.UpTo(ctx, math.MaxInt64) require.Error(t, err) @@ -595,7 +594,7 @@ func TestAllowMissing(t *testing.T) { // Confirm db version is unchanged. current, err = p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, current, 5) + require.EqualValues(t, 5, current) }) t.Run("missing_allowed", func(t *testing.T) { @@ -610,7 +609,7 @@ func TestAllowMissing(t *testing.T) { require.NoError(t, err) currentVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, currentVersion, 3) + require.EqualValues(t, 3, currentVersion) // Developer A - migration 5 (mistakenly applied) { @@ -618,28 +617,28 @@ func TestAllowMissing(t *testing.T) { require.NoError(t, err) current, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.EqualValues(t, current, 5) + require.EqualValues(t, 5, current) } // Developer B - migration 4 (missing) and 6 (new) { // 4 upResult, err := p.UpByOne(ctx) require.NoError(t, err) - require.True(t, upResult != nil) - require.EqualValues(t, upResult.Source.Version, 4) + require.NotNil(t, upResult) + require.EqualValues(t, 4, upResult.Source.Version) // 6 upResult, err = p.UpByOne(ctx) require.NoError(t, err) - require.True(t, upResult != nil) - require.EqualValues(t, upResult.Source.Version, 6) + require.NotNil(t, upResult) + require.EqualValues(t, 6, upResult.Source.Version) count, err := getGooseVersionCount(db, goose.DefaultTablename) require.NoError(t, err) - require.EqualValues(t, count, 6) + require.EqualValues(t, 6, count) current, err := p.GetDBVersion(ctx) require.NoError(t, err) // Expecting max(version_id) to be 8 - require.EqualValues(t, current, 6) + require.EqualValues(t, 6, current) } // The applied order in the database is expected to be: @@ -650,11 +649,11 @@ func TestAllowMissing(t *testing.T) { testDownAndVersion := func(wantDBVersion, wantResultVersion int64) { currentVersion, err := p.GetDBVersion(ctx) require.NoError(t, err) - require.Equal(t, currentVersion, wantDBVersion) + require.Equal(t, wantDBVersion, currentVersion) downRes, err := p.Down(ctx) require.NoError(t, err) - require.True(t, downRes != nil) - require.Equal(t, downRes.Source.Version, wantResultVersion) + require.NotNil(t, downRes) + require.Equal(t, wantResultVersion, downRes.Source.Version) } // This behaviour may need to change, see the following issues for more details: @@ -669,7 +668,7 @@ func TestAllowMissing(t *testing.T) { testDownAndVersion(1, 1) _, err = p.Down(ctx) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrNoNextVersion)) + require.ErrorIs(t, err, goose.ErrNoNextVersion) }) } @@ -739,9 +738,8 @@ func TestGoMigrationPanic(t *testing.T) { require.NoError(t, err) _, err = p.Up(ctx) require.Error(t, err) - require.Contains(t, err.Error(), wantErrString) - var expected *goose.PartialError - require.True(t, errors.As(err, &expected)) + expected := new(goose.PartialError) + require.ErrorAs(t, err, &expected) require.Contains(t, expected.Err.Error(), wantErrString) } @@ -769,7 +767,7 @@ func TestProviderApply(t *testing.T) { // This version has a corresponding down migration, but has never been applied. _, err = p.ApplyVersion(ctx, 2, false) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrNotApplied)) + require.ErrorIs(t, err, goose.ErrNotApplied) } func TestPending(t *testing.T) { @@ -790,8 +788,8 @@ func TestPending(t *testing.T) { // migrations. current, target, err := p.GetVersions(ctx) require.NoError(t, err) - require.EqualValues(t, current, 3) - require.EqualValues(t, target, len(fsys)) + require.EqualValues(t, 3, current) + require.Len(t, fsys, int(target)) hasPending, err := p.HasPending(ctx) require.NoError(t, err) require.True(t, hasPending) @@ -811,6 +809,7 @@ func TestPending(t *testing.T) { fsys := newFsys() run := func(t *testing.T, versionToApply int64) { + t.Helper() p, err := goose.NewProvider(goose.DialectSQLite3, newDB(t), fsys, goose.WithAllowOutofOrder(false), ) @@ -825,7 +824,7 @@ func TestPending(t *testing.T) { current, target, err := p.GetVersions(ctx) require.NoError(t, err) require.Equal(t, current, versionToApply) - require.EqualValues(t, target, len(fsys)) + require.Len(t, fsys, int(target)) _, err = p.HasPending(ctx) require.Error(t, err) require.Contains(t, err.Error(), "missing (out-of-order) migration") @@ -904,25 +903,25 @@ func TestGoOnly(t *testing.T) { ) require.NoError(t, err) sources := p.ListSources() - require.Equal(t, len(p.ListSources()), 2) + require.Len(t, p.ListSources(), 2) assertSource(t, sources[0], goose.TypeGo, "", 1) assertSource(t, sources[1], goose.TypeGo, "", 2) // Apply migration 1 res, err := p.UpByOne(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 1), "up", false) - require.Equal(t, countUser(db), 0) + require.Equal(t, 0, countUser(db)) require.True(t, tableExists(t, db, "users")) // Apply migration 2 res, err = p.UpByOne(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 2), "up", false) - require.Equal(t, countUser(db), 3) + require.Equal(t, 3, countUser(db)) // Rollback migration 2 res, err = p.Down(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 2), "down", false) - require.Equal(t, countUser(db), 0) + require.Equal(t, 0, countUser(db)) // Rollback migration 1 res, err = p.Down(ctx) require.NoError(t, err) @@ -960,25 +959,25 @@ func TestGoOnly(t *testing.T) { ) require.NoError(t, err) sources := p.ListSources() - require.Equal(t, len(p.ListSources()), 2) + require.Len(t, p.ListSources(), 2) assertSource(t, sources[0], goose.TypeGo, "", 1) assertSource(t, sources[1], goose.TypeGo, "", 2) // Apply migration 1 res, err := p.UpByOne(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 1), "up", false) - require.Equal(t, countUser(db), 0) + require.Equal(t, 0, countUser(db)) require.True(t, tableExists(t, db, "users")) // Apply migration 2 res, err = p.UpByOne(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 2), "up", false) - require.Equal(t, countUser(db), 3) + require.Equal(t, 3, countUser(db)) // Rollback migration 2 res, err = p.Down(ctx) require.NoError(t, err) assertResult(t, res, newSource(goose.TypeGo, "", 2), "down", false) - require.Equal(t, countUser(db), 0) + require.Equal(t, 0, countUser(db)) // Rollback migration 1 res, err = p.Down(ctx) require.NoError(t, err) @@ -1072,21 +1071,33 @@ func getTableNames(db *sql.DB) ([]string, error) { return tables, nil } -func assertStatus(t *testing.T, got *goose.MigrationStatus, state goose.State, source *goose.Source, appliedIsZero bool) { +func assertStatus( + t *testing.T, + got *goose.MigrationStatus, + state goose.State, + source *goose.Source, + appliedIsZero bool, +) { t.Helper() require.Equal(t, got.State, state) require.Equal(t, got.Source, source) require.Equal(t, got.AppliedAt.IsZero(), appliedIsZero) } -func assertResult(t *testing.T, got *goose.MigrationResult, source *goose.Source, direction string, isEmpty bool) { +func assertResult( + t *testing.T, + got *goose.MigrationResult, + source *goose.Source, + direction string, + isEmpty bool, +) { t.Helper() - require.True(t, got != nil) + require.NotNil(t, got) require.Equal(t, got.Source, source) require.Equal(t, got.Direction, direction) require.Equal(t, got.Empty, isEmpty) - require.Nil(t, got.Error) - require.True(t, got.Duration > 0) + require.NoError(t, got.Error) + require.Positive(t, got.Duration) } func assertSource(t *testing.T, got *goose.Source, typ goose.MigrationType, name string, version int64) { diff --git a/provider_test.go b/provider_test.go index 6228b80ad..283347eab 100644 --- a/provider_test.go +++ b/provider_test.go @@ -2,7 +2,6 @@ package goose_test import ( "database/sql" - "errors" "io/fs" "path/filepath" "testing" @@ -20,7 +19,7 @@ func TestProvider(t *testing.T) { t.Run("empty", func(t *testing.T) { _, err := goose.NewProvider(goose.DialectSQLite3, db, fstest.MapFS{}) require.Error(t, err) - require.True(t, errors.Is(err, goose.ErrNoMigrations)) + require.ErrorIs(t, err, goose.ErrNoMigrations) }) mapFS := fstest.MapFS{ @@ -32,7 +31,7 @@ func TestProvider(t *testing.T) { p, err := goose.NewProvider(goose.DialectSQLite3, db, fsys) require.NoError(t, err) sources := p.ListSources() - require.Equal(t, len(sources), 2) + require.Len(t, sources, 2) require.Equal(t, sources[0], newSource(goose.TypeSQL, "001_foo.sql", 1)) require.Equal(t, sources[1], newSource(goose.TypeSQL, "002_bar.sql", 2)) } diff --git a/tests/gomigrations/error/gomigrations_error_test.go b/tests/gomigrations/error/gomigrations_error_test.go index 3a62d8f21..0ebf4f889 100644 --- a/tests/gomigrations/error/gomigrations_error_test.go +++ b/tests/gomigrations/error/gomigrations_error_test.go @@ -20,19 +20,19 @@ func TestGoMigrationByOne(t *testing.T) { // Create goose table. current, err := goose.EnsureDBVersion(db) require.NoError(t, err) - require.Equal(t, current, 0) + require.Equal(t, 0, current) // Collect migrations. dir := "testdata" migrations, err := goose.CollectMigrations(dir, 0, goose.MaxVersion) require.NoError(t, err) - require.Equal(t, len(migrations), 4) + require.Len(t, migrations, 4) // Setup table. err = migrations[0].Up(db) require.NoError(t, err) version, err := goose.GetDBVersion(db) require.NoError(t, err) - require.Equal(t, version, 1) + require.Equal(t, 1, version) // Registered Go migration run outside a goose tx using *sql.DB. err = migrations[1].Up(db) @@ -40,14 +40,14 @@ func TestGoMigrationByOne(t *testing.T) { require.Contains(t, err.Error(), "failed to run go migration") version, err = goose.GetDBVersion(db) require.NoError(t, err) - require.Equal(t, version, 1) + require.Equal(t, 1, version) // This migration was inserting 100 rows, but fails at 50, and // because it's run outside a goose tx then we expect 50 rows. var count int err = db.QueryRow("SELECT COUNT(*) FROM foo").Scan(&count) require.NoError(t, err) - require.Equal(t, count, 50) + require.Equal(t, 50, count) // Truncate table so we have 0 rows. err = migrations[2].Up(db) @@ -55,7 +55,7 @@ func TestGoMigrationByOne(t *testing.T) { version, err = goose.GetDBVersion(db) require.NoError(t, err) // We're at version 3, but keep in mind 2 was never applied because it failed. - require.Equal(t, version, 3) + require.EqualValues(t, 3, version) // Registered Go migration run within a tx. err = migrations[3].Up(db) @@ -63,11 +63,11 @@ func TestGoMigrationByOne(t *testing.T) { require.Contains(t, err.Error(), "failed to run go migration") version, err = goose.GetDBVersion(db) require.NoError(t, err) - require.Equal(t, version, 3) // This migration failed, so we're still at 3. + require.Equal(t, 3, version) // This migration failed, so we're still at 3. // This migration was inserting 100 rows, but fails at 50. However, since it's // running within a tx we expect none of the inserts to persist. err = db.QueryRow("SELECT COUNT(*) FROM foo").Scan(&count) require.NoError(t, err) - require.Equal(t, count, 0) + require.Equal(t, 0, count) } diff --git a/tests/gomigrations/register/register_test.go b/tests/gomigrations/register/register_test.go index 1d5e08d73..fdfa0bbef 100644 --- a/tests/gomigrations/register/register_test.go +++ b/tests/gomigrations/register/register_test.go @@ -13,7 +13,7 @@ import ( func TestAddFunctions(t *testing.T) { goMigrations, err := goose.CollectMigrations("testdata", 0, math.MaxInt64) require.NoError(t, err) - require.Equal(t, len(goMigrations), 4) + require.Len(t, goMigrations, 4) checkMigration(t, goMigrations[0], &goose.Migration{ Version: 1, @@ -51,12 +51,12 @@ func TestAddFunctions(t *testing.T) { func checkMigration(t *testing.T, got *goose.Migration, want *goose.Migration) { t.Helper() - require.Equal(t, got.Version, want.Version) - require.Equal(t, got.Next, want.Next) - require.Equal(t, got.Previous, want.Previous) - require.Equal(t, filepath.Base(got.Source), want.Source) - require.Equal(t, got.Registered, want.Registered) - require.Equal(t, got.UseTx, want.UseTx) + require.Equal(t, want.Version, got.Version) + require.Equal(t, want.Next, got.Next) + require.Equal(t, want.Previous, got.Previous) + require.Equal(t, want.Source, filepath.Base(got.Source)) + require.Equal(t, want.Registered, got.Registered) + require.Equal(t, want.UseTx, got.UseTx) checkFunctions(t, got) } diff --git a/tests/gomigrations/success/gomigrations_success_test.go b/tests/gomigrations/success/gomigrations_success_test.go index e88ec108d..65b62a95b 100644 --- a/tests/gomigrations/success/gomigrations_success_test.go +++ b/tests/gomigrations/success/gomigrations_success_test.go @@ -23,6 +23,7 @@ func TestGoMigrationByOne(t *testing.T) { require.NoError(t, err) upByOne := func(t *testing.T) int64 { + t.Helper() err = goose.UpByOne(db, dir) t.Logf("err: %v %s", err, dir) require.NoError(t, err) @@ -31,6 +32,7 @@ func TestGoMigrationByOne(t *testing.T) { return version } downByOne := func(t *testing.T) int64 { + t.Helper() err = goose.Down(db, dir) require.NoError(t, err) version, err := goose.GetDBVersion(db) @@ -43,22 +45,25 @@ func TestGoMigrationByOne(t *testing.T) { } version, err := goose.GetDBVersion(db) require.NoError(t, err) - require.Equal(t, version, len(files)) + require.Len(t, files, int(version)) tables, err := ListTables(db) require.NoError(t, err) - require.Equal(t, tables, []string{ - "alpha", - "bravo", - "charlie", - "delta", - "echo", - "foxtrot", - "golf", - "goose_db_version", - "hotel", - "sqlite_sequence", - }) + require.Equal(t, + []string{ + "alpha", + "bravo", + "charlie", + "delta", + "echo", + "foxtrot", + "golf", + "goose_db_version", + "hotel", + "sqlite_sequence", + }, + tables, + ) // Migrate all files down-by-one. for i := len(files) - 1; i >= 0; i-- { @@ -66,14 +71,17 @@ func TestGoMigrationByOne(t *testing.T) { } version, err = goose.GetDBVersion(db) require.NoError(t, err) - require.Equal(t, version, 0) + require.Equal(t, 0, version) tables, err = ListTables(db) require.NoError(t, err) - require.Equal(t, tables, []string{ - "goose_db_version", - "sqlite_sequence", - }) + require.Equal(t, + []string{ + "goose_db_version", + "sqlite_sequence", + }, + tables, + ) } func ListTables(db *sql.DB) ([]string, error) {