From 0320a58082f922b54cb36ce57064c07a9e781aa8 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 17 Mar 2017 00:04:03 +0300 Subject: functests: Check that `-s` works as expected --- test/functional/core/main_spec.lua | 60 ++++++++++++++++++++++++++++++++++++++ test/helpers.lua | 39 +++++++++++++++++++++++-- test/unit/preprocess.lua | 26 ++++------------- 3 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 test/functional/core/main_spec.lua (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua new file mode 100644 index 0000000000..50c6009b53 --- /dev/null +++ b/test/functional/core/main_spec.lua @@ -0,0 +1,60 @@ +local lfs = require('lfs') +local helpers = require('test.functional.helpers')(after_each) +local global_helpers = require('test.helpers') + +local eq = helpers.eq +local neq = helpers.neq +local sleep = helpers.sleep +local nvim_prog = helpers.nvim_prog +local write_file = helpers.write_file + +local popen_w = global_helpers.popen_w +local popen_r = global_helpers.popen_r + +describe('Command-line option', function() + describe('-s', function() + local fname = 'Xtest-functional-core-main-s' + local dollar_fname = '$' .. fname + before_each(function() + 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)) + local pipe = popen_w( + nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', '-', + fname) + pipe:write(':call setline(1, "42")\n') + pipe:write(':wqall!\n') + pipe:close() + local max_sec = 10 + while max_sec > 0 do + local attrs = lfs.attributes(fname) + if attrs then + eq(#('42\n'), attrs.size) + break + else + max_sec = max_sec - 1 + sleep(1000) + end + end + neq(0, max_sec) + 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') + local pipe = popen_r( + nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, + fname) + local stdout = pipe:read('*a') + eq('', stdout) + local attrs = lfs.attributes(fname) + eq(#('100500\n'), attrs.size) + end) + end) +end) diff --git a/test/helpers.lua b/test/helpers.lua index e5224349c2..6ee95a4396 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -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 _, arg in ipairs(arg) do + cmd = cmd .. ' ' .. shell_quote(arg) + 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, @@ -95,7 +127,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() @@ -215,7 +247,7 @@ local function check_cores(app) 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 @@ -238,4 +270,7 @@ return { check_cores = check_cores, hasenv = hasenv, which = which, + argss_to_cmd = argss_to_cmd, + popen_r = popen_r, + popen_w = popen_w, } diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua index 363358d134..18b2284e3d 100644 --- a/test/unit/preprocess.lua +++ b/test/unit/preprocess.lua @@ -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 popen_r = global_helpers.popen_r +local argss_to_cmd = global_helpers.argss_to_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' @@ -173,9 +158,8 @@ function Gcc:dependencies(hdr) end local function repeated_call(...) - local cmd = argss_to_cmd(...) for _ = 1, 10 do - local stream = io.popen(cmd) + local stream = popen_r(...) local ret = stream:read('*a') stream:close() if ret then -- cgit From 0e9286a19ed51ba9a2d6bfd06432c90e36cad4bd Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 17 Mar 2017 10:57:19 +0300 Subject: tests: Fix CI failures --- test/functional/core/main_spec.lua | 12 ++++++------ test/helpers.lua | 18 ++++++++++++++++-- test/unit/preprocess.lua | 36 ++++++++++++------------------------ 3 files changed, 34 insertions(+), 32 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 50c6009b53..a374b4c040 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -9,7 +9,7 @@ local nvim_prog = helpers.nvim_prog local write_file = helpers.write_file local popen_w = global_helpers.popen_w -local popen_r = global_helpers.popen_r +local repeated_popen_r = global_helpers.repeated_popen_r describe('Command-line option', function() describe('-s', function() @@ -48,13 +48,13 @@ describe('Command-line option', 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') - local pipe = popen_r( + local pipe = repeated_popen_r( nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, fname) - local stdout = pipe:read('*a') - eq('', stdout) - local attrs = lfs.attributes(fname) - eq(#('100500\n'), attrs.size) + local stdout = pipe:read('*a') + eq('', stdout) + local attrs = lfs.attributes(fname) + eq(#('100500\n'), attrs.size) end) end) end) diff --git a/test/helpers.lua b/test/helpers.lua index 6ee95a4396..be83ff314b 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -17,8 +17,8 @@ local function argss_to_cmd(...) if type(arg) == 'string' then cmd = cmd .. ' ' ..shell_quote(arg) else - for _, arg in ipairs(arg) do - cmd = cmd .. ' ' .. shell_quote(arg) + for _, subarg in ipairs(arg) do + cmd = cmd .. ' ' .. shell_quote(subarg) end end end @@ -257,6 +257,19 @@ local function which(exe) end end +local function repeated_popen_r(...) + 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 + return { eq = eq, neq = neq, @@ -273,4 +286,5 @@ return { argss_to_cmd = argss_to_cmd, popen_r = popen_r, popen_w = popen_w, + repeated_popen_r = repeated_popen_r, } diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua index 18b2284e3d..ac0c5a85c1 100644 --- a/test/unit/preprocess.lua +++ b/test/unit/preprocess.lua @@ -4,8 +4,8 @@ local ffi = require("ffi") local global_helpers = require('test.helpers') -local popen_r = global_helpers.popen_r local argss_to_cmd = global_helpers.argss_to_cmd +local repeated_popen_r = global_helpers.repeated_popen_r local ccs = {} @@ -157,28 +157,15 @@ function Gcc:dependencies(hdr) end end -local function repeated_call(...) - for _ = 1, 10 do - local stream = popen_r(...) - 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_popen_r(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 @@ -207,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_popen_r(self.path, self.preprocessor_extra_flags, + self.get_defines_extra_flags, + {pseudoheader_fname}) defines = self:filter_standard_defines(defines) -- lfs = require("lfs") @@ -218,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_popen_r(self.path, + self.preprocessor_extra_flags, + self.get_declarations_extra_flags, + {pseudoheader_fname}) os.remove(pseudoheader_fname) -- cgit From 3cd7bf31e2351eb2874f8431d290a3d36b0b075e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 18 Mar 2017 00:16:23 +0300 Subject: tests: Fix repeated_popen_r usage, rename the function --- test/functional/core/main_spec.lua | 5 ++--- test/helpers.lua | 4 ++-- test/unit/preprocess.lua | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 17 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index a374b4c040..cc781a59a1 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -9,7 +9,7 @@ local nvim_prog = helpers.nvim_prog local write_file = helpers.write_file local popen_w = global_helpers.popen_w -local repeated_popen_r = global_helpers.repeated_popen_r +local repeated_read_cmd = global_helpers.repeated_read_cmd describe('Command-line option', function() describe('-s', function() @@ -48,10 +48,9 @@ describe('Command-line option', 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') - local pipe = repeated_popen_r( + local stdout = repeated_read_cmd( nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, fname) - local stdout = pipe:read('*a') eq('', stdout) local attrs = lfs.attributes(fname) eq(#('100500\n'), attrs.size) diff --git a/test/helpers.lua b/test/helpers.lua index be83ff314b..d739400c71 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -257,7 +257,7 @@ local function which(exe) end end -local function repeated_popen_r(...) +local function repeated_read_cmd(...) for _ = 1, 10 do local stream = popen_r(...) local ret = stream:read('*a') @@ -286,5 +286,5 @@ return { argss_to_cmd = argss_to_cmd, popen_r = popen_r, popen_w = popen_w, - repeated_popen_r = repeated_popen_r, + repeated_read_cmd = repeated_read_cmd, } diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua index ac0c5a85c1..1073855a7d 100644 --- a/test/unit/preprocess.lua +++ b/test/unit/preprocess.lua @@ -5,7 +5,7 @@ local ffi = require("ffi") local global_helpers = require('test.helpers') local argss_to_cmd = global_helpers.argss_to_cmd -local repeated_popen_r = global_helpers.repeated_popen_r +local repeated_read_cmd = global_helpers.repeated_read_cmd local ccs = {} @@ -162,10 +162,10 @@ function Gcc:filter_standard_defines(defines) local pseudoheader_fname = 'tmp_empty_pseudoheader.h' local pseudoheader_file = io.open(pseudoheader_fname, 'w') pseudoheader_file:close() - local standard_defines = repeated_popen_r(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 @@ -194,9 +194,9 @@ function Gcc:preprocess(previous_defines, ...) pseudoheader_file:flush() pseudoheader_file:close() - local defines = repeated_popen_r(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") @@ -205,10 +205,10 @@ function Gcc:preprocess(previous_defines, ...) -- io.stderr\write("CWD: #{lfs.currentdir!}\n") -- io.stderr\write("CMD: #{cmd}\n") - local declarations = repeated_popen_r(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) -- cgit From 1ea7541f657e64c843b97b25ec75dd388ee916cc Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 18 Mar 2017 17:49:47 +0300 Subject: functests: Alter the order of checks Check whether `repeated_read_cmd` returned nil and did not actually run nvim or it did, but still returned nil for whatever reason. --- test/functional/core/main_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index cc781a59a1..59969538fa 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -51,9 +51,9 @@ describe('Command-line option', function() local stdout = repeated_read_cmd( nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, fname) - eq('', stdout) local attrs = lfs.attributes(fname) eq(#('100500\n'), attrs.size) + eq('', stdout) end) end) end) -- cgit From 38687ee394150061c24e45b44cbeb9cfbb4be142 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 18 Mar 2017 17:54:04 +0300 Subject: functests: Make sure that line ending is LF and not CRLF --- test/functional/core/main_spec.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 59969538fa..60c89ec6c8 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -28,6 +28,7 @@ describe('Command-line option', function() local pipe = popen_w( nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', '-', fname) + pipe:write(':set fileformat=unix\n') pipe:write(':call setline(1, "42")\n') pipe:write(':wqall!\n') pipe:close() @@ -47,7 +48,7 @@ describe('Command-line option', function() 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') + write_file(dollar_fname, ':set fileformat=unix\n:call setline(1, "100500")\n:wqall!\n') local stdout = repeated_read_cmd( nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, fname) -- cgit From d4639ea6d9045edee5476ce50719a00575d565ce Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 01:53:50 +0300 Subject: functests: Use Neovim instance and system() in place of lua io.popen --- test/functional/core/main_spec.lua | 56 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 60c89ec6c8..40c161c986 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -4,18 +4,30 @@ local global_helpers = require('test.helpers') local eq = helpers.eq local neq = helpers.neq -local sleep = helpers.sleep +local clear = helpers.clear +local funcs = helpers.funcs local nvim_prog = helpers.nvim_prog local write_file = helpers.write_file local popen_w = global_helpers.popen_w local repeated_read_cmd = global_helpers.repeated_read_cmd +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 dollar_fname = '$' .. fname before_each(function() + clear() os.remove(fname) os.remove(dollar_fname) end) @@ -25,36 +37,30 @@ describe('Command-line option', function() end) it('treats - as stdin', function() eq(nil, lfs.attributes(fname)) - local pipe = popen_w( - nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', '-', - fname) - pipe:write(':set fileformat=unix\n') - pipe:write(':call setline(1, "42")\n') - pipe:write(':wqall!\n') - pipe:close() - local max_sec = 10 - while max_sec > 0 do - local attrs = lfs.attributes(fname) - if attrs then - eq(#('42\n'), attrs.size) - break - else - max_sec = max_sec - 1 - sleep(1000) - end - end - neq(0, max_sec) + eq( + ':call setline(1, "42"):wqall!"'..fname..'" "'..fname..'" [New] 1L, 3C', + 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, funcs.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, ':set fileformat=unix\n:call setline(1, "100500")\n:wqall!\n') - local stdout = repeated_read_cmd( - nvim_prog, '-u', 'NONE', '-i', 'NONE', '--headless', '-s', dollar_fname, - fname) + write_file(dollar_fname, ':call setline(1, "100500")\n:wqall!\n') + eq( + ':call setline(1, "100500"):wqall!"'..fname..'" "'..fname..'" [New] 1L, 7C', + funcs.system( + {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', + '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', + '-s', dollar_fname, fname})) + eq(0, funcs.eval('v:shell_error')) local attrs = lfs.attributes(fname) eq(#('100500\n'), attrs.size) - eq('', stdout) end) end) end) -- cgit From 29654cfee7c9c230556e436ca3c17198bce82680 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 03:33:55 +0300 Subject: functests: Fix testlint errors --- test/functional/core/main_spec.lua | 5 ----- 1 file changed, 5 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 40c161c986..ece4402703 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -1,17 +1,12 @@ local lfs = require('lfs') local helpers = require('test.functional.helpers')(after_each) -local global_helpers = require('test.helpers') local eq = helpers.eq -local neq = helpers.neq local clear = helpers.clear local funcs = helpers.funcs local nvim_prog = helpers.nvim_prog local write_file = helpers.write_file -local popen_w = global_helpers.popen_w -local repeated_read_cmd = global_helpers.repeated_read_cmd - 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 ./. -- cgit From d2268d5ebbbd472c9c4f303404dc5640208d3b3b Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 14:13:07 +0300 Subject: functests: Do not check stdout, it is different on Windows --- test/functional/core/main_spec.lua | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index ece4402703..c3aeb30398 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -32,13 +32,11 @@ describe('Command-line option', function() end) it('treats - as stdin', function() eq(nil, lfs.attributes(fname)) - eq( - ':call setline(1, "42"):wqall!"'..fname..'" "'..fname..'" [New] 1L, 3C', - funcs.system( - {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', - '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', - '-s', '-', fname}, - {':call setline(1, "42")', ':wqall!', ''})) + 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, funcs.eval('v:shell_error')) local attrs = lfs.attributes(fname) eq(#('42\n'), attrs.size) @@ -47,12 +45,10 @@ describe('Command-line option', 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') - eq( - ':call setline(1, "100500"):wqall!"'..fname..'" "'..fname..'" [New] 1L, 7C', - funcs.system( - {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', - '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', - '-s', dollar_fname, fname})) + funcs.system( + {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', + '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', + '-s', dollar_fname, fname}) eq(0, funcs.eval('v:shell_error')) local attrs = lfs.attributes(fname) eq(#('100500\n'), attrs.size) -- cgit From fdfa1ed578afd41a68f05c88dc419d88051b7240 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 16:09:48 +0300 Subject: main: Temporary fix assertion error This variant uses `fdopen()` which is not standard, but it fixes problem on my system. In next commit `scriptin` will use `FileDescriptor*` from os/fileio in place of `FILE*`. --- test/functional/core/main_spec.lua | 48 ++++++++++++++++++++++++++++++++++++++ test/unit/os/fs_spec.lua | 16 +++++++++++++ 2 files changed, 64 insertions(+) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index c3aeb30398..bad5d72142 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -1,7 +1,9 @@ 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 clear = helpers.clear local funcs = helpers.funcs local nvim_prog = helpers.nvim_prog @@ -53,5 +55,51 @@ describe('Command-line option', function() local attrs = lfs.attributes(fname) eq(#('100500\n'), attrs.size) end) + it('does not crash after reading from stdin in non-headless mode', function() + local screen = Screen.new(40, 8) + screen:attach() + eq(nil, lfs.attributes(fname)) + 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') + 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) end) end) diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 860ebfdbcb..b03040260f 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -483,6 +483,22 @@ describe('fs function', 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' -- cgit From e78e75d85d91e9f14964465ea136b3899b774d6e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 17:29:48 +0300 Subject: fileio,main: Do not restart syscall at EAGAIN when reading for -s --- test/unit/os/fs_spec.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index b03040260f..5f8be93aad 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -382,7 +382,7 @@ describe('fs function', 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 @@ -400,7 +400,7 @@ describe('fs function', 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 @@ -410,7 +410,7 @@ describe('fs function', 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() -- cgit From 7df4fc894183f19f61242706119ffd2c96016b54 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 18:53:21 +0300 Subject: functests: Test -s errors --- test/functional/core/main_spec.lua | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index bad5d72142..80838d1e9c 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -4,6 +4,7 @@ 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 @@ -22,6 +23,8 @@ 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() @@ -39,7 +42,7 @@ describe('Command-line option', function() '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', '-s', '-', fname}, {':call setline(1, "42")', ':wqall!', ''}) - eq(0, funcs.eval('v:shell_error')) + eq(0, eval('v:shell_error')) local attrs = lfs.attributes(fname) eq(#('42\n'), attrs.size) end) @@ -51,14 +54,13 @@ describe('Command-line option', function() {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', '-s', dollar_fname, fname}) - eq(0, funcs.eval('v:shell_error')) + 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() local screen = Screen.new(40, 8) screen:attach() - eq(nil, lfs.attributes(fname)) funcs.termopen({ nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile shortmess+=IFW fileformats=unix', @@ -101,5 +103,28 @@ describe('Command-line option', function() ]]) ]=] 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) -- cgit From ae4fae9d3efaa3be219f1e5646be1a7745c758ac Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 19:16:44 +0300 Subject: unittests: Add tests for new fileio functions --- test/unit/os/fileio_spec.lua | 56 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/unit/os/fileio_spec.lua b/test/unit/os/fileio_spec.lua index 7a738ce85c..eaccdcb2a2 100644 --- a/test/unit/os/fileio_spec.lua +++ b/test/unit/os/fileio_spec.lua @@ -6,8 +6,10 @@ local itp = helpers.gen_itp(it) local eq = helpers.eq local ffi = helpers.ffi local cimport = helpers.cimport +local cppimport = helpers.cppimport -local m = cimport('./src/nvim/os/fileio.h') +local m = cimport('./src/nvim/os/os.h', './src/nvim/os/fileio.h') +cppimport('fcntl.h') local fcontents = '' for i = 0, 255 do @@ -58,6 +60,18 @@ local function file_open_new(fname, flags, mode) return ret1[0], ret2 end +local function file_open_fd(fd, flags, mode) + local ret2 = ffi.new('FileDescriptor') + local ret1 = m.file_open_fd(ret2, fd, flags, mode) + return ret1, ret2 +end + +local function file_open_fd_new(fd, flags, mode) + local ret1 = ffi.new('int[?]', 1, {0}) + local ret2 = ffi.gc(m.file_open_fd_new(ret1, fd, flags, mode), nil) + return ret1[0], ret2 +end + local function file_write(fp, buf) return m.file_write(fp, buf, #buf) end @@ -88,6 +102,46 @@ local function file_skip(fp, size) return m.file_skip(fp, size) 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, m.kFileReadOnly, 0) + eq(0, err) + eq({#fcontents, fcontents}, {file_read(fp, #fcontents)}) + eq(0, m.file_close(fp)) + end) + 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, m.kFileWriteOnly, 0) + eq(0, err) + eq(4, file_write(fp, 'test')) + eq(0, m.file_close(fp)) + eq(4, lfs.attributes(filec).size) + eq('test', io.open(filec):read('*a')) + end) +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, m.kFileReadOnly, 0) + eq(0, err) + eq({#fcontents, fcontents}, {file_read(fp, #fcontents)}) + eq(0, m.file_free(fp)) + end) + 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, m.kFileWriteOnly, 0) + eq(0, err) + eq(4, file_write(fp, 'test')) + eq(0, m.file_free(fp)) + eq(4, lfs.attributes(filec).size) + eq('test', io.open(filec):read('*a')) + end) +end) + describe('file_open', function() itp('can create a rwx------ file with kFileCreate', function() local err, fp = file_open(filec, m.kFileCreate, 448) -- cgit From 99b4f25b990b8afe688bd8d7e5cf6dee209e1d31 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 Mar 2017 21:04:45 +0300 Subject: functests: Do not run termopen test on Windows --- test/functional/core/main_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 80838d1e9c..2c4d110f0f 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -59,6 +59,7 @@ describe('Command-line option', function() 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({ -- cgit From 62108c3b0be46936c83f6d4c98b44ceb5e6f77fd Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 Mar 2017 21:06:39 +0300 Subject: functests: Disable system(-s -) test on Windows Assume something with system() if second test hangs as well. Assume something with reading stdin if not. --- test/functional/core/main_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 2c4d110f0f..1641149c87 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -36,6 +36,7 @@ describe('Command-line option', function() os.remove(dollar_fname) end) it('treats - as stdin', function() + if helpers.pending_win32(pending) then return end eq(nil, lfs.attributes(fname)) funcs.system( {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', -- cgit From 387fbcd95cade4b0c037d18f404944676a59db09 Mon Sep 17 00:00:00 2001 From: b-r-o-c-k Date: Sat, 14 Apr 2018 14:21:36 -0500 Subject: win: Fix reading from stdin * Reading from stdin on Windows is fixed in the same way as it was in #8267. * The file_read function was returning without filling the destination buffer when it was called with a non-blocking file descriptor. --- test/functional/core/main_spec.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/functional/core/main_spec.lua b/test/functional/core/main_spec.lua index 1641149c87..2c4d110f0f 100644 --- a/test/functional/core/main_spec.lua +++ b/test/functional/core/main_spec.lua @@ -36,7 +36,6 @@ describe('Command-line option', function() os.remove(dollar_fname) end) it('treats - as stdin', function() - if helpers.pending_win32(pending) then return end eq(nil, lfs.attributes(fname)) funcs.system( {nvim_prog_abs(), '-u', 'NONE', '-i', 'NONE', '--headless', -- cgit