Refactor offline simulation tests so that they reliably set the env variable before ever starting the shell, since we need the env variable to also apply to presaving

This commit is contained in:
David Dworken 2023-10-22 11:28:41 -07:00
parent 53580d40e9
commit 9433bd6e94
No known key found for this signature in database
2 changed files with 16 additions and 6 deletions

View File

@ -1222,7 +1222,10 @@ func testReuploadHistoryEntries(t *testing.T, tester shellTester) {
tester.RunInteractiveShell(t, `echo 1`)
// Device 2: Record a command with a simulated network error
tester.RunInteractiveShell(t, `echo 2; export HISHTORY_SIMULATE_NETWORK_ERROR=1; echo 3`)
tester.RunInteractiveShell(t, `echo 2`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1")
tester.RunInteractiveShell(t, `echo 3`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "")
// Device 1: Run an export and confirm that the network only contains the first command
restoreSecondProfile := testutils.BackupAndRestoreWithId(t, "-install2")
@ -1236,11 +1239,11 @@ func testReuploadHistoryEntries(t *testing.T, tester shellTester) {
// Device 2: Run another command but with the network re-enabled
restoreFirstProfile = testutils.BackupAndRestoreWithId(t, "-install1")
restoreSecondProfile()
tester.RunInteractiveShell(t, `unset HISHTORY_SIMULATE_NETWORK_ERROR; echo 4`)
tester.RunInteractiveShell(t, `echo 4`)
// Device 2: Run export which contains all results (as it did all along since it is stored offline)
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail")
expectedOutput = "echo 1\necho 2; export HISHTORY_SIMULATE_NETWORK_ERROR=1; echo 3\nunset HISHTORY_SIMULATE_NETWORK_ERROR; echo 4\n"
expectedOutput = "echo 1\necho 2; echo 3\necho 4\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
@ -1248,7 +1251,7 @@ func testReuploadHistoryEntries(t *testing.T, tester shellTester) {
// Device 1: Now it too sees all the results
restoreFirstProfile()
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail")
expectedOutput = "echo 1\necho 2; export HISHTORY_SIMULATE_NETWORK_ERROR=1; echo 3\nunset HISHTORY_SIMULATE_NETWORK_ERROR; echo 4\n"
expectedOutput = "echo 1\necho 2; echo 3\necho 4\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
@ -1274,7 +1277,9 @@ func testHishtoryOffline(t *testing.T, tester shellTester) {
tester.RunInteractiveShell(t, `echo dev1-a`)
// Device 1: Query while offline
out := tester.RunInteractiveShell(t, `export HISHTORY_SIMULATE_NETWORK_ERROR=1; hishtory export | grep -v pipefail`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1")
out := tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "")
expectedOutput := "echo dev2\necho dev1-a\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
@ -1287,7 +1292,9 @@ func testHishtoryOffline(t *testing.T, tester shellTester) {
// Device 1: Query while offline before ever retrieving the command
restoreFirstProfile()
out = tester.RunInteractiveShell(t, `export HISHTORY_SIMULATE_NETWORK_ERROR=1; hishtory export | grep -v pipefail`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1")
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "")
expectedOutput = "echo dev2\necho dev1-a\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)

View File

@ -105,6 +105,8 @@ func BackupAndRestoreWithId(t testing.TB, id string) func() {
touchFile(path.Join(homedir, ".bash_history"))
touchFile(path.Join(homedir, ".zsh_history"))
touchFile(path.Join(homedir, ".local/share/fish/fish_history"))
restoreHishtoryOffline := BackupAndRestoreEnv("HISHTORY_SIMULATE_NETWORK_ERROR")
os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "")
return func() {
cmd := exec.Command("killall", "hishtory", "tmux")
stdout, err := cmd.Output()
@ -121,6 +123,7 @@ func BackupAndRestoreWithId(t testing.TB, id string) func() {
checkError(copy(getBackPath(file, id), file))
}
checkError(os.Chdir(initialWd))
restoreHishtoryOffline()
}
}