Merge #8276 'startup: Make -s - read from stdin'

This commit is contained in:
Justin M. Keyes
2018-04-17 10:33:36 +02:00
committed by GitHub
15 changed files with 398 additions and 152 deletions

View File

@@ -0,0 +1,131 @@
local lfs = require('lfs')
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local eq = helpers.eq
local feed = helpers.feed
local eval = helpers.eval
local clear = helpers.clear
local funcs = helpers.funcs
local nvim_prog = helpers.nvim_prog
local write_file = helpers.write_file
local function nvim_prog_abs()
-- system(['build/bin/nvim']) does not work for whatever reason. It needs to
-- either be executable searched in $PATH or something starting with / or ./.
if nvim_prog:match('[/\\]') then
return funcs.fnamemodify(nvim_prog, ':p')
else
return nvim_prog
end
end
describe('Command-line option', function()
describe('-s', function()
local fname = 'Xtest-functional-core-main-s'
local fname_2 = fname .. '.2'
local nonexistent_fname = fname .. '.nonexistent'
local dollar_fname = '$' .. fname
before_each(function()
clear()
os.remove(fname)
os.remove(dollar_fname)
end)
after_each(function()
os.remove(fname)
os.remove(dollar_fname)
end)
it('treats - as stdin', function()
eq(nil, lfs.attributes(fname))
funcs.system(
{nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
'--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
'-s', '-', fname},
{':call setline(1, "42")', ':wqall!', ''})
eq(0, eval('v:shell_error'))
local attrs = lfs.attributes(fname)
eq(#('42\n'), attrs.size)
end)
it('does not expand $VAR', function()
eq(nil, lfs.attributes(fname))
eq(true, not not dollar_fname:find('%$%w+'))
write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n')
funcs.system(
{nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
'--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
'-s', dollar_fname, fname})
eq(0, eval('v:shell_error'))
local attrs = lfs.attributes(fname)
eq(#('100500\n'), attrs.size)
end)
it('does not crash after reading from stdin in non-headless mode', function()
if helpers.pending_win32(pending) then return end
local screen = Screen.new(40, 8)
screen:attach()
funcs.termopen({
nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE',
'--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
'-s', '-'
})
screen:expect([[
^ |
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{2:[No Name] 0,0-1 All}|
|
|
]], {
[1] = {foreground = 4210943, special = Screen.colors.Grey0},
[2] = {special = Screen.colors.Grey0, bold = true, reverse = true}
})
feed('i:cq<CR><C-\\><C-n>')
screen:expect([[
^ |
[Process exited 1] |
|
|
|
|
|
|
]])
--[=[ Example of incorrect output:
screen:expect([[
^nvim: /var/tmp/portage/dev-libs/libuv-1.|
10.2/work/libuv-1.10.2/src/unix/core.c:5|
19: uv__close: Assertion `fd > STDERR_FI|
LENO' failed. |
|
[Process exited 6] |
|
|
]])
]=]
end)
it('errors out when trying to use nonexistent file with -s', function()
eq(
'Cannot open for reading: "'..nonexistent_fname..'": no such file or directory\n',
funcs.system(
{nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
'--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
'--cmd', 'language C',
'-s', nonexistent_fname}))
eq(2, eval('v:shell_error'))
end)
it('errors out when trying to use -s twice', function()
write_file(fname, ':call setline(1, "1")\n:wqall!\n')
write_file(dollar_fname, ':call setline(1, "2")\n:wqall!\n')
eq(
'Attempt to open script file again: "-s '..dollar_fname..'"\n',
funcs.system(
{nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless',
'--cmd', 'set noswapfile shortmess+=IFW fileformats=unix',
'--cmd', 'language C',
'-s', fname, '-s', dollar_fname, fname_2}))
eq(2, eval('v:shell_error'))
eq(nil, lfs.attributes(fname_2))
end)
end)
end)

View File

@@ -1,6 +1,38 @@
local assert = require('luassert')
local lfs = require('lfs')
local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote)
local function shell_quote(str)
if string.find(str, quote_me) or str == '' then
return '"' .. str:gsub('[$%%"\\]', '\\%0') .. '"'
else
return str
end
end
local function argss_to_cmd(...)
local cmd = ''
for i = 1, select('#', ...) do
local arg = select(i, ...)
if type(arg) == 'string' then
cmd = cmd .. ' ' ..shell_quote(arg)
else
for _, subarg in ipairs(arg) do
cmd = cmd .. ' ' .. shell_quote(subarg)
end
end
end
return cmd
end
local function popen_r(...)
return io.popen(argss_to_cmd(...), 'r')
end
local function popen_w(...)
return io.popen(argss_to_cmd(...), 'w')
end
local check_logs_useless_lines = {
['Warning: noted but unhandled ioctl']=1,
['could cause spurious value errors to appear']=2,
@@ -121,7 +153,7 @@ local uname = (function()
return platform
end
local status, f = pcall(io.popen, "uname -s")
local status, f = pcall(popen_r, 'uname', '-s')
if status then
platform = f:read("*l")
f:close()
@@ -253,7 +285,7 @@ local function check_cores(app, force)
end
local function which(exe)
local pipe = io.popen('which ' .. exe, 'r')
local pipe = popen_r('which', exe)
local ret = pipe:read('*a')
pipe:close()
if ret == '' then
@@ -263,6 +295,19 @@ local function which(exe)
end
end
local function repeated_read_cmd(...)
for _ = 1, 10 do
local stream = popen_r(...)
local ret = stream:read('*a')
stream:close()
if ret then
return ret
end
end
print('ERROR: Failed to execute ' .. argss_to_cmd(...) .. ': nil return after 10 attempts')
return nil
end
local function shallowcopy(orig)
if type(orig) ~= 'table' then
return orig
@@ -569,6 +614,7 @@ end
return {
REMOVE_THIS = REMOVE_THIS,
argss_to_cmd = argss_to_cmd,
check_cores = check_cores,
check_logs = check_logs,
concat_tables = concat_tables,
@@ -590,6 +636,9 @@ return {
mergedicts_copy = mergedicts_copy,
neq = neq,
ok = ok,
popen_r = popen_r,
popen_w = popen_w,
repeated_read_cmd = repeated_read_cmd,
shallowcopy = shallowcopy,
table_flatten = table_flatten,
tmpname = tmpname,

View File

@@ -1,4 +1,6 @@
#define _GNU_SOURCE
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <sys/stat.h>
static const mode_t kS_IFMT = S_IFMT;

View File

@@ -779,7 +779,7 @@ local function gen_itp(it)
end
local function cppimport(path)
return cimport(Paths.test_include_path .. '/' .. path)
return cimport(Paths.test_source_path .. '/test/includes/pre/' .. path)
end
cimport('./src/nvim/types.h', './src/nvim/main.h', './src/nvim/os/time.h')

View File

@@ -113,7 +113,7 @@ end
describe('file_open_fd', function()
itp('can use file descriptor returned by os_open for reading', function()
local fd = m.os_open(file1, m.kO_RDONLY, 0)
local err, fp = file_open_fd(fd, false)
local err, fp = file_open_fd(fd, m.kFileReadOnly)
eq(0, err)
eq({#fcontents, fcontents}, {file_read(fp, #fcontents)})
eq(0, m.file_close(fp, false))
@@ -121,7 +121,7 @@ describe('file_open_fd', function()
itp('can use file descriptor returned by os_open for writing', function()
eq(nil, lfs.attributes(filec))
local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
local err, fp = file_open_fd(fd, true)
local err, fp = file_open_fd(fd, m.kFileWriteOnly)
eq(0, err)
eq(4, file_write(fp, 'test'))
eq(0, m.file_close(fp, false))
@@ -133,7 +133,7 @@ end)
describe('file_open_fd_new', function()
itp('can use file descriptor returned by os_open for reading', function()
local fd = m.os_open(file1, m.kO_RDONLY, 0)
local err, fp = file_open_fd_new(fd, false)
local err, fp = file_open_fd_new(fd, m.kFileReadOnly)
eq(0, err)
eq({#fcontents, fcontents}, {file_read(fp, #fcontents)})
eq(0, m.file_free(fp, false))
@@ -141,7 +141,7 @@ describe('file_open_fd_new', function()
itp('can use file descriptor returned by os_open for writing', function()
eq(nil, lfs.attributes(filec))
local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
local err, fp = file_open_fd_new(fd, true)
local err, fp = file_open_fd_new(fd, m.kFileWriteOnly)
eq(0, err)
eq(4, file_write(fp, 'test'))
eq(0, m.file_free(fp, false))

View File

@@ -390,7 +390,7 @@ describe('fs.c', function()
buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
end
local eof = ffi.new('bool[?]', 1, {true})
local ret2 = fs.os_read(fd, eof, buf, size)
local ret2 = fs.os_read(fd, eof, buf, size, false)
local ret1 = eof[0]
local ret3 = ''
if buf ~= nil then
@@ -408,7 +408,7 @@ describe('fs.c', function()
end
local iov = ffi.new('struct iovec[?]', #sizes, bufs)
local eof = ffi.new('bool[?]', 1, {true})
local ret2 = fs.os_readv(fd, eof, iov, #sizes)
local ret2 = fs.os_readv(fd, eof, iov, #sizes, false)
local ret1 = eof[0]
local ret3 = {}
for i = 1,#sizes do
@@ -418,7 +418,7 @@ describe('fs.c', function()
return ret1, ret2, ret3
end
local function os_write(fd, data)
return fs.os_write(fd, data, data and #data or 0)
return fs.os_write(fd, data, data and #data or 0, false)
end
describe('os_path_exists', function()
@@ -491,6 +491,22 @@ describe('fs.c', function()
end)
end)
describe('os_dup', function()
itp('returns new file descriptor', function()
local dup0 = fs.os_dup(0)
local dup1 = fs.os_dup(1)
local dup2 = fs.os_dup(2)
local tbl = {[0]=true, [1]=true, [2]=true,
[tonumber(dup0)]=true, [tonumber(dup1)]=true,
[tonumber(dup2)]=true}
local i = 0
for _, _ in pairs(tbl) do
i = i + 1
end
eq(i, 6) -- All fds must be unique
end)
end)
describe('os_open', function()
local new_file = 'test_new_file'
local existing_file = 'unit-test-directory/test_existing.file'

View File

@@ -2,6 +2,10 @@
-- windows, will probably need quite a bit of adjustment to run there.
local ffi = require("ffi")
local global_helpers = require('test.helpers')
local argss_to_cmd = global_helpers.argss_to_cmd
local repeated_read_cmd = global_helpers.repeated_read_cmd
local ccs = {}
@@ -22,15 +26,6 @@ table.insert(ccs, {path = {"/usr/bin/env", "gcc-4.7"}, type = "gcc"})
table.insert(ccs, {path = {"/usr/bin/env", "clang"}, type = "clang"})
table.insert(ccs, {path = {"/usr/bin/env", "icc"}, type = "gcc"})
local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote)
local function shell_quote(str)
if string.find(str, quote_me) or str == '' then
return "'" .. string.gsub(str, "'", [['"'"']]) .. "'"
else
return str
end
end
-- parse Makefile format dependencies into a Lua table
local function parse_make_deps(deps)
-- remove line breaks and line concatenators
@@ -149,16 +144,6 @@ function Gcc:add_to_include_path(...)
end
end
local function argss_to_cmd(...)
local cmd = ''
for i = 1, select('#', ...) do
for _, arg in ipairs(select(i, ...)) do
cmd = cmd .. ' ' .. shell_quote(arg)
end
end
return cmd
end
-- returns a list of the headers files upon which this file relies
function Gcc:dependencies(hdr)
local cmd = argss_to_cmd(self.path, {'-M', hdr}) .. ' 2>&1'
@@ -172,29 +157,15 @@ function Gcc:dependencies(hdr)
end
end
local function repeated_call(...)
local cmd = argss_to_cmd(...)
for _ = 1, 10 do
local stream = io.popen(cmd)
local ret = stream:read('*a')
stream:close()
if ret then
return ret
end
end
print('ERROR: preprocess.lua: Failed to execute ' .. cmd .. ': nil return after 10 attempts')
return nil
end
function Gcc:filter_standard_defines(defines)
if not self.standard_defines then
local pseudoheader_fname = 'tmp_empty_pseudoheader.h'
local pseudoheader_file = io.open(pseudoheader_fname, 'w')
pseudoheader_file:close()
local standard_defines = repeated_call(self.path,
self.preprocessor_extra_flags,
self.get_defines_extra_flags,
{pseudoheader_fname})
local standard_defines = repeated_read_cmd(self.path,
self.preprocessor_extra_flags,
self.get_defines_extra_flags,
{pseudoheader_fname})
os.remove(pseudoheader_fname)
self.standard_defines = {}
for line in standard_defines:gmatch('[^\n]+') do
@@ -223,9 +194,9 @@ function Gcc:preprocess(previous_defines, ...)
pseudoheader_file:flush()
pseudoheader_file:close()
local defines = repeated_call(self.path, self.preprocessor_extra_flags,
self.get_defines_extra_flags,
{pseudoheader_fname})
local defines = repeated_read_cmd(self.path, self.preprocessor_extra_flags,
self.get_defines_extra_flags,
{pseudoheader_fname})
defines = self:filter_standard_defines(defines)
-- lfs = require("lfs")
@@ -234,9 +205,10 @@ function Gcc:preprocess(previous_defines, ...)
-- io.stderr\write("CWD: #{lfs.currentdir!}\n")
-- io.stderr\write("CMD: #{cmd}\n")
local declarations = repeated_call(self.path, self.preprocessor_extra_flags,
self.get_declarations_extra_flags,
{pseudoheader_fname})
local declarations = repeated_read_cmd(self.path,
self.preprocessor_extra_flags,
self.get_declarations_extra_flags,
{pseudoheader_fname})
os.remove(pseudoheader_fname)