Skip to content

Commit d05a7ba

Browse files
committed
Add tests for generate_nginx_redirects.py
1 parent 99a4565 commit d05a7ba

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

hypernode/redirect/generate_nginx_redirects.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
from pathlib import Path
44
from typing import List
55

6-
from frontmatter import Frontmatter
7-
8-
from hypernode.common.docs import get_all_docs
6+
from hypernode.common.docs import get_all_docs, read_doc
97
from hypernode.common.settings import DOCS_DIR
108

119

1210
def get_redirects_from_doc(doc: Path) -> List[str]:
13-
fm = Frontmatter.read_file(doc)
14-
attributes = fm["attributes"] or {}
15-
return attributes.get("redirect_from", [])
11+
fm, _, _ = read_doc(doc)
12+
return fm.get("redirect_from", [])
1613

1714

1815
def get_path_for_doc(doc: Path) -> str:

tests/redirect/__init__.py

Whitespace-only changes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import os
2+
from pathlib import Path
3+
4+
from hypernode.common.settings import DOCS_DIR
5+
from hypernode.redirect.generate_nginx_redirects import (
6+
get_path_for_doc,
7+
get_redirects_from_doc,
8+
main,
9+
)
10+
from tests.testcase import HypernodeTestCase
11+
12+
13+
class TestMain(HypernodeTestCase):
14+
def setUp(self) -> None:
15+
module = "hypernode.redirect.generate_nginx_redirects."
16+
self.print = self.set_up_patch("builtins.print")
17+
self.get_all_docs = self.set_up_patch(
18+
module + "get_all_docs", return_value=[DOCS_DIR / "index.html"]
19+
)
20+
self.get_redirects_from_doc = self.set_up_patch(
21+
module + "get_redirects_from_doc", return_value=["/old-url/"]
22+
)
23+
24+
def test_main_prints_rewrites(self):
25+
main()
26+
27+
expected_output = """
28+
rewrite ^/old-url/$ / permanent;
29+
""".strip()
30+
self.print.assert_called_once_with(expected_output)
31+
32+
33+
class TestGetPathForDoc(HypernodeTestCase):
34+
def test_get_path_for_doc_resolves_relative_to_docs_dir(self):
35+
result = get_path_for_doc(DOCS_DIR / "some/doc.md")
36+
37+
self.assertEqual("/some/doc.html", result)
38+
39+
def test_get_path_for_doc_replaces_md_with_html(self):
40+
result = get_path_for_doc(DOCS_DIR / "something.md")
41+
42+
self.assertEqual("/something.html", result)
43+
44+
def test_get_path_for_doc_replaces_index_html_with_slash(self):
45+
result = get_path_for_doc(DOCS_DIR / "index.md")
46+
47+
self.assertEqual("/", result)
48+
49+
def test_get_path_for_doc_prepends_docs_base_url(self):
50+
try:
51+
os.environ["DOCS_BASE_URL"] = "https://www.example.com/"
52+
53+
result = get_path_for_doc(DOCS_DIR / "some/doc.html")
54+
55+
self.assertEqual("https://www.example.com/some/doc.html", result)
56+
finally:
57+
del os.environ["DOCS_BASE_URL"]
58+
59+
60+
class TestGetRedirectsFromDoc(HypernodeTestCase):
61+
def setUp(self) -> None:
62+
module = "hypernode.redirect.generate_nginx_redirects."
63+
self.read_doc = self.set_up_patch(
64+
module + "read_doc",
65+
return_value=({"redirect_from": ["/old/path/"]}, "", ""),
66+
)
67+
self.doc = Path("/some/path.md")
68+
69+
def test_get_redirects_from_doc(self):
70+
result = get_redirects_from_doc(self.doc)
71+
72+
self.assertEqual(["/old/path/"], result)
73+
self.read_doc.assert_called_once_with(self.doc)
74+
75+
def test_get_redirects_from_doc_defaults_to_empty_list(self):
76+
self.read_doc.return_value = ({}, "", "")
77+
78+
result = get_redirects_from_doc(self.doc)
79+
80+
self.assertEqual([], result)
81+
self.read_doc.assert_called_once_with(self.doc)

0 commit comments

Comments
 (0)