fish-shell/tests/checks/fish_add_path.fish
Fabian Homborg 9354dd6971 Add fish_add_path, a simple way to add to $PATH
This is a function you can either execute once, interactively, or
stick in config.fish, and it will do the right thing.

Some options are included to choose some slightly different behavior,
like setting $PATH directly instead of $fish_user_paths, or moving
already existing components to the front/back instead of ignoring
them, or appending new components instead of prepending them.

The defaults were chosen because they are the most safe, and
especially because they allow it to be idempotent - running it again
and again and again won't change anything, it won't even run the
actual `set` because it skips that if all components are already in.

Fixes #6960.
2020-05-29 20:51:05 +02:00

56 lines
1.5 KiB
Fish

# RUN: %fish %s
#
# This deals with $PATH manipulation. We need to be careful not to step on anything.
set -l tmpdir (mktemp -d)
mkdir $tmpdir/bin
mkdir $tmpdir/sbin
mkdir $tmpdir/etc
ln -s $tmpdir/bin $tmpdir/link
# We set fish_user_paths to an empty global to have a starting point
set -g fish_user_paths
fish_add_path -v $tmpdir/bin
# CHECK: set fish_user_paths {{.*}}/bin
echo $status
# CHECK: 0
# Confirm that it actually ends up in $PATH
contains -- (builtin realpath $tmpdir/bin) $PATH
and echo Have bin
# CHECK: Have bin
# Not adding duplicates and not triggering variable handlers
function checkpath --on-variable PATH --on-variable fish_user_paths; echo CHECKPATH: $argv; end
set PATH $PATH
# CHECK: CHECKPATH: VARIABLE SET PATH
fish_add_path -v $tmpdir/bin
# Nothing happened, so the status failed.
echo $status
# CHECK: 1
functions --erase checkpath
# Not adding a link either
fish_add_path -v $tmpdir/link
echo $status
# CHECK: 1
fish_add_path -a $tmpdir/sbin
# Not printing anything because it's not verbose, the /sbin should be added at the end.
string replace -- $tmpdir '' $fish_user_paths | string join ' '
# CHECK: /bin /sbin
fish_add_path -m $tmpdir/sbin
string replace -- $tmpdir '' $fish_user_paths | string join ' '
# CHECK: /sbin /bin
set -l oldpath "$PATH"
fish_add_path -nP $tmpdir/etc | string replace -- $tmpdir ''
# Should print a set command to prepend /etc to $PATH, but not actually do it
# CHECK: set PATH /etc{{.*}}
# Confirm that $PATH didn't change.
test "$oldpath" = "$PATH"
or echo "PATH CHANGED!!!" >&2
exit 0