mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-22 08:14:02 +01:00
Improve history importing to not double import HISTFILE + improve tests
This commit is contained in:
parent
437d4a0b88
commit
e520b23858
4
Makefile
4
Makefile
@ -1,9 +1,9 @@
|
||||
forcetest:
|
||||
go clean -testcache
|
||||
HISHTORY_TEST=1 go test -p 1 -timeout 30m ./...
|
||||
go test -p 1 -timeout 30m ./...
|
||||
|
||||
test:
|
||||
HISHTORY_TEST=1 go test -p 1 -timeout 30m ./...
|
||||
go test -p 1 -timeout 30m ./...
|
||||
|
||||
acttest:
|
||||
act push -j test -e .github/push_event.json --reuse --container-architecture linux/amd64
|
||||
|
@ -32,6 +32,10 @@ func skipSlowTests() bool {
|
||||
var initialWd string
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")
|
||||
os.Setenv("HISHTORY_TEST", "1")
|
||||
defer testutils.BackupAndRestoreEnv("HISHTORY_SKIP_INIT_IMPORT")
|
||||
os.Setenv("HISHTORY_SKIP_INIT_IMPORT", "1")
|
||||
defer testutils.RunTestServer()()
|
||||
cmd := exec.Command("go", "build", "-o", "/tmp/client")
|
||||
cmd.Env = os.Environ()
|
||||
@ -704,7 +708,7 @@ func testUpdate(t *testing.T, tester shellTester) {
|
||||
}
|
||||
|
||||
// Check that the history was preserved after the update
|
||||
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail | grep -v '/tmp/client install'")
|
||||
out = tester.RunInteractiveShell(t, "hishtory export -pipefail | grep -v '/tmp/client install'")
|
||||
expectedOutput := "echo hello\nhishtory status\nhishtory update\nhishtory update\nhishtory status\n"
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
@ -1471,10 +1475,12 @@ func testHishtoryOffline(t *testing.T, tester shellTester) {
|
||||
func testInitialHistoryImport(t *testing.T, tester shellTester) {
|
||||
// Setup
|
||||
defer testutils.BackupAndRestore(t)()
|
||||
defer testutils.BackupAndRestoreEnv("HISHTORY_SKIP_INIT_IMPORT")
|
||||
os.Setenv("HISHTORY_SKIP_INIT_IMPORT", "")
|
||||
|
||||
// Record some commands before installing hishtory
|
||||
captureTerminalOutputWithShellName(t, tester, "fish", []string{"echo SPACE fishcommand ENTER"})
|
||||
randomCmdUuid := uuid.Must(uuid.NewRandom()).String()
|
||||
captureTerminalOutputWithShellName(t, tester, "fish", []string{fmt.Sprintf("echo SPACE %s-fishcommand ENTER", randomCmdUuid)})
|
||||
randomCmd := fmt.Sprintf(`echo %v-foo
|
||||
echo %v-bar`, randomCmdUuid, randomCmdUuid)
|
||||
tester.RunInteractiveShell(t, randomCmd)
|
||||
@ -1482,40 +1488,20 @@ echo %v-bar`, randomCmdUuid, randomCmdUuid)
|
||||
// Install hishtory
|
||||
installHishtory(t, tester, "")
|
||||
|
||||
// Check that hishtory export doesn't have the commands yet
|
||||
out := tester.RunInteractiveShell(t, `hishtory export `+randomCmdUuid)
|
||||
expectedOutput := ""
|
||||
// Check that hishtory export has the commands
|
||||
out := tester.RunInteractiveShell(t, `hishtory export `+randomCmdUuid[:5])
|
||||
expectedOutput := strings.ReplaceAll(`echo UUID-foo
|
||||
echo UUID-bar
|
||||
echo UUID-fishcommand
|
||||
`, "UUID", randomCmdUuid)
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
}
|
||||
|
||||
// Trigger an import
|
||||
out = tester.RunInteractiveShell(t, "echo stdincommand | hishtory import")
|
||||
r := regexp.MustCompile(`Imported (.+) history entries from your existing shell history`)
|
||||
matches := r.FindStringSubmatch(out)
|
||||
if len(matches) != 2 {
|
||||
t.Fatalf("Failed to extract history entries count from output: matches=%#v, out=%#v", matches, out)
|
||||
}
|
||||
num, err := strconv.Atoi(matches[1])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if num <= 2 {
|
||||
t.Fatalf("hishtory didn't import enough entries, only found %v entries", num)
|
||||
}
|
||||
|
||||
// Check that the previously recorded commands are in hishtory
|
||||
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
|
||||
expectedOutput = fmt.Sprintf("hishtory export %s\necho %s-foo\necho %s-bar\n/tmp/client install \nhishtory export %s\necho fishcommand\nstdincommand\necho stdincommand | hishtory import\n", randomCmdUuid, randomCmdUuid, randomCmdUuid, randomCmdUuid)
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
}
|
||||
|
||||
// And check a different way
|
||||
out = tester.RunInteractiveShell(t, `hishtory export `+randomCmdUuid)
|
||||
expectedOutput = fmt.Sprintf("hishtory export %s\necho %s-foo\necho %s-bar\nhishtory export %s\n", randomCmdUuid, randomCmdUuid, randomCmdUuid, randomCmdUuid)
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
// Compare the rest of the hishtory export
|
||||
out = tester.RunInteractiveShell(t, `hishtory export -pipefail -`+randomCmdUuid[:5])
|
||||
if out != "" {
|
||||
t.Fatalf("expected hishtory export to be empty, was=%v", out)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1534,7 +1520,7 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
|
||||
tester.RunInteractiveShell(t, randomCmd)
|
||||
|
||||
// Check that the previously recorded commands are in hishtory
|
||||
out := tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
|
||||
out := tester.RunInteractiveShell(t, `hishtory export -pipefail`)
|
||||
expectedOutput := fmt.Sprintf("echo %s-foo\necho %s-bas\necho foo\nls /tmp\n", randomCmdUuid, randomCmdUuid)
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
@ -1616,7 +1602,7 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
|
||||
installHishtory(t, tester, userSecret)
|
||||
|
||||
// And confirm that it has the commands too
|
||||
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
|
||||
out = tester.RunInteractiveShell(t, `hishtory export -pipefail`)
|
||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||
}
|
||||
@ -1781,7 +1767,7 @@ func compareGoldens(t *testing.T, out, goldenName string) {
|
||||
expected, err := os.ReadFile(goldenPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
expected = []byte{}
|
||||
expected = []byte("ERR_FILE_NOT_FOUND")
|
||||
} else {
|
||||
testutils.Check(t, err)
|
||||
}
|
||||
|
@ -528,15 +528,18 @@ func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) {
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to parse bash history: %v", err)
|
||||
}
|
||||
hctx.GetLogger().Printf("DDWORKENDEBUG: bashEntries=%#v", historyEntries)
|
||||
extraEntries, err := parseZshHistory(homedir)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
|
||||
}
|
||||
hctx.GetLogger().Printf("DDWORKENDEBUG: zshEntries=%#v", extraEntries)
|
||||
historyEntries = append(historyEntries, extraEntries...)
|
||||
extraEntries, err = parseFishHistory(homedir)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to parse fish history: %v", err)
|
||||
}
|
||||
hctx.GetLogger().Printf("DDWORKENDEBUG: fishEntries=%#v", extraEntries)
|
||||
historyEntries = append(historyEntries, extraEntries...)
|
||||
if shouldReadStdin {
|
||||
extraEntries, err = readStdin()
|
||||
@ -625,7 +628,7 @@ func parseFishHistory(homedir string) ([]string, error) {
|
||||
|
||||
func parseBashHistory(homedir string) ([]string, error) {
|
||||
histfile := os.Getenv("HISTFILE")
|
||||
if histfile == "" {
|
||||
if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "bash") {
|
||||
histfile = filepath.Join(homedir, ".bash_history")
|
||||
}
|
||||
return readFileToArray(histfile)
|
||||
@ -658,7 +661,7 @@ func readFileToArray(path string) ([]string, error) {
|
||||
|
||||
func parseZshHistory(homedir string) ([]string, error) {
|
||||
histfile := os.Getenv("HISTFILE")
|
||||
if histfile == "" {
|
||||
if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "zsh") {
|
||||
histfile = filepath.Join(homedir, ".zsh_history")
|
||||
}
|
||||
return readFileToArray(histfile)
|
||||
|
@ -69,7 +69,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
lib.CheckFatalError(lib.Setup(os.Args))
|
||||
if os.Getenv("HISHTORY_TEST") == "" {
|
||||
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") != "" {
|
||||
fmt.Println("Importing existing shell history...")
|
||||
ctx := hctx.MakeContext()
|
||||
numImported, err := lib.ImportHistory(ctx, false)
|
||||
@ -80,7 +80,7 @@ func main() {
|
||||
}
|
||||
case "install":
|
||||
lib.CheckFatalError(lib.Install())
|
||||
if os.Getenv("HISHTORY_TEST") == "" {
|
||||
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
|
||||
db, err := hctx.OpenLocalSqliteDb()
|
||||
lib.CheckFatalError(err)
|
||||
data, err := lib.Search(nil, db, "", 10)
|
||||
|
Loading…
Reference in New Issue
Block a user