Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions reframe/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def import_module_from_file(filename):
if os.path.isdir(filename):
filename = os.path.join(filename, '__init__.py')

if filename.startswith('..'):
filename = os.path.abspath(filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is the only solution. It seems we cannot use the importlib.import_module(), because we don't which package to attach to. Am I right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and it now works both with settings and checks defined with relative paths.


module_name = _get_module_name(filename)
if os.path.isabs(filename):
return _do_import_module_from_file(filename, module_name)
Expand Down
14 changes: 14 additions & 0 deletions unittests/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ def test_load_unknown_path(self):
self.assertEqual('foo', e.name)
self.assertEqual('/foo', e.path)

def test_load_directory_relative(self):
with os_ext.change_dir('reframe'):
module = util.import_module_from_file('../reframe')
self.assertEqual(reframe.VERSION, module.VERSION)
self.assertEqual('reframe', module.__name__)
self.assertIs(module, sys.modules.get('reframe'))

def test_load_file_relative(self):
with os_ext.change_dir('reframe'):
module = util.import_module_from_file('../reframe/__init__.py')
self.assertEqual(reframe.VERSION, module.VERSION)
self.assertEqual('reframe', module.__name__)
self.assertIs(module, sys.modules.get('reframe'))

def test_load_twice(self):
filename = os.path.abspath('reframe/__init__.py')
module1 = util.import_module_from_file(filename)
Expand Down