-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.sh
125 lines (109 loc) · 2.19 KB
/
tests.sh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
if [ "${1:0:2}" = "-v" ]; then
v=${1:2}*
shift
fi
. ./tildeExpand.sh
strexp() {
local v
v=$(printf %q "$1")
eval echo "${v/#\\~/"~"}"
}
t() {
exp=$("$1" "$2")
local res=t
if [ "$3" != "$exp" ]; then
res=f
fi
if [[ "$res" = $v ]]; then
printf \\n >&2
printf 'Original: %s\n' "$2" >&2
printf 'Expanded: %s\n' "$exp" >&2
printf 'Expected: %s\n' "$3" >&2
fi
if [ "$3" = "$exp" ]; then
printf 'Succeeded: '\''%s'\''\n' "$2"
else
printf 'Failed: '\''%s'\''\n' "$2"
fi
}
ta() {
t expandAssign "$@"
}
ts() {
t expandString "$@"
}
# shellcheck disable=SC2088
{
name1="~/Documents/over enthusiastic"
name2="~crl/Documents/double spaced"
name3="/work/whiffle/two spaces are better than one"
name4="~nobody/Documents/double spaced"
name5="~nobody/Documents:~nobody/me~some~tildes~"
ts "~+" ~+
ts "~+/plus" ~+/plus
ts "~-" ~-
ts "~-/minus" ~-/minus
cd /tmp; cd - >/dev/null
ts "$name1" "$(strexp "$name1")"
ts "$name2" "$(strexp "$name2")"
ts "$name3" "$(strexp "$name3")"
ts "$name4" "$(strexp "$name4")"
ts "$name5" "$(strexp "$name5")"
ts "~" "$HOME"
ts "~/" "$HOME/"
ts "~crl" ~crl
ts "~crl/" ~crl/
ts "~nobody" ~nobody
ts "~nobody/" ~nobody/
ts "~foo-bar" ~foo-bar
ts "~foo-bar/" ~foo-bar/
ts "~2-" ~2-
ts "~2-/" ~2-/
ts "~+" ~+
ts "~+0" ~+0
ts "~+/plus" ~+/plus
ts "~-" ~-
ts "~-0" ~-0
ts "~-/minus" ~-/minus
pt='~/foo:~/bar:~/baz'
ps=$(strexp "$pt")
pa=~/foo:~/bar:~/baz
ts "$pt" "$ps"
ta "$pt" "$pa"
o=$PWD
pushd / >/dev/null
pushd /tmp >/dev/null
pushd /opt >/dev/null
pushd /var >/dev/null
pushd "$o" >/dev/null
ts '~1' ~1
ts '~+1' ~+1
ts '~-1' ~-1
ts '~1+' ~1+
ts '~+1+' ~+1+
ts '~-1+' ~-1+
ts '~2' ~2
ts '~+2' ~+2
ts '~-2' ~-2
ts '~2+' ~2+
ts '~+2+' ~+2+
ts '~-2+' ~-2+
ts '~8' ~8
ts '~+8' ~+8
ts '~-8' ~-8
ts '~8+' ~8+
ts '~+8+' ~+8+
ts '~-8+' ~-8+
for ((i=0; i < 11; i++)); do
pushd "$o" >/dev/null
done
ts '~11' ~11
ts '~+11' ~+11
ts '~-11' ~-11
ts '~11+' ~11+
ts '~+11+' ~+11+
ts '~-11+' ~-11+
}
(unset -v HOME; ta '~' ~)
(unset -v HOME; ta '~/foo' ~/foo)