From 3c8f1975aa438a4493186bbc9d0172236d106ce3 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Mon, 10 Feb 2020 19:55:21 +0530 Subject: [PATCH] Replace t.Fatal with require.NoError in tests (#1213) We are using the following pattern in tests that can be replaced with `require.NoError(t, err)`. ```go if err != nil { t.Fatal(err) } ``` (cherry picked from commit 78d405a5b2616d4a14b902f4a3094fecdf745819) --- backup_test.go | 98 ++++++++++++++++++++------------------------------ db_test.go | 7 +--- txn_test.go | 27 +++++--------- 3 files changed, 47 insertions(+), 85 deletions(-) diff --git a/backup_test.go b/backup_test.go index d2aba23d8..b652c89e3 100644 --- a/backup_test.go +++ b/backup_test.go @@ -116,9 +116,8 @@ func TestBackupRestore1(t *testing.T) { func TestBackupRestore2(t *testing.T) { tmpdir, err := ioutil.TempDir("", "badger-test") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer removeDir(tmpdir) s1Path := filepath.Join(tmpdir, "test1") @@ -126,9 +125,8 @@ func TestBackupRestore2(t *testing.T) { s3Path := filepath.Join(tmpdir, "test3") db1, err := Open(getTestOptions(s1Path)) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db1.Close() key1 := []byte("key1") key2 := []byte("key2") @@ -140,9 +138,8 @@ func TestBackupRestore2(t *testing.T) { } return tx.SetEntry(NewEntry(key2, rawValue)) }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + for i := byte(1); i < N; i++ { err = db1.Update(func(tx *Txn) error { if err := tx.SetEntry(NewEntry(append(key1, i), rawValue)); err != nil { @@ -150,26 +147,21 @@ func TestBackupRestore2(t *testing.T) { } return tx.SetEntry(NewEntry(append(key2, i), rawValue)) }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + } var backup bytes.Buffer _, err = db1.Backup(&backup, 0) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + fmt.Println("backup1 length:", backup.Len()) db2, err := Open(getTestOptions(s2Path)) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db2.Close() err = db2.Load(&backup, 16) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) for i := byte(1); i < N; i++ { err = db2.View(func(tx *Txn) error { @@ -190,9 +182,8 @@ func TestBackupRestore2(t *testing.T) { } return nil }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + } for i := byte(1); i < N; i++ { @@ -202,27 +193,22 @@ func TestBackupRestore2(t *testing.T) { } return tx.SetEntry(NewEntry(append(key2, i), rawValue)) }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + } backup.Reset() _, err = db2.Backup(&backup, 0) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + fmt.Println("backup2 length:", backup.Len()) db3, err := Open(getTestOptions(s3Path)) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db3.Close() err = db3.Load(&backup, 16) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) for i := byte(1); i < N; i++ { err = db3.View(func(tx *Txn) error { @@ -243,9 +229,8 @@ func TestBackupRestore2(t *testing.T) { } return nil }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + } } @@ -313,9 +298,8 @@ func TestBackup(t *testing.T) { } t.Run("disk mode", func(t *testing.T) { tmpdir, err := ioutil.TempDir("", "badger-test") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer removeDir(tmpdir) opt := DefaultOptions(filepath.Join(tmpdir, "backup0")) runBadgerTest(t, &opt, func(t *testing.T, db *DB) { @@ -334,9 +318,8 @@ func TestBackup(t *testing.T) { func TestBackupRestore3(t *testing.T) { var bb bytes.Buffer tmpdir, err := ioutil.TempDir("", "badger-test") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer removeDir(tmpdir) N := 1000 @@ -345,9 +328,8 @@ func TestBackupRestore3(t *testing.T) { // backup { db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup1"))) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db1.Close() require.NoError(t, populateEntries(db1, entries)) @@ -360,9 +342,8 @@ func TestBackupRestore3(t *testing.T) { // restore db2, err := Open(DefaultOptions(filepath.Join(tmpdir, "restore1"))) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db2.Close() require.NoError(t, db2.Load(&bb, 16)) @@ -393,9 +374,8 @@ func TestBackupRestore3(t *testing.T) { func TestBackupLoadIncremental(t *testing.T) { tmpdir, err := ioutil.TempDir("", "badger-test") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer removeDir(tmpdir) N := 100 @@ -406,9 +386,8 @@ func TestBackupLoadIncremental(t *testing.T) { // backup { db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup2"))) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db1.Close() require.NoError(t, populateEntries(db1, entries)) @@ -467,9 +446,8 @@ func TestBackupLoadIncremental(t *testing.T) { // restore db2, err := Open(getTestOptions(filepath.Join(tmpdir, "restore2"))) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer db2.Close() require.NoError(t, db2.Load(&bb, 16)) diff --git a/db_test.go b/db_test.go index 41631dab8..ad1a29654 100644 --- a/db_test.go +++ b/db_test.go @@ -1567,9 +1567,6 @@ func TestLSMOnly(t *testing.T) { opts.ValueLogMaxEntries = 100 db, err := Open(opts) require.NoError(t, err) - if err != nil { - t.Fatal(err) - } value := make([]byte, 128) _, err = rand.Read(value) @@ -1581,9 +1578,7 @@ func TestLSMOnly(t *testing.T) { db, err = Open(opts) require.NoError(t, err) - if err != nil { - t.Fatal(err) - } + defer db.Close() require.NoError(t, db.RunValueLogGC(0.2)) } diff --git a/txn_test.go b/txn_test.go index 8450da910..647bd9806 100644 --- a/txn_test.go +++ b/txn_test.go @@ -837,9 +837,8 @@ func TestManagedDB(t *testing.T) { func TestArmV7Issue311Fix(t *testing.T) { dir, err := ioutil.TempDir("", "") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + defer removeDir(dir) db, err := Open(DefaultOptions(dir). @@ -848,31 +847,21 @@ func TestArmV7Issue311Fix(t *testing.T) { WithLevelOneSize(8 << 20). WithMaxTableSize(2 << 20). WithSyncWrites(false)) - if err != nil { - t.Fatalf("cannot open db at location %s: %v", dir, err) - } + + require.NoError(t, err) err = db.View(func(txn *Txn) error { return nil }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) err = db.Update(func(txn *Txn) error { return txn.SetEntry(NewEntry([]byte{0x11}, []byte{0x22})) }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) err = db.Update(func(txn *Txn) error { return txn.SetEntry(NewEntry([]byte{0x11}, []byte{0x22})) }) - if err != nil { - t.Fatal(err) - } - - if err = db.Close(); err != nil { - t.Fatal(err) - } + require.NoError(t, err) + require.NoError(t, db.Close()) }