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