diff options
Diffstat (limited to 'test/unit/os/fs_spec.lua')
| -rw-r--r-- | test/unit/os/fs_spec.lua | 129 |
1 files changed, 116 insertions, 13 deletions
diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 3d5c6bc885..9cca3ca244 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -19,7 +19,7 @@ require('bit') cimport('unistd.h') cimport('./src/nvim/os/shell.h') cimport('./src/nvim/option_defs.h') -cimport('./src/nvim/os/event.h') +cimport('./src/nvim/main.h') cimport('./src/nvim/fileio.h') local fs = cimport('./src/nvim/os/os.h') cppimport('sys/stat.h') @@ -32,6 +32,14 @@ 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 @@ -40,11 +48,24 @@ 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) -- Since the tests are executed, they are called by an executable. We use @@ -188,6 +209,10 @@ describe('fs function', function() end) describe('file permissions', function() + before_each(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) @@ -208,6 +233,10 @@ describe('fs function', function() return fs.os_file_is_readonly((to_cstr(filename))) end + local function os_file_is_readable(filename) + return fs.os_file_is_readable((to_cstr(filename))) + end + local function os_file_is_writable(filename) return fs.os_file_is_writable((to_cstr(filename))) end @@ -216,14 +245,6 @@ describe('fs function', function() return 0 ~= (bit.band(number, check_bit)) end - 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 - describe('os_getperm', function() it('returns -1 when the given file does not exist', function() eq(-1, (os_getperm('non-existing-file'))) @@ -270,7 +291,7 @@ describe('fs function', function() -- Some systems may not have `id` utility. if (os.execute('id -G > /dev/null 2>&1') ~= 0) then - pending('skipped (missing `id` utility)') + pending('skipped (missing `id` utility)', function() end) else it('owner of a file may change the group of the file to any group of which that owner is a member', function() local file_gid = lfs.attributes(filename, 'gid') @@ -296,7 +317,7 @@ describe('fs function', function() -- On Windows `os_fchown` always returns 0 -- because `uv_fs_chown` is no-op on this platform. if (ffi.os == 'Windows' or ffi.C.geteuid() == 0) then - pending('skipped (os_fchown is no-op on Windows)') + pending('skipped (os_fchown is no-op on Windows)', function() end) else it('returns nonzero if process has not enough permissions', function() -- chown to root @@ -322,16 +343,35 @@ describe('fs function', function() end) end) + describe('os_file_is_readable', function() + it('returns false if the file is not readable', function() + local perm = os_getperm('unit-test-directory/test.file') + perm = unset_bit(perm, ffi.C.kS_IRUSR) + perm = unset_bit(perm, ffi.C.kS_IRGRP) + perm = unset_bit(perm, ffi.C.kS_IROTH) + eq(OK, (os_setperm('unit-test-directory/test.file', perm))) + eq(false, os_file_is_readable('unit-test-directory/test.file')) + end) + + it('returns false if the file does not exist', function() + eq(false, os_file_is_readable( + 'unit-test-directory/what_are_you_smoking.gif')) + end) + + it('returns true if the file is readable', function() + eq(true, os_file_is_readable( + 'unit-test-directory/test.file')) + end) + end) + describe('os_file_is_writable', function() it('returns 0 if the file is readonly', function() local perm = os_getperm('unit-test-directory/test.file') - local perm_orig = perm perm = unset_bit(perm, ffi.C.kS_IWUSR) perm = unset_bit(perm, ffi.C.kS_IWGRP) perm = unset_bit(perm, ffi.C.kS_IWOTH) eq(OK, (os_setperm('unit-test-directory/test.file', perm))) eq(0, os_file_is_writable('unit-test-directory/test.file')) - eq(OK, (os_setperm('unit-test-directory/test.file', perm_orig))) end) it('returns 1 if the file is writable', function() @@ -486,6 +526,16 @@ describe('fs function', function() return fs.os_rmdir(to_cstr(path)) end + local function os_mkdir_recurse(path, mode) + local failed_str = ffi.new('char *[1]', {nil}) + local ret = fs.os_mkdir_recurse(path, mode, failed_str) + local str = failed_str[0] + if str ~= nil then + str = ffi.string(str) + end + return ret, str + end + describe('os_mkdir', function() it('returns non-zero when given an already existing directory', function() local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR @@ -501,6 +551,59 @@ describe('fs function', function() end) end) + describe('os_mkdir_recurse', function() + it('returns zero when given an already existing directory', function() + local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR + local ret, failed_str = os_mkdir_recurse('unit-test-directory', mode) + eq(0, ret) + eq(nil, failed_str) + end) + + it('fails to create a directory where there is a file', function() + local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR + local ret, failed_str = os_mkdir_recurse( + 'unit-test-directory/test.file', mode) + neq(0, ret) + eq('unit-test-directory/test.file', failed_str) + end) + + it('fails to create a directory where there is a file in path', function() + local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR + local ret, failed_str = os_mkdir_recurse( + 'unit-test-directory/test.file/test', mode) + neq(0, ret) + eq('unit-test-directory/test.file', failed_str) + end) + + it('succeeds to create a directory', function() + local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR + local ret, failed_str = os_mkdir_recurse( + 'unit-test-directory/new-dir-recurse', mode) + eq(0, ret) + eq(nil, failed_str) + eq(true, os_isdir('unit-test-directory/new-dir-recurse')) + lfs.rmdir('unit-test-directory/new-dir-recurse') + eq(false, os_isdir('unit-test-directory/new-dir-recurse')) + end) + + it('succeeds to create a directory tree', function() + local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR + local ret, failed_str = os_mkdir_recurse( + 'unit-test-directory/new-dir-recurse/1/2/3', mode) + eq(0, ret) + eq(nil, failed_str) + eq(true, os_isdir('unit-test-directory/new-dir-recurse')) + eq(true, os_isdir('unit-test-directory/new-dir-recurse/1')) + eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2')) + eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2/3')) + lfs.rmdir('unit-test-directory/new-dir-recurse/1/2/3') + lfs.rmdir('unit-test-directory/new-dir-recurse/1/2') + lfs.rmdir('unit-test-directory/new-dir-recurse/1') + lfs.rmdir('unit-test-directory/new-dir-recurse') + eq(false, os_isdir('unit-test-directory/new-dir-recurse')) + end) + end) + describe('os_rmdir', function() it('returns non_zero when given a non-existing directory', function() neq(0, (os_rmdir('non-existing-directory'))) |
