Improve include() function: only include files once, and allow including specific files instead of just directories
This commit is contained in:
parent
1b827d3847
commit
c8e3c0df6a
1
.hgcheck/hg-checklink-8ud9oN
Symbolic link
1
.hgcheck/hg-checklink-8ud9oN
Symbolic link
@ -0,0 +1 @@
|
||||
.
|
@ -4,6 +4,8 @@ Sony NGP (in progress)
|
||||
|
||||
* Added debugformat with C7 support for Visual Studio
|
||||
* The error message for invalid flags now includes the offending value
|
||||
* The include() function will now include each file only once
|
||||
* The include() function can now include specific files
|
||||
|
||||
|
||||
-------
|
||||
|
@ -114,11 +114,25 @@
|
||||
|
||||
|
||||
--
|
||||
-- A shortcut for including another "premake4.lua" file, often used for projects.
|
||||
-- Load and run an external script file, with a bit of extra logic to make
|
||||
-- including projects easier. if "path" is a directory, will look for
|
||||
-- path/premake4.lua. And each file is tracked, and loaded only once.
|
||||
--
|
||||
|
||||
function include(fname)
|
||||
return dofile(fname .. "/premake4.lua")
|
||||
io._includedFiles = { }
|
||||
|
||||
function include(filename)
|
||||
-- if a directory, load the premake script inside it
|
||||
if os.isdir(filename) then
|
||||
filename = path.join(filename, "premake4.lua")
|
||||
end
|
||||
|
||||
-- but only load each file once
|
||||
filename = path.getabsolute(filename)
|
||||
if not io._includedFiles[filename] then
|
||||
io._includedFiles[filename] = true
|
||||
dofile(filename)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
49
tests/base/test_include.lua
Normal file
49
tests/base/test_include.lua
Normal file
@ -0,0 +1,49 @@
|
||||
--
|
||||
-- tests/base/test_include.lua
|
||||
-- Test the include() function, for including external scripts
|
||||
-- Copyright (c) 2011 Jason Perkins and the Premake project
|
||||
--
|
||||
|
||||
|
||||
T.include = { }
|
||||
local suite = T.include
|
||||
|
||||
|
||||
--
|
||||
-- Setup and teardown
|
||||
--
|
||||
|
||||
function suite.teardown()
|
||||
-- clear the list of included files after each run
|
||||
io._includedFiles = { }
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- Tests
|
||||
--
|
||||
|
||||
function suite.include_findsPremakeFile_onFolderNameOnly()
|
||||
include "folder"
|
||||
test.isequal("ok\n", io.endcapture())
|
||||
end
|
||||
|
||||
|
||||
function suite.include_onExactFilename()
|
||||
include "folder/premake4.lua"
|
||||
test.isequal("ok\n", io.endcapture())
|
||||
end
|
||||
|
||||
|
||||
function suite.include_runsOnlyOnce_onMultipleIncludes()
|
||||
include "folder/premake4.lua"
|
||||
include "folder/premake4.lua"
|
||||
test.isequal("ok\n", io.endcapture())
|
||||
end
|
||||
|
||||
|
||||
function suite.include_runsOnlyOnce_onMultipleIncludesWithDifferentPaths()
|
||||
include "folder/premake4.lua"
|
||||
include "../tests/folder/premake4.lua"
|
||||
test.isequal("ok\n", io.endcapture())
|
||||
end
|
@ -1 +0,0 @@
|
||||
return "ok"
|
1
tests/folder/premake4.lua
Normal file
1
tests/folder/premake4.lua
Normal file
@ -0,0 +1 @@
|
||||
io.printf("ok")
|
@ -37,6 +37,7 @@
|
||||
-- The test suites
|
||||
--
|
||||
|
||||
-- Base API tests
|
||||
dofile("test_dofile.lua")
|
||||
dofile("test_string.lua")
|
||||
dofile("test_premake.lua")
|
||||
@ -48,6 +49,7 @@
|
||||
dofile("base/test_api.lua")
|
||||
dofile("base/test_action.lua")
|
||||
dofile("base/test_config.lua")
|
||||
dofile("base/test_include.lua")
|
||||
dofile("base/test_location.lua")
|
||||
dofile("base/test_os.lua")
|
||||
dofile("base/test_path.lua")
|
||||
|
@ -30,9 +30,11 @@
|
||||
function test.capture(expected)
|
||||
local actual = io.endcapture()
|
||||
|
||||
-- create line-by-line iterators for both values
|
||||
local ait = actual:gfind("(.-)" .. io.eol)
|
||||
local eit = expected:gfind("(.-)\n")
|
||||
|
||||
-- compare each value line by line
|
||||
local linenum = 1
|
||||
local atxt = ait()
|
||||
local etxt = eit()
|
||||
|
Reference in New Issue
Block a user