diff --git a/.hgcheck/hg-checklink-8ud9oN b/.hgcheck/hg-checklink-8ud9oN new file mode 120000 index 00000000..945c9b46 --- /dev/null +++ b/.hgcheck/hg-checklink-8ud9oN @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/CHANGES.txt b/CHANGES.txt index 6425d213..7e0f93a5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 ------- diff --git a/src/base/globals.lua b/src/base/globals.lua index 99c8ac7e..3b956f2b 100644 --- a/src/base/globals.lua +++ b/src/base/globals.lua @@ -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 diff --git a/tests/base/test_include.lua b/tests/base/test_include.lua new file mode 100644 index 00000000..0442a2c5 --- /dev/null +++ b/tests/base/test_include.lua @@ -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 diff --git a/tests/folder/ok.lua.2 b/tests/folder/ok.lua.2 deleted file mode 100644 index 517f3384..00000000 --- a/tests/folder/ok.lua.2 +++ /dev/null @@ -1 +0,0 @@ -return "ok" diff --git a/tests/folder/premake4.lua b/tests/folder/premake4.lua new file mode 100644 index 00000000..a4ecc909 --- /dev/null +++ b/tests/folder/premake4.lua @@ -0,0 +1 @@ +io.printf("ok") diff --git a/tests/premake4.lua b/tests/premake4.lua index 28245270..0c02f708 100644 --- a/tests/premake4.lua +++ b/tests/premake4.lua @@ -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") diff --git a/tests/testfx.lua b/tests/testfx.lua index a33a391a..ba4b2d15 100644 --- a/tests/testfx.lua +++ b/tests/testfx.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()