robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

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