undo_spec.lua 7.6 KB

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