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