Skip to content

Commit d74ad3b

Browse files
authored
Test suite cleanups (#2302)
* Use consistent working directory for running the tests. This changes the test suite to always be run from a consistent working directory, avoiding having per-test workarounds like done on the PCH tests. * Simplify directory handling in `test_sources.lua`. * Avoid creating `MyLocation` folder in the root. * Address review feedback.
1 parent c07ea89 commit d74ad3b

File tree

5 files changed

+47
-52
lines changed

5 files changed

+47
-52
lines changed

modules/gmake/tests/cpp/test_make_pch.lua

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
local wks, prj
1919
function suite.setup()
20-
os.chdir(_TESTS_DIR)
2120
wks, prj = test.createWorkspace()
2221
end
2322

@@ -71,15 +70,32 @@
7170

7271

7372
--
74-
-- The PCH can be specified relative the an includes search path.
73+
-- The PCH can be specified relative to the includes search path.
7574
--
7675

77-
function suite.pch_searchesIncludeDirs()
76+
function suite.searchesIncludeDirs()
7877
pchheader "premake.h"
79-
includedirs { "../../../src/host" }
78+
includedirs { "src/host" }
8079
prepareVars()
8180
test.capture [[
82-
PCH = ../../../src/host/premake.h
81+
PCH = src/host/premake.h
82+
]]
83+
end
84+
85+
86+
--
87+
-- The PCH can be specified relative to the includes search path.
88+
-- Due to the location being different from _MAIN_SCRIPT_DIR,
89+
-- the specified PCH path should be relative to the location.
90+
91+
92+
function suite.searchesIncludeDirs_location()
93+
location "MyProject"
94+
pchheader "premake.h"
95+
includedirs { "src/host" }
96+
prepareVars()
97+
test.capture [[
98+
PCH = ../src/host/premake.h
8399
]]
84100
end
85101

@@ -123,20 +139,3 @@ $(OBJECTS): | $(OBJDIR)
123139
endif
124140
]]
125141
end
126-
127-
128-
129-
--
130-
-- If the header is located on one of the include file
131-
-- search directories, it should get found automatically.
132-
--
133-
134-
function suite.findsPCH_onIncludeDirs()
135-
location "MyProject"
136-
pchheader "premake.h"
137-
includedirs { "../../../src/host" }
138-
prepareVars()
139-
test.capture [[
140-
PCH = ../../../../src/host/premake.h
141-
]]
142-
end

modules/gmake2/tests/test_gmake2_pch.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
local wks, prj
2222
function suite.setup()
23-
os.chdir(_TESTS_DIR)
2423
gmake2.cpp.initialize()
2524
wks, prj = test.createWorkspace()
2625
end
@@ -95,15 +94,30 @@ GCH = $(PCH_PLACEHOLDER).gch
9594

9695

9796
--
98-
-- The PCH can be specified relative the an includes search path.
97+
-- The PCH can be specified relative to the includes search path.
9998
--
10099

101-
function suite.pch_searchesIncludeDirs()
100+
function suite.searchesIncludeDirs()
102101
pchheader "premake.h"
103-
includedirs { "../../../src/host" }
102+
includedirs { "src/host" }
104103
prepareVars()
105104
test.capture [[
106-
PCH = ../../../src/host/premake.h
105+
PCH = src/host/premake.h
106+
]]
107+
end
108+
109+
--
110+
-- The PCH can be specified relative to the includes search path.
111+
-- Due to the location being different from _MAIN_SCRIPT_DIR,
112+
-- the specified PCH path should be relative to the location.
113+
114+
function suite.searchesIncludeDirs_location()
115+
location "MyProject"
116+
pchheader "premake.h"
117+
includedirs { "src/host" }
118+
prepareVars()
119+
test.capture [[
120+
PCH = ../src/host/premake.h
107121
]]
108122
end
109123

@@ -160,23 +174,6 @@ endif
160174
]]
161175
end
162176

163-
164-
165-
--
166-
-- If the header is located on one of the include file
167-
-- search directories, it should get found automatically.
168-
--
169-
170-
function suite.findsPCH_onIncludeDirs()
171-
location "MyProject"
172-
pchheader "premake.h"
173-
includedirs { "../../../src/host" }
174-
prepareVars()
175-
test.capture [[
176-
PCH = ../../../../src/host/premake.h
177-
]]
178-
end
179-
180177
--
181178
-- If the header is located on one of the include file
182179
-- search directories, it should get found automatically.

modules/self-test/test_runner.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@
8484

8585
function _.runTest(test)
8686
_.log(term.lightGreen, "[ RUN ]", string.format(" %s.%s", test.suiteName, test.testName))
87+
88+
-- Provide a consistent working directory to the tests.
89+
os.chdir(_MAIN_SCRIPT_DIR)
90+
8791
local startTime = os.clock()
88-
local cwd = os.getcwd()
8992
local hooks = _.installTestingHooks()
9093

9194
_TESTS_DIR = test.suite._TESTS_DIR
@@ -105,7 +108,6 @@
105108
err = err or terr
106109

107110
_.removeTestingHooks(hooks)
108-
os.chdir(cwd)
109111

110112
if ok then
111113
_.log(term.lightGreen, "[ OK ]", string.format(" %s.%s (%.0f ms)", test.suiteName, test.testName, (os.clock() - startTime) * 1000))

tests/project/test_sources.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515

1616
local wks, prj
1717

18-
local cwd = os.getcwd()
19-
local oldcwd
20-
2118
function suite.setup()
2219
wks, prj = test.createWorkspace()
2320

2421
-- We change the directory to get nice relative paths
25-
oldcwd = os.getcwd()
26-
os.chdir(cwd)
22+
os.chdir(_SCRIPT_DIR)
2723

2824
-- Create a token to be used in search paths
2925
p.api.register { name = "mytoken", kind = "string", scope = "config" }
@@ -32,13 +28,13 @@
3228

3329
function suite.teardown()
3430
mytoken = nil
35-
os.chdir(oldcwd)
3631
end
3732

3833
local function run()
3934
local cfg = test.getconfig(prj, "Debug")
4035

4136
local files = {}
37+
local cwd = os.getcwd()
4238
for _, file in ipairs(cfg.files) do
4339
table.insert(files, path.getrelative(cwd, file))
4440
end

tests/test_premake.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
local wks, prj
1818
function suite.setup()
19+
os.chdir(_TESTS_DIR)
1920
wks = test.createWorkspace()
2021
location "MyLocation"
2122
prj = p.workspace.getproject(wks, 1)

0 commit comments

Comments
 (0)