2012-11-04 14:21:58 +00:00
|
|
|
--
|
|
|
|
-- tests/base/test_detoken.lua
|
|
|
|
-- Test suite for the token expansion API.
|
2015-01-15 17:00:57 +00:00
|
|
|
-- Copyright (c) 2011-2014 Jason Perkins and the Premake project
|
2012-11-04 14:21:58 +00:00
|
|
|
--
|
|
|
|
|
2017-04-25 05:44:13 +00:00
|
|
|
local p = premake
|
2013-04-17 15:54:07 +00:00
|
|
|
local suite = test.declare("detoken")
|
2017-04-25 05:44:13 +00:00
|
|
|
local detoken = p.detoken
|
2012-11-04 14:21:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Setup
|
|
|
|
--
|
|
|
|
|
2015-01-15 17:00:57 +00:00
|
|
|
local x, action
|
2012-11-04 14:21:58 +00:00
|
|
|
local environ = {}
|
|
|
|
|
2015-01-15 17:00:57 +00:00
|
|
|
function suite.setup()
|
2017-04-25 05:44:13 +00:00
|
|
|
action = p.action.get("test")
|
2015-01-15 17:00:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function suite.teardown()
|
|
|
|
action.pathVars = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-11-04 14:21:58 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- The contents of the token should be executed and the results returned.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.executesTokenContents()
|
|
|
|
x = detoken.expand("MyProject%{1+1}", environ)
|
|
|
|
test.isequal("MyProject2", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- If the value contains more than one token, then should all be expanded.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.expandsMultipleTokens()
|
|
|
|
x = detoken.expand("MyProject%{'X'}and%{'Y'}and%{'Z'}", environ)
|
|
|
|
test.isequal("MyProjectXandYandZ", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- If the token replacement values contain tokens themselves, those
|
|
|
|
-- should also get expanded.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.expandsNestedTokens()
|
2015-09-03 22:03:09 +00:00
|
|
|
environ.wks = { name="MyWorkspace%{'X'}" }
|
2015-08-28 20:16:14 +00:00
|
|
|
x = detoken.expand("%{wks.name}", environ)
|
2015-09-03 22:03:09 +00:00
|
|
|
test.isequal("MyWorkspaceX", x)
|
2012-11-04 14:21:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Verify that the global namespace is still accessible.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.canUseGlobalFunctions()
|
|
|
|
x = detoken.expand("%{iif(true, 'a', 'b')}", environ)
|
|
|
|
test.isequal("a", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- If a path field contains a token, and if that token expands to an
|
|
|
|
-- absolute path itself, that should be returned as the new value.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.canExpandToAbsPath()
|
2013-04-17 15:54:07 +00:00
|
|
|
environ.cfg = { basedir = os.getcwd() }
|
2015-01-15 17:00:57 +00:00
|
|
|
x = detoken.expand("bin/debug/%{cfg.basedir}", environ, {paths=true})
|
2012-11-04 14:21:58 +00:00
|
|
|
test.isequal(os.getcwd(), x)
|
|
|
|
end
|
2012-11-04 23:48:20 +00:00
|
|
|
|
|
|
|
|
2013-04-17 15:54:07 +00:00
|
|
|
--
|
|
|
|
-- If a non-path field contains a token that expands to a path, that
|
|
|
|
-- path should be converted to a relative value.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.canExpandToRelPath()
|
|
|
|
local cwd = os.getcwd()
|
|
|
|
environ.cfg = { basedir = path.getdirectory(cwd) }
|
2015-01-15 17:00:57 +00:00
|
|
|
x = detoken.expand("cd %{cfg.basedir}", environ, {}, cwd)
|
2013-04-17 15:54:07 +00:00
|
|
|
test.isequal("cd ..", x)
|
|
|
|
end
|
|
|
|
|
2017-08-28 22:43:33 +00:00
|
|
|
--
|
|
|
|
-- but not if it is prefixed with a !
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.canExpandWithExclamationMark()
|
|
|
|
local cwd = os.getcwd()
|
|
|
|
environ.cfg = { basedir = path.getdirectory(cwd) }
|
|
|
|
x = detoken.expand("%{!cfg.basedir}", environ, {}, cwd)
|
|
|
|
test.isequal(path.getdirectory(os.getcwd()), x)
|
|
|
|
end
|
|
|
|
|
2013-04-17 15:54:07 +00:00
|
|
|
|
2019-02-14 13:14:34 +00:00
|
|
|
--
|
|
|
|
-- If a path field contains a token that expands to a deferred join,
|
|
|
|
-- it should be resolved before performing detoken.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.canExpandWithDeferredJoin()
|
|
|
|
local cwd = os.getcwd()
|
|
|
|
x = detoken.expand(path.deferredjoin(os.getcwd(), "%{_ACTION}"), environ, {}, cwd)
|
|
|
|
test.isequal(os.getcwd() .. "/test", x)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-11-04 23:48:20 +00:00
|
|
|
--
|
|
|
|
-- If the value being expanded is a table, iterate over all of its values.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.expandsAllItemsInList()
|
|
|
|
x = detoken.expand({ "A%{1}", "B%{2}", "C%{3}" }, environ)
|
|
|
|
test.isequal({ "A1", "B2", "C3" }, x)
|
|
|
|
end
|
2013-04-17 15:54:07 +00:00
|
|
|
|
2015-01-15 17:00:57 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- If the field being expanded supports path variable mapping, and the
|
|
|
|
-- action provides a map, replace tokens with the mapped values.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.replacesToken_onSupportedAndMapped()
|
2015-12-18 00:13:35 +00:00
|
|
|
action.pathVars = { ["cfg.objdir"] = { absolute = true, token = "$(IntDir)" }, }
|
2015-01-15 17:00:57 +00:00
|
|
|
x = detoken.expand("cmd %{cfg.objdir}/file", environ, {pathVars=true})
|
|
|
|
test.isequal("cmd $(IntDir)/file", x)
|
|
|
|
end
|
2015-01-21 22:22:19 +00:00
|
|
|
|
|
|
|
function suite.replacesToken_onSupportedAndMapped_inAbsPath()
|
2015-12-18 00:13:35 +00:00
|
|
|
action.pathVars = { ["cfg.objdir"] = { absolute = true, token = "$(IntDir)" }, }
|
2015-01-21 22:22:19 +00:00
|
|
|
x = detoken.expand(os.getcwd() .. "/%{cfg.objdir}/file", environ, {paths=true,pathVars=true})
|
|
|
|
test.isequal("$(IntDir)/file", x)
|
|
|
|
end
|
2015-06-15 16:15:03 +00:00
|
|
|
|
2015-12-18 00:13:35 +00:00
|
|
|
function suite.replacesToken_onSupportedAndMapped_inRelPath()
|
|
|
|
action.pathVars = { ["cfg.objdir"] = { absolute = false, token = "$(IntDir)" }, }
|
|
|
|
x = detoken.expand(os.getcwd() .. "/%{cfg.objdir}/file", environ, {paths=true,pathVars=true})
|
|
|
|
test.isequal(os.getcwd() .. "/$(IntDir)/file", x)
|
|
|
|
end
|
|
|
|
|
2015-06-15 16:15:03 +00:00
|
|
|
--
|
|
|
|
-- Escapes backslashes correctly.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.escapesBackslashes()
|
|
|
|
environ.foo = "some/path"
|
|
|
|
x = detoken.expand("%{foo:gsub('/', '\\')}", environ)
|
|
|
|
test.isequal("some\\path", x)
|
|
|
|
end
|
2015-07-07 12:43:31 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Escapes backslashes correctly, but not outside tokens.
|
|
|
|
--
|
|
|
|
|
|
|
|
function suite.escapesBackslashes2()
|
|
|
|
environ.foo = "some/path"
|
|
|
|
x = detoken.expand("%{foo:gsub('/', '\\')}\\already\\escaped", environ)
|
|
|
|
test.isequal("some\\path\\already\\escaped", x)
|
|
|
|
end
|