fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
61e0bb2a2
master
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 14mkdir -p x 15# CHECK: /{{.*}}/x 16cd x 17pwd 18 19# CHECK: /{{.*}}/x 20set -lx CDPATH .. 21cd x 22pwd 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 32set real( realpath ( mktemp -d)) 33set link$base /link 34ln -s$real $link 35cd $link 36prevd 37nextd 38test " $PWD " = " $link " || echo "\$PWD != \$link:"\n "\$PWD: $PWD "\n "\$link: $link "\n 39test ( pwd ) = " $link " || echo "(pwd) != \$link:"\n "\$PWD: " ( pwd ) \n "\$link: $link "\n 40test ( pwd -P) = " $real " || echo "(pwd -P) != \$real:"\n "\$PWD: $PWD "\n "\$real: $real "\n 41test ( 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 48echo "pwd: realpath failed: No such file or directory" >&2 49else 50mkdir -p$base /pwd_real/subdir 51ln -s$base /pwd_real$base /pwd_link 52cd $base /pwd_link/subdir 53rmdir $base /pwd_real/subdir$base /pwd_real 54pwd -P 55end 56# CHECKERR: pwd: realpath failed: {{.+}} 57 58if not set -q nosymlinks 59mkdir $real /subdir 60cd $link 61test " $PWD " = " $link " || echo "Default cd should keep symlink:"\n "\$PWD: $PWD "\n "\$link: $link "\n 62cd -P$link 63test " $PWD " = " $real " || echo "cd -P should resolve symlink:"\n "\$PWD: $PWD "\n "\$real: $real "\n 64cd $link /subdir 65test " $PWD " = " $link /subdir" || echo "Logical cd should keep subdir symlink:"\n "\$PWD: $PWD "\n "\$link/subdir: $link /subdir"\n 66cd -P .. 67test " $PWD " = " $real " || echo "cd -P .. should use physical parent:"\n "\$PWD: $PWD "\n "\$real: $real "\n 68cd $link /subdir 69cd -L .. 70test " $PWD " = " $link " || echo "cd -L .. should use logical parent:"\n "\$PWD: $PWD "\n "\$link: $link "\n 71cd $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 94cd $base /through/the/looking/glass 95else 96cd $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 110cd $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 ./ 192echo "cd: Permission denied: 'bad-perms'" >&2 193echo "fake/cd.fish (line 123):" >&2 194echo "builtin cd \$argv" >&2 195echo "^" >&2 196echo "in function 'cd' with arguments 'bad-perms'" >&2 197echo "called on line 123 of file fake/cd.fish" >&2 198else 199mkdir bad-perms 200chmod -x bad-perms 201cd 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 ./ 236echo "cd: Permission denied: 'bad-perms'" >&2 237echo "fake/cd.fish (line 123):" >&2 238echo "builtin cd \$argv" >&2 239echo "^" >&2 240echo "in function 'cd' with arguments 'bad-perms'" >&2 241echo "called on line 123 of file fake/cd.fish" >&2 242else 243cd 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 -f1 /( 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 269echo "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 277set -l dir( mktemp -d) 278cd $dir 279for iin ( seq 50 ) 280test ( command pwd) = $dir 281and test $PWD = $dir 282or echo "cd test failed" 1>&2 283sleep .002 284end 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 304echo "cd: 'fake/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'" >&2 305echo "fake/cd.fish (line 123):" >&2 306echo "builtin cd \$argv" >&2 307echo "^" >&2 308echo "in function 'cd' with arguments 'broken-symbolic-link'" >&2 309echo "called on line 123 of file fake/cd.fish" >&2 310else 311ln -s no/such/directory broken-symbolic-link 312begin 313set -lx CDPATH 314cd broken-symbolic-link 315end 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 326echo "cd: 'fake/broken-symbolic-link' is a broken symbolic link to 'no/such/directory'" >&2 327echo "fake/cd.fish (line 123):" >&2 328echo "builtin cd \$argv" >&2 329echo "^" >&2 330echo "in function 'cd' with arguments 'broken-symbolic-link'" >&2 331echo "called on line 123 of file fake/cd.fish" >&2 332else 333begin 334set -lx CDPATH other 335cd broken-symbolic-link 336end 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 346mkdir -p foo/bar/muf 347set -lx CDPATH foo/bar 348cd muf 349echo $PWD | grep -q ^/&& echo '$PWD is absolute' 350# CHECK: $PWD is absolute 351cd ../../.. 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&& 369test \ 370( sysctl kern.osproductversion2> /dev/null| string match -r\\ d+;or echo 10 ) \ 371-lt 12 } 372 # Not supported. Satisfy the CHECKs below. 373echo fake/a 374echo fake/a/b 375echo c 376else 377set -l oldpwd( pwd ) 378set -l tmp( mktemp -d) 379cd $tmp 380mkdir -p a/b/c 381chmod -r a 382 383cd a 384pwd 385# CHECK: {{.*}}/a 386 387cd b 388pwd 389ls 390# CHECK: {{.*}}/a/b 391# CHECK: c 392 393cd $oldpwd 394chmod -R +rx$tmp # we must be able to list the directory to delete its children 395rm -rf$tmp 396end 397 398HOME="" cd 399# CHECKERR: cd: Could not find home directory 400 401if set -q nosymlinks 402echo "cd: Too many levels of symbolic links: 'loop1'" >&2 403echo "fake/cd.fish (line 123):" >&2 404echo "builtin cd \$argv" >&2 405echo "^" >&2 406echo "in function 'cd' with arguments 'loop1'" >&2 407echo "called on line 123 of file fake/cd.fish" >&2 408else 409ln -s loop1 loop2 410ln -s loop2 loop1 411cd 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 repeat4096 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. 441echo "cd after move succeeded" 442else 443# cd is updated to the real PWD if PWD is moved. 444set -l tmp( mktemp -d) 445set -l tmp_moved{ $tmp } _moved 446cd $tmp 447mv $tmp $tmp_moved 448cd . 449test $PWD = $tmp_moved 450and 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. 457set tmp( mktemp -d) 458set tmp_moved{ $tmp } _moved 459cd $tmp 460mv $tmp $tmp_moved 461touch $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. 465cd ./not_a_directory2> $tmp_moved /log.txt 466if not string match -q"cd: './not_a_directory' is not a directory" < $tmp_moved /log.txt 467echo "Expected error message not found in log" 468end 469# Note we stay in the old $PWD because of the error. 470if test $PWD != $tmp 471echo "PWD was changed unexpectedly to $PWD " 472end 473end