2022-04-05 07:07:01 +02:00
|
|
|
package lib
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-04-09 21:19:01 +02:00
|
|
|
"os/user"
|
2022-01-09 06:59:28 +01:00
|
|
|
"path"
|
2022-10-11 07:04:59 +02:00
|
|
|
"reflect"
|
2022-01-09 06:59:28 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-01-10 01:39:13 +01:00
|
|
|
"time"
|
2022-04-05 07:07:01 +02:00
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-09-21 07:28:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/hctx"
|
2022-11-26 19:31:43 +01:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-10-28 06:53:47 +02:00
|
|
|
"github.com/ddworken/hishtory/shared/testutils"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
defer testutils.RunTestServer()()
|
2022-09-21 08:30:57 +02:00
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
homedir, err := os.UserHomeDir()
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-12-17 07:22:57 +01:00
|
|
|
if _, err := os.Stat(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH)); err == nil {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("hishtory secret file already exists!")
|
|
|
|
}
|
2022-11-15 04:26:56 +01:00
|
|
|
testutils.Check(t, Setup("", false))
|
2022-12-17 07:22:57 +01:00
|
|
|
if _, err := os.Stat(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH)); err != nil {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("hishtory secret file does not exist after Setup()!")
|
|
|
|
}
|
2022-12-17 07:22:57 +01:00
|
|
|
data, err := os.ReadFile(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH))
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
if len(data) < 10 {
|
|
|
|
t.Fatalf("hishtory secret has unexpected length: %d", len(data))
|
|
|
|
}
|
2022-11-03 21:16:45 +01:00
|
|
|
config := hctx.GetConf(hctx.MakeContext())
|
|
|
|
if config.IsOffline != false {
|
|
|
|
t.Fatalf("hishtory config should have been offline")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupOffline(t *testing.T) {
|
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
defer testutils.RunTestServer()()
|
|
|
|
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
testutils.Check(t, err)
|
2022-12-17 07:22:57 +01:00
|
|
|
if _, err := os.Stat(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH)); err == nil {
|
2022-11-03 21:16:45 +01:00
|
|
|
t.Fatalf("hishtory secret file already exists!")
|
|
|
|
}
|
2022-11-15 04:26:56 +01:00
|
|
|
testutils.Check(t, Setup("", true))
|
2022-12-17 07:22:57 +01:00
|
|
|
if _, err := os.Stat(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH)); err != nil {
|
2022-11-03 21:16:45 +01:00
|
|
|
t.Fatalf("hishtory secret file does not exist after Setup()!")
|
|
|
|
}
|
2022-12-17 07:22:57 +01:00
|
|
|
data, err := os.ReadFile(path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH))
|
2022-11-03 21:16:45 +01:00
|
|
|
testutils.Check(t, err)
|
|
|
|
if len(data) < 10 {
|
|
|
|
t.Fatalf("hishtory secret has unexpected length: %d", len(data))
|
|
|
|
}
|
|
|
|
config := hctx.GetConf(hctx.MakeContext())
|
|
|
|
if config.IsOffline != true {
|
|
|
|
t.Fatalf("hishtory config should have been offline, actual=%#v", string(data))
|
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildHistoryEntry(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
defer testutils.RunTestServer()()
|
2022-11-15 04:26:56 +01:00
|
|
|
testutils.Check(t, Setup("", false))
|
2022-04-15 09:04:49 +02:00
|
|
|
|
2022-04-18 04:54:17 +02:00
|
|
|
// Test building an actual entry for bash
|
2022-09-21 07:28:40 +02:00
|
|
|
entry, err := BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "bash", "120", " 123 ls /foo ", "1641774958"})
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
if entry.ExitCode != 120 {
|
|
|
|
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
|
|
|
|
}
|
2022-04-09 21:19:01 +02:00
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve user: %v", err)
|
|
|
|
}
|
|
|
|
if entry.LocalUsername != user.Username {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("history entry has unexpected user name: %v", entry.LocalUsername)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
|
|
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
|
|
|
}
|
2022-09-08 08:20:31 +02:00
|
|
|
if !strings.HasPrefix(entry.HomeDirectory, "/") {
|
|
|
|
t.Fatalf("history entry has unexpected home directory: %v", entry.HomeDirectory)
|
|
|
|
}
|
2022-04-18 04:54:17 +02:00
|
|
|
if entry.Command != "ls /foo" {
|
|
|
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-09T") && !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-10T") {
|
|
|
|
t.Fatalf("history entry has incorrect date in the start time: %v", entry.StartTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if entry.StartTime.Unix() != 1641774958 {
|
|
|
|
t.Fatalf("history entry has incorrect Unix time in the start time: %v", entry.StartTime.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test building an entry for zsh
|
2022-09-21 07:28:40 +02:00
|
|
|
entry, err = BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "zsh", "120", "ls /foo\n", "1641774958"})
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-04-18 04:54:17 +02:00
|
|
|
if entry.ExitCode != 120 {
|
|
|
|
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
|
|
|
|
}
|
|
|
|
if entry.LocalUsername != user.Username {
|
|
|
|
t.Fatalf("history entry has unexpected user name: %v", entry.LocalUsername)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
|
|
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
|
|
|
}
|
2022-09-08 08:20:31 +02:00
|
|
|
if !strings.HasPrefix(entry.HomeDirectory, "/") {
|
|
|
|
t.Fatalf("history entry has unexpected home directory: %v", entry.HomeDirectory)
|
|
|
|
}
|
2022-04-18 04:54:17 +02:00
|
|
|
if entry.Command != "ls /foo" {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
|
|
|
}
|
2022-04-09 21:57:58 +02:00
|
|
|
if !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-09T") && !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-10T") {
|
2022-04-09 21:50:01 +02:00
|
|
|
t.Fatalf("history entry has incorrect date in the start time: %v", entry.StartTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if entry.StartTime.Unix() != 1641774958 {
|
|
|
|
t.Fatalf("history entry has incorrect Unix time in the start time: %v", entry.StartTime.Unix())
|
2022-01-10 01:39:13 +01:00
|
|
|
}
|
2022-10-24 00:40:30 +02:00
|
|
|
|
|
|
|
// Test building an entry for fish
|
|
|
|
entry, err = BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "fish", "120", "ls /foo\n", "1641774958"})
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-10-24 00:40:30 +02:00
|
|
|
if entry.ExitCode != 120 {
|
|
|
|
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
|
|
|
|
}
|
|
|
|
if entry.LocalUsername != user.Username {
|
|
|
|
t.Fatalf("history entry has unexpected user name: %v", entry.LocalUsername)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
|
|
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.HomeDirectory, "/") {
|
|
|
|
t.Fatalf("history entry has unexpected home directory: %v", entry.HomeDirectory)
|
|
|
|
}
|
|
|
|
if entry.Command != "ls /foo" {
|
|
|
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-09T") && !strings.HasPrefix(entry.StartTime.Format(time.RFC3339), "2022-01-10T") {
|
|
|
|
t.Fatalf("history entry has incorrect date in the start time: %v", entry.StartTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if entry.StartTime.Unix() != 1641774958 {
|
|
|
|
t.Fatalf("history entry has incorrect Unix time in the start time: %v", entry.StartTime.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test building an entry that is empty, and thus not saved
|
|
|
|
entry, err = BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "zsh", "120", " \n", "1641774958"})
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-10-24 00:40:30 +02:00
|
|
|
if entry != nil {
|
|
|
|
t.Fatalf("expected history entry to be nil")
|
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
2022-10-24 00:40:30 +02:00
|
|
|
func TestBuildHistoryEntryWithTimestampStripping(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestoreEnv("HISTTIMEFORMAT")()
|
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
defer testutils.RunTestServer()()
|
2022-11-15 04:26:56 +01:00
|
|
|
testutils.Check(t, Setup("", false))
|
2022-09-28 07:07:54 +02:00
|
|
|
|
|
|
|
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 = ""
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, hctx.SetConfig(conf))
|
2022-09-28 07:07:54 +02:00
|
|
|
|
|
|
|
os.Setenv("HISTTIMEFORMAT", tc.histtimeformat)
|
|
|
|
entry, err := BuildHistoryEntry(hctx.MakeContext(), []string{"unused", "saveHistoryEntry", "bash", "120", tc.input, "1641774958"})
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-09-28 07:07:54 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
func TestPersist(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
testutils.Check(t, hctx.InitConfig())
|
2022-09-21 07:28:40 +02:00
|
|
|
db := hctx.GetDb(hctx.MakeContext())
|
2022-04-07 07:43:07 +02:00
|
|
|
|
2022-10-28 06:53:47 +02:00
|
|
|
entry := testutils.MakeFakeHistoryEntry("ls ~/")
|
2022-11-16 08:20:19 +01:00
|
|
|
testutils.Check(t, db.Create(entry).Error)
|
2022-04-08 05:59:40 +02:00
|
|
|
var historyEntries []*data.HistoryEntry
|
2022-04-07 07:43:07 +02:00
|
|
|
result := db.Find(&historyEntries)
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, result.Error)
|
2022-04-07 07:43:07 +02:00
|
|
|
if len(historyEntries) != 1 {
|
|
|
|
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
|
|
|
|
}
|
|
|
|
dbEntry := historyEntries[0]
|
2022-04-08 05:59:40 +02:00
|
|
|
if !data.EntryEquals(entry, *dbEntry) {
|
2022-04-07 07:43:07 +02:00
|
|
|
t.Fatalf("DB data is different than input! \ndb =%#v \ninput=%#v", *dbEntry, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearch(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
testutils.Check(t, hctx.InitConfig())
|
2022-11-01 20:15:35 +01:00
|
|
|
ctx := hctx.MakeContext()
|
|
|
|
db := hctx.GetDb(ctx)
|
2022-04-07 07:43:07 +02:00
|
|
|
|
|
|
|
// Insert data
|
2022-10-28 06:53:47 +02:00
|
|
|
entry1 := testutils.MakeFakeHistoryEntry("ls /foo")
|
2022-11-16 08:20:19 +01:00
|
|
|
testutils.Check(t, db.Create(entry1).Error)
|
2022-10-28 06:53:47 +02:00
|
|
|
entry2 := testutils.MakeFakeHistoryEntry("ls /bar")
|
2022-11-16 08:20:19 +01:00
|
|
|
testutils.Check(t, db.Create(entry2).Error)
|
2022-04-07 07:43:07 +02:00
|
|
|
|
|
|
|
// Search for data
|
2022-11-01 20:15:35 +01:00
|
|
|
results, err := Search(ctx, db, "ls", 5)
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-04-07 07:43:07 +02:00
|
|
|
if len(results) != 2 {
|
2022-11-13 15:35:03 +01:00
|
|
|
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
if !data.EntryEquals(*results[0], entry2) {
|
2022-04-07 07:43:07 +02:00
|
|
|
t.Fatalf("Search()[0]=%#v, expected: %#v", results[0], entry2)
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
if !data.EntryEquals(*results[1], entry1) {
|
2022-04-07 07:43:07 +02:00
|
|
|
t.Fatalf("Search()[0]=%#v, expected: %#v", results[1], entry1)
|
|
|
|
}
|
2022-04-07 07:44:10 +02:00
|
|
|
}
|
2022-04-09 22:09:46 +02:00
|
|
|
|
|
|
|
func TestAddToDbIfNew(t *testing.T) {
|
|
|
|
// Set up
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestore(t)()
|
|
|
|
testutils.Check(t, hctx.InitConfig())
|
2022-09-21 07:28:40 +02:00
|
|
|
db := hctx.GetDb(hctx.MakeContext())
|
2022-04-09 22:09:46 +02:00
|
|
|
|
|
|
|
// Add duplicate entries
|
2022-10-28 06:53:47 +02:00
|
|
|
entry1 := testutils.MakeFakeHistoryEntry("ls /foo")
|
2022-04-09 22:09:46 +02:00
|
|
|
AddToDbIfNew(db, entry1)
|
|
|
|
AddToDbIfNew(db, entry1)
|
2022-10-28 06:53:47 +02:00
|
|
|
entry2 := testutils.MakeFakeHistoryEntry("ls /foo")
|
2022-04-09 22:09:46 +02:00
|
|
|
AddToDbIfNew(db, entry2)
|
|
|
|
AddToDbIfNew(db, entry2)
|
|
|
|
AddToDbIfNew(db, entry1)
|
|
|
|
|
|
|
|
// Check there should only be two entries
|
|
|
|
var entries []data.HistoryEntry
|
|
|
|
result := db.Find(&entries)
|
|
|
|
if result.Error != nil {
|
|
|
|
t.Fatal(result.Error)
|
|
|
|
}
|
|
|
|
if len(entries) != 2 {
|
2022-11-13 15:35:03 +01:00
|
|
|
t.Fatalf("entries has an incorrect length: %d, entries=%#v", len(entries), entries)
|
2022-04-09 22:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-17 20:09:30 +02:00
|
|
|
|
|
|
|
func TestParseCrossPlatformInt(t *testing.T) {
|
|
|
|
res, err := parseCrossPlatformInt("123")
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-04-17 20:09:30 +02:00
|
|
|
if res != 123 {
|
|
|
|
t.Fatalf("failed to parse cross platform int %d", res)
|
|
|
|
}
|
|
|
|
res, err = parseCrossPlatformInt("123N")
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-04-17 20:09:30 +02:00
|
|
|
if res != 123 {
|
|
|
|
t.Fatalf("failed to parse cross platform int %d", res)
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 06:28:19 +02:00
|
|
|
|
2022-09-02 08:22:53 +02:00
|
|
|
func TestBuildRegexFromTimeFormat(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
formatString, regex string
|
|
|
|
}{
|
|
|
|
{"%Y ", "[0-9]{4} "},
|
|
|
|
{"%F %T ", "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} "},
|
|
|
|
{"%F%T", "[0-9]{4}-[0-9]{2}-[0-9]{2}[0-9]{2}:[0-9]{2}:[0-9]{2}"},
|
|
|
|
{"%%", "%"},
|
|
|
|
{"%%%%", "%%"},
|
|
|
|
{"%%%Y", "%[0-9]{4}"},
|
|
|
|
{"%%%F%T", "%[0-9]{4}-[0-9]{2}-[0-9]{2}[0-9]{2}:[0-9]{2}:[0-9]{2}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
if regex := buildRegexFromTimeFormat(tc.formatString); regex != tc.regex {
|
|
|
|
t.Fatalf("building a regex for %#v returned %#v (expected=%#v)", tc.formatString, regex, tc.regex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:07:54 +02:00
|
|
|
func TestGetLastCommand(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
input, expectedOutput string
|
|
|
|
}{
|
2022-10-24 00:40:30 +02:00
|
|
|
{" 0 ls", "ls"},
|
2022-09-28 07:07:54 +02:00
|
|
|
{" 33 ls", "ls"},
|
2022-10-24 00:40:30 +02:00
|
|
|
{" 33 ls --aaaa foo bar ", "ls --aaaa foo bar"},
|
2022-09-28 07:07:54 +02:00
|
|
|
{" 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)
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-09-28 07:07:54 +02:00
|
|
|
if actualOutput != tc.expectedOutput {
|
|
|
|
t.Fatalf("getLastCommand(%#v) returned %#v (expected=%#v)", tc.input, actualOutput, tc.expectedOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 06:28:19 +02:00
|
|
|
func TestMaybeSkipBashHistTimePrefix(t *testing.T) {
|
2022-10-28 06:53:47 +02:00
|
|
|
defer testutils.BackupAndRestoreEnv("HISTTIMEFORMAT")()
|
2022-06-13 06:28:19 +02:00
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
env, cmdLine, expected string
|
|
|
|
}{
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 sudo apt update", "sudo apt update"},
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 ls a b", "ls a b"},
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 ls a ", "ls a "},
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 ls a", "ls a"},
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 ls", "ls"},
|
|
|
|
{"%F %T ", "2019-07-12 13:02:31 ls -Slah", "ls -Slah"},
|
|
|
|
{"%F ", "2019-07-12 ls -Slah", "ls -Slah"},
|
|
|
|
{"%F ", "2019-07-12 ls -Slah", "ls -Slah"},
|
2022-09-02 08:22:53 +02:00
|
|
|
{"%Y", "2019ls -Slah", "ls -Slah"},
|
|
|
|
{"%Y%Y", "20192020ls -Slah", "ls -Slah"},
|
|
|
|
{"%Y%Y", "20192020ls -Slah20192020", "ls -Slah20192020"},
|
2022-06-13 06:28:19 +02:00
|
|
|
{"", "ls -Slah", "ls -Slah"},
|
|
|
|
{"[%F %T] ", "[2019-07-12 13:02:31] sudo apt update", "sudo apt update"},
|
|
|
|
{"[%F a %T] ", "[2019-07-12 a 13:02:31] sudo apt update", "sudo apt update"},
|
|
|
|
{"aaa ", "aaa sudo apt update", "sudo apt update"},
|
|
|
|
{"%c ", "Sun Aug 19 02:56:02 2012 sudo apt update", "sudo apt update"},
|
|
|
|
{"%c ", "Sun Aug 19 02:56:02 2012 ls", "ls"},
|
|
|
|
{"[%c] ", "[Sun Aug 19 02:56:02 2012] ls", "ls"},
|
2022-09-02 08:22:53 +02:00
|
|
|
{"[%c %t] ", "[Sun Aug 19 02:56:02 2012 ] ls", "ls"},
|
|
|
|
{"[%c %t]", "[Sun Aug 19 02:56:02 2012 ]ls", "ls"},
|
|
|
|
{"[%c %t]", "[Sun Aug 19 02:56:02 2012 ]foo", "foo"},
|
|
|
|
{"[%c %t", "[Sun Aug 19 02:56:02 2012 foo", "foo"},
|
2022-09-28 06:25:33 +02:00
|
|
|
{"[%F %T %z]", "[2022-09-28 04:17:06 +0000]foo", "foo"},
|
2022-09-28 07:07:54 +02:00
|
|
|
{"[%F %T %z] ", "[2022-09-28 04:17:06 +0000] foo", "foo"},
|
2022-06-13 06:28:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
os.Setenv("HISTTIMEFORMAT", tc.env)
|
2022-09-08 08:20:31 +02:00
|
|
|
stripped, err := maybeSkipBashHistTimePrefix(tc.cmdLine)
|
2022-10-28 06:53:47 +02:00
|
|
|
testutils.Check(t, err)
|
2022-09-08 08:20:31 +02:00
|
|
|
if stripped != tc.expected {
|
2022-06-13 06:28:19 +02:00
|
|
|
t.Fatalf("skipping the time prefix returned %#v (expected=%#v for %#v)", stripped, tc.expected, tc.cmdLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 07:04:59 +02:00
|
|
|
|
|
|
|
func TestChunks(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
input []int
|
|
|
|
chunkSize int
|
|
|
|
output [][]int
|
|
|
|
}{
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 2, [][]int{{1, 2}, {3, 4}, {5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 3, [][]int{{1, 2, 3}, {4, 5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 1, [][]int{{1}, {2}, {3}, {4}, {5}}},
|
|
|
|
{[]int{1, 2, 3, 4, 5}, 4, [][]int{{1, 2, 3, 4}, {5}}},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2022-11-26 19:31:43 +01:00
|
|
|
actual := shared.Chunks(tc.input, tc.chunkSize)
|
2022-10-11 07:04:59 +02:00
|
|
|
if !reflect.DeepEqual(actual, tc.output) {
|
|
|
|
t.Fatal("chunks failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 05:35:32 +02:00
|
|
|
func TestZshWeirdness(t *testing.T) {
|
|
|
|
testcases := []struct {
|
2022-11-13 01:30:59 +01:00
|
|
|
input string
|
|
|
|
output string
|
2022-10-18 05:35:32 +02:00
|
|
|
}{
|
2022-11-13 01:30:59 +01:00
|
|
|
{": 1666062975:0;bash", "bash"},
|
|
|
|
{": 16660:0;ls", "ls"},
|
|
|
|
{"ls", "ls"},
|
|
|
|
{"0", "0"},
|
|
|
|
{"hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv", "hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv"},
|
2022-10-18 05:35:32 +02:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2022-11-13 01:30:59 +01:00
|
|
|
actual := stripZshWeirdness(tc.input)
|
|
|
|
if !reflect.DeepEqual(actual, tc.output) {
|
2022-10-18 05:35:32 +02:00
|
|
|
t.Fatalf("weirdness failure for %#v", tc.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 19:03:49 +01:00
|
|
|
|
|
|
|
func TestParseTimeGenerously(t *testing.T) {
|
|
|
|
ts, err := parseTimeGenerously("2006-01-02T15:04:00-08:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02 T15:04:00 -08:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00_-08:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Unix() != 1136243040 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_T15:04:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04:00")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02T15:04")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02_15:04")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 15 || ts.Minute() != 4 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
ts, err = parseTimeGenerously("2006-01-02")
|
|
|
|
testutils.Check(t, err)
|
|
|
|
if ts.Year() != 2006 || ts.Month() != time.January || ts.Day() != 2 || ts.Hour() != 0 || ts.Minute() != 0 || ts.Second() != 0 {
|
|
|
|
t.Fatalf("parsed time incorrectly: %d", ts.Unix())
|
|
|
|
}
|
|
|
|
}
|