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

304 lines, 174 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#### $OLDPWD
52cd /
53cd $TMP
54echo "old: $OLDPWD"
55env | grep OLDPWD # It's EXPORTED too!
56cd -
57## STDOUT:
58old: /
59OLDPWD=/
60/
61## END
62## BUG mksh STDOUT:
63old: /
64/
65## END
66## BUG zsh STDOUT:
67old: /
68OLDPWD=/
69## END
70
71#### pwd
72cd /
73pwd
74## STDOUT:
75/
76## END
77
78#### pwd after cd ..
79dir=$TMP/dir-one/dir-two
80mkdir -p $dir
81cd $dir
82echo $(basename $(pwd))
83cd ..
84echo $(basename $(pwd))
85## STDOUT:
86dir-two
87dir-one
88## END
89
90#### pwd with symlink and -P
91tmp=$TMP/builtins-pwd-1
92mkdir -p $tmp/target
93ln -s -f $tmp/target $tmp/symlink
94
95cd $tmp/symlink
96
97echo pwd:
98basename $(pwd)
99
100echo pwd -P:
101basename $(pwd -P)
102
103## STDOUT:
104pwd:
105symlink
106pwd -P:
107target
108## END
109
110#### setting $PWD doesn't affect the value of 'pwd' builtin
111dir=/tmp/oil-spec-test/pwd
112mkdir -p $dir
113cd $dir
114
115PWD=foo
116echo before $PWD
117pwd
118echo after $PWD
119## STDOUT:
120before foo
121/tmp/oil-spec-test/pwd
122after foo
123## END
124
125#### unset PWD; then pwd
126dir=/tmp/oil-spec-test/pwd
127mkdir -p $dir
128cd $dir
129
130unset PWD
131echo PWD=$PWD
132pwd
133echo PWD=$PWD
134## STDOUT:
135PWD=
136/tmp/oil-spec-test/pwd
137PWD=
138## END
139
140#### 'unset PWD; pwd' before any cd (tickles a rare corner case)
141dir=/tmp/oil-spec-test/pwd-2
142mkdir -p $dir
143cd $dir
144
145# ensure clean shell process state
146$SH -c 'unset PWD; pwd'
147
148## STDOUT:
149/tmp/oil-spec-test/pwd-2
150## END
151
152#### lie about PWD; pwd before any cd
153dir=/tmp/oil-spec-test/pwd-3
154mkdir -p $dir
155cd $dir
156
157# ensure clean shell process state
158$SH -c 'PWD=foo; pwd'
159
160## STDOUT:
161/tmp/oil-spec-test/pwd-3
162## END
163
164#### remove pwd dir
165dir=/tmp/oil-spec-test/pwd
166mkdir -p $dir
167cd $dir
168pwd
169rmdir $dir
170echo status=$?
171pwd
172echo status=$?
173## STDOUT:
174/tmp/oil-spec-test/pwd
175status=0
176/tmp/oil-spec-test/pwd
177status=0
178## END
179## OK mksh STDOUT:
180/tmp/oil-spec-test/pwd
181status=0
182status=1
183## END
184
185#### pwd in symlinked dir on shell initialization
186tmp=$TMP/builtins-pwd-2
187mkdir -p $tmp
188mkdir -p $tmp/target
189ln -s -f $tmp/target $tmp/symlink
190
191cd $tmp/symlink
192$SH -c 'basename $(pwd)'
193unset PWD
194$SH -c 'basename $(pwd)'
195
196## STDOUT:
197symlink
198target
199## END
200## OK mksh STDOUT:
201target
202target
203## END
204## stderr-json: ""
205
206#### Test the current directory after 'cd ..' involving symlinks
207dir=$TMP/symlinktest
208mkdir -p $dir
209cd $dir
210mkdir -p a/b/c
211mkdir -p a/b/d
212ln -s -f a/b/c c > /dev/null
213cd c
214cd ..
215# Expecting a c/ (since we are in symlinktest) but osh gives c d (thinks we are
216# in b/)
217ls
218## STDOUT:
219a
220c
221## END
222
223#### cd with no arguments
224HOME=$TMP/home
225mkdir -p $HOME
226cd
227test $(pwd) = "$HOME" && echo OK
228## stdout: OK
229
230#### cd to nonexistent dir
231cd /nonexistent/dir
232echo status=$?
233## stdout: status=1
234## OK dash/mksh stdout: status=2
235
236#### cd away from dir that was deleted
237dir=$TMP/cd-nonexistent
238mkdir -p $dir
239cd $dir
240rmdir $dir
241cd $TMP
242echo $(basename $OLDPWD)
243echo status=$?
244## STDOUT:
245cd-nonexistent
246status=0
247## END
248
249#### cd permits double bare dash
250cd -- /
251echo $PWD
252## stdout: /
253
254#### cd to symlink with -L and -P
255targ=$TMP/cd-symtarget
256lnk=$TMP/cd-symlink
257mkdir -p $targ
258ln -s $targ $lnk
259
260# -L behavior is the default
261cd $lnk
262test $PWD = "$TMP/cd-symlink" && echo OK
263
264cd -L $lnk
265test $PWD = "$TMP/cd-symlink" && echo OK
266
267cd -P $lnk
268test $PWD = "$TMP/cd-symtarget" && echo OK || echo $PWD
269## STDOUT:
270OK
271OK
272OK
273## END
274
275#### cd to relative path with -L and -P
276die() { echo "$@"; exit 1; }
277
278targ=$TMP/cd-symtarget/subdir
279lnk=$TMP/cd-symlink
280mkdir -p $targ
281ln -s $TMP/cd-symtarget $lnk
282
283# -L behavior is the default
284cd $lnk/subdir
285test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
286cd ..
287test $PWD = "$TMP/cd-symlink" && echo OK
288
289cd $lnk/subdir
290test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
291cd -L ..
292test $PWD = "$TMP/cd-symlink" && echo OK
293
294cd $lnk/subdir
295test $PWD = "$TMP/cd-symlink/subdir" || die "failed"
296cd -P ..
297test $PWD = "$TMP/cd-symtarget" && echo OK || echo $PWD
298## STDOUT:
299OK
300OK
301OK
302## END
303
304