functionaltest: Create lua helper for os.tmpname()

In Windows Lua's os.tmpname() returns relative paths starting with \s,
prepend them with $TEMP to generate a valid path.

In OS X os.tmpname() returns paths in '/tmp' but they should be in
'/private/tmp'. We cannot use os_name() for platform detection because
some tests use tempname() before nvim is spawned, instead use one of the
following:

1. Set SYSTEM_NAME environment variable before calling the tests, it
   is set from CMAKE_SYSTEM_NAME(i.e. uname -s or 'Windows')
2. Call uname -s
3. Assume windows
This commit is contained in:
Rui Abreu Ferreira 2015-12-29 19:18:16 +00:00
parent 39c628d031
commit 9ce81f7b2b
9 changed files with 63 additions and 24 deletions

View File

@ -498,6 +498,7 @@ if(BUSTED_PRG)
-DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
-DBUILD_DIR=${CMAKE_BINARY_DIR} -DBUILD_DIR=${CMAKE_BINARY_DIR}
-DTEST_TYPE=unit -DTEST_TYPE=unit
-DSYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
DEPENDS ${UNITTEST_PREREQS} DEPENDS ${UNITTEST_PREREQS}
${TEST_TARGET_ARGS}) ${TEST_TARGET_ARGS})
@ -516,6 +517,7 @@ if(BUSTED_PRG)
-DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
-DBUILD_DIR=${CMAKE_BINARY_DIR} -DBUILD_DIR=${CMAKE_BINARY_DIR}
-DTEST_TYPE=functional -DTEST_TYPE=functional
-DSYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
DEPENDS ${FUNCTIONALTEST_PREREQS} DEPENDS ${FUNCTIONALTEST_PREREQS}
${TEST_TARGET_ARGS}) ${TEST_TARGET_ARGS})
@ -530,6 +532,7 @@ if(BUSTED_PRG)
-DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
-DBUILD_DIR=${CMAKE_BINARY_DIR} -DBUILD_DIR=${CMAKE_BINARY_DIR}
-DTEST_TYPE=benchmark -DTEST_TYPE=benchmark
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
DEPENDS ${BENCHMARK_PREREQS} DEPENDS ${BENCHMARK_PREREQS}
${TEST_TARGET_ARGS}) ${TEST_TARGET_ARGS})
@ -546,6 +549,7 @@ if(BUSTED_LUA_PRG)
-DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
-DBUILD_DIR=${CMAKE_BINARY_DIR} -DBUILD_DIR=${CMAKE_BINARY_DIR}
-DTEST_TYPE=functional -DTEST_TYPE=functional
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
DEPENDS ${FUNCTIONALTEST_PREREQS} DEPENDS ${FUNCTIONALTEST_PREREQS}
${TEST_TARGET_ARGS}) ${TEST_TARGET_ARGS})
@ -556,6 +560,7 @@ if(LUACHECK_PRG)
COMMAND ${CMAKE_COMMAND} COMMAND ${CMAKE_COMMAND}
-DLUACHECK_PRG=${LUACHECK_PRG} -DLUACHECK_PRG=${LUACHECK_PRG}
-DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-P ${PROJECT_SOURCE_DIR}/cmake/RunTestsLint.cmake) -P ${PROJECT_SOURCE_DIR}/cmake/RunTestsLint.cmake)
endif() endif()

View File

