local lfs = require('lfs')
local bit = require('bit')
local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)
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('./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', './src/nvim/path.h')
cppimport('sys/stat.h')
cppimport('fcntl.h')
cimport('uv.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 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.c', function()
local function os_isdir(name)
return fs.os_isdir(to_cstr(name))
end
before_each(function()
lfs.mkdir('unit-test-directory');
io.open('unit-test-directory/test.file', 'w').close()
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)
-- The tests are invoked with an absolute path to `busted` executable.
absolute_executable = arg[0]
-- Split the absolute_executable path into a directory and filename.
directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
end)
after_each(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()
itp('returns OK and writes current directory to the buffer', function()
local length = string.len(lfs.currentdir()) + 1
local buf = cstr(length, '')
eq(OK, fs.os_dirname(buf, length))
eq(lfs.currentdir(), ffi.string(buf))
end)
itp('returns FAIL if the buffer is too small', function()
local length = string.len(lfs.currentdir()) + 1
local buf = cstr(length - 1, '')
eq(FAIL, fs.os_dirname(buf, length - 1))
end)
end)
describe('os_chdir', function()
itp('fails with path="~"', function()
eq(false, os_isdir('~'), 'sanity check: no literal "~" directory')
local length = 4096
local expected_cwd = cstr(length, '')
local cwd = cstr(length, '')
eq(OK, fs.os_dirname(expected_cwd, length))
-- os_chdir returns 0 for success, not OK (1).
neq(0, fs.os_chdir('~')) -- fail
neq(0, fs.os_chdir('~/')) -- fail
eq(OK, fs.os_dirname(cwd, length))
-- CWD did not change.
eq(ffi.string(expected_cwd), ffi.string(cwd))
end)
end)
describe('os_isdir', function()
itp('returns false if an empty string is given', function()
eq(false, (os_isdir('')))
end)
itp('returns false if a nonexisting directory is given', function()
eq(false, (os_isdir('non-existing-directory')))
end)
itp('returns false if a nonexisting absolute directory is given', function()
eq(false, (os_isdir('/non-existing-directory')))
end)
itp('returns false if an existing file is given', function()
eq(false, (os_isdir('unit-test-directory/test.file')))
end)
itp('returns true if the current directory is given', function()
eq(true, (os_isdir('.')))
end)
itp('returns true if the parent directory is given', function()
eq(true, (os_isdir('..')))
end)
itp('returns true if an arbitrary directory is given', function()
eq(true, (os_isdir('unit-test-directory')))
end)
itp('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
|