2017-03-11 06:02:47 -07:00
|
|
|
local lfs = require('lfs')
|
|
|
|
local helpers = require('test.unit.helpers')(after_each)
|
2017-03-04 18:02:45 -07:00
|
|
|
local itp = helpers.gen_itp(it)
|
2014-06-22 06:14:25 -07:00
|
|
|
|
2017-03-11 06:02:47 -07:00
|
|
|
local eq = helpers.eq
|
|
|
|
local neq = helpers.neq
|
|
|
|
local cimport = helpers.cimport
|
2017-03-11 16:54:23 -07:00
|
|
|
local child_call_once = helpers.child_call_once
|
|
|
|
local child_cleanup_once = helpers.child_cleanup_once
|
2017-03-11 06:02:47 -07:00
|
|
|
|
|
|
|
local lib = cimport('./src/nvim/os/os.h', './src/nvim/fileio.h')
|
2014-09-09 18:17:07 -07:00
|
|
|
|
2014-06-22 06:14:25 -07:00
|
|
|
describe('tempfile related functions', function()
|
2017-03-11 16:54:23 -07:00
|
|
|
before_each(function()
|
|
|
|
local function vim_deltempdir()
|
|
|
|
lib.vim_deltempdir()
|
|
|
|
end
|
|
|
|
child_call_once(vim_deltempdir)
|
|
|
|
child_cleanup_once(vim_deltempdir)
|
2014-06-22 06:14:25 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
local vim_gettempdir = function()
|
2017-03-11 06:02:47 -07:00
|
|
|
return helpers.ffi.string(lib.vim_gettempdir())
|
2014-06-22 06:14:25 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe('vim_gettempdir', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns path to Neovim own temp directory', function()
|
2014-06-22 06:14:25 -07:00
|
|
|
local dir = vim_gettempdir()
|
|
|
|
assert.True(dir ~= nil and dir:len() > 0)
|
|
|
|
-- os_file_is_writable returns 2 for a directory which we have rights
|
|
|
|
-- to write into.
|
2017-03-11 06:02:47 -07:00
|
|
|
eq(lib.os_file_is_writable(helpers.to_cstr(dir)), 2)
|
2014-06-22 06:14:25 -07:00
|
|
|
for entry in lfs.dir(dir) do
|
|
|
|
assert.True(entry == '.' or entry == '..')
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns the same directory on each call', function()
|
2014-06-22 06:14:25 -07:00
|
|
|
local dir1 = vim_gettempdir()
|
|
|
|
local dir2 = vim_gettempdir()
|
2017-03-11 06:02:47 -07:00
|
|
|
eq(dir1, dir2)
|
2014-06-22 06:14:25 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('vim_tempname', function()
|
|
|
|
local vim_tempname = function()
|
2017-03-11 06:02:47 -07:00
|
|
|
return helpers.ffi.string(lib.vim_tempname())
|
2014-06-22 06:14:25 -07:00
|
|
|
end
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('generate name of non-existing file', function()
|
2014-06-22 06:14:25 -07:00
|
|
|
local file = vim_tempname()
|
|
|
|
assert.truthy(file)
|
2017-03-11 06:02:47 -07:00
|
|
|
assert.False(lib.os_path_exists(file))
|
2014-06-22 06:14:25 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('generate different names on each call', function()
|
2014-06-22 06:14:25 -07:00
|
|
|
local fst = vim_tempname()
|
|
|
|
local snd = vim_tempname()
|
2017-03-11 06:02:47 -07:00
|
|
|
neq(fst, snd)
|
2014-06-22 06:14:25 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('generate file name in Neovim own temp directory', function()
|
2014-06-22 06:14:25 -07:00
|
|
|
local dir = vim_gettempdir()
|
|
|
|
local file = vim_tempname()
|
2017-03-16 03:58:15 -07:00
|
|
|
eq(string.sub(file, 1, string.len(dir)), dir)
|
2014-06-22 06:14:25 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|