Add more timestamp stripping tests

This commit is contained in:
David Dworken 2022-09-27 22:07:54 -07:00
parent 2bdfe3d64f
commit 070f8cf3d3

View File

@ -35,7 +35,6 @@ func TestSetup(t *testing.T) {
func TestBuildHistoryEntry(t *testing.T) { func TestBuildHistoryEntry(t *testing.T) {
defer shared.BackupAndRestore(t)() defer shared.BackupAndRestore(t)()
defer shared.RunTestServer()()
shared.Check(t, Setup([]string{})) shared.Check(t, Setup([]string{}))
// Test building an actual entry for bash // Test building an actual entry for bash
@ -93,6 +92,35 @@ func TestBuildHistoryEntry(t *testing.T) {
} }
} }
func TestBuildHistoryEntryWithRedaction(t *testing.T) {
defer shared.BackupAndRestoreEnv("HISTTIMEFORMAT")()
defer shared.BackupAndRestore(t)()
shared.Check(t, Setup([]string{}))
testcases := []struct {
input, histtimeformat, expectedCommand string
}{
{" 123 ls /foo ", "", "ls /foo"},
{" 2389 [2022-09-28 04:38:32 +0000] echo", "", "[2022-09-28 04:38:32 +0000] echo"},
{" 2389 [2022-09-28 04:38:32 +0000] echo", "[%F %T %z] ", "echo"},
}
for _, tc := range testcases {
conf := hctx.GetConf(hctx.MakeContext())
conf.LastSavedHistoryLine = ""
shared.Check(t, hctx.SetConfig(conf))
os.Setenv("HISTTIMEFORMAT", tc.histtimeformat)
entry, err := BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "bash", "120", tc.input, "1641774958"})
shared.Check(t, err)
if entry == nil {
t.Fatalf("entry is unexpectedly nil")
}
if entry.Command != tc.expectedCommand {
t.Fatalf("BuildHistoryEntry(%#v) returned %#v (expected=%#v)", tc.input, entry.Command, tc.expectedCommand)
}
}
}
func TestPersist(t *testing.T) { func TestPersist(t *testing.T) {
defer shared.BackupAndRestore(t)() defer shared.BackupAndRestore(t)()
shared.Check(t, hctx.InitConfig()) shared.Check(t, hctx.InitConfig())
@ -165,16 +193,12 @@ func TestAddToDbIfNew(t *testing.T) {
func TestParseCrossPlatformInt(t *testing.T) { func TestParseCrossPlatformInt(t *testing.T) {
res, err := parseCrossPlatformInt("123") res, err := parseCrossPlatformInt("123")
if err != nil { shared.Check(t, err)
t.Fatalf("failed to parse int: %v", err)
}
if res != 123 { if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res) t.Fatalf("failed to parse cross platform int %d", res)
} }
res, err = parseCrossPlatformInt("123N") res, err = parseCrossPlatformInt("123N")
if err != nil { shared.Check(t, err)
t.Fatalf("failed to parse int: %v", err)
}
if res != 123 { if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res) t.Fatalf("failed to parse cross platform int %d", res)
} }
@ -200,6 +224,22 @@ func TestBuildRegexFromTimeFormat(t *testing.T) {
} }
} }
func TestGetLastCommand(t *testing.T) {
testcases := []struct {
input, expectedOutput string
}{
{" 33 ls", "ls"},
{" 2389 [2022-09-28 04:38:32 +0000] echo", "[2022-09-28 04:38:32 +0000] echo"},
}
for _, tc := range testcases {
actualOutput, err := getLastCommand(tc.input)
shared.Check(t, err)
if actualOutput != tc.expectedOutput {
t.Fatalf("getLastCommand(%#v) returned %#v (expected=%#v)", tc.input, actualOutput, tc.expectedOutput)
}
}
}
func TestMaybeSkipBashHistTimePrefix(t *testing.T) { func TestMaybeSkipBashHistTimePrefix(t *testing.T) {
defer shared.BackupAndRestoreEnv("HISTTIMEFORMAT")() defer shared.BackupAndRestoreEnv("HISTTIMEFORMAT")()
@ -229,14 +269,13 @@ func TestMaybeSkipBashHistTimePrefix(t *testing.T) {
{"[%c %t]", "[Sun Aug 19 02:56:02 2012 ]foo", "foo"}, {"[%c %t]", "[Sun Aug 19 02:56:02 2012 ]foo", "foo"},
{"[%c %t", "[Sun Aug 19 02:56:02 2012 foo", "foo"}, {"[%c %t", "[Sun Aug 19 02:56:02 2012 foo", "foo"},
{"[%F %T %z]", "[2022-09-28 04:17:06 +0000]foo", "foo"}, {"[%F %T %z]", "[2022-09-28 04:17:06 +0000]foo", "foo"},
{"[%F %T %z] ", "[2022-09-28 04:17:06 +0000] foo", "foo"},
} }
for _, tc := range testcases { for _, tc := range testcases {
os.Setenv("HISTTIMEFORMAT", tc.env) os.Setenv("HISTTIMEFORMAT", tc.env)
stripped, err := maybeSkipBashHistTimePrefix(tc.cmdLine) stripped, err := maybeSkipBashHistTimePrefix(tc.cmdLine)
if err != nil { shared.Check(t, err)
t.Fatal(err)
}
if stripped != tc.expected { if stripped != tc.expected {
t.Fatalf("skipping the time prefix returned %#v (expected=%#v for %#v)", stripped, tc.expected, tc.cmdLine) t.Fatalf("skipping the time prefix returned %#v (expected=%#v for %#v)", stripped, tc.expected, tc.cmdLine)
} }