OILS / spec / builtin-cd.test.sh View on Github | oilshell.org

333 lines, 182 significant
1## oils_failures_allowed: 1
2## compare_shells: dash bash mksh zsh
3
4#### cd and $PWD
5cd /
6echo $PWD
7## stdout: /
8
9#### cd BAD/..
10
11# Odd divergence in shells: dash and mksh normalize the path and don't check
12# this error.
13# TODO: I would like OSH to behave like bash and zsh, but separating chdir_arg
14# and pwd_arg breaks case 17.
15
16cd nonexistent_ZZ/..
17echo status=$?
18## STDOUT:
19status=1
20## END
21## BUG dash/mksh STDOUT:
22status=0
23## END
24
25#### cd with 2 or more args
26
27mkdir -p foo
28cd foo
29echo status=$?
30cd ..
31echo status=$?
32
33
34cd foo bar
35st=$?
36if test $st -ne 0; then
37 echo 'failed with multiple args'
38fi
39
40## STDOUT:
41status=0
42status=0
43failed with multiple args
44## END
45
46## BUG dash STDOUT:
47status=0
48status=0
49## END
50
51#### cd - without OLDPWD
52
53cd - > /dev/null # silence dash output
54echo status=$?
55#pwd
56
57## STDOUT:
58status=1
59## END
60
61## OK mksh STDOUT:
62status=2
63## END
64
65## BUG dash/zsh STDOUT:
66status=0
67## END
68
69#### $OLDPWD
70cd /
71cd $TMP
72echo "old: $OLDPWD"
73env | grep OLDPWD # It's EXPORTED too!
74cd -
75## STDOUT:
76old: /
77OLDPWD=/
78/
79## END
80## BUG mksh STDOUT:
81old: /
82/
83## END
84## BUG zsh STDOUT:
85old: /
86OLDPWD=/
87## END
88
89#### pwd
90cd /
91pwd
92## STDOUT:
93/
94## END
95
96#### pwd after cd ..
97dir=$TMP/dir-one/dir-two
98mkdir -p $dir
99cd $dir
100echo $(basename $(pwd))
101cd ..
102echo $(basename $(pwd))
103## STDOUT:
104dir-two
105dir-one
106## END
107
108#### pwd with symlink and -P
109tmp=$TMP/builtins-pwd-1
110mkdir -p $tmp/target
111ln -s -f $tmp/target $tmp/symlink
112
113cd $tmp/symlink
114
115echo pwd:
116basename $(pwd)
117
118echo pwd -P:
119basename $(pwd -P)
120
121## STDOUT:
122pwd:
123symlink
124pwd -P:
125target
126## END
127
128#### setting $PWD doesn't affect the value of 'pwd' builtin
129dir=/tmp/oil-spec-test/pwd
130mkdir -p $dir
131cd $dir
132
133PWD=foo
134echo before $PWD
135pwd
136echo after $PWD
137## STDOUT:
138before foo
139/tmp/oil-spec-test/pwd
140after foo
141## END
142
143#### unset PWD; then pwd
144dir=/tmp/oil-spec-test/pwd
145mkdir -p $dir
146cd $dir
147
148unset PWD
149echo PWD=$PWD
150pwd
151echo PWD=$PWD
152## STDOUT:
153PWD=
154/tmp/oil-spec-test/pwd
155PWD=
156## END
157
158#### 'unset PWD; pwd' before any cd (tickles a rare corner case)
159dir=/tmp/oil-spec-test/pwd-2
160mkdir -p $dir
161cd $dir
162
163# ensure clean shell process state
164$SH -c 'unset PWD; pwd'
165
166## STDOUT:
167/tmp/oil-spec-test/pwd-2
168## END
169
170#### lie about PWD; pwd before any cd
171dir=/tmp/oil-spec-test/pwd-3
172mkdir -p $dir
173cd $dir
174
175# ensure clean shell process state
176$SH -c 'PWD=foo; pwd'
177
178## STDOUT:
179/tmp/oil-spec-test/pwd-3
180## END
181
182#### remove pwd dir
183dir=/tmp/oil-spec-test/pwd
184mkdir -p $dir
185cd $dir
186pwd
187rmdir $dir
188echo status=$?
189pwd
190echo status=$?
191## STDOUT:
192/tmp/oil-spec-test/pwd
193status=0
194/tmp/oil-spec-test/pwd
195status=0
196## END
197## OK mksh STDOUT:
198/tmp/oil-spec-test/pwd
199status=0
200status=1
201## END
202
203#### pwd in symlinked dir on shell initialization
204tmp=$TMP/builtins-pwd-2
205mkdir -p $tmp
206mkdir -p $tmp/target
207ln -s -f $tmp/target $tmp/symlink
208
209cd $tmp/symlink
210$SH -c 'basename $(pwd)'
211unset PWD
212$SH -c 'basename $(pwd)'
213
214## STDOUT:
215symlink
216target
217## END
218## OK mksh STDOUT:
219target
220target
221## END
222## stderr-json: ""
223
224#### Test the current directory after 'cd ..' involving symlinks
225dir=$TMP/symlinktest
226mkdir -p $dir
227cd $dir
228mkdir -p a/b/c
229mkdir -p a/b/d
230ln -s -f a/b/c c > /dev/null
231cd c
232cd ..
233# Expecting a c/ (since we are in symlinktest) but osh gives c d (thinks we are
234# in b/)
235ls
236## STDOUT:
237a
238c
239## END
240
241#### cd with no arguments
242HOME=$TMP/home
243mkdir -p $HOME
244cd
245test $(pwd) = "$HOME" && echo OK
246## stdout: OK
247
248#### cd to nonexistent dir
249cd /nonexistent/dir
250echo status=$?
251## stdout: status=1
252## OK dash/mksh stdout: status=2
253
254#### cd away from dir that was deleted
255dir=$TMP/cd-nonexistent
256mkdir -p $dir
257cd $dir
258rmdir $dir
259cd $TMP
260echo $(basename $OLDPWD)
261echo status=$?
262## STDOUT:
263cd-nonexistent
264status=0
265## END
266
267#### cd permits double bare dash
268cd -- /
269echo $PWD
270## stdout: /
271
272#### cd to symlink with -L and -P
273targ=$TMP/cd-symtarget
274lnk=$TMP/cd-symlink
275mkdir -p $targ
276ln -s $targ $lnk
277
278# -L behavior is the default
279cd $lnk
280test $PWD = "$TMP/cd-symlink" && echo OK
281
282cd -L $lnk
283test $PWD = "$TMP/cd-symlink" && echo OK
284
285cd -P $lnk
286test $PWD = "$TMP/cd-symtarget" && echo OK || echo $PWD
287## STDOUT:
288OK
289OK
290OK
291## END
292
293#### cd to relative path with -L and -P
294die() { echo "$@"; exit 1; }
295
296targ=$TMP/cd-symtarget/subdir
297lnk=$TMP/cd-symlink
298mkdir -p $targ
299ln -s $TMP/cd-symtarget $lnk
300
301# -L behavior is the default
302cd $lnk/subdir
303test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
304cd ..
305test $PWD = "$TMP/cd-symlink" && echo OK
306
307cd $lnk/subdir
308test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
309cd -L ..
310test $PWD = "$TMP/cd-symlink" && echo OK
311
312cd $lnk/subdir
313test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
314cd -P ..
315test $PWD = "$TMP/cd-symtarget" && echo OK || echo $PWD
316## STDOUT:
317OK
318OK
319OK
320## END
321
322
323#### unset PWD; cd /tmp is allowed (regression)
324
325unset PWD; cd /tmp
326pwd
327
328## STDOUT:
329/tmp
330## END
331
332
333