Skip to content
forked from neovim/neovim

Commit

Permalink
refactor(tests): introduce testprg()
Browse files Browse the repository at this point in the history
Also:
- Add a describe('shell :!') section to system_spec.
- Make the test for neovim#16271 work on systems without powershell.
  • Loading branch information
justinmk authored and clason committed Jun 25, 2022
1 parent 175892f commit 71e2c49
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 104 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ if(BUSTED_PRG)
list(APPEND TEST_TARGET_ARGS "USES_TERMINAL")

set(UNITTEST_PREREQS nvim-test unittest-headers)
set(FUNCTIONALTEST_PREREQS nvim printenv-test printargs-test shell-test streams-test tty-test ${GENERATED_HELP_TAGS})
set(FUNCTIONALTEST_PREREQS nvim printenv-test printargs-test shell-test pwsh-test streams-test tty-test ${GENERATED_HELP_TAGS})
set(BENCHMARK_PREREQS nvim tty-test)

# Useful for automated build systems, if they want to manually run the tests.
Expand Down
14 changes: 7 additions & 7 deletions src/nvim/ex_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1577,11 +1577,11 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
size_t len = STRLEN(cmd) + 1; // At least enough space for cmd + NULL.

len += is_fish_shell ? sizeof("begin; " "; end") - 1
: is_pwsh ? STRLEN("Start-Process ")
: is_pwsh ? sizeof("Start-Process ")
: sizeof("(" ")") - 1;

