diff --git a/reframe/utility/__init__.py b/reframe/utility/__init__.py index 279f7115e9..d0aba8b887 100644 --- a/reframe/utility/__init__.py +++ b/reframe/utility/__init__.py @@ -88,10 +88,12 @@ def import_module_from_file(filename, force=False): module_name = _get_module_name(rel_filename) if rel_filename.startswith('..'): # We cannot use the standard Python import mechanism here, because the - # module to import is outside the top-level package + # module to import is outside the top-level package. We also mangle + # the name that we assign to the module, in order to avoid clashes + # with other modules loaded with a standard `import` or with multiple + # test files with the same name that reside in different directories. module_hash = sha256(filename.encode('utf-8')).hexdigest()[:8] - module_name = f'rfm_{module_name}_{module_hash}' - + module_name = f'{module_name}@{module_hash}' return _do_import_module_from_file(filename, module_name) # Extract module name if `filename` is under `site-packages/` or the diff --git a/unittests/test_loader.py b/unittests/test_loader.py index afd0a6fe06..2100f875d3 100644 --- a/unittests/test_loader.py +++ b/unittests/test_loader.py @@ -90,7 +90,6 @@ def test_load_fixtures(loader): def test_existing_module_name(loader, tmp_path): - print(type(tmp_path)) test_file = tmp_path / 'os.py' shutil.copyfile('unittests/resources/checks/emptycheck.py', test_file) checks = loader.load_from_file(test_file) diff --git a/unittests/test_utility.py b/unittests/test_utility.py index f6c81c694d..210889422f 100644 --- a/unittests/test_utility.py +++ b/unittests/test_utility.py @@ -486,8 +486,8 @@ def test_import_from_file_existing_module_name(tmp_path): module = util.import_module_from_file(test_file) assert module.var == 1 - assert not hasattr(module, 'abc') - assert hasattr(os, 'abc') + assert not hasattr(module, 'path') + assert hasattr(os, 'path') def test_import_from_file_load_directory_relative():