diff --git a/tests/unit/python/utils/meta/role/__init__.py b/tests/unit/python/utils/meta/role/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/utils/meta/role/test_titles.py b/tests/unit/python/utils/meta/role/test_titles.py new file mode 100644 index 0000000000..240735b917 --- /dev/null +++ b/tests/unit/python/utils/meta/role/test_titles.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path + +from utils.meta.role.names import role_names +from utils.meta.role.titles import role_title, role_titles + + +class TestRoleTitle(unittest.TestCase): + def setUp(self) -> None: + self._tmp = tempfile.TemporaryDirectory(prefix="infinito-titles-") + self.role = Path(self._tmp.name) / "web-app-demo" + (self.role / "meta").mkdir(parents=True) + self.addCleanup(self._tmp.cleanup) + + def test_the_readme_heading_is_the_title(self) -> None: + (self.role / "README.md").write_text("# Demo App\n\nText.\n", encoding="utf-8") + + self.assertEqual(role_title(self.role), "Demo App") + + def test_the_metadata_name_stands_in_without_a_heading(self) -> None: + (self.role / "meta" / "main.yml").write_text( + "galaxy_info:\n name: Declared Name\n", encoding="utf-8" + ) + + self.assertEqual(role_title(self.role), "Declared Name") + + def test_the_heading_wins_over_the_metadata(self) -> None: + (self.role / "README.md").write_text("# Demo App\n", encoding="utf-8") + (self.role / "meta" / "main.yml").write_text( + "galaxy_info:\n name: Declared Name\n", encoding="utf-8" + ) + + self.assertEqual( + role_title(self.role), + "Demo App", + "the documentation is built from the heading, so that is what a " + "reader sees the role called", + ) + + def test_only_the_first_heading_counts(self) -> None: + (self.role / "README.md").write_text( + "# Demo App\n\n# Later Heading\n", encoding="utf-8" + ) + + self.assertEqual(role_title(self.role), "Demo App") + + def test_a_role_declaring_nothing_has_no_title(self) -> None: + self.assertEqual(role_title(self.role), "") + + +class TestRepositorySources(unittest.TestCase): + def test_every_role_directory_is_listed(self) -> None: + names = role_names() + + self.assertIn("web-app-nextcloud", names) + self.assertEqual(list(names), sorted(names)) + + def test_the_titles_are_unique_and_sorted(self) -> None: + titles = role_titles() + + self.assertIn("Nextcloud", titles) + self.assertEqual(len(titles), len(set(titles))) + self.assertEqual(list(titles), sorted(titles)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/python/utils/meta/test_authors.py b/tests/unit/python/utils/meta/test_authors.py new file mode 100644 index 0000000000..0c79f65d2c --- /dev/null +++ b/tests/unit/python/utils/meta/test_authors.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import unittest + +from utils.meta.authors import author_names +from utils.roles.credits import author_urls +from utils.software import SOFTWARE_AUTHOR + + +class TestAuthorNames(unittest.TestCase): + def test_the_credited_people_are_listed(self) -> None: + self.assertIn(SOFTWARE_AUTHOR, author_names()) + + def test_the_names_are_sorted_and_unique(self) -> None: + names = author_names() + + self.assertEqual(list(names), sorted(names)) + self.assertEqual(len(names), len(set(names))) + + def test_it_carries_exactly_the_keys_of_the_url_map(self) -> None: + self.assertEqual( + set(author_names()), + set(author_urls()), + "the names are the map's keys, not a second list that could drift " + "from the Credits sections they are derived from", + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/i18n/names.py b/utils/i18n/names.py index 9002dc3788..8b92078a22 100644 --- a/utils/i18n/names.py +++ b/utils/i18n/names.py @@ -14,45 +14,15 @@ from __future__ import annotations import re from functools import lru_cache -from pathlib import Path -from utils.cache.files import PROJECT_ROOT, read_text -from utils.cache.yaml import load_yaml -from utils.roles.mapping import ROLE_FILE_META_MAIN +from utils.cache.files import PROJECT_ROOT +from utils.meta.role.names import role_names +from utils.meta.role.titles import role_titles -ROLES_DIR = "roles" -README = "README.md" SENTENCE_END = re.compile(r"[.!?:;\n]\s*[*_`\"'(\[]*\s*$") OPENERS = " *_`\"'([" -@lru_cache(maxsize=1) -def _sources(root: str) -> tuple[tuple[str, ...], tuple[str, ...]]: - """Return the role directory names and the titles they declare. - - Args: - root: repository root, as a string so the cache key stays hashable. - """ - roles, titles = [], set() - for path in sorted(Path(root, ROLES_DIR).iterdir()): - if not path.is_dir(): - continue - roles.append(path.name) - readme = path / README - if readme.is_file(): - for line in read_text(str(readme)).splitlines(): - if line.startswith("# "): - titles.add(line[2:].strip()) - break - meta = path / ROLE_FILE_META_MAIN - if meta.is_file(): - declared = load_yaml(str(meta)) or {} - name = (declared.get("galaxy_info") or {}).get("name") - if name: - titles.add(str(name).strip()) - return tuple(roles), tuple(titles) - - def _alternation(names) -> str: """Return a regex matching any of ``names``, longest first. @@ -81,7 +51,7 @@ def patterns( The pattern for hyphenated role names, which hold wherever they appear, and the one for titles, which holds only on a capitalised occurrence. """ - roles, titles = _sources(root) + roles, titles = role_names(root), role_titles(root) hyphenated = [name for name in roles if "-" in name] return ( re.compile(_alternation(hyphenated)) if hyphenated else None, diff --git a/utils/meta/authors.py b/utils/meta/authors.py new file mode 100644 index 0000000000..db8ac966be --- /dev/null +++ b/utils/meta/authors.py @@ -0,0 +1,19 @@ +"""The people the repository credits, as one source for every reader. + +The names are derived from the ``## Credits`` sections themselves rather than +kept as a list, so crediting a contributor needs no code change. The derivation +lives in :mod:`utils.roles.credits`; this module is the entry point for readers +that want the names alone. +""" + +from __future__ import annotations + +from functools import lru_cache + +from utils.roles.credits import author_urls + + +@lru_cache(maxsize=1) +def author_names() -> tuple[str, ...]: + """Return every credited person's name, sorted.""" + return tuple(sorted(author_urls())) diff --git a/utils/meta/role/__init__.py b/utils/meta/role/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/utils/meta/role/names.py b/utils/meta/role/names.py new file mode 100644 index 0000000000..e5182c14fe --- /dev/null +++ b/utils/meta/role/names.py @@ -0,0 +1,22 @@ +"""The directory names of the roles, as one source for every reader.""" + +from __future__ import annotations + +from functools import lru_cache +from pathlib import Path + +from utils.cache.files import PROJECT_ROOT + +ROLES_DIR = "roles" + + +@lru_cache(maxsize=1) +def role_names(root: str = str(PROJECT_ROOT)) -> tuple[str, ...]: + """Return every role directory name, sorted. + + Args: + root: repository root, as a string so the cache key stays hashable. + """ + return tuple( + sorted(path.name for path in Path(root, ROLES_DIR).iterdir() if path.is_dir()) + ) diff --git a/utils/meta/role/titles.py b/utils/meta/role/titles.py new file mode 100644 index 0000000000..06ee80cfb6 --- /dev/null +++ b/utils/meta/role/titles.py @@ -0,0 +1,54 @@ +"""The title a role presents itself under. + +A role declares it twice and the two may differ: the ``README.md`` heading is +what a reader sees, ``galaxy_info.name`` what the metadata carries. The heading +wins where both exist, because the documentation is built from it. +""" + +from __future__ import annotations + +from functools import lru_cache +from pathlib import Path + +from utils.cache.files import PROJECT_ROOT, read_text +from utils.cache.yaml import load_yaml +from utils.meta.role.names import ROLES_DIR +from utils.roles.mapping import ROLE_FILE_META_MAIN + +README = "README.md" + + +def role_title(role_dir: Path) -> str: + """Return the title ``role_dir`` declares, empty when it declares none. + + Args: + role_dir: the role's directory. + """ + readme = role_dir / README + if readme.is_file(): + for line in read_text(str(readme)).splitlines(): + if line.startswith("# "): + return line[2:].strip() + meta = role_dir / ROLE_FILE_META_MAIN + if meta.is_file(): + declared = (load_yaml(str(meta)) or {}).get("galaxy_info") or {} + if declared.get("name"): + return str(declared["name"]).strip() + return "" + + +@lru_cache(maxsize=1) +def role_titles(root: str = str(PROJECT_ROOT)) -> tuple[str, ...]: + """Return every declared role title, sorted and without duplicates. + + Args: + root: repository root, as a string so the cache key stays hashable. + """ + found = set() + for path in sorted(Path(root, ROLES_DIR).iterdir()): + if not path.is_dir(): + continue + title = role_title(path) + if title: + found.add(title) + return tuple(sorted(found))