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 ffi =t . ffi 8local lib =t . lib 9local NULL =t . NULL 10local OK =t . OK 11local FAIL =t . FAIL 12 13local users =cimport ( './src/nvim/os/os.h' , 'unistd.h' ) 14 15local function garray_new () 16return ffi . new ( 'garray_T[1]' ) 17end 18 19local function garray_get_len ( array ) 20return array [ 0 ]. ga_len 21end 22 23local function garray_get_item ( array , index ) 24return ( ffi . cast ( 'void **' , array [ 0 ]. ga_data ))[ index ] 25end 26 27describe ( 'users function' , function () 28-- will probably not work on windows 29local current_username =os . getenv ( 'USER' ) 30 31describe ( 'os_get_usernames' , function () 32itp ( 'returns FAIL if called with NULL' , function () 33eq ( FAIL , users . os_get_usernames ( NULL )) 34end ) 35 36itp ( 'fills the names garray with os usernames and returns OK' , function () 37local ga_users =garray_new () 38eq ( OK , users . os_get_usernames ( ga_users )) 39local user_count =garray_get_len ( ga_users ) 40t . ok ( user_count > 0 ) 41local current_username_found =false 42for i =0 , user_count - 1 do 43local name =ffi . string (( garray_get_item ( ga_users , i ))) 44if name == current_username then 45current_username_found =true 46end 47end 48t . ok ( current_username_found ) 49end ) 50end ) 51 52describe ( 'os_get_username' , function () 53itp ( 'should write the username into the buffer and return OK' , function () 54local name_out =ffi . new ( 'char[100]' ) 55eq ( OK , users . os_get_username ( name_out , 100 )) 56eq ( current_username , ffi . string ( name_out )) 57end ) 58end ) 59 60describe ( 'os_get_uname' , function () 61itp ( 'should write the username into the buffer and return OK' , function () 62local name_out =ffi . new ( 'char[100]' ) 63local user_id =lib . getuid () 64eq ( OK , users . os_get_uname ( user_id , name_out , 100 )) 65eq ( current_username , ffi . string ( name_out )) 66end ) 67 68itp ( 'should FAIL if the userid is not found' , function () 69local name_out =ffi . new ( 'char[100]' ) 70-- hoping nobody has this uid 71local user_id =2342 72eq ( FAIL , users . os_get_uname ( user_id , name_out , 100 )) 73eq ( '2342' , ffi . string ( name_out )) 74end ) 75end ) 76 77describe ( 'os_get_userdir' , function () 78itp ( 'should return NULL if called with NULL' , function () 79eq ( NULL , users . os_get_userdir ( NULL )) 80end ) 81 82itp ( 'should return $HOME for the current user' , function () 83local home =os . getenv ( 'HOME' ) 84eq ( home , ffi . string (( users . os_get_userdir ( current_username )))) 85end ) 86 87itp ( 'should return NULL if the user is not found' , function () 88eq ( NULL , users . os_get_userdir ( 'neovim_user_not_found_test' )) 89end ) 90end ) 91end )