2015-08-24 03:58:28 -07:00
|
|
|
-- Specs for :cd, :tcd, :lcd and getcwd()
|
|
|
|
|
2016-04-26 12:14:33 -07:00
|
|
|
local lfs = require('lfs')
|
2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2016-05-31 19:35:02 -07:00
|
|
|
|
|
|
|
local eq = helpers.eq
|
|
|
|
local call = helpers.call
|
|
|
|
local clear = helpers.clear
|
2017-04-08 14:12:26 -07:00
|
|
|
local command = helpers.command
|
2016-05-31 19:35:02 -07:00
|
|
|
local exc_exec = helpers.exc_exec
|
2017-11-11 01:14:44 -07:00
|
|
|
local pathsep = helpers.get_pathsep()
|
2016-08-15 16:42:12 -07:00
|
|
|
|
2015-08-24 03:58:28 -07:00
|
|
|
-- These directories will be created for testing
|
|
|
|
local directories = {
|
2016-05-31 20:30:21 -07:00
|
|
|
tab = 'Xtest-functional-ex_cmds-cd_spec.tab', -- Tab
|
|
|
|
window = 'Xtest-functional-ex_cmds-cd_spec.window', -- Window
|
|
|
|
global = 'Xtest-functional-ex_cmds-cd_spec.global', -- New global
|
2015-08-24 03:58:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
-- Shorthand writing to get the current working directory
|
2016-05-31 19:35:02 -07:00
|
|
|
local cwd = function(...) return call('getcwd', ...) end -- effective working dir
|
|
|
|
local wcwd = function() return cwd(0) end -- window dir
|
|
|
|
local tcwd = function() return cwd(-1, 0) end -- tab dir
|
2015-08-24 03:58:28 -07:00
|
|
|
|
|
|
|
-- Same, except these tell us if there is a working directory at all
|
2016-05-31 19:35:02 -07:00
|
|
|
local lwd = function(...) return call('haslocaldir', ...) end -- effective working dir
|
|
|
|
local wlwd = function() return lwd(0) end -- window dir
|
|
|
|
local tlwd = function() return lwd(-1, 0) end -- tab dir
|
2016-04-26 12:14:33 -07:00
|
|
|
--local glwd = function() return eval('haslocaldir(-1, -1)') end -- global dir
|
2015-08-24 03:58:28 -07:00
|
|
|
|
|
|
|
-- Test both the `cd` and `chdir` variants
|
|
|
|
for _, cmd in ipairs {'cd', 'chdir'} do
|
2016-05-31 20:30:21 -07:00
|
|
|
describe(':' .. cmd, function()
|
2015-08-24 03:58:28 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
2016-05-31 20:30:21 -07:00
|
|
|
for _, d in pairs(directories) do
|
2015-08-24 03:58:28 -07:00
|
|
|
lfs.mkdir(d)
|
|
|
|
end
|
2016-05-31 21:23:59 -07:00
|
|
|
directories.start = cwd()
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
2016-05-31 20:30:21 -07:00
|
|
|
for _, d in pairs(directories) do
|
2015-08-24 03:58:28 -07:00
|
|
|
lfs.rmdir(d)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2016-05-31 21:23:59 -07:00
|
|
|
describe('using explicit scope', function()
|
|
|
|
it('for window', function()
|
|
|
|
local globalDir = directories.start
|
|
|
|
local globalwin = call('winnr')
|
|
|
|
local tabnr = call('tabpagenr')
|
|
|
|
|
|
|
|
-- Everything matches globalDir to start
|
|
|
|
eq(globalDir, cwd(globalwin))
|
|
|
|
eq(globalDir, cwd(globalwin, tabnr))
|
|
|
|
eq(0, lwd(globalwin))
|
|
|
|
eq(0, lwd(globalwin, tabnr))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('bot split')
|
2016-05-31 21:23:59 -07:00
|
|
|
local localwin = call('winnr')
|
|
|
|
-- Initial window is still using globalDir
|
|
|
|
eq(globalDir, cwd(localwin))
|
|
|
|
eq(globalDir, cwd(localwin, tabnr))
|
|
|
|
eq(0, lwd(globalwin))
|
|
|
|
eq(0, lwd(globalwin, tabnr))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent l' .. cmd .. ' ' .. directories.window)
|
2016-05-31 21:23:59 -07:00
|
|
|
-- From window with local dir, the original window
|
|
|
|
-- is still reporting the global dir
|
|
|
|
eq(globalDir, cwd(globalwin))
|
|
|
|
eq(globalDir, cwd(globalwin, tabnr))
|
|
|
|
eq(0, lwd(globalwin))
|
|
|
|
eq(0, lwd(globalwin, tabnr))
|
|
|
|
|
|
|
|
-- Window with local dir reports as such
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.window, cwd(localwin))
|
|
|
|
eq(globalDir .. pathsep .. directories.window, cwd(localwin, tabnr))
|
2016-05-31 21:23:59 -07:00
|
|
|
eq(1, lwd(localwin))
|
|
|
|
eq(1, lwd(localwin, tabnr))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnew')
|
2016-05-31 21:23:59 -07:00
|
|
|
-- From new tab page, original window reports global dir
|
|
|
|
eq(globalDir, cwd(globalwin, tabnr))
|
|
|
|
eq(0, lwd(globalwin, tabnr))
|
|
|
|
|
|
|
|
-- From new tab page, local window reports as such
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.window, cwd(localwin, tabnr))
|
2016-05-31 21:23:59 -07:00
|
|
|
eq(1, lwd(localwin, tabnr))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('for tab page', function()
|
|
|
|
local globalDir = directories.start
|
|
|
|
local globaltab = call('tabpagenr')
|
2015-08-24 03:58:28 -07:00
|
|
|
|
2016-05-31 21:23:59 -07:00
|
|
|
-- Everything matches globalDir to start
|
|
|
|
eq(globalDir, cwd(-1, 0))
|
|
|
|
eq(globalDir, cwd(-1, globaltab))
|
|
|
|
eq(0, lwd(-1, 0))
|
|
|
|
eq(0, lwd(-1, globaltab))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnew')
|
|
|
|
command('silent t' .. cmd .. ' ' .. directories.tab)
|
2016-05-31 21:23:59 -07:00
|
|
|
local localtab = call('tabpagenr')
|
|
|
|
|
|
|
|
-- From local tab page, original tab reports globalDir
|
|
|
|
eq(globalDir, cwd(-1, globaltab))
|
|
|
|
eq(0, lwd(-1, globaltab))
|
|
|
|
|
|
|
|
-- new tab reports local
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd(-1, 0))
|
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd(-1, localtab))
|
2016-05-31 21:23:59 -07:00
|
|
|
eq(1, lwd(-1, 0))
|
|
|
|
eq(1, lwd(-1, localtab))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext')
|
2016-05-31 21:23:59 -07:00
|
|
|
-- From original tab page, local reports as such
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd(-1, localtab))
|
2016-05-31 21:23:59 -07:00
|
|
|
eq(1, lwd(-1, localtab))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-05-31 21:40:03 -07:00
|
|
|
describe('getcwd(-1, -1)', function()
|
|
|
|
it('works', function()
|
|
|
|
eq(directories.start, cwd(-1, -1))
|
|
|
|
eq(0, lwd(-1, -1))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with tab-local pwd', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent t' .. cmd .. ' ' .. directories.tab)
|
2016-05-31 21:40:03 -07:00
|
|
|
eq(directories.start, cwd(-1, -1))
|
|
|
|
eq(0, lwd(-1, -1))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with window-local pwd', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent l' .. cmd .. ' ' .. directories.window)
|
2016-05-31 21:40:03 -07:00
|
|
|
eq(directories.start, cwd(-1, -1))
|
|
|
|
eq(0, lwd(-1, -1))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-09-03 18:40:01 -07:00
|
|
|
describe('Local directory gets inherited', function()
|
|
|
|
it('by tabs', function()
|
|
|
|
local globalDir = directories.start
|
|
|
|
|
|
|
|
-- Create a new tab and change directory
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnew')
|
|
|
|
command('silent t' .. cmd .. ' ' .. directories.tab)
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2016-09-03 18:40:01 -07:00
|
|
|
|
|
|
|
-- Create a new tab and verify it has inherited the directory
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnew')
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2016-09-03 18:40:01 -07:00
|
|
|
|
|
|
|
-- Change tab and change back, verify that directories are correct
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext')
|
2016-09-03 18:40:01 -07:00
|
|
|
eq(globalDir, tcwd())
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabprevious')
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2016-09-03 18:40:01 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-05-31 21:23:59 -07:00
|
|
|
it('works', function()
|
|
|
|
local globalDir = directories.start
|
2015-08-24 03:58:28 -07:00
|
|
|
-- Create a new tab first and verify that is has the same working dir
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnew')
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(globalDir, cwd())
|
|
|
|
eq(globalDir, tcwd()) -- has no tab-local directory
|
|
|
|
eq(0, tlwd())
|
|
|
|
eq(globalDir, wcwd()) -- has no window-local directory
|
|
|
|
eq(0, wlwd())
|
|
|
|
|
|
|
|
-- Change tab-local working directory and verify it is different
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent t' .. cmd .. ' ' .. directories.tab)
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd())
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(cwd(), tcwd()) -- working directory maches tab directory
|
|
|
|
eq(1, tlwd())
|
|
|
|
eq(cwd(), wcwd()) -- still no window-directory
|
|
|
|
eq(0, wlwd())
|
|
|
|
|
|
|
|
-- Create a new window in this tab to test `:lcd`
|
2017-04-08 14:12:26 -07:00
|
|
|
command('new')
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(1, tlwd()) -- Still tab-local working directory
|
|
|
|
eq(0, wlwd()) -- Still no window-local working directory
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd())
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent l' .. cmd .. ' ../' .. directories.window)
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.window, cwd())
|
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(1, wlwd())
|
|
|
|
|
|
|
|
-- Verify the first window still has the tab local directory
|
2017-04-08 14:12:26 -07:00
|
|
|
command('wincmd w')
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd())
|
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(0, wlwd()) -- No window-local directory
|
|
|
|
|
|
|
|
-- Change back to initial tab and verify working directory has stayed
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext')
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(globalDir, cwd() )
|
|
|
|
eq(0, tlwd())
|
|
|
|
eq(0, wlwd())
|
|
|
|
|
|
|
|
-- Verify global changes don't affect local ones
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent ' .. cmd .. ' ' .. directories.global)
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.global, cwd())
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext')
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.tab, cwd())
|
|
|
|
eq(globalDir .. pathsep .. directories.tab, tcwd())
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(0, wlwd()) -- Still no window-local directory in this window
|
|
|
|
|
|
|
|
-- Unless the global change happened in a tab with local directory
|
2017-04-08 14:12:26 -07:00
|
|
|
command('silent ' .. cmd .. ' ..')
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(globalDir, cwd() )
|
|
|
|
eq(0 , tlwd())
|
|
|
|
eq(0 , wlwd())
|
|
|
|
-- Which also affects the first tab
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext')
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(globalDir, cwd())
|
|
|
|
|
|
|
|
-- But not in a window with its own local directory
|
2017-04-08 14:12:26 -07:00
|
|
|
command('tabnext | wincmd w')
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.window, cwd() )
|
2015-08-24 03:58:28 -07:00
|
|
|
eq(0 , tlwd())
|
2017-11-11 01:14:44 -07:00
|
|
|
eq(globalDir .. pathsep .. directories.window, wcwd())
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Test legal parameters for 'getcwd' and 'haslocaldir'
|
|
|
|
for _, cmd in ipairs {'getcwd', 'haslocaldir'} do
|
2016-04-21 00:04:10 -07:00
|
|
|
describe(cmd..'()', function()
|
2016-05-06 16:55:14 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
2015-08-24 03:58:28 -07:00
|
|
|
-- Test invalid argument types
|
2016-04-26 12:14:33 -07:00
|
|
|
local err474 = 'Vim(call):E474: Invalid argument'
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on string', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '("some string")'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on float', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '(1.0)'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on list', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '([1, 2])'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on dictionary', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '({"key": "value"})'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on funcref', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '(function("tr"))'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
|
2015-08-24 03:58:28 -07:00
|
|
|
-- Test invalid numbers
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on number less than -1', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err474, exc_exec('call ' .. cmd .. '(-2)'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-26 12:14:33 -07:00
|
|
|
local err5001 = 'Vim(call):E5001: Higher scope cannot be -1 if lower scope is >= 0.'
|
2016-04-21 00:04:10 -07:00
|
|
|
it('fails on -1 if previous arg is >=0', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err5001, exc_exec('call ' .. cmd .. '(0, -1)'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
2016-04-21 00:04:10 -07:00
|
|
|
|
2015-08-24 03:58:28 -07:00
|
|
|
-- Test wrong number of arguments
|
2016-04-26 12:14:33 -07:00
|
|
|
local err118 = 'Vim(call):E118: Too many arguments for function: ' .. cmd
|
2015-08-24 03:58:28 -07:00
|
|
|
it('fails to parse more than one argument', function()
|
2016-04-26 12:14:33 -07:00
|
|
|
eq(err118, exc_exec('call ' .. cmd .. '(0, 0, 0)'))
|
2015-08-24 03:58:28 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2016-09-04 03:28:01 -07:00
|
|
|
describe("getcwd()", function ()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2016-09-11 18:18:48 -07:00
|
|
|
lfs.mkdir(directories.global)
|
2016-09-04 03:28:01 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
2016-09-11 18:18:48 -07:00
|
|
|
helpers.rmdir(directories.global)
|
2016-09-04 03:28:01 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("returns empty string if working directory does not exist", function()
|
2017-11-11 01:14:44 -07:00
|
|
|
if helpers.iswin() then
|
|
|
|
return
|
|
|
|
end
|
2017-04-08 14:12:26 -07:00
|
|
|
command("cd "..directories.global)
|
|
|
|
command("call delete('../"..directories.global.."', 'd')")
|
2016-09-04 03:28:01 -07:00
|
|
|
eq("", helpers.eval("getcwd()"))
|
|
|
|
end)
|
2019-04-13 03:50:36 -07:00
|
|
|
|
|
|
|
it("works with 'autochdir' after local directory was set (#9892)", function()
|
|
|
|
local curdir = cwd()
|
|
|
|
command('lcd ' .. directories.global)
|
|
|
|
command('lcd -')
|
|
|
|
command('set autochdir')
|
|
|
|
command('edit ' .. directories.global .. '/foo')
|
|
|
|
eq(curdir .. pathsep .. directories.global, cwd())
|
|
|
|
end)
|
2016-09-04 03:28:01 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
|