win: vim_FullName(): force backslashes #7287

- Replace obvious cases of '/' literal with PATHSEP. (There are still
  some remaining cases that need closer inspection.)
- Fixup tests: ui/screen_basic

closes #7117
ref https://github.com/neovim/neovim/issues/2471#issuecomment-271193714
This commit is contained in:
Ignas Anikevicius
2017-09-18 21:06:55 +03:00
committed by Justin M. Keyes
parent 981387b7c8
commit 2b133101cf
4 changed files with 92 additions and 3 deletions

View File

@@ -1690,6 +1690,9 @@ int vim_FullName(const char *fname, char *buf, size_t len, bool force)
if (strlen(fname) > (len - 1)) { if (strlen(fname) > (len - 1)) {
xstrlcpy(buf, fname, len); // truncate xstrlcpy(buf, fname, len); // truncate
#ifdef WIN32
slash_adjust(buf);
#endif
return FAIL; return FAIL;
} }
@@ -1702,6 +1705,9 @@ int vim_FullName(const char *fname, char *buf, size_t len, bool force)
if (rv == FAIL) { if (rv == FAIL) {
xstrlcpy(buf, fname, len); // something failed; use the filename xstrlcpy(buf, fname, len); // something failed; use the filename
} }
#ifdef WIN32
slash_adjust(buf);
#endif
return rv; return rv;
} }
@@ -2196,11 +2202,11 @@ static int path_get_absolute_path(const char_u *fname, char_u *buf,
// expand it if forced or not an absolute path // expand it if forced or not an absolute path
if (force || !path_is_absolute_path(fname)) { if (force || !path_is_absolute_path(fname)) {
if ((p = vim_strrchr(fname, '/')) != NULL) { if ((p = vim_strrchr(fname, PATHSEP)) != NULL) {
// relative to root // relative to root
if (p == fname) { if (p == fname) {
// only one path component // only one path component
relative_directory[0] = '/'; relative_directory[0] = PATHSEP;
relative_directory[1] = NUL; relative_directory[1] = NUL;
} else { } else {
assert(p >= fname); assert(p >= fname);

View File

@@ -0,0 +1,61 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local eq = helpers.eq
local eval = helpers.eval
local get_pathsep = helpers.get_pathsep
local command = helpers.command
describe("'%:p' expanding", function()
local pathsep
local targetdir
local expected_path
local function get_full_path()
return eval('expand("%:p")')
end
local function join_path(...)
return table.concat({...}, pathsep)
end
before_each(function()
clear()
pathsep = get_pathsep()
targetdir = join_path('test', 'functional', 'fixtures')
clear(join_path(targetdir, 'tty-test.c'))
expected_path = get_full_path()
end)
it('given a relative path with current directory in the middle #7117', function()
clear(join_path(targetdir, '.', 'tty-test.c'))
eq(expected_path, get_full_path())
end)
it('given a relative path with current directory #7117', function()
clear(join_path('.', targetdir, 'tty-test.c'))
eq(expected_path, get_full_path())
end)
it('given a relative path with current directory to a file when changing directory #7117', function()
clear(join_path('.', targetdir, 'tty-test.c'))
command('cd test')
eq(expected_path, get_full_path())
end)
it('given a relative path with directory up the tree to a file #7117', function()
clear(join_path(targetdir, '..', 'fixtures', 'tty-test.c'))
eq(expected_path, get_full_path())
end)
it('given a different starting directory and a relative path with directory up the tree #7117', function()
command('cd test')
command('e ' .. join_path('..', targetdir, 'tty-test.c'))
eq(expected_path, get_full_path())
end)
it('given a different starting directory and a relative path with current directory and up the tree #7117', function()
command('cd test')
command('e ' .. join_path('.', '..', targetdir, 'tty-test.c'))
eq(expected_path, get_full_path())
end)
end)

View File

@@ -5,6 +5,7 @@ local feed, command = helpers.feed, helpers.command
local insert = helpers.insert local insert = helpers.insert
local eq = helpers.eq local eq = helpers.eq
local eval = helpers.eval local eval = helpers.eval
local iswin = helpers.iswin
describe('screen', function() describe('screen', function()
local screen local screen
@@ -120,8 +121,15 @@ describe('Screen', function()
it('has correct default title with named file', function() it('has correct default title with named file', function()
local expected = 'myfile (/mydir) - NVIM' local expected = 'myfile (/mydir) - NVIM'
if iswin() then
expected = 'myfile (C:\\mydir) - NVIM'
end
command('set title') command('set title')
if iswin() then
command('file C:\\mydir\\myfile')
else
command('file /mydir/myfile') command('file /mydir/myfile')
end
screen:expect(function() screen:expect(function()
eq(expected, screen.title) eq(expected, screen.title)
end) end)

View File

@@ -481,6 +481,20 @@ describe('path.c', function()
eq('/tmp', ffi.string(buffer)) eq('/tmp', ffi.string(buffer))
eq(OK, result) eq(OK, result)
end) end)
itp('works with a relative path with the current directory prefix #7117', function()
local force_expansion = 1
local result = vim_FullName('./unit-test-directory/test.file', buffer, length, force_expansion)
eq(OK, result)
eq(lfs.currentdir() .. '/unit-test-directory/test.file', (ffi.string(buffer)))
end)
itp('works with a relative path with the directory name mentioned twice #7117', function()
local force_expansion = 1
local result = vim_FullName('unit-test-directory/../unit-test-directory/test.file', buffer, length, force_expansion)
eq(OK, result)
eq(lfs.currentdir() .. '/unit-test-directory/test.file', (ffi.string(buffer)))
end)
end) end)
describe('path_fix_case', function() describe('path_fix_case', function()