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_app_docs_seed_sync.py · 5.4 KB · 118 lines
| 1 | """The public /docs pages (packages/app-docs/seed/doc_pages.tsv) are a |
| 2 | generated copy of docs/, written by scripts/sync_app_docs.py. A page that |
| 3 | differs from what the script makes of docs/ today is drift: someone edited a |
| 4 | doc and did not run the script, or edited the seed by hand.""" |
| 5 | |
| 6 | import re |
| 7 | from pathlib import Path |
| 8 | |
| 9 | import pytest |
| 10 | |
| 11 | from scripts import sync_app_docs |
| 12 | |
| 13 | REPO = Path(__file__).resolve().parents[1] |
| 14 | PAGES = {"guide", "other"} |
| 15 | |
| 16 | |
| 17 | def test_every_seeded_page_is_what_the_script_makes_of_docs_today(): |
| 18 | current = sync_app_docs.read_seed() |
| 19 | wanted, changed = sync_app_docs.build(current) |
| 20 | assert not changed, ( |
| 21 | f"/docs pages differ from docs/: {', '.join(changed)}. Run " |
| 22 | "`python scripts/sync_app_docs.py`, commit packages/app-docs/seed/doc_pages.tsv, " |
| 23 | "and bump the version in packages/app-docs/dbbasic-package.json if it has been released." |
| 24 | ) |
| 25 | # and the file is byte for byte what the script writes |
| 26 | assert sync_app_docs.SEED.read_text(encoding="utf-8") == sync_app_docs.dump(wanted) |
| 27 | |
| 28 | |
| 29 | def test_every_page_has_a_docs_file_and_no_banned_name(): |
| 30 | rows = sync_app_docs.read_seed() |
| 31 | assert len(rows) > 30 |
| 32 | for row in rows[1:]: |
| 33 | assert sync_app_docs.doc_path(row[0]).is_file(), row[0] |
| 34 | assert not sync_app_docs.BANNED.search(row[-1]), row[0] |
| 35 | |
| 36 | |
| 37 | def test_the_title_goes_and_links_point_at_pages(): |
| 38 | doc = ( |
| 39 | "# Guide\n\nSee [the other\npage](other.md#part), [here](#below), " |
| 40 | "[the readme](../README.md), [status](status.md) and (see docs/other.md)\n" |
| 41 | ) |
| 42 | assert sync_app_docs.render("guide", doc, PAGES) == ( |
| 43 | "See [the other\npage](/docs/other#part), [here](#below), " |
| 44 | "the readme, status and (see `docs/other.md`)\n" |
| 45 | ) |
| 46 | with pytest.raises(sync_app_docs.SyncError): |
| 47 | sync_app_docs.render("guide", "No title line\n\ntext\n", PAGES) |
| 48 | |
| 49 | |
| 50 | def test_the_repository_address_becomes_a_placeholder(): |
| 51 | url = sync_app_docs.REPOSITORY_URL |
| 52 | doc = f"# Guide\n\ngit clone {url}\ncd dbbasic-object-server\ngit clone {url} /opt/x\n" |
| 53 | assert sync_app_docs.render("guide", doc, PAGES) == ( |
| 54 | "git clone <repository-url> dbbasic-object-server\ncd dbbasic-object-server\n" |
| 55 | "git clone <repository-url> /opt/x\n" |
| 56 | ) |
| 57 | org_link = "# Guide\n\nThe console ([Scroll](https://github.com/askrobots/dbbasic-scroll)) too.\n" |
| 58 | assert sync_app_docs.render("guide", org_link, PAGES) == "The console (Scroll) too.\n" |
| 59 | |
| 60 | |
| 61 | def test_a_banned_name_with_no_rewording_is_refused_not_shipped(monkeypatch): |
| 62 | doc = "# Guide\n\nAs q9 did before.\n" |
| 63 | with pytest.raises(sync_app_docs.SyncError, match="REWORDED"): |
| 64 | sync_app_docs.render("guide", doc, PAGES) |
| 65 | monkeypatch.setitem(sync_app_docs.REWORDED, "guide", [("As q9 did", "As an earlier project did")]) |
| 66 | assert sync_app_docs.render("guide", doc, PAGES) == "As an earlier project did before.\n" |
| 67 | # a rewording whose text docs/ no longer has is an error, so the list cannot rot |
| 68 | with pytest.raises(sync_app_docs.SyncError, match="no longer says"): |
| 69 | sync_app_docs.render("guide", "# Guide\n\nNothing to swap.\n", PAGES) |
| 70 | |
| 71 | |
| 72 | def test_every_rewording_is_for_a_seeded_page(): |
| 73 | ids = {row[0] for row in sync_app_docs.read_seed()[1:]} |
| 74 | assert set(sync_app_docs.REWORDED) <= ids |
| 75 | assert set(sync_app_docs.DOC_FILES) <= ids |
| 76 | |
| 77 | |
| 78 | def test_a_link_to_a_page_that_is_not_seeded_is_refused(): |
| 79 | with pytest.raises(sync_app_docs.SyncError, match="not a seeded page"): |
| 80 | sync_app_docs.render("guide", "# Guide\n\n[x](/docs/nowhere)\n", PAGES) |
| 81 | |
| 82 | |
| 83 | def test_check_mode_names_a_page_that_differs(tmp_path, monkeypatch, capsys): |
| 84 | rows = sync_app_docs.read_seed() |
| 85 | rows[1][-1] += "edited by hand\n" |
| 86 | seed = tmp_path / "doc_pages.tsv" |
| 87 | seed.write_text(sync_app_docs.dump(rows), encoding="utf-8", newline="") |
| 88 | monkeypatch.setattr(sync_app_docs, "SEED", seed) |
| 89 | assert sync_app_docs.main(["--check"]) == 1 |
| 90 | assert rows[1][0] in capsys.readouterr().out |
| 91 | assert sync_app_docs.main([]) == 0 |
| 92 | assert sync_app_docs.main(["--check"]) == 0 |
| 93 | |
| 94 | |
| 95 | def _commands(markdown): |
| 96 | lines = [] |
| 97 | for block in re.findall(r"```[^\n]*\n(.*?)```", markdown, re.S): |
| 98 | lines += [line.strip() for line in block.split("\n")] |
| 99 | return lines |
| 100 | |
| 101 | |
| 102 | def test_documented_install_matches_install_sh(): |
| 103 | """The pages are copies of docs/, so docs/ is held to scripts/install.sh.""" |
| 104 | script = (REPO / "scripts" / "install.sh").read_text(encoding="utf-8") |
| 105 | repo_url = re.search(r'REPO_URL="\$\{DBBASIC_REPO_URL:-([^}]+)\}"', script).group(1) |
| 106 | assert repo_url == sync_app_docs.REPOSITORY_URL |
| 107 | extras = re.search(r'pip install -q -e "\$\{CODE_DIR\}(\[[a-z,]+\])"', script).group(1) |
| 108 | assert "dbbasic-object-daemon.service" in script # so an upgrade restarts it too |
| 109 | for name in ("quickstart", "single-vm-deployment"): |
| 110 | text = (REPO / "docs" / f"{name}.md").read_text(encoding="utf-8") |
| 111 | commands = _commands(text) |
| 112 | clones = [c for c in commands if "git clone" in c] |
| 113 | assert clones and all(f"git clone {repo_url}" in c for c in clones), (name, clones) |
| 114 | installs = [c for c in commands if "pip install -e" in c] |
| 115 | assert installs and all(f"'.{extras}'" in c for c in installs), (name, installs) |
| 116 | upgrade = text[text.index("git pull --ff-only"):] |
| 117 | restart = next(line for line in upgrade.split("\n") if "systemctl restart" in line) |
| 118 | assert "dbbasic-object-server" in restart and "dbbasic-object-daemon" in restart, (name, restart) |