Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 41 additions & 0 deletions test/lib/helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,47 @@ 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' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add a negative test, where you're trying to add the same path segment twice, to show that it will only be added once...

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 duplicate' {
new_paths='/tmp/fake-pathmunge-path1:/tmp/fake pathmunge path2:/tmp/fake-pathmunge-path1:/tmp/fake-pathmunge-path3'
want_paths='/tmp/fake pathmunge path2:/tmp/fake-pathmunge-path1:/tmp/fake-pathmunge-path3'
old_path="${PATH}"
pathmunge "${new_paths}"
assert_equal "${want_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'"
Expand Down