|
| 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