Add tests for fix for #238

This commit is contained in:
David Dworken 2024-08-29 22:53:50 -07:00
parent f5d3899433
commit 5ea4fef6f6
No known key found for this signature in database
2 changed files with 56 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
@ -3307,4 +3308,58 @@ func TestSanitizeEscapeCodes(t *testing.T) {
require.Contains(t, out, "Search Query: >\n")
}
func TestCreatedBashProfileSourcesProfile(t *testing.T) {
if runtime.GOOS == "linux" {
t.Skip("hishtory doesn't create .bash_profile on linux")
}
markTestForSharding(t, 18)
defer testutils.BackupAndRestore(t)()
tester := zshTester{}
homedir, err := os.UserHomeDir()
require.NoError(t, err)
// Delete .bash_profile so hishtory will create a new one
require.NoError(t, os.Remove(filepath.Join(homedir, ".bash_profile")))
// Install hishtory
installHishtory(t, tester, "")
// Check that both files exist now
require.FileExists(t, filepath.Join(homedir, ".bash_profile"))
require.FileExists(t, filepath.Join(homedir, ".profile"))
// Check that .bash_profile sources .profile and that it contains the hishtory config
bashProfileContent, err := os.ReadFile(filepath.Join(homedir, ".bash_profile"))
require.NoError(t, err)
require.Contains(t, string(bashProfileContent), "source ~/.profile")
require.Contains(t, string(bashProfileContent), "# Hishtory Config:\n")
}
func TestExistingBashProfileDoesNotSourceProfile(t *testing.T) {
if runtime.GOOS == "linux" {
t.Skip("hishtory doesn't create .bash_profile on linux")
}
markTestForSharding(t, 18)
defer testutils.BackupAndRestore(t)()
tester := zshTester{}
homedir, err := os.UserHomeDir()
require.NoError(t, err)
// Ensure .bash_profile exists so that hishtory doesn't create it
require.FileExists(t, filepath.Join(homedir, ".bash_profile"))
// Install hishtory
installHishtory(t, tester, "")
// Check that both files exist now
require.FileExists(t, filepath.Join(homedir, ".bash_profile"))
require.FileExists(t, filepath.Join(homedir, ".profile"))
// Check that .bash_profile does not source .profile and that it does contain the hishtory config
bashProfileContent, err := os.ReadFile(filepath.Join(homedir, ".bash_profile"))
require.NoError(t, err)
require.NotContains(t, string(bashProfileContent), "source ~/.profile")
require.Contains(t, string(bashProfileContent), "# Hishtory Config:\n")
}
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed

View File

@ -99,6 +99,7 @@ func BackupAndRestoreWithId(t testing.TB, id string) func() {
path.Join(homedir, ".zshrc"),
path.Join(homedir, ".bashrc"),
path.Join(homedir, ".bash_profile"),
path.Join(homedir, ".profile"),
}
for _, file := range copyFiles {
touchFile(file)