fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes Altmanningertests/checks/cd: reformat61e0bb2a2

master
13.6 KiB473 linesraw
1# RUN: fish=%fish %fish %s
2
3set -g fish (realpath $fish)
4
5cygwin_nosymlinks && set nosymlinks
6
7# Store pwd to later go back before cleaning up
8set -l oldpwd (pwd)
9
10set -l tmp (mktemp -d)
11cd $tmp
12# resolve CDPATH (issue 6220)
13begin
14    mkdir -p x
15    # CHECK: /{{.*}}/x
16    cd x
17    pwd
18
19    # CHECK: /{{.*}}/x
20    set -lx CDPATH ..
21    cd x
22    pwd
23end
24cd $oldpwd
25rm -rf $tmp
26
27# Create a test directory to store our stuff.
28# macOS likes to return symlinks from (mktemp -d), make sure it does not.
29set -l base (realpath (mktemp -d))
30
31if not set -q nosymlinks
32    set real (realpath (mktemp -d))
33    set link $base/link
34    ln -s $real $link
35    cd $link
36    prevd
37    nextd
38    test "$PWD" = "$link" || echo "\$PWD != \$link:"\n "\$PWD: $PWD"\n "\$link: $link"\n
39    test (pwd) = "$link" || echo "(pwd) != \$link:"\n "\$PWD: "(pwd)\n "\$link: $link"\n
40    test (pwd -P) = "$real" || echo "(pwd -P) != \$real:"\n "\$PWD: $PWD"\n "\$real: $real"\n
41    test (pwd -P -L) = "$link" || echo "(pwd -P -L) != \$link:"\n "\$PWD: $PWD"\n "\$link: $link"\n
42end
43# Expect no output on success.
44pwd abc
45# CHECKERR: pwd: expected 0 arguments; got 1
46
47if set -q nosymlinks
48    echo "pwd: realpath failed: No such file or directory" >&2
49else
50    mkdir -p $base/pwd_real/subdir
51    ln -s $base/pwd_real $base/pwd_link
52    cd $base/pwd_link/subdir
53    rmdir $base/pwd_real/subdir $base/pwd_real
54    pwd -P
55end
56# CHECKERR: pwd: realpath failed: {{.+}}
57
58if not set -q nosymlinks
59    mkdir $real/subdir
60    cd $link
61    test "$PWD" = "$link" || echo "Default cd should keep symlink:"\n "\$PWD: $PWD"\n "\$link: $link"\n
62    cd -P $link
63    test "$PWD" = "$real" || echo "cd -P should resolve symlink:"\n "\$PWD: $PWD"\n "\$real: $real"\n
64    cd $link/subdir
65    test "$PWD" = "$link/subdir" || echo "Logical cd should keep subdir symlink:"\n "\$PWD: $PWD"\n "\$link/subdir: $link/subdir"\n
66    cd -P ..
67    test "$PWD" = "$real" || echo "cd -P .. should use physical parent:"\n "\$PWD: $PWD"\n "\$real: $real"\n
68    cd $link/subdir
69    cd -L ..
70    test "$PWD" = "$link" || echo "cd -L .. should use logical parent:"\n "\$PWD: $PWD"\n "\$link: $link"\n
71    cd $base
72end
73
74# Create a symlink and verify logical completion.
75# create directory $base/through/the/looking/glass
76# symlink $base/somewhere/rabbithole -> $base/through/the/looking/glass
77# verify that .. completions work
78cd $base
79mkdir -p $base/through/the/looking/glass
80
81mkdir -p $base/somewhere
82mkdir $base/somewhere/a1
83mkdir $base/somewhere/a2
84mkdir $base/somewhere/a3
85touch $base/through/the/looking/b(seq 1 3)
86mkdir $base/through/the/looking/d1
87mkdir $base/through/the/looking/d2
88mkdir $base/through/the/looking/d3
89ln -s $base/through/the/looking/glass $base/somewhere/rabbithole
90
91if set -q nosymlinks
92    # This is where we would be going if symlinks were working. This invalidates
93    # that particular test case, but now we can proceed with the tests
94    cd $base/through/the/looking/glass
95else
96    cd $base/somewhere/rabbithole
97end
98echo "ls:"
99complete -C'ls ../'
100#CHECK: ls:
101#CHECK: ../b1
102#CHECK: ../b2
103#CHECK: ../b3
104#CHECK: ../d1/
105#CHECK: ../d2/
106#CHECK: ../d3/
107#CHECK: ../glass/
108
109if set -q nosymlinks
110    cd $base/somewhere/rabbithole
111end
112echo "cd:"
113complete -C'cd ../'
114#CHECK: cd:
115#CHECK: ../a1/
116#CHECK: ../a2/
117#CHECK: ../a3/
118#CHECK: ../rabbithole/
119
120# PWD should be imported and respected by fish
121cd $oldpwd
122mkdir -p $base/realhome
123ln -s $base/realhome $base/linkhome
124cd $base/linkhome
125set -l real_getcwd (pwd -P)
126env HOME=$base/linkhome $fish -c 'echo PWD is $PWD'
127#CHECK: PWD is {{.*}}/linkhome
128
129# Do not inherit a virtual PWD that fails to resolve to getcwd (#5647)
130env HOME=$base/linkhome PWD=/tmp $fish -c 'echo $PWD' | read output_pwd
131test (realpath $output_pwd) = $real_getcwd
132and echo "BogusPWD test 1 succeeded"
133or echo "BogusPWD test 1 failed: $output_pwd vs $real_getcwd"
134#CHECK: BogusPWD test 1 succeeded
135
136env HOME=$base/linkhome PWD=/path/to/nowhere $fish -c 'echo $PWD' | read output_pwd
137test (realpath $output_pwd) = $real_getcwd
138and echo "BogusPWD test 2 succeeded"
139or echo "BogusPWD test 2 failed: $output_pwd vs $real_getcwd"
140#CHECK: BogusPWD test 2 succeeded
141
142# $CDPATH
143set -g CDPATH $base
144cd linkhome
145test $PWD = $base/linkhome; and echo Gone to linkhome via CDPATH
146#CHECK: Gone to linkhome via CDPATH
147
148set -g CDPATH /tmp
149cd $base
150test $PWD = $base; and echo Gone to base
151#CHECK: Gone to base
152
153cd linkhome
154test $PWD = $base/linkhome; and echo Gone to linkhome via implicit . in CDPATH
155#CHECK: Gone to linkhome via implicit . in CDPATH
156
157set -g CDPATH ./
158cd $base
159test $PWD = $base; and echo No crash with ./ CDPATH
160#CHECK: No crash with ./ CDPATH
161
162# test for directories beginning with a hyphen
163mkdir $base/-testdir
164cd $base
165cd -- -testdir
166test $PWD = $base/-testdir
167echo $status
168#CHECK: 0
169
170# test a few error cases - nonexistent directory
171set -l old_cdpath $CDPATH
172set -l old_path $PWD
173cd nonexistent
174#CHECKERR: cd: The directory 'nonexistent' does not exist
175#CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
176#CHECKERR: builtin cd $argv
177#CHECKERR: ^
178#CHECKERR: in function 'cd' with arguments 'nonexistent'
179#CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
180
181touch file
182cd file
183#CHECKERR: cd: 'file' is not a directory
184#CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
185#CHECKERR: builtin cd $argv
186#CHECKERR: ^
187#CHECKERR: in function 'cd' with arguments 'file'
188#CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
189
190# a directory that isn't executable
191if cygwin_noacl ./
192    echo "cd: Permission denied: 'bad-perms'" >&2
193    echo "fake/cd.fish (line 123):" >&2
194    echo "builtin cd \$argv" >&2
195    echo "^" >&2
196    echo "in function 'cd' with arguments 'bad-perms'" >&2
197    echo "called on line 123 of file fake/cd.fish" >&2
198else
199    mkdir bad-perms
200    chmod -x bad-perms
201    cd bad-perms
202end
203#CHECKERR: cd: Permission denied: 'bad-perms'
204#CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
205#CHECKERR: builtin cd $argv
206#CHECKERR: ^
207#CHECKERR: in function 'cd' with arguments 'bad-perms'
208#CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
209
210cd $old_path
211mkdir -p cdpath-dir/bad-perms
212mkdir -p cdpath-dir/nonexistent
213mkdir -p cdpath-dir/file
214set CDPATH $PWD/cdpath-dir $old_cdpath
215
216# See that the completions also check the current directory
217complete -C'cd ' | string match -q cdpath-dir/
218and echo cdpath-dir is in
219# CHECK: cdpath-dir is in
220
221# A different directory with the same name that is first in $CDPATH works.
222cd bad-perms
223cd $old_path
224cd nonexistent
225cd $old_path
226cd file
227cd $old_path
228
229# Even if the good dirs are later in $CDPATH most errors still aren't a problem
230# - they just cause us to keep looking.
231cd $old_path
232set CDPATH $old_cdpath $PWD/cdpath-dir
233cd nonexistent
234cd $old_path
235if cygwin_noacl ./
236    echo "cd: Permission denied: 'bad-perms'" >&2
237    echo "fake/cd.fish (line 123):" >&2
238    echo "builtin cd \$argv" >&2
239    echo "^" >&2
240    echo "in function 'cd' with arguments 'bad-perms'" >&2
241    echo "called on line 123 of file fake/cd.fish" >&2
242else
243    cd bad-perms
244end
245# Permission errors are still a problem!
246#CHECKERR: cd: Permission denied: 'bad-perms'
247#CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
248#CHECKERR: builtin cd $argv
249#CHECKERR: ^
250#CHECKERR: in function 'cd' with arguments 'bad-perms'
251#CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
252cd $old_path
253cd file
254cd $old_path
255
256# Test that going up to the root directory using .. works
257cd /(string split --no-empty -f 1 / (pwd))
258cd ..
259pwd
260#CHECK: /
261
262# cd back before removing the test directory again.
263cd $oldpwd
264rm -Rf $base
265set -g CDPATH ./
266
267# Verify that PWD on-variable events are sent
268function __fish_test_changed_pwd --on-variable PWD
269    echo "Changed to $PWD"
270end
271cd /
272functions --erase __fish_test_changed_pwd
273#CHECK: Changed to /
274
275# Verify that cds don't stomp on each other.
276function __fish_test_thrash_cd
277    set -l dir (mktemp -d)
278    cd $dir
279    for i in (seq 50)
280        test (command pwd) = $dir
281        and test $PWD = $dir
282        or echo "cd test failed" 1>&2
283        sleep .002
284    end
285end
286__fish_test_thrash_cd |
287    __fish_test_thrash_cd |
288    __fish_test_thrash_cd |
289    __fish_test_thrash_cd |
290    __fish_test_thrash_cd
291
292cd ""
293# CHECKERR: cd: Empty directory '' does not exist
294# CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
295# CHECKERR: builtin cd $argv
296# CHECKERR: ^
297# CHECKERR: in function 'cd' with arguments '""'
298# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
299echo $status
300# CHECK: 1
301
302cd (mktemp -d)
303if set -q nosymlinks
304    echo "cd: 'fake/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'" >&2
305    echo "fake/cd.fish (line 123):" >&2
306    echo "builtin cd \$argv" >&2
307    echo "^" >&2
308    echo "in function 'cd' with arguments 'broken-symbolic-link'" >&2
309    echo "called on line 123 of file fake/cd.fish" >&2
310else
311    ln -s no/such/directory broken-symbolic-link
312    begin
313        set -lx CDPATH
314        cd broken-symbolic-link
315    end
316end
317# CHECKERR: cd: '{{.*}}/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'
318# CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
319# CHECKERR: builtin cd $argv
320# CHECKERR: ^
321# CHECKERR: in function 'cd' with arguments 'broken-symbolic-link'
322# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
323
324# Make sure that "broken symlink" is reported over "no such file or directory".
325if set -q nosymlinks
326    echo "cd: 'fake/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'" >&2
327    echo "fake/cd.fish (line 123):" >&2
328    echo "builtin cd \$argv" >&2
329    echo "^" >&2
330    echo "in function 'cd' with arguments 'broken-symbolic-link'" >&2
331    echo "called on line 123 of file fake/cd.fish" >&2
332else
333    begin
334        set -lx CDPATH other
335        cd broken-symbolic-link
336    end
337end
338# CHECKERR: cd: '{{.*}}/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'
339# CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
340# CHECKERR: builtin cd $argv
341# CHECKERR: ^
342# CHECKERR: in function 'cd' with arguments 'broken-symbolic-link'
343# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
344
345begin
346    mkdir -p foo/bar/muf
347    set -lx CDPATH foo/bar
348    cd muf
349    echo $PWD | grep -q ^/ && echo '$PWD is absolute'
350    # CHECK: $PWD is absolute
351    cd ../../..
352end
353
354complete -C'cd .'
355# CHECK: ./
356# CHECK: ../
357
358# Check that cd works with minimal permissions (issue #10432).
359# This is first supported on macOS 12.
360# `sysctl kern.osproductversion` emits something like:
361#   kern.osproductversion: 14.3.1
362# Note that there is no kern.osproductversion under older OS X releases!
363#
364# NetBSD 10 does not support it.
365# Cygwin/MSYS does not support it when using ACL. And without ACL, a directory
366# cannot be made unreadable, making the test pointless. So either way, skip it
367if test (uname) = NetBSD || __fish_is_cygwin ||
368    { test (uname) = Darwin &&
369        test \
370        (sysctl kern.osproductversion 2>/dev/null | string match -r \\d+; or echo 10) \
371        -lt 12 }
372    # Not supported. Satisfy the CHECKs below.
373    echo fake/a
374    echo fake/a/b
375    echo c
376else
377    set -l oldpwd (pwd)
378    set -l tmp (mktemp -d)
379    cd $tmp
380    mkdir -p a/b/c
381    chmod -r a
382
383    cd a
384    pwd
385    # CHECK: {{.*}}/a
386
387    cd b
388    pwd
389    ls
390    # CHECK: {{.*}}/a/b
391    # CHECK: c
392
393    cd $oldpwd
394    chmod -R +rx $tmp # we must be able to list the directory to delete its children
395    rm -rf $tmp
396end
397
398HOME="" cd
399# CHECKERR: cd: Could not find home directory
400
401if set -q nosymlinks
402    echo "cd: Too many levels of symbolic links: 'loop1'" >&2
403    echo "fake/cd.fish (line 123):" >&2
404    echo "builtin cd \$argv" >&2
405    echo "^" >&2
406    echo "in function 'cd' with arguments 'loop1'" >&2
407    echo "called on line 123 of file fake/cd.fish" >&2
408else
409    ln -s loop1 loop2
410    ln -s loop2 loop1
411    cd loop1
412end
413# CHECKERR: cd: Too many levels of symbolic links: 'loop1'
414# CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
415# CHECKERR: builtin cd $argv
416# CHECKERR: ^
417# CHECKERR: in function 'cd' with arguments 'loop1'
418# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
419
420# According to https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits,
421# the longest filename supported is with Reiser4 (3976 bytes)
422cd (string repeat 4096 a)
423# CHECKERR: cd: {{.+}}
424# CHECKERR: cd: Unknown error trying to locate directory '{{.*}}'
425# CHECKERR: {{.*}}/cd.fish (line {{\d+}}):
426# CHECKERR: builtin cd $argv
427# CHECKERR: ^
428# CHECKERR: in function 'cd' with arguments '{{.*}}'
429# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
430
431# Ensures `cd` doesn't create `<pwd>+/+<dir>` internally, when pwd is `/`, i.e.
432# results in `//<dir>`. This test will (hopefully) fail on platforms where such
433# a path has a special meaning (e.g. Windows would fail trying to access a server
434# named "bin")
435cd /
436cd bin
437
438# Test that cd works after the current directory has been moved (issue #12700)
439if __fish_is_cygwin
440    # Not supported on Cygwin/MSYS. Satisfy the CHECK below.
441    echo "cd after move succeeded"
442else
443    # cd is updated to the real PWD if PWD is moved.
444    set -l tmp (mktemp -d)
445    set -l tmp_moved {$tmp}_moved
446    cd $tmp
447    mv $tmp $tmp_moved
448    cd .
449    test $PWD = $tmp_moved
450    and echo "cd after move succeeded"
451end
452# CHECK: cd after move succeeded
453
454if not __fish_is_cygwin
455    # If updating to the real PWD is possible, but cd still fails, fish reports an error
456    # and keeps $PWD the same.
457    set tmp (mktemp -d)
458    set tmp_moved {$tmp}_moved
459    cd $tmp
460    mv $tmp $tmp_moved
461    touch $tmp_moved/not_a_directory
462    # Now we have two problems: $tmp has been moved to $tmp_moved, AND we can't cd into the directory
463    # even after repairing $PWD because it's not a directory.
464    # Here we should print an error about the failed target and NOT change $PWD.
465    cd ./not_a_directory 2>$tmp_moved/log.txt
466    if not string match -q "cd: './not_a_directory' is not a directory" <$tmp_moved/log.txt
467        echo "Expected error message not found in log"
468    end
469    # Note we stay in the old $PWD because of the error.
470    if test $PWD != $tmp
471        echo "PWD was changed unexpectedly to $PWD"
472    end
473end