support multiple paths in pathmunge

pull/1664/head
cornfeedhobo 2020-09-24 11:41:36 -05:00
parent 4365fa8d69
commit 79ca9a708d
No known key found for this signature in database
GPG Key ID: 724357093F994B26
2 changed files with 45 additions and 6 deletions

View File

@ -680,12 +680,18 @@ then
example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH'
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'
if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then
if [ "$2" = "after" ] ; then
export PATH=$PATH:$1
else
export PATH=$1:$PATH
IFS=':' local -a 'a=($1)'
local i=${#a[@]}
while [[ $i -gt 0 ]] ; do
i=$(( i - 1 ))
p=${a[i]}
if ! [[ $PATH =~ (^|:)$p($|:) ]] ; then
if [[ "$2" = "after" ]] ; then
export PATH=$PATH:$p
else
export PATH=$p:$PATH
fi
fi
fi
done
}
fi

View File

@ -39,6 +39,39 @@ function local_setup {
assert_failure
}
@test 'helpers: pathmunge: ensure function is defined' {
run type -t pathmunge
assert_line 'function'
}
@test 'helpers: pathmunge: single path' {
new_paths='/tmp/fake-pathmunge-path'
old_path="${PATH}"
pathmunge "${new_paths}"
assert_equal "${new_paths}:${old_path}" "${PATH}"
}
@test 'helpers: pathmunge: single path, with space' {
new_paths='/tmp/fake pathmunge path'
old_path="${PATH}"
pathmunge "${new_paths}"
assert_equal "${new_paths}:${old_path}" "${PATH}"
}
@test 'helpers: pathmunge: multiple paths' {
new_paths='/tmp/fake-pathmunge-path1:/tmp/fake-pathmunge-path2'
old_path="${PATH}"
pathmunge "${new_paths}"
assert_equal "${new_paths}:${old_path}" "${PATH}"
}
@test 'helpers: pathmunge: multiple paths, with space' {
new_paths='/tmp/fake pathmunge path1:/tmp/fake pathmunge path2'
old_path="${PATH}"
pathmunge "${new_paths}"
assert_equal "${new_paths}:${old_path}" "${PATH}"
}
@test "helpers: bash-it help aliases ag" {
run bash-it help aliases "ag"
assert_line -n 0 "ag='ag --smart-case --pager=\"less -MIRFX'"