fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
944ab91fa
master
1#RUN: %fish %s 2# The "path" builtin for dealing with paths 3 4cygwin_nosymlinks && set nosymlinks 5 6# Extension - for figuring out the file extension of a given path. 7path extension / 8or echo None 9# CHECK: 10# CHECK: None 11 12# No extension 13path extension /. 14or echo Filename is just a dot, no extension 15# CHECK: 16# CHECK: Filename is just a dot, no extension 17 18# No extension - ".foo" is the filename 19path extension /.foo 20or echo None again 21# CHECK: 22# CHECK: None again 23 24path extension /foo 25or echo None once more 26# CHECK: 27# CHECK: None once more 28path extension /foo.txt 29and echo Success 30# CHECK: .txt 31# CHECK: Success 32path extension /foo.txt/bar 33or echo Not even here 34# CHECK: 35# CHECK: Not even here 36path extension . .. 37or echo No extension 38# CHECK: 39# CHECK: No extension 40path extension ./foo.mp4 41# CHECK: .mp4 42path extension ../banana 43# CHECK: 44# nothing, status 1 45echo $status 46# CHECK: 1 47path extension ~/.config 48# CHECK: 49# nothing, status 1 50echo $status 51# CHECK: 1 52path extension ~/.config.d 53# CHECK: .d 54path extension ~/.config. 55echo $status 56# status 0 57# CHECK: . 58# CHECK: 0 59 60path change-extension'' ./foo.mp4 61# CHECK: ./foo 62path change-extension wmv ./foo.mp4 63# CHECK: ./foo.wmv 64path change-extension .wmv ./foo.mp4 65# CHECK: ./foo.wmv 66path change-extension'' ../banana 67# CHECK: ../banana 68# still status 0, because there was an argument 69echo $status 70# CHECK: 0 71path change-extension'' ~/.config 72# CHECK: {{.*}}/.config 73echo $status 74# CHECK: 0 75 76path basename ./foo.mp4 77# CHECK: foo.mp4 78path basename ../banana 79# CHECK: banana 80path basename /usr/bin/ 81# CHECK: bin 82path dirname ./foo.mp4 83# CHECK: . 84path basename ../banana 85# CHECK: banana 86path basename /usr/bin/ 87# CHECK: bin 88 89cd $TMPDIR 90cygwin_noacl ./&& set -l noacl 91mkdir -p bin 92touch bin/{ bash, bssh, chsh, dash, fish, slsh, ssh, zsh} 93ln -s$TMPDIR /bin/bash bin/sh 94 95chmod +x bin/* 96# We need files from here on 97path filter bin argagagji 98# The (hopefully) nonexistent argagagji is filtered implicitly: 99# CHECK: bin 100 101# With --invert, the existing bin is filtered 102path filter --invert bin argagagji 103# CHECK: argagagji 104 105# With --invert and a type, bin fails the type, 106# and argagagji doesn't exist, so both are printed. 107path filter -vf bin argagagji 108# CHECK: bin 109# CHECK: argagagji 110 111# With --all, return true if all paths are passed. 112path filter --all bin bin/bash 113echo $status 114# CHECK: 0 115path filter --all bin argagagji 116echo $status 117# CHECK: 1 118# With --all and --invert, return true if none of paths is passed. 119path filter --all --invert bin bin/bash 120echo $status 121# CHECK: 1 122path filter --all --invert argagagji argagagji2 123echo $status 124# CHECK: 0 125 126path filter --type file bin bin/fish 127# Only fish is a file 128# CHECK: bin/fish 129chmod 500 bin/fish 130path filter --type file,dir --perm exec,write bin/fish . 131# fish is a file, which passes, and executable, which passes, 132# but not writable, which fails. 133# 134# . is a directory and both writable and executable, typically. 135# So it passes. 136# CHECK: . 137 138mkdir -p sbin 139touch sbin/setuid-exe sbin/setgid-exe 140 141# Without POSIX permission, there is no way to set the setuid bit, so fake 142# the output. 143if set -q noacl 144echo sbin/setuid-exe 145else 146chmod u+s,a+x sbin/setuid-exe 147path filter --perm suid sbin/* 148end 149# CHECK: sbin/setuid-exe 150 151# Without POSIX permission, there is no way to set the setgid bit, so fake 152# the result. 153# And on at least FreeBSD on our CI this fails with "permission denied". 154# So we can't test it, and we fake the output there too. 155if set -q noacl 156echo sbin/setgid-exe 157else if chmod g+s,a+x sbin/setgid-exe2> /dev/null 158path filter --perm sgid sbin/* 159else 160echo sbin/setgid-exe 161end 162# CHECK: sbin/setgid-exe 163 164mkdir stuff 165touch stuff/{ read, write, exec, readwrite, readexec, writeexec, all, none} 166if set -q noacl 167echo "#!/bin/sh" > stuff/exec 168echo "#!/bin/sh" > stuff/readexec 169echo "#!/bin/sh" > stuff/writeexec 170echo "#!/bin/sh" > stuff/all 171end 172chmod 400 stuff/read 173chmod 200 stuff/write 174chmod 100 stuff/exec 175chmod 600 stuff/readwrite 176chmod 500 stuff/readexec 177chmod 300 stuff/writeexec 178chmod 700 stuff/all 179chmod 000 stuff/none 180 181# Validate that globs are sorted. 182test ( path filter stuff/*| path sort| string join"," ) = ( path filter stuff/*| string join"," ) 183 184# echo/CHECK to separate the output of different tests since they all blend 185# together otherwise, making it hard to know which test failed. 186echo "=== test --perm read" 187# CHECK: === test --perm read 188 189if set -q noacl 190# noacl cannot mark files non-readable, filter out known bad files 191path filter --perm read stuff/*| string match -rv".*/(?:write|exec|writeexec|none).*" 192else 193path filter --perm read stuff/* 194end 195# CHECK: stuff/all 196# CHECK: stuff/read 197# CHECK: stuff/readexec 198# CHECK: stuff/readwrite 199 200echo "=== test -r" 201# CHECK: === test -r 202 203if set -q noacl 204path filter -r stuff/*| string match -rv".*/(?:write|exec|writeexec|none).*" 205else 206path filter -r stuff/* 207end 208# CHECK: stuff/all 209# CHECK: stuff/read 210# CHECK: stuff/readexec 211# CHECK: stuff/readwrite 212 213echo "=== test --perm write" 214# CHECK: === test --perm write 215 216path filter --perm write stuff/* 217# CHECK: stuff/all 218# CHECK: stuff/readwrite 219# CHECK: stuff/write 220# CHECK: stuff/writeexec 221 222echo "=== test -w" 223# CHECK: === test -w 224 225path filter -w stuff/* 226# CHECK: stuff/all 227# CHECK: stuff/readwrite 228# CHECK: stuff/write 229# CHECK: stuff/writeexec 230 231echo "=== test --perm exec" 232# CHECK: === test --perm exec 233 234begin 235path filter --perm exec stuff/* 236# Cygwin with ACL doesn't return non-readable files, so add them manually to pass the test 237if __fish_is_cygwin && ! set -q noacl 238echo stuff/exec 239echo stuff/writeexec 240end 241end | sort 242# CHECK: stuff/all 243# CHECK: stuff/exec 244# CHECK: stuff/readexec 245# CHECK: stuff/writeexec 246 247echo "=== test -x" 248# CHECK: === test -x 249 250begin 251path filter -x exec stuff/* 252if __fish_is_cygwin && ! set -q noacl 253echo stuff/exec 254echo stuff/writeexec 255end 256end | sort 257# CHECK: stuff/all 258# CHECK: stuff/exec 259# CHECK: stuff/readexec 260# CHECK: stuff/writeexec 261 262echo "=== test --perm read,write" 263# CHECK: === test --perm read,write 264 265if set -q noacl 266path filter --perm read,write stuff/*| string match -rv".*/(?:write|writeexec).*" 267else 268path filter --perm read,write stuff/* 269end 270# CHECK: stuff/all 271# CHECK: stuff/readwrite 272 273echo "=== test --perm read,exec" 274# CHECK: === test --perm read,exec 275 276if set -q noacl 277path filter --perm read,exec stuff/*| string match -rv".*/(?:exec|writeexec).*" 278else 279path filter --perm read,exec stuff/* 280end 281# CHECK: stuff/all 282# CHECK: stuff/readexec 283 284echo "=== test --perm write,exec" 285# CHECK: === test --perm write,exec 286 287begin 288path filter --perm write,exec stuff/* 289if __fish_is_cygwin && ! set -q noacl 290echo stuff/writeexec 291end 292end | sort 293# CHECK: stuff/all 294# CHECK: stuff/writeexec 295 296echo "=== test --perm read,write,exec" 297# CHECK: === test --perm read,write,exec 298 299if set -q noacl 300path filter --perm read,write,exec stuff/*| string match -rv".*/(?:writeexec).*" 301else 302path filter --perm read,write,exec stuff/* 303end 304# CHECK: stuff/all 305 306echo "=== test all" 307# CHECK: === test all 308 309path filter stuff/* 310# CHECK: stuff/all 311# CHECK: stuff/exec 312# CHECK: stuff/none 313# CHECK: stuff/read 314# CHECK: stuff/readexec 315# CHECK: stuff/readwrite 316# CHECK: stuff/write 317# CHECK: stuff/writeexec 318 319path normalize /usr/bin//../../etc/fish 320# The "//" is squashed and the ".." components neutralize the components before 321# CHECK: /etc/fish 322path normalize /bin//bash 323# The "//" is squashed, but /bin isn't resolved even if your system links it to /usr/bin. 324# CHECK: /bin/bash 325 326# Paths with "-" get a "./": 327path normalize -- -/foo -foo/foo 328# CHECK: ./-/foo 329# CHECK: ./-foo/foo 330path normalize -- ../-foo 331# CHECK: ../-foo 332 333# This goes for filter as well 334touch -- -foo 335path filter -f -- -foo 336# CHECK: ./-foo 337 338# We need to remove the rest of the path because we have no idea what its value looks like. 339if set -q nosymlinks 340echo bin/bash 341else 342path resolve bin//sh| string match -r --'bin/bash$' 343end 344# The "//" is squashed, and the symlink is resolved. 345# sh here is bash 346# CHECK: bin/bash 347 348# "../" cancels out even files. 349path resolve bin//sh/../| string match -r --'bin$' 350# CHECK: bin 351 352# `path resolve` with nonexistent paths 353set -l path( path resolve foo/bar) 354string match -rq"^" ( pwd -P| string escape --style=regex) '/' --$path 355and echo It matches pwd! 356or echo pwd is\' $PWD \' resolved path is\' $path \' 357# CHECK: It matches pwd! 358string replace -r"^" ( pwd -P| string escape --style=regex) '/' "" --$path 359# CHECK: foo/bar 360 361path resolve /banana//terracota/terracota/booooo/../pie 362# CHECK: /banana/terracota/terracota/pie 363 364path sort --key=basename{ def, abc} /{ 456 , 123 , 789 , abc, def, 0 } | path sort --key=dirname -r 365# CHECK: def/0 366# CHECK: def/123 367# CHECK: def/456 368# CHECK: def/789 369# CHECK: def/abc 370# CHECK: def/def 371# CHECK: abc/0 372# CHECK: abc/123 373# CHECK: abc/456 374# CHECK: abc/789 375# CHECK: abc/abc 376# CHECK: abc/def 377 378path sort --unique --key=basename{ def, abc} /{ 456 , 123 , 789 } def/{ abc, def, 0 } abc/{ foo, bar, baz} 379# CHECK: def/0 380# CHECK: def/123 381# CHECK: def/456 382# CHECK: def/789 383# CHECK: def/abc 384# CHECK: abc/bar 385# CHECK: abc/baz 386# CHECK: def/def 387# CHECK: abc/foo 388 389# Symlink loop. 390# It goes brrr. 391if not set -q nosymlinks 392ln -s target link 393ln -s link target 394end 395 396if set -q nosymlinks|| 397test ( path resolve target) = ( pwd -P) /target 398echo target resolves to target 399end 400# CHECK: target resolves to target 401 402if set -q nosymlinks|| 403test ( path resolve link) = ( pwd -P) /link 404echo link resolves to link 405end 406# CHECK: link resolves to link 407 408# path mtime 409# These tests deal with *time*, so we have to account 410# for slow systems (like CI). 411# So we should only test with a lot of slack. 412 413echo bananana>> foo 414test ( math abs( date +%s) -( path mtime foo)) -lt 20 415or echo MTIME IS BOGUS 416 417sleep 2 418 419set -l mtime( path mtime --relative foo) 420test $mtime -ge 1 421or echo mtime is too small 422 423test $mtime -lt 20 424or echo mtime is too large 425 426touch -m -t197001020000 epoch 427set -l epochtime( path mtime epoch) 428# Allow for timezone shenanigans 429test $epochtime -gt 0 -a $epochtime -lt 180000 430or echo Oops not mtime 431 432path basename -Z foo bar baz| path sort 433# CHECK: bar 434# CHECK: baz 435# CHECK: foo 436 437path basename -E foo.txt /usr/local/foo.bar /foo.tar.gz 438# CHECK: foo 439# CHECK: foo 440# CHECK: foo.tar 441 442path basename --null-out bar baz| string escape 443# CHECK: bar\x00baz\x00 444 445path basename --quiet=foo 446# CHECKERR: path basename: --quiet=foo: option does not take an argument 447 448path basename --unknown-option 449# CHECKERR: path basename: --unknown-option: unknown option 450# CHECKERR: {{.*}}/checks/path.fish (line {{\d+}}): 451# CHECKERR: path basename --unknown-option 452# CHECKERR: ^ 453# CHECKERR: (Type 'help path' for related documentation) 454 455path filter -t invalid_type 456# CHECKERR: path filter: Invalid type 'invalid_type' 457 458path filter -p999 459# CHECKERR: path filter: Invalid permission '999' 460 461path sort --relative 462# CHECKERR: path sort: --relative: unknown option 463# CHECKERR: {{.*}}/checks/path.fish (line {{\d+}}): 464# CHECKERR: path sort --relative 465# CHECKERR: ^ 466# CHECKERR: (Type 'help path' for related documentation) 467 468path change-extension 469# CHECKERR: path change-extension: missing argument 470 471echo some.file| path basename other.file 472# CHECKERR: path basename: too many arguments 473 474path sort --key=invalid-key 475# CHECKERR: path sort: Invalid sort key 'invalid-key' 476 477path 478# CHECKERR: path: missing subcommand 479# CHECKERR: {{.*}}/checks/path.fish (line {{\d+}}): 480# CHECKERR: path 481# CHECKERR: ^ 482# CHECKERR: (Type 'help path' for related documentation) 483 484path invalid-subcmd 485# CHECKERR: path invalid-subcmd: invalid subcommand 486# CHECKERR: {{.*}}/checks/path.fish (line {{\d+}}): 487# CHECKERR: path invalid-subcmd 488# CHECKERR: ^ 489# CHECKERR: (Type 'help path' for related documentation)