aboutsummaryrefslogtreecommitdiff
path: root/test/unit/os
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/os')
-rw-r--r--test/unit/os/env_spec.lua12
-rw-r--r--test/unit/os/fs_spec.lua53
-rw-r--r--test/unit/os/shell_spec.lua77
-rw-r--r--test/unit/os/users_spec.lua8
4 files changed, 97 insertions, 53 deletions
diff --git a/test/unit/os/env_spec.lua b/test/unit/os/env_spec.lua
index 8e18c599d9..9e00a3e8f8 100644
--- a/test/unit/os/env_spec.lua
+++ b/test/unit/os/env_spec.lua
@@ -1,11 +1,9 @@
local helpers = require('test.unit.helpers')
local cimport = helpers.cimport
-local internalize = helpers.internalize
local eq = helpers.eq
local neq = helpers.neq
local ffi = helpers.ffi
-local lib = helpers.lib
local cstr = helpers.cstr
local to_cstr = helpers.to_cstr
local NULL = helpers.NULL
@@ -15,15 +13,15 @@ require('lfs')
local env = cimport('./src/nvim/os/os.h')
describe('env function', function()
- function os_setenv(name, value, override)
+ local function os_setenv(name, value, override)
return env.os_setenv((to_cstr(name)), (to_cstr(value)), override)
end
- function os_unsetenv(name, value, override)
+ local function os_unsetenv(name, _, _)
return env.os_unsetenv((to_cstr(name)))
end
- function os_getenv(name)
+ local function os_getenv(name)
local rval = env.os_getenv((to_cstr(name)))
if rval ~= NULL then
return ffi.string(rval)
@@ -150,8 +148,8 @@ describe('env function', function()
local name = 'NEOVIM_UNIT_TEST_EXPAND_ENV_ESCN'
local value = 'NEOVIM_UNIT_TEST_EXPAND_ENV_ESCV'
os_setenv(name, value, 1)
- -- TODO(bobtwinkles) This only tests UNIX expansions. There should be a
- -- test for windows as well
+ -- TODO(bobtwinkles) This only tests Unix expansions. There should be a
+ -- test for Windows as well
local input1 = to_cstr('$NEOVIM_UNIT_TEST_EXPAND_ENV_ESCN/test')
local input2 = to_cstr('${NEOVIM_UNIT_TEST_EXPAND_ENV_ESCN}/test')
local output_buff1 = cstr(255, '')
diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua
index c7a1f55b5d..71b5e7f576 100644
--- a/test/unit/os/fs_spec.lua
+++ b/test/unit/os/fs_spec.lua
@@ -1,3 +1,6 @@
+local lfs = require('lfs')
+local bit = require('bit')
+
local helpers = require('test.unit.helpers')
local cimport = helpers.cimport
@@ -6,16 +9,12 @@ local internalize = helpers.internalize
local eq = helpers.eq
local neq = helpers.neq
local ffi = helpers.ffi
-local lib = helpers.lib
local cstr = helpers.cstr
local to_cstr = helpers.to_cstr
local OK = helpers.OK
local FAIL = helpers.FAIL
local NULL = helpers.NULL
-require('lfs')
-require('bit')
-
cimport('unistd.h')
cimport('./src/nvim/os/shell.h')
cimport('./src/nvim/option_defs.h')
@@ -23,11 +22,10 @@ cimport('./src/nvim/main.h')
cimport('./src/nvim/fileio.h')
local fs = cimport('./src/nvim/os/os.h')
cppimport('sys/stat.h')
-cppimport('sys/fcntl.h')
-cppimport('sys/errno.h')
+cppimport('fcntl.h')
+cppimport('uv-errno.h')
-local len = 0
-local buf = ""
+local buffer = ""
local directory = nil
local absolute_executable = nil
local executable_name = nil
@@ -85,24 +83,26 @@ describe('fs function', function()
end)
describe('os_dirname', function()
+ local length
+
local function os_dirname(buf, len)
return fs.os_dirname(buf, len)
end
before_each(function()
- len = (string.len(lfs.currentdir())) + 1
- buf = cstr(len, '')
+ 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(buf, len)))
- eq(lfs.currentdir(), (ffi.string(buf)))
+ 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((len - 1), '')
- eq(FAIL, (os_dirname(buf, (len - 1))))
+ local buf = cstr((length - 1), '')
+ eq(FAIL, (os_dirname(buf, (length - 1))))
end)
end)
@@ -148,7 +148,7 @@ describe('fs function', function()
local function os_can_exe(name)
local buf = ffi.new('char *[1]')
buf[0] = NULL
- local ok = fs.os_can_exe(to_cstr(name), buf)
+ local ok = 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.
@@ -213,15 +213,6 @@ describe('fs function', function()
os_setperm('unit-test-directory/test.file', orig_test_file_perm)
end)
- local function os_getperm(filename)
- local perm = fs.os_getperm((to_cstr(filename)))
- return tonumber(perm)
- end
-
- local function os_setperm(filename, perm)
- return fs.os_setperm((to_cstr(filename)), perm)
- end
-
local function os_fchown(filename, user_id, group_id)
local fd = ffi.C.open(filename, 0)
local res = fs.os_fchown(fd, user_id, group_id)
@@ -242,8 +233,8 @@ describe('fs function', function()
end
describe('os_getperm', function()
- it('returns -1 when the given file does not exist', function()
- eq(-1, (os_getperm('non-existing-file')))
+ it('returns UV_ENOENT when the given file does not exist', function()
+ eq(ffi.C.UV_ENOENT, (os_getperm('non-existing-file')))
end)
it('returns a perm > 0 when given an existing file', function()
@@ -454,8 +445,8 @@ describe('fs function', function()
local new_file = 'test_new_file'
local existing_file = 'unit-test-directory/test_existing.file'
- it('returns -ENOENT for O_RDWR on a non-existing file', function()
- eq(-ffi.C.kENOENT, (os_open('non-existing-file', ffi.C.kO_RDWR, 0)))
+ it('returns UV_ENOENT for O_RDWR on a non-existing file', function()
+ eq(ffi.C.UV_ENOENT, (os_open('non-existing-file', ffi.C.kO_RDWR, 0)))
end)
it('returns non-negative for O_CREAT on a non-existing file', function()
@@ -468,9 +459,9 @@ describe('fs function', function()
assert.is_true(0 <= (os_open(existing_file, ffi.C.kO_CREAT, 0)))
end)
- it('returns -EEXIST for O_CREAT|O_EXCL on a existing file', function()
+ it('returns UV_EEXIST for O_CREAT|O_EXCL on a existing file', function()
assert_file_exists(existing_file)
- eq(-ffi.C.kEEXIST, (os_open(existing_file, (bit.bor(ffi.C.kO_CREAT, ffi.C.kO_EXCL)), 0)))
+ eq(ffi.C.kUV_EEXIST, (os_open(existing_file, (bit.bor(ffi.C.kO_CREAT, ffi.C.kO_EXCL)), 0)))
end)
it('sets `rwx` permissions for O_CREAT 700', function()
@@ -611,7 +602,7 @@ describe('fs function', function()
it('removes the given directory and returns 0', function()
lfs.mkdir('unit-test-directory/new-dir')
- eq(0, (os_rmdir('unit-test-directory/new-dir', mode)))
+ eq(0, os_rmdir('unit-test-directory/new-dir'))
eq(false, (os_isdir('unit-test-directory/new-dir')))
end)
end)
diff --git a/test/unit/os/shell_spec.lua b/test/unit/os/shell_spec.lua
index 20cfc17950..93103e4e8c 100644
--- a/test/unit/os/shell_spec.lua
+++ b/test/unit/os/shell_spec.lua
@@ -11,29 +11,46 @@ if allowed_os[jit.os] ~= true then
end
local helpers = require('test.unit.helpers')
-local shell = helpers.cimport(
+local cimported = helpers.cimport(
'./src/nvim/os/shell.h',
'./src/nvim/option_defs.h',
'./src/nvim/main.h',
- './src/nvim/misc1.h'
+ './src/nvim/misc1.h',
+ './src/nvim/memory.h'
)
-local ffi, eq, neq = helpers.ffi, helpers.eq, helpers.neq
+local ffi, eq = helpers.ffi, helpers.eq
local intern = helpers.internalize
local to_cstr = helpers.to_cstr
local NULL = ffi.cast('void *', 0)
describe('shell functions', function()
setup(function()
- shell.event_init()
-- os_system() can't work when the p_sh and p_shcf variables are unset
- shell.p_sh = to_cstr('/bin/bash')
- shell.p_shcf = to_cstr('-c')
+ cimported.p_sh = to_cstr('/bin/bash')
+ cimported.p_shcf = to_cstr('-c')
end)
teardown(function()
- shell.event_teardown()
+ cimported.event_teardown()
end)
+ local function shell_build_argv(cmd, extra_args)
+ local res = cimported.shell_build_argv(
+ cmd and to_cstr(cmd),
+ extra_args and to_cstr(extra_args))
+ local argc = 0
+ local ret = {}
+ -- Explicitly free everything, so if it is not in allocated memory it will
+ -- crash.
+ while res[argc] ~= nil do
+ ret[#ret + 1] = ffi.string(res[argc])
+ cimported.xfree(res[argc])
+ argc = argc + 1
+ end
+ cimported.xfree(res)
+ return ret
+ end
+
local function os_system(cmd, input)
local input_or = input and to_cstr(input) or NULL
local input_len = (input ~= nil) and string.len(input) or 0
@@ -41,8 +58,8 @@ describe('shell functions', function()
local nread = ffi.new('size_t[1]')
local argv = ffi.cast('char**',
- shell.shell_build_argv(to_cstr(cmd), nil))
- local status = shell.os_system(argv, input_or, input_len, output, nread)
+ cimported.shell_build_argv(to_cstr(cmd), nil))
+ local status = cimported.os_system(argv, input_or, input_len, output, nread)
return status, intern(output[0], nread[0])
end
@@ -70,8 +87,48 @@ describe('shell functions', function()
end)
it ('returns non-zero exit code', function()
- local status, output = os_system('exit 2')
+ local status = os_system('exit 2')
eq(2, status)
end)
end)
+
+ describe('shell_build_argv', function()
+ local saved_opts = {}
+
+ setup(function()
+ saved_opts.p_sh = cimported.p_sh
+ saved_opts.p_shcf = cimported.p_shcf
+ end)
+
+ teardown(function()
+ cimported.p_sh = saved_opts.p_sh
+ cimported.p_shcf = saved_opts.p_shcf
+ end)
+
+ it('works with NULL arguments', function()
+ eq({'/bin/bash'}, shell_build_argv(nil, nil))
+ end)
+
+ it('works with cmd', function()
+ eq({'/bin/bash', '-c', 'abc def'}, shell_build_argv('abc def', nil))
+ end)
+
+ it('works with extra_args', function()
+ eq({'/bin/bash', 'ghi jkl'}, shell_build_argv(nil, 'ghi jkl'))
+ end)
+
+ it('works with cmd and extra_args', function()
+ eq({'/bin/bash', 'ghi jkl', '-c', 'abc def'}, shell_build_argv('abc def', 'ghi jkl'))
+ end)
+
+ it('splits and unquotes &shell and &shellcmdflag', function()
+ cimported.p_sh = to_cstr('/Program" "Files/zsh -f')
+ cimported.p_shcf = to_cstr('-x -o "sh word split" "-"c')
+ eq({'/Program Files/zsh', '-f',
+ 'ghi jkl',
+ '-x', '-o', 'sh word split',
+ '-c', 'abc def'},
+ shell_build_argv('abc def', 'ghi jkl'))
+ end)
+ end)
end)
diff --git a/test/unit/os/users_spec.lua b/test/unit/os/users_spec.lua
index df5d2365c6..236481e9e7 100644
--- a/test/unit/os/users_spec.lua
+++ b/test/unit/os/users_spec.lua
@@ -1,26 +1,24 @@
local helpers = require('test.unit.helpers')
local cimport = helpers.cimport
-local internalize = helpers.internalize
local eq = helpers.eq
local ffi = helpers.ffi
local lib = helpers.lib
-local cstr = helpers.cstr
local NULL = helpers.NULL
local OK = helpers.OK
local FAIL = helpers.FAIL
local users = cimport('./src/nvim/os/os.h', 'unistd.h')
-function garray_new()
+local function garray_new()
return ffi.new('garray_T[1]')
end
-function garray_get_len(array)
+local function garray_get_len(array)
return array[0].ga_len
end
-function garray_get_item(array, index)
+local function garray_get_item(array, index)
return (ffi.cast('void **', array[0].ga_data))[index]
end