robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f0a0182285
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , setup , teardown = 5t . describe , t . it , t . before_each , t . setup , t . teardown 6local clear =n . clear 7local eq =t . eq 8local exec_lua =n . exec_lua 9local fnamemodify =n . fn . fnamemodify 10local getcwd =n . fn . getcwd 11local command =n . command 12local poke_eventloop =n . poke_eventloop 13local write_file =t . write_file 14local is_os =t . is_os 15local chdir =n . fn . chdir 16 17describe ( 'fnamemodify()' , function () 18setup ( function () 19write_file ( 'Xtest-fnamemodify.txt' , [[foobar]] ) 20t . mkdir ( 'foo' ) 21write_file ( 'foo/bar' , [[bar]] ) 22end ) 23 24before_each ( clear ) 25 26teardown ( function () 27os . remove ( 'Xtest-fnamemodify.txt' ) 28n . rmdir ( 'foo' ) 29end ) 30 31it ( 'root path' , function () 32local root =assert ( t . fix_slashes ( n . pathroot ())) 33eq ( root , fnamemodify ( [[/]] , ':p:h' )) 34eq ( root , fnamemodify ( [[/]] , ':p' )) 35if is_os ( 'win' ) then 36eq ( root , fnamemodify ( [[\]] , ':p:h' )) 37eq ( root , fnamemodify ( [[\]] , ':p' )) 38command ( 'set shellslash' ) 39eq ( root , fnamemodify ( [[\]] , ':p:h' )) 40eq ( root , fnamemodify ( [[\]] , ':p' )) 41eq ( root , fnamemodify ( [[/]] , ':p:h' )) 42eq ( root , fnamemodify ( [[/]] , ':p' )) 43 44local letter_colon =root : sub ( 1 , 2 ) 45local old_dir =( '%s/' ): format ( getcwd ()) 46local foo_dir =old_dir .. 'foo/' 47eq ( old_dir , fnamemodify ( letter_colon , ':p' )) 48eq ( old_dir , fnamemodify ( letter_colon .. '.' , ':p' )) 49eq ( old_dir , fnamemodify ( letter_colon .. './' , ':p' )) 50eq ( foo_dir , fnamemodify ( letter_colon .. './foo' , ':p' )) 51eq ( foo_dir , fnamemodify ( letter_colon .. 'foo' , ':p' )) 52chdir ( 'foo' ) 53eq ( old_dir , fnamemodify ( letter_colon .. '..' , ':p' )) 54eq ( old_dir , fnamemodify ( letter_colon .. '../' , ':p' )) 55eq ( foo_dir .. 'bar' , fnamemodify ( letter_colon .. 'bar' , ':p' )) 56end 57end ) 58 59it ( '~user in a fast event #40707' , function () 60exec_lua ( [[ 61local timer = assert(vim.uv.new_timer()) 62timer:start(0, 0, function() 63timer:close() 64assert(vim.in_fast_event()) 65_G.result = vim.fn.fnamemodify('~neovim_user_not_found_test/', ':p') 66end) 67]] ) 68poke_eventloop () 69local expected ='~neovim_user_not_found_test/' 70if is_os ( 'win' ) then 71-- Windows never expands ~user, ':p' just prefixes the cwd. 72expected =fnamemodify ( '.' , ':p' ) .. expected 73end 74eq ( expected , exec_lua ( 'return _G.result' )) 75end ) 76 77it ( ':8 (DOS 8.3 short format)' , function () 78eq ( 'Xtest-fnamemodify.txt' , fnamemodify ( [[Xtest-fnamemodify.txt]] , ':8' )) 79end ) 80 81it ( 'examples from ":help filename-modifiers"' , function () 82-- src/ cannot be a symlink in this test. 83n . api . nvim_set_current_dir ( t . paths . test_source_path ) 84 85local filename ='src/version.c' 86local cwd =vim . fs . normalize ( getcwd ()) 87 88eq (( '%s/src/version.c' ): format ( cwd ), fnamemodify ( filename , ':p' )) 89 90eq ( 'src/version.c' , fnamemodify ( filename , ':p:.' )) 91eq (( '%s/src' ): format ( cwd ), fnamemodify ( filename , ':p:h' )) 92eq ( cwd , fnamemodify ( filename , ':p:h:h' )) 93eq ( 'version.c' , fnamemodify ( filename , ':p:t' )) 94eq (( '%s/src/version' ): format ( cwd ), fnamemodify ( filename , ':p:r' )) 95 96eq (( '%s/src/main.c' ): format ( cwd ), fnamemodify ( filename , ':s?version?main?:p' )) 97 98local converted_cwd =cwd : gsub ( '/' , '\\' ) 99eq ( converted_cwd .. '\\src\\version.c' , fnamemodify ( filename , ':p:gs?/?\\\\?' )) 100 101eq ( 'src' , fnamemodify ( filename , ':h' )) 102eq ( 'version.c' , fnamemodify ( filename , ':t' )) 103eq ( 'src/version' , fnamemodify ( filename , ':r' )) 104eq ( 'version' , fnamemodify ( filename , ':t:r' )) 105eq ( 'c' , fnamemodify ( filename , ':e' )) 106 107eq ( 'src/main.c' , fnamemodify ( filename , ':s?version?main?' )) 108end ) 109 110it ( 'advanced examples from ":help filename-modifiers"' , function () 111local filename ='src/version.c.gz' 112 113eq ( 'gz' , fnamemodify ( filename , ':e' )) 114eq ( 'c.gz' , fnamemodify ( filename , ':e:e' )) 115eq ( 'c.gz' , fnamemodify ( filename , ':e:e:e' )) 116 117eq ( 'c' , fnamemodify ( filename , ':e:e:r' )) 118 119eq ( 'src/version.c' , fnamemodify ( filename , ':r' )) 120eq ( 'c' , fnamemodify ( filename , ':r:e' )) 121 122eq ( 'src/version' , fnamemodify ( filename , ':r:r' )) 123eq ( 'src/version' , fnamemodify ( filename , ':r:r:r' )) 124end ) 125 126it ( ':h' , function () 127-- generic path 128eq ( '.' , fnamemodify ( 'hello.txt' , ':h' )) 129-- Repeated ":h" on a bare name: the first ":h" replaces the buffer with ".", the 130-- next must re-anchor into it and not read the freed buffer (Coverity CID 649200). 131eq ( '.' , fnamemodify ( 'hello.txt' , ':h:h' )) 132eq ( '.' , fnamemodify ( 'hello.txt' , ':h:h:h' )) 133eq ( 'path/to' , fnamemodify ( 'path/to/hello.txt' , ':h' )) 134eq ( '/' , fnamemodify ( '/' , ':h' )) 135eq ( '/' , fnamemodify ( '/foo' , ':h' )) 136 137-- collapses more than two leading slashes into a single slash 138eq ( '/' , fnamemodify ( '///' , ':h' )) 139eq ( '/' , fnamemodify ( '////' , ':h' )) 140eq ( '/' , fnamemodify ( '///foo' , ':h' )) 141eq ( '/foo' , fnamemodify ( '///foo/bar' , ':h' )) 142eq ( '/foo' , fnamemodify ( '///foo////bar' , ':h' )) 143eq ( '/foo' , fnamemodify ( '////foo/bar' , ':h' )) 144 145-- preserves exactly two leading slashes 146eq ( '//' , fnamemodify ( '//' , ':h' )) 147 148if not is_os ( 'win' ) then 149-- POSIX permits special handling for exactly two leading slashes. 150eq ( '//' , fnamemodify ( '//' , ':h' )) 151eq ( '//' , fnamemodify ( '//foo' , ':h' )) 152eq ( '//foo' , fnamemodify ( '//foo/bar' , ':h' )) 153eq ( '//foo' , fnamemodify ( '//foo///bar' , ':h' )) 154else 155eq ( '//server' , fnamemodify ( '//server' , ':h' )) 156eq ( '//server/share' , fnamemodify ( '//server/share' , ':h' )) 157eq ( '//server/share/' , fnamemodify ( '//server/share/' , ':h' )) 158eq ( '//server///share' , fnamemodify ( '//server///share' , ':h' )) 159eq ( '//server/share/' , fnamemodify ( '//server/share/foo' , ':h' )) 160 161eq ( [[//foo/C$]] , fnamemodify ( [[\\foo\C$]] , ':h' )) 162eq ( [[//foo/C$/]] , fnamemodify ( [[\\foo\C$\]] , ':h' )) 163eq ( [[//foo/C$/]] , fnamemodify ( [[\\foo\C$\bar]] , ':h' )) 164eq ( [[//foo/C$/]] , fnamemodify ( [[\\foo\C$\bar]] , ':h:h' )) 165eq ( [[//foo/C$/]] , fnamemodify ( [[//foo\C$/bar]] , ':h' )) 166-- `C$` is a share name, not a file name 167eq ( '' , fnamemodify ( '//foo/C$/bar' , ':h:t' )) 168 169eq ( 'C:' , fnamemodify ( 'C:foo' , ':h' )) 170eq ( 'C:/' , fnamemodify ( 'C:/foo' , ':h' )) 171 172eq ( '//?/C:/' , fnamemodify ( '//?/C:/' , ':h' )) 173eq ( '//?/C:/' , fnamemodify ( '//?/C:/foo' , ':h' )) 174eq ( 175'//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/' , 176fnamemodify ( '//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/foo' , ':h' ) 177) 178eq ( 179'//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/' , 180fnamemodify ( '///?/Volume{b75e2c83-0000-0000-0000-602f00000000}/foo' , ':h' ) 181) 182eq ( '//?/UNC/server' , fnamemodify ( '//?/UNC/server' , ':h' )) 183eq ( '//?/UNC/server/share' , fnamemodify ( '//?/UNC/server/share' , ':h' )) 184eq ( '//?/UNC/server/share/' , fnamemodify ( '//?/UNC/server/share/foo' , ':h' )) 185 186-- relative "./" and ".\" prefixes 187eq ( './path/to' , fnamemodify ( './path/to/hello.txt' , ':h' )) 188eq ( './path' , fnamemodify ( './path/to/hello.txt' , ':h:h' )) 189eq ( './path/to' , fnamemodify ( [[.\path\to\hello.txt]] , ':h' )) 190end 191end ) 192 193it ( ':t' , function () 194eq ( 'hello.txt' , fnamemodify ( 'hello.txt' , ':t' )) 195eq ( 'hello.txt' , fnamemodify ( 'path/to/hello.txt' , ':t' )) 196end ) 197 198it ( ':r' , function () 199eq ( 'hello' , fnamemodify ( 'hello.txt' , ':r' )) 200eq ( 'path/to/hello' , fnamemodify ( 'path/to/hello.txt' , ':r' )) 201end ) 202 203it ( ':e' , function () 204eq ( 'txt' , fnamemodify ( 'hello.txt' , ':e' )) 205eq ( 'txt' , fnamemodify ( 'path/to/hello.txt' , ':e' )) 206end ) 207 208it ( ':~' , function () 209local cwd =vim . fs . normalize ( fnamemodify ( '.' , ':p' )) 210local full_path =( '%s/foo' ): format ( cwd ) 211 212command (( [[let $HOME='%s']] ): format ( cwd )) 213eq ( '~/foo' , fnamemodify ( full_path , ':~' )) 214 215command (( [[let $HOME='%s/']] ): format ( cwd )) -- a trailing slash 216-- `init_home` calls `os_realpath` on Unix, which removes the trailing slash 217eq ( is_os ( 'win' ) and full_path or '~/foo' , fnamemodify ( full_path , ':~' )) 218end ) 219 220it ( 'regex replacements' , function () 221eq ( 'content-there-here.txt' , fnamemodify ( 'content-here-here.txt' , ':s/here/there/' )) 222eq ( 'content-there-there.txt' , fnamemodify ( 'content-here-here.txt' , ':gs/here/there/' )) 223end ) 224 225it ( 'shell escape' , function () 226local expected 227 228if is_os ( 'win' ) then 229-- we expand with double-quotes on Windows 230expected =[["hello there! quote ' newline]] .. '\n' .. [["]] 231else 232expected =[['hello there! quote '\'' newline]] .. '\n' .. [[']] 233end 234 235eq ( expected , fnamemodify ( "hello there! quote ' newline\n" , ':S' )) 236end ) 237 238it ( 'can combine :e and :r' , function () 239-- simple, single extension filename 240eq ( 'c' , fnamemodify ( 'a.c' , ':e' )) 241eq ( 'c' , fnamemodify ( 'a.c' , ':e:e' )) 242eq ( 'c' , fnamemodify ( 'a.c' , ':e:e:r' )) 243eq ( 'c' , fnamemodify ( 'a.c' , ':e:e:r:r' )) 244 245-- multi extension filename 246eq ( 'rb' , fnamemodify ( 'a.spec.rb' , ':e:r' )) 247eq ( 'rb' , fnamemodify ( 'a.spec.rb' , ':e:r:r' )) 248 249eq ( 'spec' , fnamemodify ( 'a.spec.rb' , ':e:e:r' )) 250eq ( 'spec' , fnamemodify ( 'a.spec.rb' , ':e:e:r:r' )) 251 252eq ( 'spec' , fnamemodify ( 'a.b.spec.rb' , ':e:e:r' )) 253eq ( 'b.spec' , fnamemodify ( 'a.b.spec.rb' , ':e:e:e:r' )) 254eq ( 'b' , fnamemodify ( 'a.b.spec.rb' , ':e:e:e:r:r' )) 255 256eq ( 'spec' , fnamemodify ( 'a.b.spec.rb' , ':r:e' )) 257eq ( 'b' , fnamemodify ( 'a.b.spec.rb' , ':r:r:e' )) 258 259-- extraneous :e expansions 260eq ( 'c' , fnamemodify ( 'a.b.c.d.e' , ':r:r:e' )) 261eq ( 'b.c' , fnamemodify ( 'a.b.c.d.e' , ':r:r:e:e' )) 262 263-- :e never includes the whole filename, so "a.b":e:e:e --> "b" 264eq ( 'b.c' , fnamemodify ( 'a.b.c.d.e' , ':r:r:e:e:e' )) 265eq ( 'b.c' , fnamemodify ( 'a.b.c.d.e' , ':r:r:e:e:e:e' )) 266end ) 267end )