summaryrefslogtreecommitdiff
path: root/test/unit/__expand_tilde_by_ref.exp
blob: bcb931e1ef8df7608f54a2afd7d6d12f2ec9e76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# @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 {id -un 2>/dev/null || 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"
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/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/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/\$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/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/*"
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 "'~user;echo hello' should return '~user;echo hello' (not expanded)"
set cmd [format {var="~%s;echo hello"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list [format "~%s;echo hello" $user] $cmd $test
sync_after_int

set test "'~user/a;echo hello' should return '$home/a;echo hello'"
set cmd [format {var="~%s/a;echo hello"; __expand_tilde_by_ref var; printf "%%s\n" "$var"} $user]
assert_bash_list "$home/a;echo hello" $cmd $test
sync_after_int


teardown