Skip to content

Commit

Permalink
Merge ec43e8c into 8dc3eca
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmk committed Apr 24, 2017
2 parents 8dc3eca + ec43e8c commit c914998
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/nvim/os/fs.c
Expand Up @@ -61,9 +61,9 @@ void fs_init(void)
}


/// Change to the given directory.
/// Changes the current directory to `path`.
///
/// @return `0` on success, a libuv error code on failure.
/// @return 0 on success, or negative error code.
int os_chdir(const char *path)
FUNC_ATTR_NONNULL_ALL
{
Expand Down
52 changes: 30 additions & 22 deletions test/unit/os/fs_spec.lua
Expand Up @@ -35,7 +35,6 @@ for i = 0, 255 do
end
local fcontents = s:rep(16)

local buffer = ""
local directory = nil
local absolute_executable = nil
local executable_name = nil
Expand Down Expand Up @@ -65,7 +64,11 @@ local function os_getperm(filename)
return tonumber(perm)
end

describe('fs function', function()
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');

Expand All @@ -91,32 +94,37 @@ 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()
length = (string.len(lfs.currentdir())) + 1
buffer = cstr(length, '')
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 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?
itp('returns FAIL if the buffer is too small', function()
local buf = cstr((length - 1), '')
eq(FAIL, (os_dirname(buf, (length - 1))))
local length = string.len(lfs.currentdir()) + 1
local buf = cstr(length - 1, '')
eq(FAIL, fs.os_dirname(buf, length - 1))
end)
end)

local function os_isdir(name)
return fs.os_isdir((to_cstr(name)))
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()
Expand Down

0 comments on commit c914998

Please sign in to comment.