3
0
Fork 0

feat: add util.resource_path() function

need that now that we have configurable mako template paths
This commit is contained in:
Lance Edgar 2024-08-26 10:12:52 -05:00
parent 94868bbaa9
commit b401fac04f
3 changed files with 84 additions and 0 deletions

View file

@ -277,3 +277,43 @@ class TestProgressLoop(TestCase):
# without progress
mod.progress_loop(act, [1, 2, 3], None,
message="whatever")
class TestResourcePath(TestCase):
def test_basic(self):
# package spec is resolved to path
path = mod.resource_path('wuttjamaican:util.py')
self.assertTrue(path.endswith('wuttjamaican/util.py'))
# absolute path returned as-is
self.assertEqual(mod.resource_path('/tmp/doesnotexist.txt'), '/tmp/doesnotexist.txt')
def test_basic_pre_python_3_9(self):
# the goal here is to get coverage for code which would only
# run on python 3.8 and older, but we only need that coverage
# if we are currently testing python 3.9+
if sys.version_info.major == 3 and sys.version_info.minor < 9:
pytest.skip("this test is not relevant before python 3.9")
from importlib.resources import files, as_file
orig_import = __import__
def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == 'importlib.resources':
raise ImportError
if name == 'importlib_resources':
return MagicMock(files=files, as_file=as_file)
return orig_import(name, globals, locals, fromlist, level)
with patch('builtins.__import__', side_effect=mock_import):
# package spec is resolved to path
path = mod.resource_path('wuttjamaican:util.py')
self.assertTrue(path.endswith('wuttjamaican/util.py'))
# absolute path returned as-is
self.assertEqual(mod.resource_path('/tmp/doesnotexist.txt'), '/tmp/doesnotexist.txt')