]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
PackageLoader works with single module file 1542/head
authorDavid Lord <davidism@gmail.com>
Tue, 9 Nov 2021 20:14:48 +0000 (12:14 -0800)
committerDavid Lord <davidism@gmail.com>
Tue, 9 Nov 2021 20:18:53 +0000 (12:18 -0800)
CHANGES.rst
src/jinja2/loaders.py
tests/test_loader.py

index a000888d57e84aecc372fc001da3b18670a06a67..d93d3fe399ac9246daab7d9b19a4bca86a1f0a55 100644 (file)
@@ -13,6 +13,8 @@ Unreleased
     :issue:`1514`
 -   Revert change to ``hash(Node)`` behavior. Nodes are hashed by id
     again :issue:`1521`
+-   ``PackageLoader`` works when the package is a single module file.
+    :issue:`1512`
 
 
 Version 3.0.2
index 513c858a80c183d70ac805d471b3b8cca276553f..4fac3a0966546c387f4018644979780302f4cd4c 100644 (file)
@@ -297,10 +297,18 @@ class PackageLoader(BaseLoader):
             self._archive = loader.archive
             pkgdir = next(iter(spec.submodule_search_locations))  # type: ignore
             template_root = os.path.join(pkgdir, package_path)
-        elif spec.submodule_search_locations:
-            # This will be one element for regular packages and multiple
-            # for namespace packages.
-            for root in spec.submodule_search_locations:
+        else:
+            roots: t.List[str] = []
+
+            # One element for regular packages, multiple for namespace
+            # packages, or None for single module file.
+            if spec.submodule_search_locations:
+                roots.extend(spec.submodule_search_locations)
+            # A single module file, use the parent directory instead.
+            elif spec.origin is not None:
+                roots.append(os.path.dirname(spec.origin))
+
+            for root in roots:
                 root = os.path.join(root, package_path)
 
                 if os.path.isdir(root):
index 1533d0234ca7d747506b5e2796ecc0219cd3870f..b300c8f24411a7119aa0982ed08983ea6a8dbf21 100644 (file)
@@ -313,6 +313,28 @@ def test_package_dir_list(package_dir_loader):
     assert "test.html" in templates
 
 
+@pytest.fixture()
+def package_file_loader(monkeypatch):
+    monkeypatch.syspath_prepend(Path(__file__).parent / "res")
+    return PackageLoader("__init__")
+
+
+@pytest.mark.parametrize(
+    ("template", "expect"), [("foo/test.html", "FOO"), ("test.html", "BAR")]
+)
+def test_package_file_source(package_file_loader, template, expect):
+    source, name, up_to_date = package_file_loader.get_source(None, template)
+    assert source.rstrip() == expect
+    assert name.endswith(os.path.join(*split_template_path(template)))
+    assert up_to_date()
+
+
+def test_package_file_list(package_file_loader):
+    templates = package_file_loader.list_templates()
+    assert "foo/test.html" in templates
+    assert "test.html" in templates
+
+
 @pytest.fixture()
 def package_zip_loader(monkeypatch):
     package_zip = (Path(__file__) / ".." / "res" / "package.zip").resolve()