test/unit/undo_spec.lua: fixup after rebase #4985

This commit is contained in:
Justin M. Keyes
2018-04-27 10:06:42 +02:00
parent d6a1640260
commit bd17ef75b3

View File

@@ -1,5 +1,11 @@
local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)
local lfs = require('lfs') local lfs = require('lfs')
local helpers = require('test.unit.helpers') local child_call_once = helpers.child_call_once
local global_helpers = require('test.helpers')
local hexdump = global_helpers.hexdump
local sleep = global_helpers.sleep
local ffi = helpers.ffi local ffi = helpers.ffi
local cimport = helpers.cimport local cimport = helpers.cimport
@@ -14,12 +20,31 @@ local options = cimport('./src/nvim/option_defs.h')
local undo = cimport('./src/nvim/undo.h') local undo = cimport('./src/nvim/undo.h')
local buffer = cimport('./src/nvim/buffer.h') local buffer = cimport('./src/nvim/buffer.h')
local old_p_udir = options.p_udir -- save the old value of p_udir (undodir) local old_p_udir = nil
-- Values expected by tests. Set in the setup function and destroyed in teardown -- Values expected by tests. Set in the setup function and destroyed in teardown
local file_buffer = nil local file_buffer = nil
local buffer_hash = nil local buffer_hash = nil
child_call_once(function()
if old_p_udir == nil then
old_p_udir = options.p_udir -- save the old value of p_udir (undodir)
end
-- create a new buffer
local c_file = to_cstr('Xtest-unit-undo')
file_buffer = buffer.buflist_new(c_file, c_file, 1, buffer.BLN_LISTED)
file_buffer.b_u_numhead = 1 -- Pretend that the buffer has been changed
-- TODO(christopher.waldon.dev@gmail.com): replace the 32 with UNDO_HASH_SIZE
-- requires refactor of UNDO_HASH_SIZE into constant/enum for ffi
--
-- compute a hash for this undofile
buffer_hash = ffi.new('char_u[32]')
undo.u_compute_hash(buffer_hash)
end)
describe('u_write_undo', function() describe('u_write_undo', function()
setup(function() setup(function()
lfs.mkdir('unit-test-directory') lfs.mkdir('unit-test-directory')
@@ -36,20 +61,6 @@ describe('u_write_undo', function()
options.p_udir = old_p_udir --restore old p_udir options.p_udir = old_p_udir --restore old p_udir
end) end)
before_each(function()
-- create a new buffer
local c_file = to_cstr('../test/unit/undo_spec.lua')
file_buffer = buffer.buflist_new(c_file, c_file, 1, buffer.BLN_LISTED)
file_buffer.b_u_numhead = 1 -- Pretend that the buffer has been changed
-- TODO(christopher.waldon.dev@gmail.com): replace the 32 with UNDO_HASH_SIZE
-- requires refactor of UNDO_HASH_SIZE into constant/enum for ffi
--
-- compute a hash for this undofile
buffer_hash = ffi.new('char_u[32]')
undo.u_compute_hash(buffer_hash)
end)
-- Lua wrapper for u_write_undo -- Lua wrapper for u_write_undo
local function u_write_undo(name, forceit, buf, buf_hash) local function u_write_undo(name, forceit, buf, buf_hash)
if name ~= nil then if name ~= nil then
@@ -59,7 +70,7 @@ describe('u_write_undo', function()
return undo.u_write_undo(name, forceit, buf, buf_hash) return undo.u_write_undo(name, forceit, buf, buf_hash)
end end
it('writes an undo file to undodir given a buffer and hash', function() itp('writes an undo file to undodir given a buffer and hash', function()
u_write_undo(nil, false, file_buffer, buffer_hash) u_write_undo(nil, false, file_buffer, buffer_hash)
local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false)) local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false))
local undo_file = io.open(correct_name, "r") local undo_file = io.open(correct_name, "r")
@@ -71,7 +82,7 @@ describe('u_write_undo', function()
end end
end) end)
it('writes a correctly-named undo file to undodir given a name, buffer, and hash', function() itp('writes a correctly-named undo file to undodir given a name, buffer, and hash', function()
local correct_name = "undofile.test" local correct_name = "undofile.test"
u_write_undo(correct_name, false, file_buffer, buffer_hash) u_write_undo(correct_name, false, file_buffer, buffer_hash)
local undo_file = io.open(correct_name, "r") local undo_file = io.open(correct_name, "r")
@@ -83,12 +94,12 @@ describe('u_write_undo', function()
end end
end) end)
it('does not write an undofile when the buffer has no valid undofile name', function() itp('does not write an undofile when the buffer has no valid undofile name', function()
-- TODO(christopher.waldon.dev@gmail.com): Figure out how to test this. -- TODO(christopher.waldon.dev@gmail.com): Figure out how to test this.
-- it's hard because u_get_undo_file_name() would need to return null -- it's hard because u_get_undo_file_name() would need to return null
end) end)
it('writes the undofile with the same permissions as the original file', function() itp('writes the undofile with the same permissions as the original file', function()
-- Create Test file and set permissions -- Create Test file and set permissions
local test_file_name = "./test.file" local test_file_name = "./test.file"
local test_permission_file = io.open(test_file_name, "w") local test_permission_file = io.open(test_file_name, "w")
@@ -121,7 +132,7 @@ describe('u_write_undo', function()
end end
end) end)
it('writes an undofile only readable by the user if the buffer is unnamed', function() itp('writes an undofile only readable by the user if the buffer is unnamed', function()
local correct_permissions = "rw-------" local correct_permissions = "rw-------"
local undo_file_name = "test.undo" local undo_file_name = "test.undo"
@@ -142,24 +153,16 @@ describe('u_write_undo', function()
end end
end) end)
it('forces writing undo file for :wundo! command', function() itp('forces writing undo file for :wundo! command', function()
local file_contents = "testing permissions" local file_contents = "testing permissions"
-- Write a text file where the undofile should go -- Write a text file where the undofile should go
local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false)) local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false))
local text_file, err = io.open(correct_name, "w") global_helpers.write_file(correct_name, file_contents, true, false)
if err then
pending()
end
text_file:write(file_contents)
text_file:close()
u_write_undo(nil, true, file_buffer, buffer_hash) -- Call with forceit flag enabled -- Call with `forceit`.
u_write_undo(correct_name, true, file_buffer, buffer_hash)
local undo_file, undo_file_err = io.open(correct_name, "r") local undo_file_contents = global_helpers.read_file(correct_name)
if undo_file_err then
pending()
end
local undo_file_contents = undo_file:read("*a") -- Read all
neq(file_contents, undo_file_contents) neq(file_contents, undo_file_contents)
local success, deletion_err = os.remove(correct_name) -- delete the file now that we're done with it. local success, deletion_err = os.remove(correct_name) -- delete the file now that we're done with it.
@@ -168,13 +171,13 @@ describe('u_write_undo', function()
end end
end) end)
it('overwrites an existing undo file', function() itp('overwrites an existing undo file', function()
u_write_undo(nil, false, file_buffer, buffer_hash) u_write_undo(nil, false, file_buffer, buffer_hash)
local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false)) local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false))
local file_last_modified = lfs.attributes(correct_name).modification local file_last_modified = lfs.attributes(correct_name).modification
os.execute('sleep 1s') -- Ensure there will be some difference in timestamps sleep(1000) -- Ensure difference in timestamps.
file_buffer.b_u_numhead = 1 -- Mark it as if there are changes file_buffer.b_u_numhead = 1 -- Mark it as if there are changes
u_write_undo(nil, false, file_buffer, buffer_hash) u_write_undo(nil, false, file_buffer, buffer_hash)
@@ -188,15 +191,15 @@ describe('u_write_undo', function()
end end
end) end)
it('does not overwrite an existing file that is not an undo file', function() itp('does not overwrite an existing file that is not an undo file', function()
-- TODO: write test -- TODO: write test
end) end)
it('does not overwrite an existing file that has the wrong permissions', function() itp('does not overwrite an existing file that has the wrong permissions', function()
-- TODO: write test -- TODO: write test
end) end)
it('does not write an undo file if there is no undo information for the buffer', function() itp('does not write an undo file if there is no undo information for the buffer', function()
file_buffer.b_u_numhead = 0 -- Mark it as if there is no undo information file_buffer.b_u_numhead = 0 -- Mark it as if there is no undo information
local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false)) local correct_name = ffi.string(undo.u_get_undo_file_name(file_buffer.b_ffname, false))