Improve history importing to not double import HISTFILE + improve tests

This commit is contained in:
David Dworken 2022-11-11 18:12:23 -05:00
parent 437d4a0b88
commit e520b23858
No known key found for this signature in database
4 changed files with 30 additions and 41 deletions

View File

@ -1,9 +1,9 @@
forcetest: forcetest:
go clean -testcache go clean -testcache
HISHTORY_TEST=1 go test -p 1 -timeout 30m ./... go test -p 1 -timeout 30m ./...
test: test:
HISHTORY_TEST=1 go test -p 1 -timeout 30m ./... go test -p 1 -timeout 30m ./...
acttest: acttest:
act push -j test -e .github/push_event.json --reuse --container-architecture linux/amd64 act push -j test -e .github/push_event.json --reuse --container-architecture linux/amd64

View File

@ -32,6 +32,10 @@ func skipSlowTests() bool {
var initialWd string var initialWd string
func TestMain(m *testing.M) { 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()() defer testutils.RunTestServer()()
cmd := exec.Command("go", "build", "-o", "/tmp/client") cmd := exec.Command("go", "build", "-o", "/tmp/client")
cmd.Env = os.Environ() cmd.Env = os.Environ()
@ -704,7 +708,7 @@ func testUpdate(t *testing.T, tester shellTester) {
} }
// Check that the history was preserved after the update // 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" expectedOutput := "echo hello\nhishtory status\nhishtory update\nhishtory update\nhishtory status\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" { if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) 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) { func testInitialHistoryImport(t *testing.T, tester shellTester) {
// Setup // Setup
defer testutils.BackupAndRestore(t)() defer testutils.BackupAndRestore(t)()
defer testutils.BackupAndRestoreEnv("HISHTORY_SKIP_INIT_IMPORT")
os.Setenv("HISHTORY_SKIP_INIT_IMPORT", "")
// Record some commands before installing hishtory // Record some commands before installing hishtory
captureTerminalOutputWithShellName(t, tester, "fish", []string{"echo SPACE fishcommand ENTER"})
randomCmdUuid := uuid.Must(uuid.NewRandom()).String() 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 randomCmd := fmt.Sprintf(`echo %v-foo
echo %v-bar`, randomCmdUuid, randomCmdUuid) echo %v-bar`, randomCmdUuid, randomCmdUuid)
tester.RunInteractiveShell(t, randomCmd) tester.RunInteractiveShell(t, randomCmd)
@ -1482,40 +1488,20 @@ echo %v-bar`, randomCmdUuid, randomCmdUuid)
// Install hishtory // Install hishtory
installHishtory(t, tester, "") installHishtory(t, tester, "")
// Check that hishtory export doesn't have the commands yet // Check that hishtory export has the commands
out := tester.RunInteractiveShell(t, `hishtory export `+randomCmdUuid) out := tester.RunInteractiveShell(t, `hishtory export `+randomCmdUuid[:5])
expectedOutput := "" expectedOutput := strings.ReplaceAll(`echo UUID-foo
echo UUID-bar
echo UUID-fishcommand
`, "UUID", randomCmdUuid)
if diff := cmp.Diff(expectedOutput, out); diff != "" { if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
} }
// Trigger an import // Compare the rest of the hishtory export
out = tester.RunInteractiveShell(t, "echo stdincommand | hishtory import") out = tester.RunInteractiveShell(t, `hishtory export -pipefail -`+randomCmdUuid[:5])
r := regexp.MustCompile(`Imported (.+) history entries from your existing shell history`) if out != "" {
matches := r.FindStringSubmatch(out) t.Fatalf("expected hishtory export to be empty, was=%v", 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)
} }
} }
@ -1534,7 +1520,7 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
tester.RunInteractiveShell(t, randomCmd) tester.RunInteractiveShell(t, randomCmd)
// Check that the previously recorded commands are in hishtory // 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) expectedOutput := fmt.Sprintf("echo %s-foo\necho %s-bas\necho foo\nls /tmp\n", randomCmdUuid, randomCmdUuid)
if diff := cmp.Diff(expectedOutput, out); diff != "" { if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) 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) installHishtory(t, tester, userSecret)
// And confirm that it has the commands too // 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 != "" { if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) 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) expected, err := os.ReadFile(goldenPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
expected = []byte{} expected = []byte("ERR_FILE_NOT_FOUND")
} else { } else {
testutils.Check(t, err) testutils.Check(t, err)
} }

View File

@ -528,15 +528,18 @@ func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) {
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse bash history: %v", err) return 0, fmt.Errorf("failed to parse bash history: %v", err)
} }
hctx.GetLogger().Printf("DDWORKENDEBUG: bashEntries=%#v", historyEntries)
extraEntries, err := parseZshHistory(homedir) extraEntries, err := parseZshHistory(homedir)
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse zsh history: %v", err) return 0, fmt.Errorf("failed to parse zsh history: %v", err)
} }
hctx.GetLogger().Printf("DDWORKENDEBUG: zshEntries=%#v", extraEntries)
historyEntries = append(historyEntries, extraEntries...) historyEntries = append(historyEntries, extraEntries...)
extraEntries, err = parseFishHistory(homedir) extraEntries, err = parseFishHistory(homedir)
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse fish history: %v", err) return 0, fmt.Errorf("failed to parse fish history: %v", err)
} }
hctx.GetLogger().Printf("DDWORKENDEBUG: fishEntries=%#v", extraEntries)
historyEntries = append(historyEntries, extraEntries...) historyEntries = append(historyEntries, extraEntries...)
if shouldReadStdin { if shouldReadStdin {
extraEntries, err = readStdin() extraEntries, err = readStdin()
@ -625,7 +628,7 @@ func parseFishHistory(homedir string) ([]string, error) {
func parseBashHistory(homedir string) ([]string, error) { func parseBashHistory(homedir string) ([]string, error) {
histfile := os.Getenv("HISTFILE") histfile := os.Getenv("HISTFILE")
if histfile == "" { if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "bash") {
histfile = filepath.Join(homedir, ".bash_history") histfile = filepath.Join(homedir, ".bash_history")
} }
return readFileToArray(histfile) return readFileToArray(histfile)
@ -658,7 +661,7 @@ func readFileToArray(path string) ([]string, error) {
func parseZshHistory(homedir string) ([]string, error) { func parseZshHistory(homedir string) ([]string, error) {
histfile := os.Getenv("HISTFILE") histfile := os.Getenv("HISTFILE")
if histfile == "" { if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "zsh") {
histfile = filepath.Join(homedir, ".zsh_history") histfile = filepath.Join(homedir, ".zsh_history")
} }
return readFileToArray(histfile) return readFileToArray(histfile)

View File

@ -69,7 +69,7 @@ func main() {
} }
} }
lib.CheckFatalError(lib.Setup(os.Args)) lib.CheckFatalError(lib.Setup(os.Args))
if os.Getenv("HISHTORY_TEST") == "" { if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") != "" {
fmt.Println("Importing existing shell history...") fmt.Println("Importing existing shell history...")
ctx := hctx.MakeContext() ctx := hctx.MakeContext()
numImported, err := lib.ImportHistory(ctx, false) numImported, err := lib.ImportHistory(ctx, false)
@ -80,7 +80,7 @@ func main() {
} }
case "install": case "install":
lib.CheckFatalError(lib.Install()) lib.CheckFatalError(lib.Install())
if os.Getenv("HISHTORY_TEST") == "" { if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
db, err := hctx.OpenLocalSqliteDb() db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err) lib.CheckFatalError(err)
data, err := lib.Search(nil, db, "", 10) data, err := lib.Search(nil, db, "", 10)