diff options
author | zeertzjq <zeertzjq@outlook.com> | 2024-05-17 18:39:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-17 18:39:01 +0800 |
commit | 42aa69b076cb338e20b5b4656771f1873e8930d8 (patch) | |
tree | cc1b82eb2e91c93c78af88a9feb62889833848b8 /test/unit/path_spec.lua | |
parent | 10f917351906ca2d3c61a6b9bd8b8ae88a26ed8f (diff) | |
download | rneovim-42aa69b076cb338e20b5b4656771f1873e8930d8.tar.gz rneovim-42aa69b076cb338e20b5b4656771f1873e8930d8.tar.bz2 rneovim-42aa69b076cb338e20b5b4656771f1873e8930d8.zip |
fix(path): avoid chdir() when resolving path (#28799)
Use uv_fs_realpath() instead.
It seems that uv_fs_realpath() has some problems on non-Linux platforms:
- macOS and other BSDs: this function will fail with UV_ELOOP if more
than 32 symlinks are found while resolving the given path. This limit
is hardcoded and cannot be sidestepped.
- Windows: while this function works in the common case, there are a
number of corner cases where it doesn't:
- Paths in ramdisk volumes created by tools which sidestep the Volume
Manager (such as ImDisk) cannot be resolved.
- Inconsistent casing when using drive letters.
- Resolved path bypasses subst'd drives.
Ref: https://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_realpath
I don't know if the old implementation that uses uv_chdir() and uv_cwd()
also suffers from the same problems.
- For the ELOOP case, chdir() seems to have the same limitations.
- On Windows, Vim doesn't use anything like chdir() either. It uses
_wfullpath(), while libuv uses GetFinalPathNameByHandleW().
Diffstat (limited to 'test/unit/path_spec.lua')
-rw-r--r-- | test/unit/path_spec.lua | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index 44919368bf..6f6a80f44e 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -22,11 +22,16 @@ local buffer = nil describe('path.c', function() describe('path_full_dir_name', function() + local old_dir + setup(function() + old_dir = uv.cwd() mkdir('unit-test-directory') + uv.fs_symlink(old_dir .. '/unit-test-directory', 'unit-test-symlink') end) teardown(function() + uv.fs_unlink('unit-test-symlink') uv.fs_rmdir('unit-test-directory') end) @@ -37,35 +42,64 @@ describe('path.c', function() before_each(function() -- Create empty string buffer which will contain the resulting path. - length = string.len(uv.cwd()) + 22 + length = string.len(old_dir) + 22 buffer = cstr(length, '') end) + after_each(function() + uv.chdir(old_dir) + end) + itp('returns the absolute directory name of a given relative one', function() - local result = path_full_dir_name('..', buffer, length) - eq(OK, result) - local old_dir = uv.cwd() + eq(OK, path_full_dir_name('..', buffer, length)) uv.chdir('..') local expected = uv.cwd() uv.chdir(old_dir) - eq(expected, (ffi.string(buffer))) + eq(expected, ffi.string(buffer)) end) itp('returns the current directory name if the given string is empty', function() - eq(OK, (path_full_dir_name('', buffer, length))) - eq(uv.cwd(), (ffi.string(buffer))) + eq(OK, path_full_dir_name('', buffer, length)) + eq(old_dir, ffi.string(buffer)) + end) + + local function test_full_dir_absolute() + itp('works with a normal absolute dir', function() + eq(OK, path_full_dir_name(old_dir .. '/unit-test-directory', buffer, length)) + eq(old_dir .. '/unit-test-directory', ffi.string(buffer)) + end) + + itp('works with a symlinked absolute dir', function() + eq(OK, path_full_dir_name(old_dir .. '/unit-test-symlink', buffer, length)) + eq(old_dir .. '/unit-test-directory', ffi.string(buffer)) + end) + end + + test_full_dir_absolute() + + describe('when cwd does not exist #28786', function() + before_each(function() + mkdir('dir-to-remove') + uv.chdir('dir-to-remove') + uv.fs_rmdir(old_dir .. '/dir-to-remove') + end) + + test_full_dir_absolute() end) itp('works with a normal relative dir', function() - local result = path_full_dir_name('unit-test-directory', buffer, length) - eq(uv.cwd() .. '/unit-test-directory', (ffi.string(buffer))) - eq(OK, result) + eq(OK, path_full_dir_name('unit-test-directory', buffer, length)) + eq(old_dir .. '/unit-test-directory', ffi.string(buffer)) + end) + + itp('works with a symlinked relative dir', function() + eq(OK, path_full_dir_name('unit-test-symlink', buffer, length)) + eq(old_dir .. '/unit-test-directory', ffi.string(buffer)) end) itp('works with a non-existing relative dir', function() - local result = path_full_dir_name('does-not-exist', buffer, length) - eq(uv.cwd() .. '/does-not-exist', (ffi.string(buffer))) - eq(OK, result) + eq(OK, path_full_dir_name('does-not-exist', buffer, length)) + eq(old_dir .. '/does-not-exist', ffi.string(buffer)) end) itp('fails with a non-existing absolute dir', function() |