mirror of
https://github.com/neovim/neovim.git
synced 2024-12-20 03:05:11 -07:00
vim-patch:8.2.3468: problem with :cd when editing file in non-existent directory
Problem: Problem with :cd when editing file in non-existent directory. (Yee
Cheng Chin)
Solution: Prepend the current directory to get the full path. (closes vim/vim#8903)
c6376c7984
This commit is contained in:
parent
f71be1f87b
commit
0f58ba10e2
@ -2245,11 +2245,17 @@ int path_full_dir_name(char *directory, char *buffer, size_t len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (os_chdir(directory) != SUCCESS) {
|
if (os_chdir(directory) != SUCCESS) {
|
||||||
// Do not return immediately since we may be in the wrong directory.
|
// Path does not exist (yet). For a full path fail,
|
||||||
retval = FAIL;
|
// will use the path as-is. For a relative path use
|
||||||
}
|
// the current directory and append the file name.
|
||||||
|
if (path_is_absolute((const char_u *)directory)) {
|
||||||
if (retval == FAIL || os_dirname((char_u *)buffer, len) == FAIL) {
|
// Do not return immediately since we may be in the wrong directory.
|
||||||
|
retval = FAIL;
|
||||||
|
} else {
|
||||||
|
xstrlcpy(buffer, old_dir, len);
|
||||||
|
append_path(buffer, directory, len);
|
||||||
|
}
|
||||||
|
} else if (os_dirname((char_u *)buffer, len) == FAIL) {
|
||||||
// Do not return immediately since we are in the wrong directory.
|
// Do not return immediately since we are in the wrong directory.
|
||||||
retval = FAIL;
|
retval = FAIL;
|
||||||
}
|
}
|
||||||
|
@ -215,3 +215,22 @@ func Test_cd_from_non_existing_dir()
|
|||||||
cd -
|
cd -
|
||||||
call assert_equal(saveddir, getcwd())
|
call assert_equal(saveddir, getcwd())
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_cd_unknown_dir()
|
||||||
|
call mkdir('Xa')
|
||||||
|
cd Xa
|
||||||
|
call writefile(['text'], 'Xb.txt')
|
||||||
|
edit Xa/Xb.txt
|
||||||
|
let first_buf = bufnr()
|
||||||
|
cd ..
|
||||||
|
edit
|
||||||
|
call assert_equal(first_buf, bufnr())
|
||||||
|
edit Xa/Xb.txt
|
||||||
|
call assert_notequal(first_buf, bufnr())
|
||||||
|
|
||||||
|
bwipe!
|
||||||
|
exe "bwipe! " .. first_buf
|
||||||
|
call delete('Xa', 'rf')
|
||||||
|
endfunc
|
||||||
|
|
||||||
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
@ -29,7 +29,7 @@ describe('filename modifiers', function()
|
|||||||
call assert_equal('test.out', fnamemodify('test.out', ':.'))
|
call assert_equal('test.out', fnamemodify('test.out', ':.'))
|
||||||
call assert_equal('../testdir/a', fnamemodify('../testdir/a', ':.'))
|
call assert_equal('../testdir/a', fnamemodify('../testdir/a', ':.'))
|
||||||
call assert_equal(fnamemodify(tmpdir, ':~').'/test.out', fnamemodify('test.out', ':~'))
|
call assert_equal(fnamemodify(tmpdir, ':~').'/test.out', fnamemodify('test.out', ':~'))
|
||||||
call assert_equal('../testdir/a', fnamemodify('../testdir/a', ':~'))
|
call assert_equal(fnamemodify(tmpdir, ':~').'/../testdir/a', fnamemodify('../testdir/a', ':~'))
|
||||||
call assert_equal('a', fnamemodify('../testdir/a', ':t'))
|
call assert_equal('a', fnamemodify('../testdir/a', ':t'))
|
||||||
call assert_equal('', fnamemodify('.', ':p:t'))
|
call assert_equal('', fnamemodify('.', ':p:t'))
|
||||||
call assert_equal('test.out', fnamemodify('test.out', ':p:t'))
|
call assert_equal('test.out', fnamemodify('test.out', ':p:t'))
|
||||||
|
@ -143,8 +143,8 @@ describe('URI methods', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('uri_to_fname returns non-file scheme URI without authority unchanged', function()
|
it('uri_to_fname returns non-file scheme URI without authority unchanged', function()
|
||||||
eq('zipfile:/path/to/archive.zip%3A%3Afilename.txt', exec_lua [[
|
eq('zipfile:///path/to/archive.zip%3A%3Afilename.txt', exec_lua [[
|
||||||
return vim.uri_to_fname('zipfile:/path/to/archive.zip%3A%3Afilename.txt')
|
return vim.uri_to_fname('zipfile:///path/to/archive.zip%3A%3Afilename.txt')
|
||||||
]])
|
]])
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
@ -186,7 +186,7 @@ describe('URI methods', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris without authority', function()
|
it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris without authority', function()
|
||||||
local uri = 'zipfile:/path/to/archive.zip%3A%3Afilename.txt'
|
local uri = 'zipfile:///path/to/archive.zip%3A%3Afilename.txt'
|
||||||
local test_case = string.format([[
|
local test_case = string.format([[
|
||||||
local uri = '%s'
|
local uri = '%s'
|
||||||
return vim.uri_from_bufnr(vim.uri_to_bufnr(uri))
|
return vim.uri_from_bufnr(vim.uri_to_bufnr(uri))
|
||||||
|
@ -54,15 +54,21 @@ describe('path.c', function()
|
|||||||
eq(lfs.currentdir(), (ffi.string(buffer)))
|
eq(lfs.currentdir(), (ffi.string(buffer)))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
itp('fails if the given directory does not exist', function()
|
|
||||||
eq(FAIL, path_full_dir_name('does_not_exist', buffer, length))
|
|
||||||
end)
|
|
||||||
|
|
||||||
itp('works with a normal relative dir', function()
|
itp('works with a normal relative dir', function()
|
||||||
local result = path_full_dir_name('unit-test-directory', buffer, length)
|
local result = path_full_dir_name('unit-test-directory', buffer, length)
|
||||||
eq(lfs.currentdir() .. '/unit-test-directory', (ffi.string(buffer)))
|
eq(lfs.currentdir() .. '/unit-test-directory', (ffi.string(buffer)))
|
||||||
eq(OK, result)
|
eq(OK, result)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
itp('works with a non-existing relative dir', function()
|
||||||
|
local result = path_full_dir_name('does-not-exist', buffer, length)
|
||||||
|
eq(lfs.currentdir() .. '/does-not-exist', (ffi.string(buffer)))
|
||||||
|
eq(OK, result)
|
||||||
|
end)
|
||||||
|
|
||||||
|
itp('fails with a non-existing absolute dir', function()
|
||||||
|
eq(FAIL, path_full_dir_name('/does_not_exist', buffer, length))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('path_full_compare', function()
|
describe('path_full_compare', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user