robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4 5local cimport =t . cimport 6local eq =t . eq 7local neq =t . neq 8local ffi =t . ffi 9local cstr =t . cstr 10local to_cstr =t . to_cstr 11local NULL =t . NULL 12local OK =0 13 14local cimp =cimport ( './src/nvim/os/os.h' ) 15 16describe ( 'env.c' , function () 17local function os_env_exists ( name , nonempty ) 18return cimp . os_env_exists ( to_cstr ( name ), nonempty ) 19end 20 21local function os_setenv ( name , value , override ) 22return cimp . os_setenv ( to_cstr ( name ), to_cstr ( value ), override ) 23end 24 25local function os_unsetenv ( name ) 26return cimp . os_unsetenv ( to_cstr ( name )) 27end 28 29local function os_getenv ( name ) 30local rval =cimp . os_getenv ( to_cstr ( name )) 31if rval ~= NULL then 32return ffi . string ( rval ) 33else 34return NULL 35end 36end 37 38local function os_getenv_buf ( name , buf , bufsize ) 39local rval =cimp . os_getenv_buf ( to_cstr ( name ), buf , bufsize ) 40if rval ~= NULL then 41return ffi . string ( rval ) 42else 43return NULL 44end 45end 46 47local function os_getenv_noalloc ( name ) 48local rval =cimp . os_getenv_noalloc ( to_cstr ( name )) 49if rval ~= NULL then 50return ffi . string ( rval ) 51else 52return NULL 53end 54end 55 56itp ( 'os_env_exists(..., false)' , function () 57eq ( false , os_env_exists ( '' , false )) 58eq ( false , os_env_exists ( ' ' , false )) 59eq ( false , os_env_exists ( '\t' , false )) 60eq ( false , os_env_exists ( '\n' , false )) 61eq ( false , os_env_exists ( 'AaあB <= very weird name...' , false )) 62 63local varname ='NVIM_UNIT_TEST_os_env_exists' 64eq ( false , os_env_exists ( varname , false )) 65eq ( OK , os_setenv ( varname , 'foo bar baz ...' , 1 )) 66eq ( true , os_env_exists ( varname , false )) 67eq ( OK , os_setenv ( varname , 'f' , 1 )) 68eq ( true , os_env_exists ( varname , true )) 69end ) 70 71itp ( 'os_env_exists(..., true)' , function () 72eq ( false , os_env_exists ( '' , true )) 73eq ( false , os_env_exists ( ' ' , true )) 74eq ( false , os_env_exists ( '\t' , true )) 75eq ( false , os_env_exists ( '\n' , true )) 76eq ( false , os_env_exists ( 'AaあB <= very weird name...' , true )) 77 78local varname ='NVIM_UNIT_TEST_os_env_defined' 79eq ( false , os_env_exists ( varname , true )) 80eq ( OK , os_setenv ( varname , '' , 1 )) 81eq ( false , os_env_exists ( varname , true )) 82eq ( OK , os_setenv ( varname , 'foo bar baz ...' , 1 )) 83eq ( true , os_env_exists ( varname , true )) 84eq ( OK , os_setenv ( varname , 'f' , 1 )) 85eq ( true , os_env_exists ( varname , true )) 86end ) 87 88describe ( 'os_setenv' , function () 89itp ( 'sets an env var and returns success' , function () 90local name ='NVIM_UNIT_TEST_SETENV_1N' 91local value ='NVIM_UNIT_TEST_SETENV_1V' 92eq ( nil , os . getenv ( name )) 93eq ( OK , os_setenv ( name , value , 1 )) 94eq ( value , os . getenv ( name )) 95 96-- Set empty, then set non-empty, then retrieve. 97eq ( OK , os_setenv ( name , '' , 1 )) 98eq ( '' , os . getenv ( name )) 99eq ( OK , os_setenv ( name , 'non-empty' , 1 )) 100eq ( 'non-empty' , os . getenv ( name )) 101end ) 102 103itp ( '`overwrite` behavior' , function () 104local name ='NVIM_UNIT_TEST_SETENV_2N' 105local value ='NVIM_UNIT_TEST_SETENV_2V' 106local value_updated ='NVIM_UNIT_TEST_SETENV_2V_UPDATED' 107eq ( OK , os_setenv ( name , value , 0 )) 108eq ( value , os . getenv ( name )) 109eq ( OK , os_setenv ( name , value_updated , 0 )) 110eq ( value , os . getenv ( name )) 111eq ( OK , os_setenv ( name , value_updated , 1 )) 112eq ( value_updated , os . getenv ( name )) 113end ) 114end ) 115 116describe ( 'os_setenv_append_path' , function () 117itp ( 'appends :/foo/bar to $PATH' , function () 118local original_path =os . getenv ( 'PATH' ) 119eq ( true , cimp . os_setenv_append_path ( to_cstr ( '/foo/bar/baz.exe' ))) 120eq ( original_path .. ':/foo/bar' , os . getenv ( 'PATH' )) 121end ) 122 123itp ( 'avoids redundant separator when appending to $PATH #7377' , function () 124os_setenv ( 'PATH' , '/a/b/c:' , true ) 125eq ( true , cimp . os_setenv_append_path ( to_cstr ( '/foo/bar/baz.exe' ))) 126-- Must not have duplicate separators. #7377 127eq ( '/a/b/c:/foo/bar' , os . getenv ( 'PATH' )) 128end ) 129 130itp ( 'returns false if `fname` is not absolute' , function () 131local original_path =os . getenv ( 'PATH' ) 132eq ( false , cimp . os_setenv_append_path ( to_cstr ( 'foo/bar/baz.exe' ))) 133eq ( original_path , os . getenv ( 'PATH' )) 134end ) 135end ) 136 137describe ( 'os_shell_is_cmdexe' , function () 138itp ( 'returns true for expected names' , function () 139eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( 'cmd.exe' ))) 140eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( 'cmd' ))) 141eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( 'CMD.EXE' ))) 142eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( 'CMD' ))) 143 144os_setenv ( 'COMSPEC' , '/foo/bar/cmd.exe' , 0 ) 145eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( '$COMSPEC' ))) 146os_setenv ( 'COMSPEC' , [[C:\system32\cmd.exe]] , 0 ) 147eq ( true , cimp . os_shell_is_cmdexe ( to_cstr ( '$COMSPEC' ))) 148end ) 149itp ( 'returns false for unexpected names' , function () 150eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( '' ))) 151eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( 'powershell' ))) 152eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( ' cmd.exe ' ))) 153eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( 'cm' ))) 154eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( 'md' ))) 155eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( 'cmd.ex' ))) 156 157os_setenv ( 'COMSPEC' , '/foo/bar/cmd' , 0 ) 158eq ( false , cimp . os_shell_is_cmdexe ( to_cstr ( '$COMSPEC' ))) 159end ) 160end ) 161 162describe ( 'os_getenv' , function () 163itp ( 'reads an env var' , function () 164local name ='NVIM_UNIT_TEST_GETENV_1N' 165local value ='NVIM_UNIT_TEST_GETENV_1V' 166eq ( NULL , os_getenv ( name )) 167-- Use os_setenv because Lua doesn't have setenv. 168os_setenv ( name , value , 1 ) 169eq ( value , os_getenv ( name )) 170 171-- Shortest non-empty value 172os_setenv ( name , 'z' , 1 ) 173eq ( 'z' , os_getenv ( name )) 174 175-- Get a big value. 176local bigval =( 'x' ): rep ( 256 ) 177eq ( OK , os_setenv ( name , bigval , 1 )) 178eq ( bigval , os_getenv ( name )) 179 180-- Set non-empty, then set empty. 181eq ( OK , os_setenv ( name , 'non-empty' , 1 )) 182eq ( 'non-empty' , os_getenv ( name )) 183eq ( OK , os_setenv ( name , '' , 1 )) 184eq ( NULL , os_getenv ( name )) 185end ) 186 187itp ( 'returns NULL if the env var is not found' , function () 188eq ( NULL , os_getenv ( 'NVIM_UNIT_TEST_GETENV_NOTFOUND' )) 189end ) 190end ) 191 192describe ( 'os_getenv_buf' , function () 193itp ( 'reads an env var into given buffer' , function () 194local name ='NVIM_UNIT_TEST_GETENV_1N' 195local value ='NVIM_UNIT_TEST_GETENV_1V' 196local bufsize =200 197local buf =cstr ( bufsize , '' ) 198eq ( NULL , os_getenv_buf ( name , buf , bufsize )) 199-- Use os_setenv because Lua doesn't have setenv. 200os_setenv ( name , value , 1 ) 201eq ( value , os_getenv_buf ( name , buf , bufsize )) 202 203-- Shortest non-empty value 204os_setenv ( name , 'z' , 1 ) 205eq ( 'z' , os_getenv_buf ( name , buf , bufsize )) 206 207-- Variable size above `bufsize` gets truncated 208local verybigval =( 'y' ): rep ( bufsize + 10 ) 209local trunc =string . sub ( verybigval , 0 , bufsize - 1 ) 210eq ( OK , os_setenv ( name , verybigval , 1 )) 211eq ( trunc , os_getenv_buf ( name , buf , bufsize )) 212end ) 213end ) 214 215describe ( 'os_getenv_noalloc' , function () 216itp ( 'reads an env var without memory allocation' , function () 217local name ='NVIM_UNIT_TEST_GETENV_1N' 218local value ='NVIM_UNIT_TEST_GETENV_1V' 219eq ( NULL , os_getenv_noalloc ( name )) 220-- Use os_setenv because Lua doesn't have setenv. 221os_setenv ( name , value , 1 ) 222eq ( value , os_getenv_noalloc ( name )) 223 224-- Shortest non-empty value 225os_setenv ( name , 'z' , 1 ) 226eq ( 'z' , os_getenv_noalloc ( name )) 227 228local bigval =( 'x' ): rep ( 256 ) 229eq ( OK , os_setenv ( name , bigval , 1 )) 230eq ( bigval , os_getenv_noalloc ( name )) 231 232-- Variable size above NameBuff size gets truncated 233-- This assumes MAXPATHL is 4096 bytes. 234local verybigval =( 'y' ): rep ( 4096 ) 235local trunc =string . sub ( verybigval , 0 , 4095 ) 236eq ( OK , os_setenv ( name , verybigval , 1 )) 237eq ( trunc , os_getenv_noalloc ( name )) 238 239-- Set non-empty, then set empty. 240eq ( OK , os_setenv ( name , 'non-empty' , 1 )) 241eq ( 'non-empty' , os_getenv_noalloc ( name )) 242eq ( OK , os_setenv ( name , '' , 1 )) 243eq ( NULL , os_getenv_noalloc ( name )) 244end ) 245 246itp ( 'returns NULL if the env var is not found' , function () 247eq ( NULL , os_getenv_noalloc ( 'NVIM_UNIT_TEST_GETENV_NOTFOUND' )) 248end ) 249end ) 250 251itp ( 'os_unsetenv' , function () 252local name ='TEST_UNSETENV' 253local value ='TESTVALUE' 254os_setenv ( name , value , 1 ) 255eq ( OK , os_unsetenv ( name )) 256neq ( value , os_getenv ( name )) 257-- Depending on the platform the var might be unset or set as '' 258t . ok ( os_getenv ( name ) == nil or os_getenv ( name ) == '' ) 259if os_getenv ( name ) == nil then 260eq ( false , os_env_exists ( name , false )) 261end 262end ) 263 264describe ( 'os_getenvname_at_index' , function () 265itp ( 'returns names of environment variables' , function () 266local test_name ='NVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1N' 267local test_value ='NVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1V' 268os_setenv ( test_name , test_value , 1 ) 269local i =0 270local names ={} 271local found_name =false 272local name =cimp . os_getenvname_at_index ( i ) 273while name ~= NULL do 274table . insert ( names , ffi . string ( name )) 275if ( ffi . string ( name )) == test_name then 276found_name =true 277end 278i =i + 1 279name =cimp . os_getenvname_at_index ( i ) 280end 281eq ( true , # names > 0 ) 282eq ( true , found_name ) 283end ) 284 285itp ( 'returns NULL if the index is out of bounds' , function () 286local huge =ffi . new ( 'size_t' , 10000 ) 287local maxuint32 =ffi . new ( 'size_t' , 4294967295 ) 288eq ( NULL , cimp . os_getenvname_at_index ( huge )) 289eq ( NULL , cimp . os_getenvname_at_index ( maxuint32 )) 290 291if ffi . abi ( '64bit' ) then 292-- couldn't use a bigger number because it gets converted to 293-- double somewhere, should be big enough anyway 294-- maxuint64 = ffi.new 'size_t', 18446744073709551615 295local maxuint64 =ffi . new ( 'size_t' , 18446744073709000000 ) 296eq ( NULL , cimp . os_getenvname_at_index ( maxuint64 )) 297end 298end ) 299end ) 300 301describe ( 'os_get_pid' , function () 302itp ( 'returns the process ID' , function () 303local stat_file =io . open ( '/proc/self/stat' ) 304if stat_file then 305local stat_str =stat_file : read ( '*l' ) 306stat_file : close () 307local pid =tonumber (( stat_str : match ( '%d+' ))) 308eq ( pid , tonumber ( cimp . os_get_pid ())) 309else 310-- /proc is not available on all systems, test if pid is nonzero. 311eq ( true , ( cimp . os_get_pid () > 0 )) 312end 313end ) 314end ) 315 316describe ( 'os_get_hostname' , function () 317itp ( 'returns the hostname' , function () 318local handle =io . popen ( 'hostname' ) 319local hostname =handle : read ( '*l' ) 320handle : close () 321local hostname_buf =cstr ( 255 , '' ) 322cimp . os_get_hostname ( hostname_buf , 255 ) 323eq ( hostname , ( ffi . string ( hostname_buf ))) 324end ) 325end ) 326 327describe ( 'expand_env_esc' , function () 328itp ( 'expands environment variables' , function () 329local name ='NVIM_UNIT_TEST_EXPAND_ENV_ESCN' 330local value ='NVIM_UNIT_TEST_EXPAND_ENV_ESCV' 331os_setenv ( name , value , 1 ) 332-- TODO(bobtwinkles) This only tests Unix expansions. There should be a 333-- test for Windows as well 334local input1 =to_cstr ( '$NVIM_UNIT_TEST_EXPAND_ENV_ESCN/test' ) 335local input2 =to_cstr ( '${NVIM_UNIT_TEST_EXPAND_ENV_ESCN}/test' ) 336local output_buff1 =cstr ( 255 , '' ) 337local output_buff2 =cstr ( 255 , '' ) 338local output_expected ='NVIM_UNIT_TEST_EXPAND_ENV_ESCV/test' 339cimp . expand_env_esc ( input1 , output_buff1 , 255 , NULL , true , NULL ) 340cimp . expand_env_esc ( input2 , output_buff2 , 255 , NULL , true , NULL ) 341eq ( output_expected , ffi . string ( output_buff1 )) 342eq ( output_expected , ffi . string ( output_buff2 )) 343end ) 344 345itp ( 'expands ~ once when `one` is true' , function () 346local input ='~/foo ~ foo' 347local homedir =cstr ( 255 , '' ) 348cimp . expand_env_esc ( to_cstr ( '~' ), homedir , 255 , NULL , true , NULL ) 349local output_expected =ffi . string ( homedir ) .. '/foo ~ foo' 350local output =cstr ( 255 , '' ) 351cimp . expand_env_esc ( to_cstr ( input ), output , 255 , NULL , true , NULL ) 352eq ( ffi . string ( output ), ffi . string ( output_expected )) 353end ) 354 355itp ( 'expands ~ every time when `one` is false' , function () 356local input =to_cstr ( '~/foo ~ foo' ) 357local dst =cstr ( 255 , '' ) 358cimp . expand_env_esc ( to_cstr ( '~' ), dst , 255 , NULL , true , NULL ) 359local homedir =ffi . string ( dst ) 360local output_expected =homedir .. '/foo ' .. homedir .. ' foo' 361local output =cstr ( 255 , '' ) 362cimp . expand_env_esc ( input , output , 255 , NULL , false , NULL ) 363eq ( output_expected , ffi . string ( output )) 364end ) 365 366itp ( 'does not crash #3725' , function () 367local name_out =ffi . new ( 'char[100]' ) 368cimp . os_get_username ( name_out , 100 ) 369local curuser =ffi . string ( name_out ) 370 371local src = 372to_cstr ( '~' .. curuser .. '/Vcs/django-rest-framework/rest_framework/renderers.py' ) 373local dst =cstr ( 256 , '~' .. curuser ) 374cimp . expand_env_esc ( src , dst , 256 , NULL , false , NULL ) 375local len =string . len ( ffi . string ( dst )) 376t . ok ( len > 56 ) 377t . ok ( len < 256 ) 378end ) 379 380itp ( 'respects `dstlen` without expansion' , function () 381local input =to_cstr ( 'this is a very long thing that will not fit' ) 382-- The buffer is long enough to actually contain the full input in case the 383-- test fails, but we don't tell expand_env_esc that 384local output =cstr ( 255 , '' ) 385cimp . expand_env_esc ( input , output , 5 , NULL , true , NULL ) 386-- Make sure the first few characters are copied properly and that there is a 387-- terminating null character 388for i =0 , 3 do 389eq ( input [ i ], output [ i ]) 390end 391eq ( 0 , output [ 4 ]) 392end ) 393 394itp ( 'respects `dstlen` with expansion' , function () 395local varname =to_cstr ( 'NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENN' ) 396local varval =to_cstr ( 'NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENV' ) 397cimp . os_setenv ( varname , varval , 1 ) 398-- TODO(bobtwinkles) This test uses unix-specific environment variable accessing, 399-- should have some alternative for windows 400local input =to_cstr ( '$NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENN/even more stuff' ) 401-- The buffer is long enough to actually contain the full input in case the 402-- test fails, but we don't tell expand_env_esc that 403local output =cstr ( 255 , '' ) 404cimp . expand_env_esc ( input , output , 5 , NULL , true , NULL ) 405-- Make sure the first few characters are copied properly and that there is a 406-- terminating null character 407-- expand_env_esc SHOULD NOT expand the variable if there is not enough space to 408-- contain the result 409for i =0 , 3 do 410eq ( input [ i ], output [ i ]) 411end 412eq ( 0 , output [ 4 ]) 413end ) 414end ) 415end )