local lfs = require('lfs')
local bit = require('bit')
local helpers = require('test.unit.helpers')
local cimport = helpers.cimport
local cppimport = helpers.cppimport
local internalize = helpers.internalize
local ok = helpers.ok
local eq = helpers.eq
local neq = helpers.neq
local ffi = helpers.ffi
local cstr = helpers.cstr
local to_cstr = helpers.to_cstr
local OK = helpers.OK
local FAIL = helpers.FAIL
local NULL = helpers.NULL
local NODE_NORMAL = 0
local NODE_WRITABLE = 1
cimport('unistd.h')
cimport('./src/nvim/os/shell.h')
cimport('./src/nvim/option_defs.h')
cimport('./src/nvim/main.h')
cimport('./src/nvim/fileio.h')
local fs = cimport('./src/nvim/os/os.h')
cppimport('sys/stat.h')
cppimport('fcntl.h')
cppimport('uv-errno.h')
local s = ''
for i = 0, 255 do
s = s .. (i == 0 and '\0' or ('%c'):format(i))
end
local fcontents = s:rep(16)
local buffer = ""
local directory = nil
local absolute_executable = nil
local executable_name = nil
local function set_bit(number, to_set)
return bit.bor(number, to_set)
end
local function unset_bit(number, to_unset)
return bit.band(number, (bit.bnot(to_unset)))
end
local function assert_file_exists(filepath)
neq(nil, lfs.attributes(filepath))
end
local function assert_file_does_not_exist(filepath)
eq(nil, lfs.attributes(filepath))
end
local function os_setperm(filename, perm)
return fs.os_setperm((to_cstr(filename)), perm)
end
local function os_getperm(filename)
local perm = fs.os_getperm((to_cstr(filename)))
return tonumber(perm)
end
describe('fs function', function()
local orig_test_file_perm
setup(function()
lfs.mkdir('unit-test-directory');
io.open('unit-test-directory/test.file', 'w').close()
orig_test_file_perm = os_getperm('unit-test-directory/test.file')
io.open('unit-test-directory/test_2.file', 'w').close()
lfs.link('test.file', 'unit-test-directory/test_link.file', true)
lfs.link('non_existing_file.file', 'unit-test-directory/test_broken_link.file', true)
-- Since the tests are executed, they are called by an executable. We use
-- that executable for several asserts.
absolute_executable = arg[0]
-- Split absolute_executable into a directory and the actual file name for
-- later usage.
directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
end)
teardown(function()
os.remove('unit-test-directory/test.file')
os.remove('unit-test-directory/test_2.file')
os.remove('unit-test-directory/test_link.file')
os.remove('unit-test-directory/test_hlink.file')
os.remove('unit-test-directory/test_broken_link.file')
lfs.rmdir('unit-test-directory')
end)
describe('os_dirname', function()
local length
local function os_dirname(buf, len)
return fs.os_dirname(buf, len)
end
before_each(function()
length = (string.len(lfs.currentdir())) + 1
buffer = cstr(length, '')
end)
it('returns OK and writes current directory into the buffer if it is large\n enough', function()
eq(OK, (os_dirname(buffer, length)))
eq(lfs.currentdir(), (ffi.string(buffer)))
end)
-- What kind of other failing cases are possible?
it('returns FAIL if the buffer is too small', function()
local buf = cstr((length - 1), '')
eq(FAIL, (os_dirname(buf, (length - 1))))
end)
end)
local function os_isdir(name)
return fs.os_isdir((to_cstr(name)))
end
describe('os_isdir', function()
it('returns false if an empty string is given', function()
eq(false, (os_isdir('')))
end)
it('returns false if a nonexisting directory is given', function()
eq(false, (os_isdir('non-existing-directory')))
end)
it('returns false if a nonexisting absolute directory is given', function()
eq(false, (os_isdir('/non-existing-directory')))
end)
it('returns false if an existing file is given', function()
eq(false, (os_isdir('unit-test-directory/test.file')))
end)
it('returns true if the current directory is given', function()
eq(true, (os_isdir('.')))
end)
it('returns true if the parent directory is given', function()
eq(true, (os_isdir('..')))
end)
it('returns true if an arbitrary directory is given', function()
eq(true, (os_isdir('unit-test-directory')))
end)
it('returns true if an absolute directory is given', function()
eq(true, (os_isdir(directory)))
end)
end)
describe('os_can_exe', function()
local function os_can_exe(name)
local buf = ffi.new('char *[1]')
buf[0] = NULL
local ce_ret = fs.os_can_exe(to_cstr(name), buf, true)
-- When os_can_exe returns true, it must set the path.
-- When it returns false, the path must be NULL.
if ce_ret then
neq(NULL, buf[0])
return internalize(buf[0])
else
eq(NULL, buf[0])
return nil
end
end
local function cant_exe(name)
eq(nil, os_can_exe(name))
end
local function exe(name)
return os_can_exe(name)
end
it('returns false when given a directory', function()
cant_exe('./unit-test-directory')
end)
it('returns false when given a regular file without executable bit set', function()
cant_exe
|