1# cd-p.tst: test of the cd built-in for any POSIX-compliant shell
2
3# Tests in this file may fail if the pathname of the current directory is too
4# long, making the pathname of temporary directories exceed PATH_MAX.
5
6posix="true"
7
8cd -P .
9export ORIGPWD="$PWD"
10mkdir -p cdpath1/foo cdpath2/foo/bar cdpath2/dev dev
11mkdir -m 400 no_search_dir
12ln -s cdpath2/foo link
13>file
14
15test_oE 'default operand is HOME (-L)'
16HOME=/dev
17cd -L
18echo --- $?
19pwd
20__IN__
21--- 0
22/dev
23__OUT__
24
25test_oE 'default operand is HOME (-P)'
26HOME=/dev
27cd -P
28echo --- $?
29pwd
30__IN__
31--- 0
32/dev
33__OUT__
34
35(
36# Ensure $PWD is safe to assign to $PATH
37case $PWD in (*[:%]*)
38    skip="true"
39esac
40
41testcase "$LINENO" 'found in first cd path (-L)' \
42    3<<\__IN__ 5</dev/null 4<<__OUT__
43CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
44cd -L foo
45echo --- $?
46pwd
47__IN__
48$ORIGPWD/cdpath1/foo
49--- 0
50$ORIGPWD/cdpath1/foo
51__OUT__
52
53testcase "$LINENO" 'found in first cd path (-P)' \
54    3<<\__IN__ 5</dev/null 4<<__OUT__
55CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
56cd -P foo
57echo --- $?
58pwd
59__IN__
60$ORIGPWD/cdpath1/foo
61--- 0
62$ORIGPWD/cdpath1/foo
63__OUT__
64
65testcase "$LINENO" 'found in last cd path (-L)' \
66    3<<\__IN__ 5</dev/null 4<<__OUT__
67CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
68cd -L foo/bar
69echo --- $?
70pwd
71__IN__
72$ORIGPWD/cdpath2/foo/bar
73--- 0
74$ORIGPWD/cdpath2/foo/bar
75__OUT__
76
77testcase "$LINENO" 'found in last cd path (-P)' \
78    3<<\__IN__ 5</dev/null 4<<__OUT__
79CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
80cd -P foo/bar
81echo --- $?
82pwd
83__IN__
84$ORIGPWD/cdpath2/foo/bar
85--- 0
86$ORIGPWD/cdpath2/foo/bar
87__OUT__
88
89testcase "$LINENO" 'found in empty cd path (-L)' \
90    3<<\__IN__ 5</dev/null 4<<__OUT__
91CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
92cd -L dev
93echo --- $?
94pwd
95__IN__
96--- 0
97$ORIGPWD/dev
98__OUT__
99
100testcase "$LINENO" 'found in empty cd path (-P)' \
101    3<<\__IN__ 5</dev/null 4<<__OUT__
102CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
103cd -P dev
104echo --- $?
105pwd
106__IN__
107--- 0
108$ORIGPWD/dev
109__OUT__
110
111testcase "$LINENO" 'found in dot cd path (-L)' \
112    3<<\__IN__ 5</dev/null 4<<__OUT__
113CDPATH=$ORIGPWD/cdpath1:.:$ORIGPWD/cdpath2
114cd -L dev
115echo --- $?
116pwd
117__IN__
118$ORIGPWD/dev
119--- 0
120$ORIGPWD/dev
121__OUT__
122
123testcase "$LINENO" 'found in dot cd path (-P)' \
124    3<<\__IN__ 5</dev/null 4<<__OUT__
125CDPATH=$ORIGPWD/cdpath1:.:$ORIGPWD/cdpath2
126cd -P dev
127echo --- $?
128pwd
129__IN__
130$ORIGPWD/dev
131--- 0
132$ORIGPWD/dev
133__OUT__
134
135test_oE 'cd path ending with slash (-L)'
136CDPATH=/
137cd -L dev
138echo --- $?
139pwd
140__IN__
141/dev
142--- 0
143/dev
144__OUT__
145
146test_oE 'cd path ending with slash (-P)'
147CDPATH=/
148cd -P dev
149echo --- $?
150pwd
151__IN__
152/dev
153--- 0
154/dev
155__OUT__
156
157testcase "$LINENO" 'found not in any cd path, but in PWD (-L)' \
158    3<<\__IN__ 5</dev/null 4<<__OUT__
159CDPATH=$ORIGPWD/cdpath1:$ORIGPWD/cdpath2
160cd -L cdpath1
161echo --- $?
162pwd
163__IN__
164--- 0
165$ORIGPWD/cdpath1
166__OUT__
167
168testcase "$LINENO" 'found not in any cd path, but in PWD (-P)' \
169    3<<\__IN__ 5</dev/null 4<<__OUT__
170CDPATH=$ORIGPWD/cdpath1:$ORIGPWD/cdpath2
171cd -P cdpath1
172echo --- $?
173pwd
174__IN__
175--- 0
176$ORIGPWD/cdpath1
177__OUT__
178
179test_oE 'cd paths are ignored for absolute path operand (-L)'
180CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
181cd -L /dev
182echo --- $?
183pwd
184__IN__
185--- 0
186/dev
187__OUT__
188
189test_oE 'cd paths are ignored for absolute path operand (-P)'
190CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
191cd -P /dev
192echo --- $?
193pwd
194__IN__
195--- 0
196/dev
197__OUT__
198
199testcase "$LINENO" 'cd paths are ignored for operand starting with dot (-L)' \
200    3<<\__IN__ 5</dev/null 4<<__OUT__
201CDPATH=$ORIGPWD/cdpath2
202cd -L ./dev
203echo --- $?
204pwd
205__IN__
206--- 0
207$ORIGPWD/dev
208__OUT__
209
210testcase "$LINENO" 'cd paths are ignored for operand starting with dot (-P)' \
211    3<<\__IN__ 5</dev/null 4<<__OUT__
212CDPATH=$ORIGPWD/cdpath2
213cd -P ./dev
214echo --- $?
215pwd
216__IN__
217--- 0
218$ORIGPWD/dev
219__OUT__
220
221testcase "$LINENO" 'cd paths are ignored for operand starting with dot-dot (-L)' \
222    3<<\__IN__ 5</dev/null 4<<__OUT__
223unset CDPATH
224cd -L cdpath1
225CDPATH=$ORIGPWD/cdpath2
226cd ../dev
227echo --- $?
228pwd
229__IN__
230--- 0
231$ORIGPWD/dev
232__OUT__
233
234testcase "$LINENO" 'cd paths are ignored for operand starting with dot-dot (-P)' \
235    3<<\__IN__ 5</dev/null 4<<__OUT__
236unset CDPATH
237cd -P cdpath1
238CDPATH=$ORIGPWD/cdpath2
239cd ../dev
240echo --- $?
241pwd
242__IN__
243--- 0
244$ORIGPWD/dev
245__OUT__
246
247testcase "$LINENO" -d 'not found in any cd path nor in PWD (-L)' \
248    3<<\__IN__ 4<<__OUT__
249CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
250cd -L _no_such_path_
251echo --- $((!$?))
252pwd
253__IN__
254--- 0
255$ORIGPWD
256__OUT__
257
258testcase "$LINENO" -d 'not found in any cd path nor in PWD (-P)' \
259    3<<\__IN__ 4<<__OUT__
260CDPATH=$ORIGPWD/cdpath1::$ORIGPWD/cdpath2
261cd -P _no_such_path_
262echo --- $((!$?))
263pwd
264__IN__
265--- 0
266$ORIGPWD
267__OUT__
268
269)
270
271testcase "$LINENO" -d 'directory not found (with unset CDPATH, -L)' \
272    3<<\__IN__ 4<<__OUT__
273unset CDPATH
274cd -L _no_such_path_
275echo --- $((!$?))
276pwd
277__IN__
278--- 0
279$ORIGPWD
280__OUT__
281
282testcase "$LINENO" -d 'directory not found (with unset CDPATH, -P)' \
283    3<<\__IN__ 4<<__OUT__
284unset CDPATH
285cd -P _no_such_path_
286echo --- $((!$?))
287pwd
288__IN__
289--- 0
290$ORIGPWD
291__OUT__
292
293test_O -d -e n 'non-directory file in operand component (-L)'
294cd -L ./file/../dev
295__IN__
296
297test_O -d -e n 'non-directory file in operand component (-P)'
298cd -P ./file/../dev
299__IN__
300
301test_O -d -e n 'non-existing file in operand component (-L)'
302cd -L ./_no_such_file_/../dev
303__IN__
304
305test_O -d -e n 'non-existing file in operand component (-P)'
306cd -P ./_no_such_file_/../dev
307__IN__
308
309testcase "$LINENO" 'target pathname is canonicalized (-L)' \
310    3<<\__IN__ 4<<__OUT__
311unset CDPATH
312cd -L link/./../dev/.
313printf 'PWD=%s\n' "$PWD"
314pwd
315__IN__
316PWD=$ORIGPWD/dev
317$ORIGPWD/dev
318__OUT__
319
320testcase "$LINENO" 'symbolic links are resolved (in operand, -P)' \
321    3<<\__IN__ 4<<__OUT__
322unset CDPATH
323cd -P link/./../dev/.
324printf 'PWD=%s\n' "$PWD"
325pwd
326__IN__
327PWD=$ORIGPWD/cdpath2/dev
328$ORIGPWD/cdpath2/dev
329__OUT__
330
331testcase "$LINENO" 'symbolic links are resolved (in old PWD, -P)' \
332    3<<\__IN__ 4<<__OUT__
333unset CDPATH
334cd -L link
335cd -P ./../dev/.
336printf 'PWD=%s\n' "$PWD"
337pwd
338__IN__
339PWD=$ORIGPWD/cdpath2/dev
340$ORIGPWD/cdpath2/dev
341__OUT__
342
343testcase "$LINENO" 'default option is -L' \
344    3<<\__IN__ 4<<__OUT__
345unset CDPATH
346cd link/./../dev/.
347printf 'PWD=%s\n' "$PWD"
348pwd
349__IN__
350PWD=$ORIGPWD/dev
351$ORIGPWD/dev
352__OUT__
353
354testcase "$LINENO" 'the last option wins (-L)' \
355    3<<\__IN__ 4<<__OUT__
356unset CDPATH
357cd -P -L -PL link/./../dev/.
358printf 'PWD=%s\n' "$PWD"
359pwd
360__IN__
361PWD=$ORIGPWD/dev
362$ORIGPWD/dev
363__OUT__
364
365testcase "$LINENO" 'the last option wins (-P)' \
366    3<<\__IN__ 4<<__OUT__
367unset CDPATH
368cd -L -P -LP link/./../dev/.
369printf 'PWD=%s\n' "$PWD"
370pwd
371__IN__
372PWD=$ORIGPWD/cdpath2/dev
373$ORIGPWD/cdpath2/dev
374__OUT__
375
376(
377# Skip if we're root.
378if [ -d no_search_dir/. ]; then
379    skip="true"
380fi
381
382test_O -d -e n 'changing to unsearchable directory (-L)'
383cd -L no_search_dir
384__IN__
385
386test_O -d -e n 'changing to unsearchable directory (-P)'
387cd -P no_search_dir
388__IN__
389
390)
391
392test_oE 'hyphen operand means OLDPWD (-L)'
393OLDPWD=/dev
394cd -L -
395echo --- $?
396pwd
397__IN__
398/dev
399--- 0
400/dev
401__OUT__
402
403test_oE 'hyphen operand means OLDPWD (-P)'
404OLDPWD=/dev
405cd -P -
406echo --- $?
407pwd
408__IN__
409/dev
410--- 0
411/dev
412__OUT__
413
414testcase "$LINENO" 'OLDPWD is set to old PWD (-L)' \
415    3<<\__IN__ 4<<__OUT__
416unset CDPATH
417cd -L /
418printf 'OLDPWD=%s\n' "$OLDPWD"
419__IN__
420OLDPWD=$ORIGPWD
421__OUT__
422
423# vim: set ft=sh ts=8 sts=4 sw=4 noet:
424