def test_lazily_locate_dotted_path_error_if_no_package_spec(dotted_path_str): with pytest.raises(ModuleNotFoundError) as excinfo: dotted_path.lazily_locate_dotted_path(dotted_path_str) assert (f"Error processing dotted path '{dotted_path_str}', no " "module named 'a'" in str(excinfo.value))
def test_lazily_locate_dotted_path_error_if_invalid_dotted_path( dotted_path_str): with pytest.raises(ValueError) as excinfo: dotted_path.lazily_locate_dotted_path(dotted_path_str) expected = (f"Invalid dotted path '{dotted_path_str}'. " "Value must be a dot " "separated string, with at least two parts: " "[module_name].[function_name]") assert str(excinfo.value) == expected
def test_lazily_locate_dotted_path_missing_module(tmp_directory, add_current_to_sys_path, no_sys_modules_cache): Path('a').mkdir() Path('a', '__init__.py').touch() with pytest.raises(ModuleNotFoundError) as excinfo: dotted_path.lazily_locate_dotted_path('a.b.c') assert "No module named 'a.b'. Expected to find one of" in str( excinfo.value)
def test_error_if_doesnt_define_name(tmp_directory, add_current_to_sys_path, no_sys_modules_cache): Path('a.py').touch() with pytest.raises(AttributeError) as excinfo: dotted_path.lazily_locate_dotted_path('a.unknown_name') assert "Failed to locate dotted path 'a.unknown_name'" in str( excinfo.value) assert "a.py" in str(excinfo.value) assert "a function named 'unknown_name'" in str(excinfo.value)
def get_loc(self): if self._from_dotted_path: loc, _ = lazily_locate_dotted_path(self._primitive) return loc else: path = getfile(self.load()) _, line = inspect.getsourcelines(self.load()) return '{}:{}'.format(path, line)
def test_lazily_located_dotted_path(dotted_path_str, tmp_imports): loc, source = dotted_path.lazily_locate_dotted_path(dotted_path_str) obj = dotted_path.load_dotted_path(dotted_path_str) loc_real = getfile(obj) lines, line = inspect.getsourcelines(obj) source_expected = ''.join(lines) loc_expected = f'{loc_real}:{line}' assert loc == loc_expected assert source == source_expected
def get_source(self): if self._from_dotted_path: _, source = lazily_locate_dotted_path(self._primitive) return source else: return inspect.getsource(self.load())