mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-21 04:17:45 +02:00
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:
parent
165cdd9187
commit
1975f51052
@ -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
|
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@ -18,6 +17,11 @@ func TestMain(m *testing.M) {
|
|||||||
// Set env variable
|
// Set env variable
|
||||||
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()
|
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()
|
||||||
os.Setenv("HISHTORY_TEST", "1")
|
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) {
|
func TestPersist(t *testing.T) {
|
||||||
@ -34,7 +38,7 @@ func TestPersist(t *testing.T) {
|
|||||||
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
|
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
|
||||||
}
|
}
|
||||||
dbEntry := historyEntries[0]
|
dbEntry := historyEntries[0]
|
||||||
require.Equal(t, entry, *dbEntry)
|
requireEntriesEqual(t, entry, *dbEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearch(t *testing.T) {
|
func TestSearch(t *testing.T) {
|
||||||
@ -55,8 +59,8 @@ func TestSearch(t *testing.T) {
|
|||||||
if len(results) != 2 {
|
if len(results) != 2 {
|
||||||
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
||||||
}
|
}
|
||||||
require.Equal(t, entry2, *results[0])
|
requireEntriesEqual(t, entry2, *results[0])
|
||||||
require.Equal(t, entry1, *results[1])
|
requireEntriesEqual(t, entry1, *results[1])
|
||||||
|
|
||||||
// Search but exclude bar
|
// Search but exclude bar
|
||||||
results, err = Search(ctx, db, "ls -bar", 5)
|
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")))
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user