askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_docs_no_line_references.py · 1.4 KB · 34 lines
| 1 | """docs/ and the README never point at code by line number. |
| 2 | |
| 3 | `object_records.py:5411` is wrong after the next commit to that file, and |
| 4 | nothing notices: name the function (`object_records._is_empty`) instead.""" |
| 5 | |
| 6 | import re |
| 7 | from pathlib import Path |
| 8 | |
| 9 | REPO = Path(__file__).resolve().parents[1] |
| 10 | LINE_REFERENCE = re.compile(r"[A-Za-z0-9_./-]+\.py:\d+(?:-\d+)?") |
| 11 | |
| 12 | |
| 13 | def line_references(text): |
| 14 | return LINE_REFERENCE.findall(text) |
| 15 | |
| 16 | |
| 17 | def test_the_pattern_finds_line_references_and_spares_names(): |
| 18 | assert line_references("see `object_records.py:5411`, `5546`") == ["object_records.py:5411"] |
| 19 | assert line_references("delete (object_records.py:1384-1487)") == ["object_records.py:1384-1487"] |
| 20 | assert line_references("tests/test_x.py:12: AssertionError") == ["tests/test_x.py:12"] |
| 21 | assert line_references("`object_records._is_empty`, `tests/test_x.py::test_y`, port 8001") == [] |
| 22 | |
| 23 | |
| 24 | def test_no_doc_points_at_code_by_line_number(): |
| 25 | files = sorted((REPO / "docs").glob("*.md")) + [REPO / "README.md"] |
| 26 | assert len(files) > 30 |
| 27 | found = [] |
| 28 | for path in files: |
| 29 | for number, line in enumerate(path.read_text(encoding="utf-8").split("\n"), 1): |
| 30 | found += [f"{path.relative_to(REPO)} line {number}: {ref}" for ref in line_references(line)] |
| 31 | assert not found, ( |
| 32 | "Line numbers go stale with the next commit; name the function instead " |
| 33 | "(`module.function`):\n" + "\n".join(found) |
| 34 | ) |