bash-completion/test/unit/__expand_tilde_by_ref.exp

88 lines
2.0 KiB
Plaintext
Raw Normal View History

# @param string $out Reference to variable to hold value of bash environment
# variable $HOME.
proc setup {home user} {
upvar $home _home
upvar $user _user
save_env
assert_bash_exec {echo "$HOME"} {} /@ _home
set _home [string trim $_home]
assert_bash_exec {echo "$USER"} {} /@ _user
set _user [string trim $_user]
}
proc teardown {} {
assert_env_unmodified {
/var=/d
}
}
setup home user
set test "function should run without errors"
assert_bash_exec {__expand_tilde_by_ref > /dev/null} $test
sync_after_int
set test "function should not pollute environment"
# NOTE: A possible environment pollution is detected by assert_env_modified() in teardown()
assert_bash_exec {foo() { local aa="~"; __expand_tilde_by_ref aa; }; foo; unset foo} $test
sync_after_int
set test "~user should return /home/user"
set cmd [format {var="~%s"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list "$home" $cmd $test
sync_after_int
set test "~/foo should return /home/user/foo"
set cmd {var='~/foo'; __expand_tilde_by_ref var; printf "%s\n" "$var"}
assert_bash_list "$home/foo" $cmd $test
sync_after_int
set test "~user/bar should return /home/user/bar"
set cmd [format {var="~%s/bar"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list "$home/bar" $cmd $test
sync_after_int
set test "~user/\$HOME should return /home/user/\$HOME"
set cmd [format {var="~%s/\$HOME"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list "$home/\$HOME" $cmd $test
sync_after_int
set test "'~user/a b' should return '/home/user/a b'"
set cmd [format {var="~%s/a b"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list [list [format {%s/a b} $home]] $cmd $test
sync_after_int
set test "~user/* should return /home/user/*"
set cmd [format {var="~%s/*"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list "$home/\*" $cmd $test
sync_after_int
teardown