Skip to content

Commit 1c2319c

Browse files
committed
fix unit tests for running on the ESP32 device
When running unit tests on the ESP32, the current directory is root, while on the PC (or in the GitHub Actions), the tests/ directory is the current directory. Unit tests that rely on fixture files fails because of this. Now test scripts can look for fixture files relative to their own path to make them independent of the current directory.
1 parent 037bc58 commit 1c2319c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

tests/preprocess.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ def test(param):
1111
tests.append(param)
1212

1313

14+
def resolve_relative_path(filename):
15+
"""
16+
Returns the full path to the filename provided, taken relative to the current file
17+
e.g.
18+
if this file was file.py at /path/to/file.py
19+
and the provided relative filename was tests/unit.py
20+
then the resulting path would be /path/to/tests/unit.py
21+
"""
22+
r = __file__.rsplit("/", 1) # poor man's os.path.dirname(__file__)
23+
head = r[0]
24+
if len(r) == 1 or not head:
25+
return filename
26+
return "%s/%s" % (head, filename)
27+
28+
1429
@test
1530
def test_replace_defines_should_return_empty_line_given_empty_string():
1631
p = Preprocessor()
@@ -204,7 +219,7 @@ def preprocess_should_replace_BIT_with_empty_string_unless_defined():
204219
def test_process_include_file():
205220
p = Preprocessor()
206221

207-
defines = p.process_include_file('fixtures/incl.h')
222+
defines = p.process_include_file(resolve_relative_path('fixtures/incl.h'))
208223

209224
assert defines['CONST1'] == '42'
210225
assert defines['CONST2'] == '99'
@@ -216,8 +231,8 @@ def test_process_include_file():
216231
def test_process_include_file_with_multiple_files():
217232
p = Preprocessor()
218233

219-
defines = p.process_include_file('fixtures/incl.h')
220-
defines = p.process_include_file('fixtures/incl2.h')
234+
defines = p.process_include_file(resolve_relative_path('fixtures/incl.h'))
235+
defines = p.process_include_file(resolve_relative_path('fixtures/incl2.h'))
221236

222237
assert defines['CONST1'] == '42', "constant from incl.h"
223238
assert defines['CONST2'] == '123', "constant overridden by incl2.h"
@@ -232,8 +247,8 @@ def test_process_include_file_using_database():
232247
p = Preprocessor()
233248
p.use_db(db)
234249

235-
p.process_include_file('fixtures/incl.h')
236-
p.process_include_file('fixtures/incl2.h')
250+
p.process_include_file(resolve_relative_path('fixtures/incl.h'))
251+
p.process_include_file(resolve_relative_path('fixtures/incl2.h'))
237252

238253
assert db['CONST1'] == '42', "constant from incl.h"
239254
assert db['CONST2'] == '123', "constant overridden by incl2.h"
@@ -250,7 +265,7 @@ def test_process_include_file_should_not_load_database_keys_into_instance_define
250265
p = Preprocessor()
251266
p.use_db(db)
252267

253-
p.process_include_file('fixtures/incl.h')
268+
p.process_include_file(resolve_relative_path('fixtures/incl.h'))
254269

255270
# a bit hackish to reference instance-internal state
256271
# but it's important to verify this, as we otherwise run out of memory on device

0 commit comments

Comments
 (0)