Skip to content

Commit 676aade

Browse files
authored
Merge pull request #3337 from vkarak/bugfix/testlib-nested-imports
[bugfix] Fix relative imports in deeply nested tests
2 parents 2e3dee8 + a186646 commit 676aade

File tree

6 files changed

+48
-11
lines changed

6 files changed

+48
-11
lines changed

reframe/frontend/loader.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,31 @@ def load_from_file(self, filename, force=False):
193193

194194
try:
195195
dirname = os.path.dirname(filename)
196-
with osext.change_dir(dirname):
197-
with util.temp_sys_path(dirname):
198-
if os.path.exists(os.path.join(dirname, '__init__.py')):
199-
# If the containing directory is a package,
200-
# import it, too.
201-
parent = util.import_module_from_file(dirname).__name__
202-
else:
203-
parent = None
204196

197+
# Load all parent modules of test file
198+
parents = []
199+
while os.path.exists(os.path.join(dirname, '__init__.py')):
200+
parents.append(os.path.join(dirname))
201+
dirname = os.path.split(dirname)[0]
202+
203+
parent_module = None
204+
for pdir in reversed(parents):
205+
with osext.change_dir(pdir):
206+
with util.temp_sys_path(pdir):
207+
package_path = os.path.join(pdir, '__init__.py')
208+
parent_module = util.import_module_from_file(
209+
package_path, parent=parent_module
210+
).__name__
211+
212+
# Now load the actual test file
213+
if not parents:
214+
pdir = dirname
215+
216+
with osext.change_dir(pdir):
217+
with util.temp_sys_path(pdir):
205218
return self.load_from_module(
206-
util.import_module_from_file(filename, force, parent)
219+
util.import_module_from_file(filename, force,
220+
parent_module)
207221
)
208222
except Exception:
209223
exc_info = sys.exc_info()

unittests/resources/checks_unlisted/testlib/nested/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2016-2024 Swiss National Supercomputing Centre (CSCS/ETH Zurich)
2+
# ReFrame Project Developers. See the top-level LICENSE file for details.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
6+
import reframe as rfm
7+
import reframe.utility.sanity as sn
8+
from ..utility import dummy_fixture
9+
10+
11+
@rfm.simple_test
12+
class dummy_test(rfm.RunOnlyRegressionTest):
13+
valid_systems = ['*']
14+
valid_prog_environs = ['*']
15+
executable = 'true'
16+
sanity_patterns = sn.assert_true(1)
17+
dummy = fixture(dummy_fixture)

unittests/resources/checks_unlisted/testlib/simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class simple_echo_check(rfm.RunOnlyRegressionTest, pin_prefix=True):
1616
executable = 'echo'
1717
executable_opts = ['Hello']
1818
message = variable(str, value='World')
19-
dummy = fixture(dummy_fixture, scope='environment')
19+
dummy = fixture(dummy_fixture)
2020

2121
@run_before('run')
2222
def set_executable_opts(self):

unittests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ def test_testlib_inherit_fixture_in_different_files(run_reframe):
12631263
action='run',
12641264
)
12651265
assert returncode == 0
1266-
assert 'Ran 3/3 test case(s)' in stdout
1266+
assert 'Ran 4/4 test case(s)' in stdout
12671267
assert 'FAILED' not in stdout
12681268

12691269

unittests/test_loader.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,9 @@ def test_relative_import_outside_rfm_prefix(loader, tmp_path):
154154
)
155155
tests = loader.load_from_file(str(tmp_path / 'testlib' / 'simple.py'))
156156
assert len(tests) == 2
157+
158+
# Test nested library tests
159+
tests = loader.load_from_file(
160+
str(tmp_path / 'testlib' / 'nested' / 'dummy.py')
161+
)
162+
assert len(tests) == 2

0 commit comments

Comments
 (0)