fish-shell/tests/checks/symlinks-not-overwritten.fish
Ilya Grigoriev 762f3aa0ce
Rewrite the real file if history file is a symlink (#7754)
* Rewrite the real file if history file is a symlink

When the history file is a symbolic link, `fish` used to overwrite
the link with a real file whenever it saved history. This makes
it follow the symlink and overwrite the real file instead.

The same issue was fixed for the `fish_variables` file in 622f2868e
from https://github.com/fish-shell/fish-shell/pull/7728.
This makes `fish_history` behave in the same way. The implementation
is nearly identical.

Since the tests for the two issues are so similar, I combined them
together and slightly expanded the older test.

This also addresses https://github.com/fish-shell/fish-shell/issues/7553.

* Add user-facing error when history renaming fails

Currently, when history file renaming fails, no message is shown to the
user. This happens, for instance, if the history file is a symlink
pointing to another filesystem.

This copies code (with a bit of variation, after reviewer comments) from

589eb34571/src/env_universal_common.cpp (L486-L491)

into `history.cpp`, so that a message is shown to the user.

* fixup! Rewrite the real file if history file is a symlink
2021-03-08 17:46:17 +01:00

49 lines
1.5 KiB
Fish

#RUN: %fish -C 'set -g fish %fish' %s
set -gx XDG_CONFIG_HOME (mktemp -d)
set -gx XDG_DATA_HOME $XDG_CONFIG_HOME
mkdir -p $XDG_CONFIG_HOME/fish
# fish_variables
set -l target_file $XDG_CONFIG_HOME/fish/target_fish_variables
set -l fish_variables $XDG_CONFIG_HOME/fish/fish_variables
set -l backup_file $XDG_CONFIG_HOME/fish/fish_variables_backup
echo >$target_file
cp $target_file $backup_file
ln -sf $target_file $fish_variables
$fish -c 'set -U variable value'
if not test -L $fish_variables
echo fish_variables has been overwritten
else if cmp $target_file $backup_file >/dev/null
echo fish_variables was never written
else
echo fish_variables is still a symlink
end
# CHECK: fish_variables is still a symlink
# fish_history
set -l history_file $XDG_DATA_HOME/fish/fish_history
set -l target_file $XDG_DATA_HOME/fish/target_fish_history
set -l backup_file $XDG_DATA_HOME/fish/fish_history_backup
echo '- cmd: echo I will be deleted from history
when: 1614577746' >$target_file
cp $target_file $backup_file
ln -sf $target_file $history_file
# The one way to ensure non-interactive fish writes to the history file
$fish -c 'echo all | history delete deleted | grep echo'
# CHECK: [1] echo I will be deleted from history
if not test -L $history_file
echo fish_history has been overwritten
else if cmp $target_file $backup_file &>/dev/null
# cmp writes to stderr when one file is empty, thus &> above
echo fish_history was never written
else
echo fish_history is still a symlink
end
# CHECK: fish_history is still a symlink