feat: add tests, CI and P0 reliability features
This commit is contained in:
1 parent
0754065dae
commit
626650e44a
22 files changed
+1576
-49
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""Check relative links and image paths in Markdown files.
|
||||
|
||||
Usage:
|
||||
python tools/check_links.py
|
||||
python tools/check_links.py docs README.md
|
||||
|
||||
External links are not fetched; only local targets are verified. Returns a
|
||||
non-zero exit code when a local link points to a missing file or directory.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.parse
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
SKIP_DIRS = {".git", ".gitea", "cache", "data", "__pycache__", "node_modules"}
|
||||
EXTERNAL_PREFIXES = ("http://", "https://", "mailto:", "tel:", "data:")
|
||||
LINK_RE = re.compile(r"!?\[[^\]]*\]\(([^)]+)\)")
|
||||
|
||||
|
||||
def iter_markdown(paths):
|
||||
for path in paths:
|
||||
if os.path.isdir(path):
|
||||
for dirpath, dirs, files in os.walk(path):
|
||||
dirs[:] = sorted(d for d in dirs if d not in SKIP_DIRS)
|
||||
for name in sorted(files):
|
||||
if name.endswith(".md"):
|
||||
yield os.path.join(dirpath, name)
|
||||
elif path.endswith(".md"):
|
||||
yield path
|
||||
|
||||
|
||||
def link_targets(text):
|
||||
for lineno, line in enumerate(text.splitlines(), 1):
|
||||
for raw in LINK_RE.findall(line):
|
||||
target = raw.strip()
|
||||
if target.startswith("<") and target.endswith(">"):
|
||||
target = target[1:-1].strip()
|
||||
if not target or target.startswith(EXTERNAL_PREFIXES):
|
||||
continue
|
||||
target = target.split("#", 1)[0].strip()
|
||||
if not target:
|
||||
continue
|
||||
yield lineno, target
|
||||
|
||||
|
||||
def check_file(path):
|
||||
errors = []
|
||||
with open(path, encoding="utf-8", errors="replace") as fh:
|
||||
text = fh.read()
|
||||
for lineno, target in link_targets(text):
|
||||
if target.startswith("/"):
|
||||
resolved = os.path.join(BASE_DIR, target.lstrip("/"))
|
||||
else:
|
||||
resolved = os.path.join(os.path.dirname(path), target)
|
||||
resolved = os.path.normpath(urllib.parse.unquote(resolved.split(" ", 1)[0]))
|
||||
if not os.path.exists(resolved):
|
||||
errors.append(f"{path}:{lineno}: missing local link target: {target}")
|
||||
return errors
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
targets = [os.path.join(BASE_DIR, p) for p in argv] or [BASE_DIR]
|
||||
files = list(iter_markdown(targets))
|
||||
errors = []
|
||||
for path in files:
|
||||
errors.extend(check_file(path))
|
||||
if errors:
|
||||
print("\n".join(errors), file=sys.stderr)
|
||||
print(f"Broken links: {len(errors)}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"Checked {len(files)} Markdown files: no broken local links")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+38
-1
@@ -6,6 +6,7 @@ from datetime import datetime, timezone
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
|
||||
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
|
||||
SCHEMA_VERSION = 1
|
||||
|
||||
MOOD_PRESETS = {
|
||||
"adventure-rpg-party": {
|
||||
@@ -233,10 +234,46 @@ def _ensure_column(con, table, column, declaration):
|
||||
con.execute(f"ALTER TABLE {table} ADD COLUMN {column} {declaration}")
|
||||
|
||||
|
||||
def _get_schema_version(con):
|
||||
try:
|
||||
row = con.execute("SELECT value FROM schema_meta WHERE key = 'schema_version'").fetchone()
|
||||
except sqlite3.OperationalError:
|
||||
return 0
|
||||
if not row or row[0] is None:
|
||||
return 0
|
||||
try:
|
||||
return int(row[0])
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def _migration_1(con):
|
||||
"""Add game_details.source_appid, introduced before schema versioning."""
|
||||
_ensure_column(con, "game_details", "source_appid", "INTEGER")
|
||||
|
||||
|
||||
MIGRATIONS = {
|
||||
1: _migration_1,
|
||||
}
|
||||
|
||||
|
||||
def init(con):
|
||||
"""Create or upgrade the database schema.
|
||||
|
||||
Migrations run in order from the stored ``schema_meta.schema_version``.
|
||||
A database created by a newer release is rejected instead of being
|
||||
silently modified by an older one.
|
||||
"""
|
||||
version = _get_schema_version(con)
|
||||
if version > SCHEMA_VERSION:
|
||||
raise RuntimeError(
|
||||
f"database schema version {version} is newer than supported {SCHEMA_VERSION}")
|
||||
with open(SCHEMA_PATH, encoding="utf-8") as f:
|
||||
con.executescript(f.read())
|
||||
_ensure_column(con, "game_details", "source_appid", "INTEGER")
|
||||
for target in range(version + 1, SCHEMA_VERSION + 1):
|
||||
MIGRATIONS[target](con)
|
||||
con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('schema_version', ?)",
|
||||
(str(SCHEMA_VERSION),))
|
||||
con.commit()
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user