|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Tests for the thin-page hub exemption and for reading the page a gap edits. |
| 3 | +
|
| 4 | + python pipeline/test_hub_and_reads.py |
| 5 | +
|
| 6 | +Runs against a temp docs tree, so no repo file is read and none is written. |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | +import unittest |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 15 | + |
| 16 | +import detect_gaps # noqa: E402 |
| 17 | +import generate # noqa: E402 |
| 18 | + |
| 19 | + |
| 20 | +class TempDocsTree(unittest.TestCase): |
| 21 | + def setUp(self): |
| 22 | + self._tmp = tempfile.TemporaryDirectory() |
| 23 | + self.root = Path(self._tmp.name) |
| 24 | + self._saved = (detect_gaps.REPO_ROOT, generate.REPO_ROOT) |
| 25 | + detect_gaps.REPO_ROOT = self.root |
| 26 | + generate.REPO_ROOT = self.root |
| 27 | + self.addCleanup(self._restore) |
| 28 | + |
| 29 | + def _restore(self): |
| 30 | + detect_gaps.REPO_ROOT, generate.REPO_ROOT = self._saved |
| 31 | + self._tmp.cleanup() |
| 32 | + |
| 33 | + def write(self, rel, text="---\ntitle: T\n---\n\nSome body copy.\n"): |
| 34 | + path = self.root / rel |
| 35 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 36 | + path.write_text(text, encoding="utf-8") |
| 37 | + |
| 38 | + |
| 39 | +class HasChildPagesTest(unittest.TestCase): |
| 40 | + def test_a_page_with_children_is_a_hub(self): |
| 41 | + pages = ["docs/notices", "docs/notices/july-2024"] |
| 42 | + self.assertTrue(detect_gaps.has_child_pages(pages, "docs/notices")) |
| 43 | + |
| 44 | + def test_a_leaf_is_not(self): |
| 45 | + self.assertFalse(detect_gaps.has_child_pages(["docs/a", "docs/b"], "docs/a")) |
| 46 | + |
| 47 | + def test_a_name_prefix_is_not_a_child(self): |
| 48 | + """'docs/x-ray' must not count as a child of 'docs/x'.""" |
| 49 | + self.assertFalse(detect_gaps.has_child_pages(["docs/x", "docs/x-ray"], "docs/x")) |
| 50 | + |
| 51 | + |
| 52 | +class ThinPageDetectionTest(TempDocsTree): |
| 53 | + def test_hub_is_not_flagged_but_a_real_thin_page_is(self): |
| 54 | + short = "---\ntitle: T\ndescription: d\n---\n\nToo short.\n" |
| 55 | + for page in ("docs/hub", "docs/hub/child", "docs/lonely"): |
| 56 | + self.write(f"{page}.mdx", short) |
| 57 | + docs_json = {"navigation": {"tabs": [ |
| 58 | + {"tab": "Home", "pages": ["docs/hub", "docs/hub/child", "docs/lonely"]}, |
| 59 | + ]}} |
| 60 | + |
| 61 | + thin = [g["description"] for g in detect_gaps.detect_gaps(docs_json, [], {}) |
| 62 | + if g["type"] == "thin_page"] |
| 63 | + self.assertEqual(sorted(thin), [ |
| 64 | + "docs/hub/child has under 100 words", # a child can still be thin |
| 65 | + "docs/lonely has under 100 words", |
| 66 | + ]) |
| 67 | + |
| 68 | + |
| 69 | +class LoadExistingPageTest(TempDocsTree): |
| 70 | + def test_reads_the_page(self): |
| 71 | + self.write("docs/a.mdx", "---\ntitle: T\n---\n\nReal content.\n") |
| 72 | + page = generate.load_existing_page({"type": "thin_page", "path": "docs/a.mdx"}) |
| 73 | + self.assertIn("Real content.", page) |
| 74 | + |
| 75 | + def test_a_missing_file_raises_instead_of_returning_nothing(self): |
| 76 | + """An empty read used to become "write this page from scratch".""" |
| 77 | + with self.assertRaises(FileNotFoundError): |
| 78 | + generate.load_existing_page({"type": "thin_page", "path": "docs/ghost.mdx"}) |
| 79 | + |
| 80 | + def test_a_directory_raises_file_not_found_not_is_a_directory(self): |
| 81 | + self.write("docs/a/child.mdx") |
| 82 | + with self.assertRaises(FileNotFoundError): |
| 83 | + generate.load_existing_page({"type": "thin_page", "path": "docs/a"}) |
| 84 | + |
| 85 | + def test_an_empty_page_raises(self): |
| 86 | + self.write("docs/blank.mdx", " \n") |
| 87 | + with self.assertRaises(ValueError): |
| 88 | + generate.load_existing_page({"type": "thin_page", "path": "docs/blank.mdx"}) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main(verbosity=2) |
0 commit comments