separate messages, add test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent f54fa11 · Browse files · 3 files·+18 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+18 added−7 removed
- tests:+5 added−0 removed
- other:+13 added−7 removed
Provenance
- Commit
aaa083d- Tree
9efb818- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
f54fa11
3 files changed+18 added−7 removed
modified:src/jinja2/loaders.py+11 added-7 removed
325 unmodified lines326 loader = spec.loader327 assert loader is not None, "A loader was not found for the package."328 self._loader = loader329 self._archive = None330- template_root = None330 331 if isinstance(loader, zipimport.zipimporter):332 self._archive = loader.archive333 pkgdir = next(iter(spec.submodule_search_locations)) # type: ignore8 unmodified lines342 # A single module file, use the parent directory instead.343 elif spec.origin is not None:344 roots.append(os.path.dirname(spec.origin))345 346+ if not roots:347+ raise ValueError(348+ f"The {package_name!r} package was not installed in a"349+ " way that PackageLoader understands."350+ )351+352 for root in roots:353 root = os.path.join(root, package_path)354 355 if os.path.isdir(root):356 template_root = root357 break358+ else:359+ raise ValueError(360+ f"PackageLoader could not find a {package_path!r} directory"361+ f" in the {package_name!r} package."362+ )363 354- if template_root is None:355- raise ValueError(356- f"PackageLoader could not find a '{package_path}' directory for the "357- f"{package_name!r} package."358- )359-364 self._template_root = template_root365 366 def get_source(367 self, environment: "Environment", template: str326 unmodified linesmodified:tests/test_loader.py+5 added
427 unmodified lines428 package_loader = PackageLoader("res")429 assert "test.html" in package_loader.list_templates()430 finally:431 sys.meta_path[:] = before432+433+434+def test_package_loader_no_dir() -> None:435+ with pytest.raises(ValueError, match="could not find a 'templates' directory"):436+ PackageLoader("jinja2")modified:CHANGES.rst+2 added
35 unmodified lines36 :issue:`2027`37 - ``Environment.overlay(enable_async)`` is applied correctly. :pr:`2061`38 - The error message from ``FileSystemLoader`` includes the paths that were39 searched. :issue:`1661`40+- ``PackageLoader`` shows a clearer error message when the package does not41+ contain the templates directory. :issue:`1705`42 43 44 Version 3.1.445 -------------994 unmodified lines