Call m.Run() in TestMain so that lib tests actually get executed, and fix test breakages that existed because lib tests weren't running

This commit is contained in:
David Dworken 2023-11-09 23:00:20 -08:00
parent 165cdd9187
commit 1975f51052
No known key found for this signature in database
2 changed files with 24 additions and 20 deletions

View File

@ -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

View File

@ -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")))
}