Skip to content

Commit

Permalink
Improve install.py script to attempt to detect when /tmp/ is noexec (#…
Browse files Browse the repository at this point in the history
…172)

* Improve install.py script to attempt to detect when /tmp/ is noexec

* Add test to install from python script at HEAD

* Remove incorrect duplicated line

* Delete the tmp hishtory-client download since it may be dropped in CWD rather than /tmp/
  • Loading branch information
ddworken committed Aug 11, 2024
1 parent cd6b46a commit ae5edb7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
17 changes: 15 additions & 2 deletions backend/web/landing/www/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@
with urllib.request.urlopen(download_url) as response:
hishtory_binary = response.read()

tmpdir = os.environ.get('TMPDIR', '') or '/tmp/'
tmpFilePath = tmpdir+'hishtory-client'
def get_executable_tmpdir():
specified_dir = os.environ.get('TMPDIR', '')
if specified_dir:
return specified_dir
try:
if hasattr(os, 'ST_NOEXEC'):
if os.statvfs("/tmp").f_flag & os.ST_NOEXEC:
# /tmp/ is mounted as NOEXEC, so fall back to the current working directory
return os.getcwd()
except:
pass
return "/tmp/"

tmpFilePath = os.path.join(get_executable_tmpdir(), 'hishtory-client')
if os.path.exists(tmpFilePath):
os.remove(tmpFilePath)
with open(tmpFilePath, 'wb') as f:
Expand All @@ -44,4 +56,5 @@
exitCode = os.system(cmd)
if exitCode != 0:
raise Exception("failed to install downloaded hishtory client via `" + tmpFilePath +" install` (is that directory mounted noexec? Consider setting an alternate directory via the TMPDIR environment variable)!")
os.remove(tmpFilePath)
print('Succesfully installed hishtory! Open a new terminal, try running a command, and then running `hishtory query`.')
36 changes: 36 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,42 @@ func testInstallViaPythonScriptChild(t *testing.T, tester shellTester) {
}
}

func TestInstallViaPythonScriptFromHead(t *testing.T) {
defer testutils.BackupAndRestore(t)()
tester := zshTester{}

// Set up
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()

// Install via the python script
out := tester.RunInteractiveShell(t, `cat backend/web/landing/www/install.py | python3 -`)
require.Contains(t, out, "Succesfully installed hishtory")
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
matches := r.FindStringSubmatch(out)
if len(matches) != 2 {
t.Fatalf("Failed to extract userSecret from output=%#v: matches=%#v", out, matches)
}
userSecret := matches[1]

// Test the status subcommand
downloadData, err := cmd.GetDownloadData(makeTestOnlyContextWithFakeConfig())
require.NoError(t, err)
out = tester.RunInteractiveShell(t, `hishtory status`)
expectedOut := fmt.Sprintf("hiSHtory: %s\nEnabled: true\nSecret Key: %s\nCommit Hash: ", downloadData.Version, userSecret)
require.Contains(t, out, expectedOut)

// And test that it recorded that command
time.Sleep(time.Second)
out = tester.RunInteractiveShell(t, `hishtory export -pipefail`)
if out != "hishtory status\n" {
t.Fatalf("unexpected output from hishtory export=%#v", out)
}

// And check that it installed in online mode
out = tester.RunInteractiveShell(t, `hishtory status -v`)
require.Contains(t, out, "\nSync Mode: Enabled\n")
}

func testExportWithQuery(t *testing.T, tester shellTester) {
// Setup
defer testutils.BackupAndRestore(t)()
Expand Down

0 comments on commit ae5edb7

Please sign in to comment.