File size: 4,846 Bytes
6ff40c8
 
 
 
 
 
 
 
 
 
 
 
 
d9f8582
6ff40c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9f8582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""The README must stay complete.

Not a style rule. Each item here is something a stranger needs in order to
decide whether to trust this and how to run it, and every one of them has gone
missing from some README in this portfolio at some point -- a badge pointing at
a workflow that had been renamed, a quickstart whose output no longer matched,
a contributing pointer to a file that did not exist.

The checks are structural. Whether the *content* is honest is enforced
separately, by the guards that re-run the transcripts and diff them.
"""
from __future__ import annotations

import os
import re
from pathlib import Path

HERE = Path(__file__).resolve().parents[1]
_RAW = (HERE / "README.md").read_text(encoding="utf-8")
# Hugging Face cards open with YAML front-matter. It is metadata, not prose,
# and counting it as the top of the document made the tagline check look past
# the tagline entirely.
README = re.sub(r"\A---\n.*?\n---\n", "", _RAW, flags=re.S)


def test_there_is_a_one_line_description_near_the_top() -> None:
    """A stranger should know what this is within two screens-worth of text."""
    head = "\n".join(README.splitlines()[:14])
    # The tagline may wrap; `re.S` lets `.` cross the line break.
    assert re.search(r"^\*\*.+?\*\*", head, re.M | re.S), (
        "no bold description in the first 14 lines after any front-matter"
    )


def test_every_required_section_is_present() -> None:
    missing = [s for s in ['Why this exists', '30-second quickstart', 'Scope, honestly', 'Contributing', 'Citation', 'Licence'] if f"## {s}" not in README]
    assert not missing, f"README is missing section(s): {missing}"


def test_every_badge_is_an_image_link_with_an_https_target() -> None:
    badges = re.findall(r"!\[[^\]]*\]\(([^)]+)\)", README)
    assert badges, "no badges"
    bad = [b for b in badges if not b.startswith("https://")]
    assert not bad, f"badge(s) with a non-https target: {bad}"


def test_local_links_point_at_files_that_exist() -> None:
    """A dead relative link is the cheapest possible broken promise."""
    targets = re.findall(r"\]\((?!https?://|#)([^)\s]+)\)", README)
    missing = [t for t in targets if not (HERE / t.split("#")[0]).exists()]
    assert not missing, f"README links to path(s) that do not exist: {missing}"


def test_it_cross_links_the_portfolio_and_the_docs_site() -> None:
    assert "nickharris808.github.io/physics-lint" in README, "no docs-site link"
    assert re.search(r"rest of the toolkit|## Related", README), (
        "no cross-links to the sibling artifacts"
    )


def test_the_scope_section_says_what_this_does_not_prove() -> None:
    m = re.search(r"^## Scope, honestly$(.*?)(?=^## |\Z)", README, re.M | re.S)
    assert m, "no honest-scope section"
    body = m.group(1).lower()
    assert re.search(r"\bnot\b|never|does not", body), (
        "the scope section states no limitation, which is not a scope section"
    )


def test_the_tests_badge_matches_the_number_of_tests_that_actually_exist() -> None:
    """A hardcoded `tests-N collected` badge is decorative unless something checks it.

    This badge is the one number on the page a reader cannot verify by looking,
    so it is the one most likely to rot. Collect the suite in a subprocess and
    compare. If collection itself fails we skip rather than fail: a broken
    environment is not evidence that the badge is wrong.
    """
    import subprocess
    import sys

    m = re.search(r"tests-(\d+)%20collected", README)
    if not m:
        return  # this card carries no tests badge
    claimed = int(m.group(1))

    proc = subprocess.run(
        [sys.executable, "-m", "pytest", "tests/", "--collect-only", "-q"],
        cwd=HERE,
        capture_output=True,
        text=True,
        # Inherit the environment and override one key. A stripped env drops
        # SYSTEMROOT on Windows, where CPython needs it to initialise sockets,
        # so the subprocess would die there and take CI with it.
        env={
            **{k: v for k, v in os.environ.items()
               if k not in ("PYTEST_ADDOPTS", "PYTEST_PLUGINS")},
            "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
        },
    )
    # Deliberately not a skip. A gate that goes green when it cannot run is
    # the same decoration as a hardcoded number, so a broken collection is a
    # failure here rather than a shrug.
    assert proc.returncode == 0, (
        f"could not collect the suite to check the badge:\n{proc.stdout}\n{proc.stderr}"
    )
    got = re.search(r"^(\d+) tests? collected", proc.stdout, re.M)
    assert got, f"could not parse a collected count from:\n{proc.stdout}"
    actual = int(got.group(1))
    assert claimed == actual, (
        f"README badge claims {claimed} tests, pytest collects {actual}. "
        f"Regenerate the badge rather than editing it by hand."
    )