if (itmp != NULL) {
len += is_pwsh ? STRLEN(itmp) + STRLEN(" -RedirectStandardInput ")
len += is_pwsh ? STRLEN(itmp) + sizeof(" -RedirectStandardInput ")
: STRLEN(itmp) + sizeof(" { " " < " " } ") - 1;
}
if (otmp != NULL) {
Expand Down Expand Up @@ -1609,16 +1609,16 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
} else {
xstrlcat(buf, " < ", len - 1);
}
xstrlcat(buf, (const char *)itmp, len - 1);
xstrlcat(buf, (char *)itmp, len - 1);
}
#else
// For shells that don't understand braces around commands, at least allow
// the use of commands in a pipe.
if (is_pwsh) {
xstrlcpy(buf, "Start-Process ", len);
xstrlcat(buf, (char *)cmd, len);
xstrlcat(buf, cmd, len);
} else {
xstrlcpy(buf, (char *)cmd, len);
xstrlcpy(buf, cmd, len);
}
if (itmp != NULL) {
// If there is a pipe, we have to put the '<' in front of it.
Expand All @@ -1635,9 +1635,9 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
} else {
xstrlcat(buf, " < ", len);
}
xstrlcat(buf, (const char *)itmp, len);
xstrlcat(buf, itmp, len);
if (*p_shq == NUL) {
const char *const p = find_pipe((const char *)cmd);
const char *const p = find_pipe(cmd);
if (p != NULL) {
xstrlcat(buf, " ", len - 1); // Insert a space before the '|' for DOS
xstrlcat(buf, p, len - 1);
Expand Down
6 changes: 3 additions & 3 deletions test/functional/autocmd/termxx_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local luv = require('luv')
local helpers = require('test.functional.helpers')(after_each)

local clear, command, nvim, nvim_dir =
helpers.clear, helpers.command, helpers.nvim, helpers.nvim_dir
local clear, command, nvim, testprg =
helpers.clear, helpers.command, helpers.nvim, helpers.testprg
local eval, eq, neq, retry =
helpers.eval, helpers.eq, helpers.neq, helpers.retry
local ok = helpers.ok
Expand All @@ -12,7 +12,7 @@ local iswin = helpers.iswin
describe('autocmd TermClose', function()
before_each(function()
clear()
nvim('set_option', 'shell', nvim_dir .. '/shell-test')
nvim('set_option', 'shell', testprg('shell-test'))
command('set shellcmdflag=EXE shellredir= shellpipe= shellquote= shellxquote=')
end)

Expand Down
7 changes: 3 additions & 4 deletions test/functional/core/job_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local helpers = require('test.functional.helpers')(after_each)
local clear, eq, eval, exc_exec, feed_command, feed, insert, neq, next_msg, nvim,
nvim_dir, ok, source, write_file, mkdir, rmdir = helpers.clear,
testprg, ok, source, write_file, mkdir, rmdir = helpers.clear,
helpers.eq, helpers.eval, helpers.exc_exec, helpers.feed_command, helpers.feed,
helpers.insert, helpers.neq, helpers.next_msg, helpers.nvim,
helpers.nvim_dir, helpers.ok, helpers.source,
helpers.testprg, helpers.ok, helpers.source,
helpers.write_file, helpers.mkdir, helpers.rmdir
local assert_alive = helpers.assert_alive
local command = helpers.command
Expand Down Expand Up @@ -1042,8 +1042,7 @@ describe('jobs', function()
return a:data
endfunction
]])
local ext = iswin() and '.exe' or ''
insert(nvim_dir..'/tty-test'..ext) -- Full path to tty-test.
insert(testprg('tty-test'))
nvim('command', 'let g:job_opts.pty = 1')
nvim('command', 'let exec = [expand("<cfile>:p")]')
nvim('command', "let j = jobstart(exec, g:job_opts)")
Expand Down
4 changes: 2 additions & 2 deletions test/functional/ex_cmds/ls_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local eq = helpers.eq
local eval = helpers.eval
local feed = helpers.feed
local nvim = helpers.nvim
local nvim_dir = helpers.nvim_dir
local testprg = helpers.testprg
local retry = helpers.retry

describe(':ls', function()
Expand All @@ -14,7 +14,7 @@ describe(':ls', function()
end)

it('R, F for :terminal buffers', function()
nvim('set_option', 'shell', string.format('"%s" INTERACT', nvim_dir..'/shell-test'))
nvim('set_option', 'shell', string.format('"%s" INTERACT', testprg('shell-test')))

command('edit foo')
command('set hidden')
Expand Down
6 changes: 3 additions & 3 deletions test/functional/ex_cmds/make_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local eval = helpers.eval
local has_powershell = helpers.has_powershell
local matches = helpers.matches
local nvim = helpers.nvim
local nvim_dir = helpers.nvim_dir
local testprg = helpers.testprg

describe(':make', function()
clear()
Expand All @@ -22,7 +22,7 @@ describe(':make', function()
end)

it('captures stderr & non zero exit code #14349', function ()
nvim('set_option', 'makeprg', nvim_dir..'/shell-test foo')
nvim('set_option', 'makeprg', testprg('shell-test')..' foo')
local out = eval('execute("make")')
-- Make program exit code correctly captured
matches('\nshell returned 3', out)
Expand All @@ -31,7 +31,7 @@ describe(':make', function()
end)

it('captures stderr & zero exit code #14349', function ()
nvim('set_option', 'makeprg', nvim_dir..'/shell-test')
nvim('set_option', 'makeprg', testprg('shell-test'))
local out = eval('execute("make")')
-- Ensure there are no "shell returned X" messages between
-- command and last line (indicating zero exit)
Expand Down
2 changes: 2 additions & 0 deletions test/functional/fixtures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ add_executable(tty-test EXCLUDE_FROM_ALL tty-test.c)
target_link_libraries(tty-test ${LIBUV_LIBRARIES})

add_executable(shell-test EXCLUDE_FROM_ALL shell-test.c)
# Fake pwsh (powershell) for testing make_filter_cmd(). #16271
add_executable(pwsh-test EXCLUDE_FROM_ALL shell-test.c)
add_executable(printargs-test EXCLUDE_FROM_ALL printargs-test.c)
add_executable(printenv-test EXCLUDE_FROM_ALL printenv-test.c)
if(WIN32)
Expand Down
25 changes: 22 additions & 3 deletions test/functional/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,17 @@ function module.has_powershell()
return module.eval('executable("'..(iswin() and 'powershell' or 'pwsh')..'")') == 1
end

function module.set_shell_powershell()
local shell = iswin() and 'powershell' or 'pwsh'
assert(module.has_powershell())
--- Sets Nvim shell to powershell.
---
--- @param fake (boolean) If true, a fake will be used if powershell is not
--- found on the system.
--- @returns true if powershell was found on the system, else false.
function module.set_shell_powershell(fake)
local found = module.has_powershell()
if not fake then
assert(found)
end
local shell = found and (iswin() and 'powershell' or 'pwsh') or module.testprg('pwsh-test')
local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
local cmd = set_encoding..'Remove-Item -Force '..table.concat(iswin()
and {'alias:cat', 'alias:echo', 'alias:sleep'}
Expand All @@ -515,6 +523,7 @@ function module.set_shell_powershell()
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
]])
return found
end

function module.nvim(method, ...)
Expand Down Expand Up @@ -774,11 +783,21 @@ function module.get_pathsep()
return iswin() and '\\' or '/'
end

--- Gets the filesystem root dir, namely "/" or "C:/".
function module.pathroot()
local pathsep = package.config:sub(1,1)
return iswin() and (module.nvim_dir:sub(1,2)..pathsep) or '/'
end

--- Gets the full `…/build/bin/{name}` path of a test program produced by
--- `test/functional/fixtures/CMakeLists.txt`.
---
--- @param name (string) Name of the test program.
function module.testprg(name)
local ext = module.iswin() and '.exe' or ''
return ('%s/%s%s'):format(module.nvim_dir, name, ext)
end

-- Returns a valid, platform-independent Nvim listen address.
-- Useful for communicating with child instances.
function module.new_pipename()
Expand Down
4 changes: 2 additions & 2 deletions test/functional/terminal/cursor_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local thelpers = require('test.functional.terminal.helpers')
local feed, clear, nvim = helpers.feed, helpers.clear, helpers.nvim
local nvim_dir, command = helpers.nvim_dir, helpers.command
local testprg, command = helpers.testprg, helpers.command
local nvim_prog = helpers.nvim_prog
local eq, eval = helpers.eq, helpers.eval
local feed_command = helpers.feed_command
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('cursor with customized highlighting', function()
[3] = {bold = true},
})
screen:attach({rgb=false})
command('call termopen(["'..nvim_dir..'/tty-test"])')
command('call termopen(["'..testprg('tty-test')..'"])')
feed_command('startinsert')
end)

Expand Down
4 changes: 2 additions & 2 deletions test/functional/terminal/edit_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local screen = require('test.functional.ui.screen')

local curbufmeths = helpers.curbufmeths
local curwinmeths = helpers.curwinmeths
local nvim_dir = helpers.nvim_dir
local testprg = helpers.testprg
local command = helpers.command
local funcs = helpers.funcs
local meths = helpers.meths
Expand All @@ -21,7 +21,7 @@ describe(':edit term://*', function()

before_each(function()
clear()
meths.set_option('shell', nvim_dir .. '/shell-test')
meths.set_option('shell', testprg('shell-test'))
meths.set_option('shellcmdflag', 'EXE')
end)

Expand Down
10 changes: 5 additions & 5 deletions test/functional/terminal/ex_terminal_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local assert_alive = helpers.assert_alive
local clear, poke_eventloop, nvim = helpers.clear, helpers.poke_eventloop, helpers.nvim
local nvim_dir, source, eq = helpers.nvim_dir, helpers.source, helpers.eq
local testprg, source, eq = helpers.testprg, helpers.source, helpers.eq
local feed = helpers.feed
local feed_command, eval = helpers.feed_command, helpers.eval
local funcs = helpers.funcs
Expand All @@ -28,7 +28,7 @@ describe(':terminal', function()
echomsg "msg3"
]])
-- Invoke a command that emits frequent terminal activity.
feed([[:terminal "]]..nvim_dir..[[/shell-test" REP 9999 !terminal_output!<cr>]])
feed([[:terminal "]]..testprg('shell-test')..[[" REP 9999 !terminal_output!<cr>]])
feed([[<C-\><C-N>]])
poke_eventloop()
-- Wait for some terminal activity.
Expand Down Expand Up @@ -131,7 +131,7 @@ describe(':terminal (with fake shell)', function()
screen = Screen.new(50, 4)
screen:attach({rgb=false})
-- shell-test.c is a fake shell that prints its arguments and exits.
nvim('set_option', 'shell', nvim_dir..'/shell-test')
nvim('set_option', 'shell', testprg('shell-test'))
nvim('set_option', 'shellcmdflag', 'EXE')
end)

Expand Down Expand Up @@ -167,7 +167,7 @@ describe(':terminal (with fake shell)', function()

it("with no argument, but 'shell' has arguments, acts like termopen()", function()
if helpers.pending_win32(pending) then return end
nvim('set_option', 'shell', nvim_dir..'/shell-test -t jeff')
nvim('set_option', 'shell', testprg('shell-test')..' -t jeff')
terminal_with_fake_shell()
screen:expect([[
^jeff $ |
Expand All @@ -191,7 +191,7 @@ describe(':terminal (with fake shell)', function()

it("executes a given command through the shell, when 'shell' has arguments", function()
if helpers.pending_win32(pending) then return end
nvim('set_option', 'shell', nvim_dir..'/shell-test -t jeff')
nvim('set_option', 'shell', testprg('shell-test')..' -t jeff')
command('set shellxquote=') -- win: avoid extra quotes
terminal_with_fake_shell('echo hi')
screen:expect([[
Expand Down
4 changes: 2 additions & 2 deletions test/functional/terminal/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- operate on the _host_ session, _not_ the child session.
local helpers = require('test.functional.helpers')(nil)
local Screen = require('test.functional.ui.screen')
local nvim_dir = helpers.nvim_dir
local testprg = helpers.testprg
local feed_command, nvim = helpers.feed_command, helpers.nvim

local function feed_data(data)
Expand Down Expand Up @@ -37,7 +37,7 @@ local function clear_attrs() feed_termcode('[0;10m') end
local function enable_mouse() feed_termcode('[?1002h') end
local function disable_mouse() feed_termcode('[?1002l') end

local default_command = '["'..nvim_dir..'/tty-test'..'"]'
local default_command = '["'..testprg('tty-test')..'"]'

local function screen_setup(extra_rows, command, cols, opts)
extra_rows = extra_rows and extra_rows or 0
Expand Down
8 changes: 4 additions & 4 deletions test/functional/terminal/highlight_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local thelpers = require('test.functional.terminal.helpers')
local feed, clear, nvim = helpers.feed, helpers.clear, helpers.nvim
local nvim_dir, command = helpers.nvim_dir, helpers.command
local testprg, command = helpers.testprg, helpers.command
local nvim_prog_abs = helpers.nvim_prog_abs
local eq, eval = helpers.eq, helpers.eval
local funcs = helpers.funcs
Expand All @@ -28,7 +28,7 @@ describe(':terminal highlight', function()
[11] = {background = 11},
})
screen:attach({rgb=false})
command('enew | call termopen(["'..nvim_dir..'/tty-test"])')
command(("enew | call termopen(['%s'])"):format(testprg('tty-test')))
feed('i')
screen:expect([[
tty ready |
Expand Down Expand Up @@ -174,7 +174,7 @@ describe(':terminal highlight forwarding', function()
[4] = {{foreground = tonumber('0xff8000')}, {}},
})
screen:attach()
command('enew | call termopen(["'..nvim_dir..'/tty-test"])')
command(("enew | call termopen(['%s'])"):format(testprg('tty-test')))
feed('i')
screen:expect([[
tty ready |
Expand Down Expand Up @@ -226,7 +226,7 @@ describe(':terminal highlight with custom palette', function()
})
screen:attach({rgb=true})
nvim('set_var', 'terminal_color_3', '#123456')
command('enew | call termopen(["'..nvim_dir..'/tty-test"])')
command(("enew | call termopen(['%s'])"):format(testprg('tty-test')))
feed('i')
screen:expect([[
tty ready |
Expand Down
12 changes: 6 additions & 6 deletions test/functional/terminal/scrollback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Screen = require('test.functional.ui.screen')
local helpers = require('test.functional.helpers')(after_each)
local thelpers = require('test.functional.terminal.helpers')
local clear, eq, curbuf = helpers.clear, helpers.eq, helpers.curbuf
local feed, nvim_dir, feed_command = helpers.feed, helpers.nvim_dir, helpers.feed_command
local feed, testprg, feed_command = helpers.feed, helpers.testprg, helpers.feed_command
local iswin = helpers.iswin
local eval = helpers.eval
local command = helpers.command
Expand Down Expand Up @@ -349,7 +349,7 @@ describe(':terminal prints more lines than the screen height and exits', functio
clear()
local screen = Screen.new(30, 7)
screen:attach({rgb=false})
feed_command('call termopen(["'..nvim_dir..'/tty-test", "10"]) | startinsert')
feed_command(("call termopen(['%s', '10']) | startinsert"):format(testprg('tty-test')))
poke_eventloop()
screen:expect([[
line6 |
Expand Down Expand Up @@ -381,7 +381,7 @@ describe("'scrollback' option", function()

local function set_fake_shell()
-- shell-test.c is a fake shell that prints its arguments and exits.
nvim('set_option', 'shell', nvim_dir..'/shell-test')
nvim('set_option', 'shell', testprg('shell-test'))
nvim('set_option', 'shellcmdflag', 'EXE')
end

Expand All @@ -402,7 +402,7 @@ describe("'scrollback' option", function()
end

curbufmeths.set_option('scrollback', 0)
feed_data(nvim_dir..'/shell-test REP 31 line'..(iswin() and '\r' or '\n'))
feed_data(('%s REP 31 line%s'):format(testprg('shell-test'), iswin() and '\r' or '\n'))
screen:expect{any='30: line '}
retry(nil, nil, function() expect_lines(7) end)
end)
Expand All @@ -422,7 +422,7 @@ describe("'scrollback' option", function()
-- Wait for prompt.
screen:expect{any='%$'}

feed_data(nvim_dir.."/shell-test REP 31 line"..(iswin() and '\r' or '\n'))
feed_data(('%s REP 31 line%s'):format(testprg('shell-test'), iswin() and '\r' or '\n'))
screen:expect{any='30: line '}

retry(nil, nil, function() expect_lines(33, 2) end)
Expand All @@ -435,7 +435,7 @@ describe("'scrollback' option", function()
-- 'scrollback' option is synchronized with the internal sb_buffer.
command('sleep 100m')

feed_data(nvim_dir.."/shell-test REP 41 line"..(iswin() and '\r' or '\n'))
feed_data(('%s REP 41 line%s'):format(testprg('shell-test'), iswin() and '\r' or '\n'))
if iswin() then
screen:expect{grid=[[
37: line |
Expand Down
Loading

0 comments on commit 71e2c49

Please sign in to comment.