mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-24 01:03:14 +01:00
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:
parent
4a9af23f30
commit
76fa568d95
@ -9,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -3307,4 +3308,58 @@ func TestSanitizeEscapeCodes(t *testing.T) {
|
|||||||
require.Contains(t, out, "Search Query: >\n")
|
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
|
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed
|
||||||
|
@ -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
|
// Check if we need to configure the bash_profile and configure it if so
|
||||||
if doesBashProfileNeedConfig(homedir) {
|
if doesBashProfileNeedConfig(homedir) {
|
||||||
|
_, err := os.Stat(path.Join(homedir, ".bash_profile"))
|
||||||
|
bashProfileExists := !errors.Is(err, os.ErrNotExist)
|
||||||
|
|
||||||
bashProfileIsConfigured, err := isBashProfileConfigured(homedir)
|
bashProfileIsConfigured, err := isBashProfileConfigured(homedir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to check ~/.bash_profile: %w", err)
|
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 {
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
|
@ -99,6 +99,7 @@ func BackupAndRestoreWithId(t testing.TB, id string) func() {
|
|||||||
path.Join(homedir, ".zshrc"),
|
path.Join(homedir, ".zshrc"),
|
||||||
path.Join(homedir, ".bashrc"),
|
path.Join(homedir, ".bashrc"),
|
||||||
path.Join(homedir, ".bash_profile"),
|
path.Join(homedir, ".bash_profile"),
|
||||||
|
path.Join(homedir, ".profile"),
|
||||||
}
|
}
|
||||||
for _, file := range copyFiles {
|
for _, file := range copyFiles {
|
||||||
touchFile(file)
|
touchFile(file)
|
||||||
|
Loading…
Reference in New Issue
Block a user