@ -28,6 +28,7 @@ if(DEFINED ENV{TEST_FILTER})
set(TEST_TAG "--filter=$ENV{TEST_FILTER}") set(TEST_TAG "--filter=$ENV{TEST_FILTER}")
endif() endif()
set(ENV{SYSTEM_NAME} ${SYSTEM_NAME})
execute_process( execute_process(
COMMAND ${BUSTED_PRG} ${TEST_TAG} ${TEST_FILTER} -v -o ${BUSTED_OUTPUT_TYPE} COMMAND ${BUSTED_PRG} ${TEST_TAG} ${TEST_FILTER} -v -o ${BUSTED_OUTPUT_TYPE}
--lua=${LUA_PRG} --lazy --helper=${TEST_DIR}/${TEST_TYPE}/preload.lua --lua=${LUA_PRG} --lazy --helper=${TEST_DIR}/${TEST_TYPE}/preload.lua

View File

@ -13,7 +13,7 @@ describe('vim_* functions', function()
describe('command', function() describe('command', function()
it('works', function() it('works', function()
local fname = os.tmpname() local fname = helpers.tmpname()
nvim('command', 'new') nvim('command', 'new')
nvim('command', 'edit '..fname) nvim('command', 'edit '..fname)
nvim('command', 'normal itesting\napi') nvim('command', 'normal itesting\napi')

View File

@ -91,7 +91,7 @@ describe('jobs', function()
it('preserves NULs', function() it('preserves NULs', function()
-- Make a file with NULs in it. -- Make a file with NULs in it.
local filename = os.tmpname() local filename = helpers.tmpname()
write_file(filename, "abc\0def\n") write_file(filename, "abc\0def\n")
nvim('command', "let j = jobstart(['cat', '"..filename.."'], g:job_opts)") nvim('command', "let j = jobstart(['cat', '"..filename.."'], g:job_opts)")

View File

@ -5,9 +5,9 @@ local helpers = require('test.functional.helpers')(after_each)
local eval = helpers.eval local eval = helpers.eval
local command = helpers.command local command = helpers.command
local eq, neq = helpers.eq, helpers.neq local eq, neq = helpers.eq, helpers.neq
local tempfile = os.tmpname() local tempfile = helpers.tmpname()
-- os.tmpname() also creates the file on POSIX systems. Remove it again. -- tmpname() also creates the file on POSIX systems. Remove it again.
-- We just need the name, ignoring any race conditions. -- We just need the name, ignoring any race conditions.
if lfs.attributes(tempfile, 'uid') then if lfs.attributes(tempfile, 'uid') then
os.remove(tempfile) os.remove(tempfile)

View File

@ -291,15 +291,51 @@ local function write_file(name, text, dont_dedent)
file:close() file:close()
end end
local function source(code) -- Tries to get platform name, from $SYSTEM_NAME, uname,
local tmpname = os.tmpname() -- fallback is 'Windows'
if os_name() == 'osx' and string.match(tmpname, '^/tmp') then local uname = (function()
tmpname = '/private'..tmpname local platform = nil
return (function()
if platform then
return platform
end end
write_file(tmpname, code)
nvim_command('source '..tmpname) platform = os.getenv("SYSTEM_NAME")
os.remove(tmpname) if platform then
return tmpname 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)
nvim_command('source '..fname)
os.remove(fname)
return fname
end end
local function nvim(method, ...) local function nvim(method, ...)
@ -431,10 +467,10 @@ local function create_callindex(func)
end end
-- Helper to skip tests. Returns true in Windows systems. -- Helper to skip tests. Returns true in Windows systems.
-- pending_func is the pending() from busted -- pending_func is pending() from busted
local function pending_win32(pending_func) local function pending_win32(pending_func)
clear() clear()
if os_name() == 'windows' then if uname() == 'Windows' then
if pending_func ~= nil then if pending_func ~= nil then
pending_func('FIXME: Windows', function() end) pending_func('FIXME: Windows', function() end)
end end
@ -508,6 +544,7 @@ return function(after_each)
curwinmeths = curwinmeths, curwinmeths = curwinmeths,
curtabmeths = curtabmeths, curtabmeths = curtabmeths,
pending_win32 = pending_win32, pending_win32 = pending_win32,
tmpname = tmpname,
NIL = mpack.NIL, NIL = mpack.NIL,
} }
end end

View File

@ -3,7 +3,6 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local clear, execute, expect = helpers.clear, helpers.execute, helpers.expect local clear, execute, expect = helpers.clear, helpers.execute, helpers.expect
local source, write_file = helpers.source, helpers.write_file local source, write_file = helpers.source, helpers.write_file
local os_name = helpers.os_name
local function sixlines(text) local function sixlines(text)
local result = '' local result = ''
@ -14,19 +13,16 @@ local function sixlines(text)
end end
local function diff(text, nodedent) local function diff(text, nodedent)
local tmpname = os.tmpname() local fname = helpers.tmpname()
if os_name() == 'osx' and string.match(tmpname, '^/tmp') then execute('w! '..fname)
tmpname = '/private'..tmpname
end
execute('w! '..tmpname)
helpers.wait() helpers.wait()
local data = io.open(tmpname):read('*all') local data = io.open(fname):read('*all')
if nodedent then if nodedent then
helpers.eq(text, data) helpers.eq(text, data)
else else
helpers.eq(helpers.dedent(text), data) helpers.eq(helpers.dedent(text), data)
end end
os.remove(tmpname) os.remove(fname)
end end
describe('character classes in regexp', function() describe('character classes in regexp', function()

View File

@ -6,7 +6,7 @@ local clear, call, eq = helpers.clear, helpers.call, helpers.eq
local neq, exc_exec = helpers.neq, helpers.exc_exec local neq, exc_exec = helpers.neq, helpers.exc_exec
describe('Test getting and setting file permissions', function() describe('Test getting and setting file permissions', function()
local tempfile = os.tmpname() local tempfile = helpers.tmpname()
before_each(function() before_each(function()
os.remove(tempfile) os.remove(tempfile)

View File

@ -5,7 +5,7 @@ local write_file, merge_args = helpers.write_file, helpers.merge_args
local mpack = require('mpack') local mpack = require('mpack')
local tmpname = os.tmpname() local tmpname = helpers.tmpname()
local additional_cmd = '' local additional_cmd = ''
local function nvim_argv() local function nvim_argv()