From 00f6bed62c616f36b74fa0f84eaade0a2f6f6663 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 4 Jun 2022 22:27:04 -0700 Subject: [PATCH] Add a test for the install.py script --- client/client_test.go | 35 +++++++++++++++++++++++++++++++++++ client/lib/lib.go | 16 ++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index c57d4f6..adf02c6 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -123,6 +123,7 @@ func TestParameterized(t *testing.T) { t.Run("testTableDisplayCwd/"+tester.ShellName(), func(t *testing.T) { testTableDisplayCwd(t, tester) }) t.Run("testTimestampsAreReasonablyCorrect/"+tester.ShellName(), func(t *testing.T) { testTimestampsAreReasonablyCorrect(t, tester) }) t.Run("testRequestAndReceiveDbDump/"+tester.ShellName(), func(t *testing.T) { testRequestAndReceiveDbDump(t, tester) }) + t.Run("testInstallViaPythonScript/"+tester.ShellName(), func(t *testing.T) { testInstallViaPythonScript(t, tester) }) } } @@ -1011,4 +1012,38 @@ echo other`) } } +func testInstallViaPythonScript(t *testing.T, tester shellTester) { + // Set up + defer shared.BackupAndRestore(t)() + + // Install via the python script + out := tester.RunInteractiveShell(t, `curl https://hishtory.dev/install.py | python3 -`) + if !strings.Contains(out, "Succesfully installed hishtory") { + t.Fatalf("unexpected output when installing hishtory, out=%#v", out) + } + r := regexp.MustCompile(`Setting secret hishtory key to (.*)`) + matches := r.FindStringSubmatch(out) + if len(matches) != 2 { + t.Fatalf("Failed to extract userSecret from output: matches=%#v", matches) + } + userSecret := matches[1] + + // Test the status subcommand + downloadData, err := lib.GetDownloadData() + if err != nil { + t.Fatal(err) + } + out = tester.RunInteractiveShell(t, `hishtory status`) + expectedOut := fmt.Sprintf("Hishtory: %s\nEnabled: true\nSecret Key: %s\nCommit Hash: ", downloadData.Version, userSecret) + if !strings.Contains(out, expectedOut) { + t.Fatalf("status command has unexpected output: actual=%#v, expected=%#v", out, expectedOut) + } + + // And test that it recorded that command + out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`) + if out != "hishtory status\n" { + t.Fatalf("unexpected output from hishtory export=%#v", out) + } +} + // TODO: write a test that runs hishtroy export | grep -v pipefail and then see if that shows up in query/export, I think there is weird behavior here diff --git a/client/lib/lib.go b/client/lib/lib.go index 1a093ac..0a798fc 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -514,16 +514,24 @@ func copyFile(src, dst string) error { return err } -func Update() error { - // Download the binary +func GetDownloadData() (shared.UpdateInfo, error) { respBody, err := ApiGet("/api/v1/download") if err != nil { - return fmt.Errorf("failed to download update info: %v", err) + return shared.UpdateInfo{}, fmt.Errorf("failed to download update info: %v", err) } var downloadData shared.UpdateInfo err = json.Unmarshal(respBody, &downloadData) if err != nil { - return fmt.Errorf("failed to parse update info: %v", err) + return shared.UpdateInfo{}, fmt.Errorf("failed to parse update info: %v", err) + } + return downloadData, nil +} + +func Update() error { + // Download the binary + downloadData, err := GetDownloadData() + if err != nil { + return err } if downloadData.Version == "v0."+Version { fmt.Printf("Latest version (v0.%s) is already installed\n", Version)