robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , setup , teardown , pending = 5t . describe , t . it , t . before_each , t . setup , t . teardown , t . pending 6local eq , clear , call , write_file , command =t . eq , n . clear , n . call , t . write_file , n . command 7local eval , fn =n . eval , n . fn 8local is_os =t . is_os 9local pcall_err =t . pcall_err 10 11describe ( 'executable()' , function () 12before_each ( clear ) 13 14it ( 'returns 1 for commands in $PATH' , function () 15local exe =is_os ( 'win' ) and 'ping' or 'ls' 16eq ( 1 , call ( 'executable' , exe )) 17command ( 'let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")' ) 18eq ( 1 , call ( 'executable' , 'null' )) 19eq ( 1 , call ( 'executable' , 'true' )) 20eq ( 1 , call ( 'executable' , 'false' )) 21end ) 22 23it ( 'exepath respects shellslash #13787' , function () 24t . skip ( not is_os ( 'win' ), 'N/A for non-Windows' ) 25-- test/ cannot be a symlink in this test. 26n . api . nvim_set_current_dir ( t . paths . test_source_path ) 27 28command ( 'let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")' ) 29eq (( [[%s\test\functional\fixtures\bin\null.CMD]] ): format ( fn . getcwd ()), fn . exepath ( 'null' )) 30command ( 'set shellslash' ) 31eq (( '%s/test/functional/fixtures/bin/null.CMD' ): format ( fn . getcwd ()), fn . exepath ( 'null' )) 32end ) 33 34it ( 'stdpath returns consistent slashes #13787' , function () 35t . skip ( not is_os ( 'win' ), 'N/A for non-Windows' ) 36-- Needs to check paths relative to repo root dir. 37n . api . nvim_set_current_dir ( t . paths . test_source_path ) 38 39t . matches ( 'build/Xtest_xdg[%w_]*/share/nvim%-data' , fn . stdpath ( 'data' )) 40command ( 'set shellslash' ) 41t . matches ( 'build/Xtest_xdg[%w_]*/share/nvim%-data' , fn . stdpath ( 'data' )) 42end ) 43 44it ( 'fails for invalid values' , function () 45for _ , input in ipairs ( { 'v:null' , 'v:true' , 'v:false' , '{}' , '[]' } ) do 46eq ( 47'Vim(call):E1174: String required for argument 1' , 48pcall_err ( command , 'call executable(' .. input .. ')' ) 49) 50end 51command ( 'let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")' ) 52for _ , input in ipairs ( { 'v:null' , 'v:true' , 'v:false' } ) do 53eq ( 54'Vim(call):E1174: String required for argument 1' , 55pcall_err ( command , 'call executable(' .. input .. ')' ) 56) 57end 58end ) 59 60it ( 'returns 0 for empty strings' , function () 61eq ( 0 , call ( 'executable' , '""' )) 62end ) 63 64it ( 'returns 0 for non-existent files' , function () 65eq ( 0 , call ( 'executable' , 'no_such_file_exists_209ufq23f' )) 66end ) 67 68it ( 'sibling to nvim binary' , function () 69-- Some executable in build/bin/, *not* in $PATH nor CWD. 70local sibling_exe ='printargs-test' 71-- Windows: siblings are in Nvim's "pseudo-$PATH". 72local expected =is_os ( 'win' ) and 1 or 0 73if is_os ( 'win' ) then 74eq ( 'arg1=lemon;arg2=sky;arg3=tree;' , call ( 'system' , sibling_exe .. ' lemon sky tree' )) 75end 76eq ( expected , call ( 'executable' , sibling_exe )) 77end ) 78 79describe ( 'exec-bit' , function () 80setup ( function () 81clear () 82write_file ( 'Xtest_not_executable' , 'non-executable file' ) 83write_file ( 'Xtest_executable' , 'executable file (exec-bit set)' ) 84if not is_os ( 'win' ) then -- N/A for Windows. 85call ( 'system' , { 'chmod' , '-x' , 'Xtest_not_executable' } ) 86call ( 'system' , { 'chmod' , '+x' , 'Xtest_executable' } ) 87end 88end ) 89 90teardown ( function () 91os . remove ( 'Xtest_not_executable' ) 92os . remove ( 'Xtest_executable' ) 93end ) 94 95it ( 'not set' , function () 96eq ( 0 , call ( 'executable' , 'Xtest_not_executable' )) 97eq ( 0 , call ( 'executable' , './Xtest_not_executable' )) 98end ) 99 100it ( 'set, unqualified and not in $PATH' , function () 101eq ( 0 , call ( 'executable' , 'Xtest_executable' )) 102end ) 103 104it ( 'set, qualified as a path' , function () 105local expected =is_os ( 'win' ) and 0 or 1 106eq ( expected , call ( 'executable' , './Xtest_executable' )) 107end ) 108end ) 109end ) 110 111describe ( 'executable() (Windows)' , function () 112if not is_os ( 'win' ) then 113pending ( 'N/A for non-windows' ) 114return 115end 116 117local exts ={ 'bat' , 'exe' , 'com' , 'cmd' } 118setup ( function () 119for _ , ext in ipairs ( exts ) do 120write_file ( 'test_executable_' .. ext .. '.' .. ext , '' ) 121end 122write_file ( 'test_executable_zzz.zzz' , '' ) 123end ) 124 125teardown ( function () 126for _ , ext in ipairs ( exts ) do 127os . remove ( 'test_executable_' .. ext .. '.' .. ext ) 128end 129os . remove ( 'test_executable_zzz.zzz' ) 130end ) 131 132it ( 'tries default extensions on a filename if $PATHEXT is empty' , function () 133-- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd". 134clear ( { env ={ PATHEXT ='' } } ) 135for _ , ext in ipairs ( exts ) do 136eq ( 1 , call ( 'executable' , 'test_executable_' .. ext )) 137end 138eq ( 0 , call ( 'executable' , 'test_executable_zzz' )) 139end ) 140 141it ( 'tries default extensions on a filepath if $PATHEXT is empty' , function () 142-- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd". 143clear ( { env ={ PATHEXT ='' } } ) 144for _ , ext in ipairs ( exts ) do 145eq ( 1 , call ( 'executable' , '.\\test_executable_' .. ext )) 146end 147eq ( 0 , call ( 'executable' , '.\\test_executable_zzz' )) 148end ) 149 150it ( 'system([…]), jobstart([…]) use $PATHEXT #9569' , function () 151-- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd". 152clear ( { env ={ PATHEXT ='' } } ) 153-- Invoking `cmdscript` should find/execute `cmdscript.cmd`. 154eq ( 'much success\n' , call ( 'system' , { 'test/functional/fixtures/cmdscript' } )) 155assert ( 0 < call ( 'jobstart' , { 'test/functional/fixtures/cmdscript' } )) 156end ) 157 158it ( 'full path with extension' , function () 159-- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd". 160clear ( { env ={ PATHEXT ='' } } ) 161-- Some executable we can expect in the test env. 162local exe ='printargs-test' 163local exedir =eval ( "fnamemodify(v:progpath, ':h')" ) 164local exepath =exedir .. '/' .. exe .. '.exe' 165eq ( 1 , call ( 'executable' , exepath )) 166eq ( 'arg1=lemon;arg2=sky;arg3=tree;' , call ( 'system' , exepath .. ' lemon sky tree' )) 167end ) 168 169it ( 'full path without extension' , function () 170-- Empty $PATHEXT defaults to ".com;.exe;.bat;.cmd". 171clear ( { env ={ PATHEXT ='' } } ) 172-- Some executable we can expect in the test env. 173local exe ='printargs-test' 174local exedir =eval ( "fnamemodify(v:progpath, ':h')" ) 175local exepath =exedir .. '/' .. exe 176eq ( 'arg1=lemon;arg2=sky;arg3=tree;' , call ( 'system' , exepath .. ' lemon sky tree' )) 177eq ( 1 , call ( 'executable' , exepath )) 178end ) 179 180it ( 'respects $PATHEXT when trying extensions on a filename' , function () 181clear ( { env ={ PATHEXT ='.zzz' } } ) 182for _ , ext in ipairs ( exts ) do 183eq ( 0 , call ( 'executable' , 'test_executable_' .. ext )) 184end 185eq ( 1 , call ( 'executable' , 'test_executable_zzz' )) 186end ) 187 188it ( 'respects $PATHEXT when trying extensions on a filepath' , function () 189clear ( { env ={ PATHEXT ='.zzz' } } ) 190for _ , ext in ipairs ( exts ) do 191eq ( 0 , call ( 'executable' , '.\\test_executable_' .. ext )) 192end 193eq ( 1 , call ( 'executable' , '.\\test_executable_zzz' )) 194end ) 195 196it ( 'with weird $PATHEXT' , function () 197clear ( { env ={ PATHEXT =';' } } ) 198eq ( 0 , call ( 'executable' , '.\\test_executable_zzz' )) 199clear ( { env ={ PATHEXT =';;;.zzz;;' } } ) 200eq ( 1 , call ( 'executable' , '.\\test_executable_zzz' )) 201end ) 202 203it ( "unqualified filename, Unix-style 'shell'" , function () 204clear ( { env ={ PATHEXT ='' } } ) 205command ( 'set shell=sh' ) 206for _ , ext in ipairs ( exts ) do 207eq ( 0 , call ( 'executable' , 'test_executable_' .. ext .. '.' .. ext )) 208end 209eq ( 0 , call ( 'executable' , 'test_executable_zzz.zzz' )) 210end ) 211 212it ( "relative path, Unix-style 'shell' (backslashes)" , function () 213clear ( { env ={ PATHEXT ='' } } ) 214command ( 'set shell=bash.exe' ) 215for _ , ext in ipairs ( exts ) do 216eq ( 1 , call ( 'executable' , '.\\test_executable_' .. ext .. '.' .. ext )) 217eq ( 1 , call ( 'executable' , './test_executable_' .. ext .. '.' .. ext )) 218end 219eq ( 1 , call ( 'executable' , '.\\test_executable_zzz.zzz' )) 220eq ( 1 , call ( 'executable' , './test_executable_zzz.zzz' )) 221end ) 222 223it ( 'unqualified filename, $PATHEXT contains dot' , function () 224clear ( { env ={ PATHEXT ='.;.zzz' } } ) 225for _ , ext in ipairs ( exts ) do 226eq ( 1 , call ( 'executable' , 'test_executable_' .. ext .. '.' .. ext )) 227end 228eq ( 1 , call ( 'executable' , 'test_executable_zzz.zzz' )) 229clear ( { env ={ PATHEXT ='.zzz;.' } } ) 230for _ , ext in ipairs ( exts ) do 231eq ( 1 , call ( 'executable' , 'test_executable_' .. ext .. '.' .. ext )) 232end 233eq ( 1 , call ( 'executable' , 'test_executable_zzz.zzz' )) 234end ) 235 236it ( 'relative path, $PATHEXT contains dot (backslashes)' , function () 237clear ( { env ={ PATHEXT ='.;.zzz' } } ) 238for _ , ext in ipairs ( exts ) do 239eq ( 1 , call ( 'executable' , '.\\test_executable_' .. ext .. '.' .. ext )) 240eq ( 1 , call ( 'executable' , './test_executable_' .. ext .. '.' .. ext )) 241end 242eq ( 1 , call ( 'executable' , '.\\test_executable_zzz.zzz' )) 243eq ( 1 , call ( 'executable' , './test_executable_zzz.zzz' )) 244end ) 245 246it ( 'ignores case of extension' , function () 247clear ( { env ={ PATHEXT ='.ZZZ' } } ) 248eq ( 1 , call ( 'executable' , 'test_executable_zzz.zzz' )) 249end ) 250 251it ( 'relative path does not search $PATH' , function () 252clear ( { env ={ PATHEXT ='' } } ) 253eq ( 0 , call ( 'executable' , './System32/notepad.exe' )) 254eq ( 0 , call ( 'executable' , '.\\System32\\notepad.exe' )) 255eq ( 0 , call ( 'executable' , '../notepad.exe' )) 256eq ( 0 , call ( 'executable' , '..\\notepad.exe' )) 257end ) 258end )