Remove testutils.Check(t, err) and replace it with require.NoError which gives a clearer error message and a full stacktrace

This commit is contained in:
David Dworken
2023-09-24 16:05:01 -07:00
parent c77d5a5424
commit 9fda54d4c2
7 changed files with 156 additions and 160 deletions

View File

@ -10,16 +10,17 @@ import (
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/ddworken/hishtory/shared/testutils"
"github.com/stretchr/testify/require"
)
func TestBuildHistoryEntry(t *testing.T) {
defer testutils.BackupAndRestore(t)()
defer testutils.RunTestServer()()
testutils.Check(t, lib.Setup("", false))
require.NoError(t, lib.Setup("", false))
// Test building an actual entry for bash
entry, err := buildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "bash", "120", " 123 ls /foo ", "1641774958"})
testutils.Check(t, err)
require.NoError(t, err)
if entry.ExitCode != 120 {
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
}
@ -48,7 +49,7 @@ func TestBuildHistoryEntry(t *testing.T) {
// Test building an entry for zsh
entry, err = buildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "zsh", "120", "ls /foo\n", "1641774958"})
testutils.Check(t, err)
require.NoError(t, err)
if entry.ExitCode != 120 {
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
}
@ -73,7 +74,7 @@ func TestBuildHistoryEntry(t *testing.T) {
// Test building an entry for fish
entry, err = buildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "fish", "120", "ls /foo\n", "1641774958"})
testutils.Check(t, err)
require.NoError(t, err)
if entry.ExitCode != 120 {
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
}
@ -98,7 +99,7 @@ func TestBuildHistoryEntry(t *testing.T) {
// Test building an entry that is empty, and thus not saved
entry, err = buildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "zsh", "120", " \n", "1641774958"})
testutils.Check(t, err)
require.NoError(t, err)
if entry != nil {
t.Fatalf("expected history entry to be nil")
}
@ -108,7 +109,7 @@ func TestBuildHistoryEntryWithTimestampStripping(t *testing.T) {
defer testutils.BackupAndRestoreEnv("HISTTIMEFORMAT")()
defer testutils.BackupAndRestore(t)()
defer testutils.RunTestServer()()
testutils.Check(t, lib.Setup("", false))
require.NoError(t, lib.Setup("", false))
testcases := []struct {
input, histtimeformat, expectedCommand string
@ -120,11 +121,11 @@ func TestBuildHistoryEntryWithTimestampStripping(t *testing.T) {
for _, tc := range testcases {
conf := hctx.GetConf(hctx.MakeContext())
conf.LastSavedHistoryLine = ""
testutils.Check(t, hctx.SetConfig(conf))
require.NoError(t, hctx.SetConfig(conf))
os.Setenv("HISTTIMEFORMAT", tc.histtimeformat)
entry, err := buildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "bash", "120", tc.input, "1641774958"})
testutils.Check(t, err)
require.NoError(t, err)
if entry == nil {
t.Fatalf("entry is unexpectedly nil")
}
@ -136,12 +137,12 @@ func TestBuildHistoryEntryWithTimestampStripping(t *testing.T) {
func TestParseCrossPlatformInt(t *testing.T) {
res, err := parseCrossPlatformInt("123")
testutils.Check(t, err)
require.NoError(t, err)
if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res)
}
res, err = parseCrossPlatformInt("123N")
testutils.Check(t, err)
require.NoError(t, err)
if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res)
}
@ -177,7 +178,7 @@ func TestGetLastCommand(t *testing.T) {
}
for _, tc := range testcases {
actualOutput, err := getLastCommand(tc.input)
testutils.Check(t, err)
require.NoError(t, err)
if actualOutput != tc.expectedOutput {
t.Fatalf("getLastCommand(%#v) returned %#v (expected=%#v)", tc.input, actualOutput, tc.expectedOutput)
}
@ -219,7 +220,7 @@ func TestMaybeSkipBashHistTimePrefix(t *testing.T) {
for _, tc := range testcases {
os.Setenv("HISTTIMEFORMAT", tc.env)
stripped, err := maybeSkipBashHistTimePrefix(tc.cmdLine)
testutils.Check(t, err)
require.NoError(t, err)
if stripped != tc.expected {
t.Fatalf("skipping the time prefix returned %#v (expected=%#v for %#v)", stripped, tc.expected, tc.cmdLine)
}