Skip to content

Commit 1b468e1

Browse files
authored
Merge pull request #248 from python-ellar/html_extension_fix
fix: Skip template name with any extension
2 parents cd2d5af + 2e206fb commit 1b468e1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

ellar/core/templating/service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import typing as t
23
from functools import lru_cache
34

@@ -13,8 +14,14 @@
1314

1415
@lru_cache(1200)
1516
def get_template_name(template_name: str) -> str:
16-
if not template_name.endswith(".html"):
17+
# Split the filename and extension
18+
name, ext = os.path.splitext(template_name)
19+
20+
# If there's no extension, add .html
21+
if not ext:
1722
return template_name + ".html"
23+
24+
# Otherwise, return the original name
1825
return template_name
1926

2027

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from ellar.core.templating.service import get_template_name
3+
4+
5+
@pytest.mark.parametrize(
6+
"input_name, expected_output",
7+
[
8+
("template", "template.html"),
9+
("page.html", "page.html"),
10+
("document.txt", "document.txt"),
11+
("sds.rd.xml", "sds.rd.xml"),
12+
("nested/path/file", "nested/path/file.html"),
13+
("nested/path/file.jpg", "nested/path/file.jpg"),
14+
("", ".html"), # Edge case: empty string
15+
(".gitignore", ".gitignore"), # Edge case: hidden file
16+
],
17+
)
18+
def test_get_template_name(input_name, expected_output):
19+
assert get_template_name(input_name) == expected_output
20+
21+
22+
def test_get_template_name_caching():
23+
# Test that the function is actually cached
24+
assert get_template_name.cache_info().hits == 0
25+
get_template_name("test")
26+
get_template_name("test")
27+
assert get_template_name.cache_info().hits == 1

0 commit comments

Comments
 (0)