local uv = vim.uv
local bit = require('bit')
local t = require('test.unit.testutil')
local describe, before_each, after_each, pending =
t.describe, t.before_each, t.after_each, t.pending
local itp = t.gen_itp(t.it)
local cimport = t.cimport
local cppimport = t.cppimport
local internalize = t.internalize
local ok = t.ok
local eq = t.eq
local neq = t.neq
local ffi = t.ffi
local cstr = t.cstr
local to_cstr = t.to_cstr
local OK = t.OK
local FAIL = t.FAIL
local NULL = t.NULL
local mkdir = t.mkdir
local endswith = vim.endswith
local NODE_NORMAL = 0
local NODE_WRITABLE = 1
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, uv.fs_stat(filepath))
end
local function assert_file_does_not_exist(filepath)
eq(nil, uv.fs_stat(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()
mkdir('unit-test-directory')
io.open('unit-test-directory/test.file', 'w'):close()
io.open('unit-test-directory/test_2.file', 'w'):close()
uv.fs_symlink('test.file', 'unit-test-directory/test_link.file')
uv.fs_symlink('non_existing_file.file', 'unit-test-directory/test_broken_link.file')
-- The tests are invoked with an absolute executable path in arg[0].
absolute_executable = vim.fs.normalize(assert(uv.exepath()))
-- 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')
uv.fs_rmdir('unit-test-directory')
end)
describe('os_dirname', function()
itp('returns OK and writes current directory to the buffer', function()
local length = string.len(uv.cwd()) + 1
local buf = cstr(length, '')
eq(OK, fs.os_dirname(buf, length))
eq(uv.cwd(), ffi.string(buf))
end)
itp('returns FAIL if the buffer is too small', function()
local length = string.len(uv.cwd()) + 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'
|