mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-25 22:42:04 +02:00
Add more timestamp stripping tests
This commit is contained in:
parent
2bdfe3d64f
commit
070f8cf3d3
@ -35,7 +35,6 @@ func TestSetup(t *testing.T) {
|
||||
|
||||
func TestBuildHistoryEntry(t *testing.T) {
|
||||
defer shared.BackupAndRestore(t)()
|
||||
defer shared.RunTestServer()()
|
||||
shared.Check(t, Setup([]string{}))
|
||||
|
||||
// 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) {
|
||||
defer shared.BackupAndRestore(t)()
|
||||
shared.Check(t, hctx.InitConfig())
|
||||
@ -165,16 +193,12 @@ func TestAddToDbIfNew(t *testing.T) {
|
||||
|
||||
func TestParseCrossPlatformInt(t *testing.T) {
|
||||
res, err := parseCrossPlatformInt("123")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse int: %v", err)
|
||||
}
|
||||
shared.Check(t, err)
|
||||
if res != 123 {
|
||||
t.Fatalf("failed to parse cross platform int %d", res)
|
||||
}
|
||||
res, err = parseCrossPlatformInt("123N")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse int: %v", err)
|
||||
}
|
||||
shared.Check(t, err)
|
||||
if res != 123 {
|
||||
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) {
|
||||
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"},
|
||||
{"[%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 {
|
||||
os.Setenv("HISTTIMEFORMAT", tc.env)
|
||||
stripped, err := maybeSkipBashHistTimePrefix(tc.cmdLine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
shared.Check(t, err)
|
||||
if stripped != tc.expected {
|
||||
t.Fatalf("skipping the time prefix returned %#v (expected=%#v for %#v)", stripped, tc.expected, tc.cmdLine)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user