From 1975f5105294ca5bd324f16dc4679a0c0ffb1812 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Thu, 9 Nov 2023 23:00:20 -0800 Subject: [PATCH] Call m.Run() in TestMain so that lib tests actually get executed, and fix test breakages that existed because lib tests weren't running --- client/client_test.go | 16 ++++++++++++++++ client/lib/lib_test.go | 28 ++++++++-------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 02f1204..997f8d1 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2584,4 +2584,20 @@ func BenchmarkImport(b *testing.B) { } } +func TestAugmentedIsOfflineError(t *testing.T) { + defer testutils.BackupAndRestore(t)() + installHishtory(t, zshTester{}, "") + defer testutils.BackupAndRestoreEnv("HISHTORY_SIMULATE_NETWORK_ERROR")() + ctx := hctx.MakeContext() + + // By default, when the hishtory server is up, then IsOfflineError checks the error msg + require.True(t, lib.CanReachHishtoryServer(ctx)) + require.False(t, lib.IsOfflineError(ctx, fmt.Errorf("unchecked error type"))) + + // When the hishtory server is down, then all error messages are treated as being due to offline errors + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1") + require.False(t, lib.CanReachHishtoryServer(ctx)) + require.True(t, lib.IsOfflineError(ctx, fmt.Errorf("unchecked error type"))) +} + // TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed diff --git a/client/lib/lib_test.go b/client/lib/lib_test.go index 1b007c8..61a60d2 100644 --- a/client/lib/lib_test.go +++ b/client/lib/lib_test.go @@ -1,7 +1,6 @@ package lib import ( - "fmt" "os" "reflect" "testing" @@ -18,6 +17,11 @@ func TestMain(m *testing.M) { // Set env variable defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")() os.Setenv("HISHTORY_TEST", "1") + m.Run() +} + +func requireEntriesEqual(t *testing.T, expected, actual data.HistoryEntry) { + require.Equal(t, normalizeEntryTimezone(expected), normalizeEntryTimezone(actual)) } func TestPersist(t *testing.T) { @@ -34,7 +38,7 @@ func TestPersist(t *testing.T) { t.Fatalf("DB has %d entries, expected 1!", len(historyEntries)) } dbEntry := historyEntries[0] - require.Equal(t, entry, *dbEntry) + requireEntriesEqual(t, entry, *dbEntry) } func TestSearch(t *testing.T) { @@ -55,8 +59,8 @@ func TestSearch(t *testing.T) { if len(results) != 2 { t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results) } - require.Equal(t, entry2, *results[0]) - require.Equal(t, entry1, *results[1]) + requireEntriesEqual(t, entry2, *results[0]) + requireEntriesEqual(t, entry1, *results[1]) // Search but exclude bar results, err = Search(ctx, db, "ls -bar", 5) @@ -283,19 +287,3 @@ func TestSplitEscaped(t *testing.T) { } } } - -func TestAugmentedIsOfflineError(t *testing.T) { - defer testutils.BackupAndRestore(t)() - defer testutils.RunTestServer()() - defer testutils.BackupAndRestoreEnv("HISHTORY_SIMULATE_NETWORK_ERROR")() - ctx := hctx.MakeContext() - - // By default, when the hishtory server is up, then IsOfflineError checks the error msg - require.True(t, CanReachHishtoryServer(ctx)) - require.False(t, IsOfflineError(ctx, fmt.Errorf("unchecked error type"))) - - // When the hishtory server is down, then all error messages are treated as being due to offline errors - os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1") - require.False(t, CanReachHishtoryServer(ctx)) - require.True(t, IsOfflineError(ctx, fmt.Errorf("unchecked error type"))) -}