aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-01-03 08:46:44 +0300
committerZyX <kp-pav@yandex.ru>2017-01-03 22:54:54 +0300
commit410d18ef5c24a325f4d63c9b01991015199194c2 (patch)
treea6e2acad16f222e35c2c0be979d0396cc1a17813
parentefe1476d4293170496f0e933a4d3c955f0559b03 (diff)
downloadrneovim-410d18ef5c24a325f4d63c9b01991015199194c2.tar.gz
rneovim-410d18ef5c24a325f4d63c9b01991015199194c2.tar.bz2
rneovim-410d18ef5c24a325f4d63c9b01991015199194c2.zip
unittest: Allow multiple indirect includes
Works by saving all preprocessor defines and reusing them on each run. This also saves NVIM_HEADER_H defines. Saving other defines is needed for defines like `Map(foo, bar)` which are sometimes used to declare types or functions. Saving types or function declarations is not needed because they are recorded as luajit state. Fixes #5857
-rw-r--r--test/functional/helpers.lua41
-rw-r--r--test/helpers.lua40
-rw-r--r--test/unit/formatc.lua6
-rw-r--r--test/unit/helpers.lua6
-rw-r--r--test/unit/preprocess.lua43
5 files changed, 80 insertions, 56 deletions
diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua
index f3332cff4f..d67f225ed1 100644
--- a/test/functional/helpers.lua
+++ b/test/functional/helpers.lua
@@ -21,6 +21,9 @@ local nvim_argv = {nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N',
local mpack = require('mpack')
+local tmpname = global_helpers.tmpname
+local uname = global_helpers.uname
+
-- Formulate a path to the directory containing nvim. We use this to
-- help run test executables. It helps to keep the tests working, even
-- when the build is not in the default location.
@@ -334,44 +337,6 @@ local function write_file(name, text, dont_dedent)
file:close()
end
--- Tries to get platform name from $SYSTEM_NAME, uname; fallback is "Windows".
-local uname = (function()
- local platform = nil
- return (function()
- if platform then
- return platform
- end
-
- platform = os.getenv("SYSTEM_NAME")
- if platform then
- return platform
- end
-
- local status, f = pcall(io.popen, "uname -s")
- if status then
- platform = f:read("*l")
- else
- platform = 'Windows'
- end
- return platform
- end)
-end)()
-
-local function tmpname()
- local fname = os.tmpname()
- if uname() == 'Windows' and fname:sub(1, 2) == '\\s' then
- -- In Windows tmpname() returns a filename starting with
- -- special sequence \s, prepend $TEMP path
- local tmpdir = os.getenv('TEMP')
- return tmpdir..fname
- elseif fname:match('^/tmp') and uname() == 'Darwin' then
- -- In OS X /tmp links to /private/tmp
- return '/private'..fname
- else
- return fname
- end
-end
-
local function source(code)
local fname = tmpname()
write_file(fname, code)
diff --git a/test/helpers.lua b/test/helpers.lua
index 4c50c7644f..0bc62da5d7 100644
--- a/test/helpers.lua
+++ b/test/helpers.lua
@@ -52,9 +52,49 @@ local function check_logs()
assert(0 == runtime_errors)
end
+-- Tries to get platform name from $SYSTEM_NAME, uname; fallback is "Windows".
+local uname = (function()
+ local platform = nil
+ return (function()
+ if platform then
+ return platform
+ end
+
+ platform = os.getenv("SYSTEM_NAME")
+ if platform then
+ return platform
+ end
+
+ local status, f = pcall(io.popen, "uname -s")
+ if status then
+ platform = f:read("*l")
+ else
+ platform = 'Windows'
+ end
+ return platform
+ end)
+end)()
+
+local function tmpname()
+ local fname = os.tmpname()
+ if uname() == 'Windows' and fname:sub(1, 2) == '\\s' then
+ -- In Windows tmpname() returns a filename starting with
+ -- special sequence \s, prepend $TEMP path
+ local tmpdir = os.getenv('TEMP')
+ return tmpdir..fname
+ elseif fname:match('^/tmp') and uname() == 'Darwin' then
+ -- In OS X /tmp links to /private/tmp
+ return '/private'..fname
+ else
+ return fname
+ end
+end
+
return {
eq = eq,
neq = neq,
ok = ok,
check_logs = check_logs,
+ uname = uname,
+ tmpname = tmpname,
}
diff --git a/test/unit/formatc.lua b/test/unit/formatc.lua
index 00637e0b8d..9b47a318f0 100644
--- a/test/unit/formatc.lua
+++ b/test/unit/formatc.lua
@@ -219,12 +219,10 @@ local function standalone(...) -- luacheck: ignore
Preprocess.add_to_include_path('./../../build/include')
Preprocess.add_to_include_path('./../../.deps/usr/include')
- local input = Preprocess.preprocess_stream(arg[1])
- local raw = input:read('*all')
- input:close()
+ local raw = Preprocess.preprocess(arg[1])
if raw == nil then
- print("ERROR: Preprocess.preprocess_stream():read() returned empty")
+ print("ERROR: Preprocess.preprocess() returned empty")
end
local formatted
diff --git a/test/unit/helpers.lua b/test/unit/helpers.lua
index 3564f76442..c9885f73d0 100644
--- a/test/unit/helpers.lua
+++ b/test/unit/helpers.lua
@@ -68,14 +68,12 @@ local function cimport(...)
local body = nil
for _ = 1, 10 do
- local stream = Preprocess.preprocess_stream(unpack(paths))
- body = stream:read("*a")
- stream:close()
+ body = Preprocess.preprocess(unpack(paths))
if body ~= nil then break end
end
if body == nil then
- print("ERROR: helpers.lua: Preprocess.preprocess_stream():read() returned empty")
+ print("ERROR: helpers.lua: Preprocess.preprocess() returned empty")
end
-- format it (so that the lines are "unique" statements), also filter out
diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua
index 10ba997758..99537cd66a 100644
--- a/test/unit/preprocess.lua
+++ b/test/unit/preprocess.lua
@@ -1,8 +1,12 @@
-- helps managing loading different headers into the LuaJIT ffi. Untested on
-- windows, will probably need quite a bit of adjustment to run there.
+local global_helpers = require('test.helpers')
+
local ffi = require("ffi")
+local tmpname = global_helpers.tmpname
+
local ccs = {}
local env_cc = os.getenv("CC")
@@ -61,12 +65,12 @@ end
-- will produce a string that represents a meta C header file that includes
-- all the passed in headers. I.e.:
--
--- headerize({"stdio.h", "math.h", true}
+-- headerize({"stdio.h", "math.h"}, true)
-- produces:
-- #include <stdio.h>
-- #include <math.h>
--
--- headerize({"vim.h", "memory.h", false}
+-- headerize({"vim.h", "memory.h"}, false)
-- produces:
-- #include "vim.h"
-- #include "memory.h"
@@ -79,8 +83,7 @@ local function headerize(headers, global)
end
local formatted = {}
- for i = 1, #headers do
- local hdr = headers[i]
+ for i, hdr in ipairs(headers) do
formatted[#formatted + 1] = "#include " ..
tostring(pre) ..
tostring(hdr) ..
@@ -111,7 +114,8 @@ local Gcc = {
'-D "_Nullable="',
'-D "_Nonnull="',
'-U__BLOCKS__',
- }
+ },
+ added_header_defines = '',
}
function Gcc:new(obj)
@@ -145,21 +149,40 @@ end
-- returns a stream representing a preprocessed form of the passed-in headers.
-- Don't forget to close the stream by calling the close() method on it.
-function Gcc:preprocess_stream(...)
+function Gcc:preprocess(...)
-- create pseudo-header
local pseudoheader = headerize({...}, false)
+ local pseudoheader_fname = 'tmp_pseudoheader.h'
+ local pseudoheader_file = io.open(pseudoheader_fname, 'w')
+ pseudoheader_file:write(self.added_header_defines)
+ pseudoheader_file:write("\n")
+ pseudoheader_file:write(pseudoheader)
+ pseudoheader_file:flush()
+ pseudoheader_file:close()
local defines = table.concat(self.preprocessor_extra_flags, ' ')
local cmd = ("echo $hdr | " ..
tostring(self.path) ..
" " ..
tostring(defines) ..
- " -std=c99 -P -E -"):gsub('$hdr', shell_quote(pseudoheader))
+ " -std=c99 -P -E " .. shell_quote(pseudoheader_fname))
+ local def_cmd = ("echo $hdr | " ..
+ tostring(self.path) ..
+ " " ..
+ tostring(defines) ..
+ " -std=c99 -dM -E " .. shell_quote(pseudoheader_fname))
+ local def_stream = io.popen(def_cmd)
+ self.added_header_defines = def_stream:read('*a')
+ def_stream:close()
-- lfs = require("lfs")
-- print("CWD: #{lfs.currentdir!}")
-- print("CMD: #{cmd}")
-- io.stderr\write("CWD: #{lfs.currentdir!}\n")
-- io.stderr\write("CMD: #{cmd}\n")
- return io.popen(cmd)
+ local stream = io.popen(cmd)
+ local ret = stream:read('*a')
+ stream:close()
+ os.remove(pseudoheader_fname)
+ return ret
end
local Clang = Gcc:new()
@@ -197,8 +220,8 @@ return {
includes = function(hdr)
return cc:dependencies(hdr)
end,
- preprocess_stream = function(...)
- return cc:preprocess_stream(...)
+ preprocess = function(...)
+ return cc:preprocess(...)
end,
add_to_include_path = function(...)
return cc:add_to_include_path(...)