Implement fix for #238 by sourcing .profile from .bash_profile to prevent breaking users that rely on .profile from bash (#240)

* Implement fix for #238 by sourcing .profile from .bash_profile to prevent breaking users that rely on .profile from bash

* Add tests for fix for #238
This commit is contained in:
David Dworken 2024-08-30 08:40:07 -07:00 committed by GitHub
parent 4a9af23f30
commit 76fa568d95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 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

@ -398,6 +398,9 @@ func configureBashrc(homedir, binaryPath string, skipConfigModification bool) er
}
// Check if we need to configure the bash_profile and configure it if so
if doesBashProfileNeedConfig(homedir) {
_, err := os.Stat(path.Join(homedir, ".bash_profile"))
bashProfileExists := !errors.Is(err, os.ErrNotExist)
bashProfileIsConfigured, err := isBashProfileConfigured(homedir)
if err != nil {
return fmt.Errorf("failed to check ~/.bash_profile: %w", err)
@ -407,6 +410,14 @@ func configureBashrc(homedir, binaryPath string, skipConfigModification bool) er
if err != nil {
return err
}
if !bashProfileExists {
// If the .bash_profile doesn't exist and we just created it, we're breaking the sourcing of .profile (see #238)
// So we need to source .profile from .bash_profile
err = addToShellConfig(path.Join(homedir, ".bash_profile"), "\n# Source .profile:\nsource ~/.profile\n", skipConfigModification)
if err != nil {
return err
}
}
}
}
return nil

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)