robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.unit.testutil' ) 2local describe , setup , teardown =t . describe , t . setup , t . teardown 3local itp =t . gen_itp ( t . it ) 4local uv =vim . uv 5local child_call_once =t . child_call_once 6local sleep =uv . sleep 7 8local ffi =t . ffi 9local cimport =t . cimport 10local to_cstr =t . to_cstr 11local neq =t . neq 12local eq =t . eq 13local mkdir =t . mkdir 14 15local options =cimport ( './src/nvim/option_vars.h' ) 16local undo =cimport ( './src/nvim/undo.h' ) 17local buffer =cimport ( './src/nvim/buffer.h' ) 18 19local old_p_udir =nil 20 21-- Values expected by tests. Set in the setup function and destroyed in teardown 22local file_buffer =nil 23local buffer_hash =nil 24 25child_call_once ( function () 26if old_p_udir == nil then 27old_p_udir =options . p_udir -- save the old value of p_udir (undodir) 28end 29 30-- create a new buffer 31local c_file =to_cstr ( 'Xtest-unit-undo' ) 32file_buffer =buffer . buflist_new ( c_file , c_file , 1 , buffer . BLN_LISTED ) 33file_buffer . b_u_numhead =1 -- Pretend that the buffer has been changed 34 35-- TODO(christopher.waldon.dev@gmail.com): replace the 32 with UNDO_HASH_SIZE 36-- requires refactor of UNDO_HASH_SIZE into constant/enum for ffi 37-- 38-- compute a hash for this undofile 39buffer_hash =ffi . new ( 'char[32]' ) 40undo . u_compute_hash ( file_buffer , buffer_hash ) 41end ) 42 43describe ( 'u_write_undo' , function () 44setup ( function () 45mkdir ( 'unit-test-directory' ) 46uv . chdir ( 'unit-test-directory' ) 47options . p_udir =to_cstr ( uv . cwd ()) -- set p_udir to be the test dir 48end ) 49 50teardown ( function () 51uv . chdir ( '..' ) 52local success , err =uv . fs_rmdir ( 'unit-test-directory' ) 53if not success then 54( err ) -- inform tester if directory fails to delete 55end 56options . p_udir =old_p_udir --restore old p_udir 57end ) 58 59-- Lua wrapper for u_write_undo 60local function u_write_undo ( name , forceit , buf , buf_hash ) 61if name ~= nil then 62name =to_cstr ( name ) 63end 64 65return undo . u_write_undo ( name , forceit , buf , buf_hash ) 66end 67 68itp ( 'writes an undo file to undodir given a buffer and hash' , function () 69u_write_undo ( nil , false , file_buffer , buffer_hash ) 70local correct_name =ffi . string ( undo . u_get_undo_file_name ( file_buffer . b_ffname , false )) 71local undo_file =io . open ( correct_name , 'r' ) 72 73neq ( nil , undo_file ) 74local success , err =os . remove ( correct_name ) -- delete the file now that we're done with it. 75if not success then 76( err ) -- inform tester if undofile fails to delete 77end 78end ) 79 80itp ( 'writes a correctly-named undo file to undodir given a name, buffer, and hash' , function () 81local correct_name ='undofile.test' 82u_write_undo ( correct_name , false , file_buffer , buffer_hash ) 83local undo_file =io . open ( correct_name , 'r' ) 84 85neq ( nil , undo_file ) 86local success , err =os . remove ( correct_name ) -- delete the file now that we're done with it. 87if not success then 88( err ) -- inform tester if undofile fails to delete 89end 90end ) 91 92itp ( 'does not write an undofile when the buffer has no valid undofile name' , function () 93-- TODO(christopher.waldon.dev@gmail.com): Figure out how to test this. 94-- it's hard because u_get_undo_file_name() would need to return null 95end ) 96 97itp ( 'writes the undofile with the same permissions as the original file' , function () 98-- Create Test file and set permissions 99local test_file_name ='./test.file' 100local test_permission_file =io . open ( test_file_name , 'w' ) 101test_permission_file : write ( 'testing permissions' ) 102test_permission_file : close () 103local test_permissions =uv . fs_stat ( test_file_name ). mode 104 105-- Create vim buffer 106local c_file =to_cstr ( test_file_name ) 107file_buffer =buffer . buflist_new ( c_file , c_file , 1 , buffer . BLN_LISTED ) 108file_buffer . b_u_numhead =1 -- Pretend that the buffer has been changed 109 110u_write_undo ( nil , false , file_buffer , buffer_hash ) 111 112-- Find out the correct name of the undofile 113local undo_file_name =ffi . string ( undo . u_get_undo_file_name ( file_buffer . b_ffname , false )) 114 115-- Find out the permissions of the new file 116local permissions =uv . fs_stat ( undo_file_name ). mode 117eq ( test_permissions , permissions ) 118 119-- delete the file now that we're done with it. 120local success , err =os . remove ( test_file_name ) 121if not success then 122( err ) -- inform tester if undofile fails to delete 123end 124success , err =os . remove ( undo_file_name ) 125if not success then 126( err ) -- inform tester if undofile fails to delete 127end 128end ) 129 130itp ( 'writes an undofile only readable by the user if the buffer is unnamed' , function () 131local correct_permissions =33152 132local undo_file_name ='test.undo' 133 134-- Create vim buffer 135file_buffer =buffer . buflist_new ( nil , nil , 1 , buffer . BLN_LISTED ) 136file_buffer . b_u_numhead =1 -- Pretend that the buffer has been changed 137 138u_write_undo ( undo_file_name , false , file_buffer , buffer_hash ) 139 140-- Find out the permissions of the new file 141local permissions =uv . fs_stat ( undo_file_name ). mode 142eq ( correct_permissions , permissions ) 143 144-- delete the file now that we're done with it. 145local success , err =os . remove ( undo_file_name ) 146if not success then 147( err ) -- inform tester if undofile fails to delete 148end 149end ) 150 151itp ( 'forces writing undo file for :wundo! command' , function () 152local file_contents ='testing permissions' 153-- Write a text file where the undofile should go 154local correct_name =ffi . string ( undo . u_get_undo_file_name ( file_buffer . b_ffname , false )) 155t . write_file ( correct_name , file_contents , true , false ) 156 157-- Call with `forceit`. 158u_write_undo ( correct_name , true , file_buffer , buffer_hash ) 159 160local undo_file_contents =t . read_file ( correct_name ) 161 162neq ( file_contents , undo_file_contents ) 163local success , deletion_err =os . remove ( correct_name ) -- delete the file now that we're done with it. 164if not success then 165( deletion_err ) -- inform tester if undofile fails to delete 166end 167end ) 168 169itp ( 'overwrites an existing undo file' , function () 170u_write_undo ( nil , false , file_buffer , buffer_hash ) 171local correct_name =ffi . string ( undo . u_get_undo_file_name ( file_buffer . b_ffname , false )) 172 173local file_last_modified =uv . fs_stat ( correct_name ). mtime . sec 174 175sleep ( 1000 ) -- Ensure difference in timestamps. 176file_buffer . b_u_numhead =1 -- Mark it as if there are changes 177u_write_undo ( nil , false , file_buffer , buffer_hash ) 178 179local file_last_modified_2 =uv . fs_stat ( correct_name ). mtime . sec 180 181-- print(file_last_modified, file_last_modified_2) 182neq ( file_last_modified , file_last_modified_2 ) 183local success , err =os . remove ( correct_name ) -- delete the file now that we're done with it. 184if not success then 185( err ) -- inform tester if undofile fails to delete 186end 187end ) 188 189itp ( 'does not overwrite an existing file that is not an undo file' , function () 190-- TODO: write test 191end ) 192 193itp ( 'does not overwrite an existing file that has the wrong permissions' , function () 194-- TODO: write test 195end ) 196 197itp ( 'does not write an undo file if there is no undo information for the buffer' , function () 198file_buffer . b_u_numhead =0 -- Mark it as if there is no undo information 199local correct_name =ffi . string ( undo . u_get_undo_file_name ( file_buffer . b_ffname , false )) 200 201local existing_file =io . open ( correct_name , 'r' ) 202if existing_file then 203existing_file : close () 204os . remove ( correct_name ) 205end 206u_write_undo ( nil , false , file_buffer , buffer_hash ) 207local undo_file =io . open ( correct_name , 'r' ) 208 209eq ( nil , undo_file ) 210end ) 211end )