[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / tpwd-test.sh
blob2f222da79f667c81ec2e664fb892e29bef68b6e1
1 #!/bin/sh
2 ##
3 ## Test for tpwd script
4 ## Copyright (c) 2014 by Michal Nazareicz (mina86/AT/mina86.com)
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
19 ## This is part of Tiny Applications Collection
20 ## -> http://tinyapps.sourceforge.net/
23 set -eu
26 self="$PWD/tpwd-test.sh"
27 tpwd="$PWD/tpwd"
28 if ! [ -x "$self" ] || ! [ -x "$tpwd" ]; then
29 echo "Expected $self and $tpwd to be executable." >&2
30 exit 2
33 temp=$(mktemp -d || exit 2)
34 trap 'rm -r -- "$temp"' 0
36 mkdir "$temp/foobar" "$temp/foobar/baz"
37 cd "$temp/foobar"
38 ln -s baz qux
39 cd "$temp/foobar/qux"
41 echo >$temp/expected '---------- >8 --------------------------------------------------
42 TPWD_VARS=unset
44 $ tpwd <~/foobar/qux
45 > 0
46 $ tpwd 5 <...ux
47 > 0
48 $ tpwd 5 { <{/qux
49 > 0
50 $ tpwd 5 ... 1 <.../qux
51 > 0
52 $ tpwd 5 1 </qux
53 > 0
54 $ tpwd -n <~/foobar/qux> 0
55 $ tpwd -n 5 <...ux> 0
56 $ tpwd -n 5 { <{/qux> 0
57 $ tpwd -n 5 ... 1 <.../qux> 0
58 $ tpwd -n 5 1 </qux> 0
59 $ tpwd -L <~/foobar/qux
60 > 0
61 $ tpwd -L 5 <...ux
62 > 0
63 $ tpwd -L 5 { <{/qux
64 > 0
65 $ tpwd -L 5 ... 1 <.../qux
66 > 0
67 $ tpwd -L 5 1 </qux
68 > 0
69 $ tpwd -L -n <~/foobar/qux> 0
70 $ tpwd -L -n 5 <...ux> 0
71 $ tpwd -L -n 5 { <{/qux> 0
72 $ tpwd -L -n 5 ... 1 <.../qux> 0
73 $ tpwd -L -n 5 1 </qux> 0
74 $ tpwd -P <~/foobar/baz
75 > 0
76 $ tpwd -P 5 <...az
77 > 0
78 $ tpwd -P 5 { <{/baz
79 > 0
80 $ tpwd -P 5 ... 1 <.../baz
81 > 0
82 $ tpwd -P 5 1 </baz
83 > 0
84 $ tpwd -P -n <~/foobar/baz> 0
85 $ tpwd -P -n 5 <...az> 0
86 $ tpwd -P -n 5 { <{/baz> 0
87 $ tpwd -P -n 5 ... 1 <.../baz> 0
88 $ tpwd -P -n 5 1 </baz> 0
89 ---------- >8 --------------------------------------------------
90 TPWD_VARS=HOME
92 $ tpwd <$HOME/foobar/qux
93 > 0
94 $ tpwd 5 <...ux
95 > 0
96 $ tpwd 5 { <{/qux
97 > 0
98 $ tpwd 5 ... 1 <.../qux
99 > 0
100 $ tpwd 5 1 </qux
102 $ tpwd -n <$HOME/foobar/qux> 0
103 $ tpwd -n 5 <...ux> 0
104 $ tpwd -n 5 { <{/qux> 0
105 $ tpwd -n 5 ... 1 <.../qux> 0
106 $ tpwd -n 5 1 </qux> 0
107 $ tpwd -L <$HOME/foobar/qux
109 $ tpwd -L 5 <...ux
111 $ tpwd -L 5 { <{/qux
113 $ tpwd -L 5 ... 1 <.../qux
115 $ tpwd -L 5 1 </qux
117 $ tpwd -L -n <$HOME/foobar/qux> 0
118 $ tpwd -L -n 5 <...ux> 0
119 $ tpwd -L -n 5 { <{/qux> 0
120 $ tpwd -L -n 5 ... 1 <.../qux> 0
121 $ tpwd -L -n 5 1 </qux> 0
122 $ tpwd -P <$HOME/foobar/baz
124 $ tpwd -P 5 <...az
126 $ tpwd -P 5 { <{/baz
128 $ tpwd -P 5 ... 1 <.../baz
130 $ tpwd -P 5 1 </baz
132 $ tpwd -P -n <$HOME/foobar/baz> 0
133 $ tpwd -P -n 5 <...az> 0
134 $ tpwd -P -n 5 { <{/baz> 0
135 $ tpwd -P -n 5 ... 1 <.../baz> 0
136 $ tpwd -P -n 5 1 </baz> 0
137 ---------- >8 --------------------------------------------------
138 TPWD_VARS=~
140 $ tpwd <~/foobar/qux
142 $ tpwd 5 <...ux
144 $ tpwd 5 { <{/qux
146 $ tpwd 5 ... 1 <.../qux
148 $ tpwd 5 1 </qux
150 $ tpwd -n <~/foobar/qux> 0
151 $ tpwd -n 5 <...ux> 0
152 $ tpwd -n 5 { <{/qux> 0
153 $ tpwd -n 5 ... 1 <.../qux> 0
154 $ tpwd -n 5 1 </qux> 0
155 $ tpwd -L <~/foobar/qux
157 $ tpwd -L 5 <...ux
159 $ tpwd -L 5 { <{/qux
161 $ tpwd -L 5 ... 1 <.../qux
163 $ tpwd -L 5 1 </qux
165 $ tpwd -L -n <~/foobar/qux> 0
166 $ tpwd -L -n 5 <...ux> 0
167 $ tpwd -L -n 5 { <{/qux> 0
168 $ tpwd -L -n 5 ... 1 <.../qux> 0
169 $ tpwd -L -n 5 1 </qux> 0
170 $ tpwd -P <~/foobar/baz
172 $ tpwd -P 5 <...az
174 $ tpwd -P 5 { <{/baz
176 $ tpwd -P 5 ... 1 <.../baz
178 $ tpwd -P 5 1 </baz
180 $ tpwd -P -n <~/foobar/baz> 0
181 $ tpwd -P -n 5 <...az> 0
182 $ tpwd -P -n 5 { <{/baz> 0
183 $ tpwd -P -n 5 ... 1 <.../baz> 0
184 $ tpwd -P -n 5 1 </baz> 0
185 ---------- >8 --------------------------------------------------
186 TPWD_VARS=temp
188 $ tpwd <$temp/foobar/qux
190 $ tpwd 5 <...ux
192 $ tpwd 5 { <{/qux
194 $ tpwd 5 ... 1 <.../qux
196 $ tpwd 5 1 </qux
198 $ tpwd -n <$temp/foobar/qux> 0
199 $ tpwd -n 5 <...ux> 0
200 $ tpwd -n 5 { <{/qux> 0
201 $ tpwd -n 5 ... 1 <.../qux> 0
202 $ tpwd -n 5 1 </qux> 0
203 $ tpwd -L <$temp/foobar/qux
205 $ tpwd -L 5 <...ux
207 $ tpwd -L 5 { <{/qux
209 $ tpwd -L 5 ... 1 <.../qux
211 $ tpwd -L 5 1 </qux
213 $ tpwd -L -n <$temp/foobar/qux> 0
214 $ tpwd -L -n 5 <...ux> 0
215 $ tpwd -L -n 5 { <{/qux> 0
216 $ tpwd -L -n 5 ... 1 <.../qux> 0
217 $ tpwd -L -n 5 1 </qux> 0
218 $ tpwd -P <$temp/foobar/baz
220 $ tpwd -P 5 <...az
222 $ tpwd -P 5 { <{/baz
224 $ tpwd -P 5 ... 1 <.../baz
226 $ tpwd -P 5 1 </baz
228 $ tpwd -P -n <$temp/foobar/baz> 0
229 $ tpwd -P -n 5 <...az> 0
230 $ tpwd -P -n 5 { <{/baz> 0
231 $ tpwd -P -n 5 ... 1 <.../baz> 0
232 $ tpwd -P -n 5 1 </baz> 0
233 ---------- >8 --------------------------------------------------
234 TPWD_VARS=long_variable_name_which_is_hopefully_longer_than_length_of_temp:~
236 $ tpwd <~/foobar/qux
238 $ tpwd 5 <...ux
240 $ tpwd 5 { <{/qux
242 $ tpwd 5 ... 1 <.../qux
244 $ tpwd 5 1 </qux
246 $ tpwd -n <~/foobar/qux> 0
247 $ tpwd -n 5 <...ux> 0
248 $ tpwd -n 5 { <{/qux> 0
249 $ tpwd -n 5 ... 1 <.../qux> 0
250 $ tpwd -n 5 1 </qux> 0
251 $ tpwd -L <~/foobar/qux
253 $ tpwd -L 5 <...ux
255 $ tpwd -L 5 { <{/qux
257 $ tpwd -L 5 ... 1 <.../qux
259 $ tpwd -L 5 1 </qux
261 $ tpwd -L -n <~/foobar/qux> 0
262 $ tpwd -L -n 5 <...ux> 0
263 $ tpwd -L -n 5 { <{/qux> 0
264 $ tpwd -L -n 5 ... 1 <.../qux> 0
265 $ tpwd -L -n 5 1 </qux> 0
266 $ tpwd -P <~/foobar/baz
268 $ tpwd -P 5 <...az
270 $ tpwd -P 5 { <{/baz
272 $ tpwd -P 5 ... 1 <.../baz
274 $ tpwd -P 5 1 </baz
276 $ tpwd -P -n <~/foobar/baz> 0
277 $ tpwd -P -n 5 <...az> 0
278 $ tpwd -P -n 5 { <{/baz> 0
279 $ tpwd -P -n 5 ... 1 <.../baz> 0
280 $ tpwd -P -n 5 1 </baz> 0'
282 run() {
283 set -- tpwd "$@"
284 printf "$ %s <" "$*"
285 shift
286 ec=0
287 HOME=$temp "$tpwd" "$@" || ec=$?
288 printf '> %d\n' $ec
291 long_variable_name_which_is_hopefully_longer_than_length_of_temp=$temp
292 export long_variable_name_which_is_hopefully_longer_than_length_of_temp
293 export temp
294 for vars in unset HOME '~' temp long_variable_name_which_is_hopefully_longer_than_length_of_temp:\~; do
295 echo '---------- >8 --------------------------------------------------'
296 echo "TPWD_VARS=$vars"
297 echo
298 if [ x"$vars" = xunset ]; then
299 unset TPWD_VARS
300 else
301 TPWD_VARS=$vars
302 export TPWD_VARS
305 for lp in '' -L -P; do
306 for n in '' -n; do
307 run $lp $n
308 run $lp $n 5
309 run $lp $n 5 '{'
310 run $lp $n 5 ... 1
311 run $lp $n 5 '' 1
312 done
313 done
315 done >$temp/got
317 if cmp -s "$temp/expected" "$temp/got"; then
318 echo "All good."
319 else
320 tput setf 4; tput bold; echo 'FAILED; got difference:'
321 diff -u "$temp/expected" "$temp/got"
322 exit 1