Canon marker check: refuse repeated keys, read BOM files, fullmatch ids, loud bad base ref; untrack pyc (Soul)
Some checks failed
Canon marker / check (pull_request) Has been cancelled
Some checks failed
Canon marker / check (pull_request) Has been cancelled
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1ff6ef085a
commit
ddf79e4373
3 changed files with 102 additions and 7 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -32,3 +32,4 @@ Thumbs.db
|
|||
/quartz-site/node_modules/
|
||||
/quartz-site/public/
|
||||
/quartz-site/*.log
|
||||
__pycache__/
|
||||
|
|
|
|||
|
|
@ -15,17 +15,34 @@ def git(*a):
|
|||
return subprocess.run(["git", *a], check=True, capture_output=True, text=True).stdout
|
||||
|
||||
|
||||
class UniqueKeyLoader(yaml.SafeLoader):
|
||||
"""safe_load that refuses a repeated key: one page, one reading of `kind`."""
|
||||
|
||||
def construct_mapping(self, node, deep=False):
|
||||
seen = set()
|
||||
for key_node, _ in node.value:
|
||||
key = self.construct_object(key_node, deep=True)
|
||||
if key in seen:
|
||||
raise yaml.constructor.ConstructorError(
|
||||
None, None, f"duplicate key {key!r}", key_node.start_mark)
|
||||
seen.add(key)
|
||||
return super().construct_mapping(node, deep)
|
||||
|
||||
|
||||
FRONT = re.compile(r"---\n(?:(.*?)\n)?---[ \t]*(?:\n|\Z)", re.S)
|
||||
|
||||
|
||||
def frontmatter(text):
|
||||
"""Return (kind, rulings or None, error). Frontmatter is real YAML."""
|
||||
if not text.startswith("---\n"):
|
||||
return None, None, None
|
||||
end = text.find("\n---", 4)
|
||||
if end < 0:
|
||||
m = FRONT.match(text)
|
||||
if not m:
|
||||
return None, None, "unterminated frontmatter"
|
||||
try:
|
||||
data = yaml.safe_load(text[4:end])
|
||||
data = yaml.load(m.group(1) or "", Loader=UniqueKeyLoader)
|
||||
except yaml.YAMLError as e:
|
||||
return None, None, f"frontmatter is not valid YAML ({type(e).__name__})"
|
||||
return None, None, f"frontmatter is not valid YAML ({type(e).__name__}: {e})"
|
||||
if data is None:
|
||||
data = {}
|
||||
if not isinstance(data, dict):
|
||||
|
|
@ -55,11 +72,20 @@ def changed(base):
|
|||
|
||||
|
||||
def main(base):
|
||||
try:
|
||||
rows = changed(base)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"CANON CHECK FAILED: cannot diff against {base!r}: {e.stderr.strip()}")
|
||||
return 1
|
||||
failed = False
|
||||
for st, path in changed(base):
|
||||
for st, path in rows:
|
||||
if not path.endswith(".md"):
|
||||
continue
|
||||
kind, rulings, err = frontmatter(open(path, encoding="utf-8").read())
|
||||
try:
|
||||
with open(path, encoding="utf-8-sig") as f:
|
||||
kind, rulings, err = frontmatter(f.read())
|
||||
except (OSError, UnicodeDecodeError) as e:
|
||||
kind, rulings, err = None, None, f"cannot read page ({type(e).__name__})"
|
||||
problem = None
|
||||
if err:
|
||||
problem = err
|
||||
|
|
@ -69,7 +95,7 @@ def main(base):
|
|||
else:
|
||||
print(f"notice: {path} has no kind (legacy); tag it when you next touch it")
|
||||
elif kind == "canon":
|
||||
bad = [r for r in (rulings or []) if not ID.match(r)]
|
||||
bad = [r for r in (rulings or []) if not ID.fullmatch(r)]
|
||||
if not rulings or bad:
|
||||
problem = ("kind: canon requires `rulings:` with at least one id of the form "
|
||||
f"<campaign>:ruling:<label>\n (found: {bad or 'none'})")
|
||||
|
|
|
|||
|
|
@ -129,6 +129,74 @@ class CanonMarker(unittest.TestCase):
|
|||
self.assertEqual(code, 1, out)
|
||||
self.assertNotIn("Traceback", out)
|
||||
|
||||
def test_a_python_object_tag_is_refused_not_executed(self):
|
||||
text = "---\nkind: !!python/object/apply:os.getcwd []\n---\nx\n"
|
||||
code, out = self.run_pages({"Aetheria/A.md": text})
|
||||
self.assertEqual(code, 1, out)
|
||||
self.assertNotIn("Traceback", out)
|
||||
|
||||
def test_a_repeated_key_fails(self):
|
||||
text = f"---\nkind: proposal\nkind: canon\nrulings:\n - {RULING}\n---\nx\n"
|
||||
code, out = self.run_pages({"Aetheria/A.md": text})
|
||||
self.assertEqual(code, 1, out)
|
||||
self.assertIn("duplicate", out)
|
||||
|
||||
def test_a_bom_does_not_hide_the_marker(self):
|
||||
code, out = self.run_pages({"Aetheria/Legacy.md": "\ufeff---\nkind: canon\n---\nx\n"})
|
||||
self.assertEqual(code, 1, out)
|
||||
|
||||
def test_a_ruling_id_with_a_trailing_newline_is_malformed(self):
|
||||
text = f'---\nkind: canon\nrulings:\n - "{RULING}\\n"\n---\nx\n'
|
||||
code, out = self.run_pages({"Aetheria/A.md": text})
|
||||
self.assertEqual(code, 1, out)
|
||||
|
||||
def test_empty_frontmatter_is_a_kindless_page_not_unterminated(self):
|
||||
code, out = self.run_pages({"Aetheria/A.md": "---\n---\nx\n"})
|
||||
self.assertEqual(code, 1, out)
|
||||
self.assertNotIn("unterminated", out)
|
||||
self.assertIn("needs `kind", out)
|
||||
|
||||
def test_a_lookalike_closing_marker_does_not_end_the_frontmatter(self):
|
||||
text = "---\nkind: proposal\n----\nstill frontmatter?\n---\nx\n"
|
||||
code, out = self.run_pages({"Aetheria/A.md": text})
|
||||
self.assertEqual(code, 1, out)
|
||||
|
||||
def test_a_renamed_legacy_page_passes_with_a_notice(self):
|
||||
def rename(r):
|
||||
git(r.dir, "mv", "Aetheria/Legacy.md", "Aetheria/Moved.md")
|
||||
code, out = self.run_pages({}, after=rename)
|
||||
self.assertEqual(code, 0, out)
|
||||
self.assertIn("notice", out)
|
||||
|
||||
def test_a_page_main_changed_after_the_branch_point_is_not_charged_to_the_pr(self):
|
||||
# Old.md is a bad canon page that main already holds; the PR merges it in
|
||||
# and does not touch it, then main edits it again. Two-dot diffing
|
||||
# would see the PR's copy as modified and fail the PR for main's page.
|
||||
r = Repo()
|
||||
r.write("Aetheria/Mine.md", "---\nkind: proposal\n---\nx\n")
|
||||
r.commit()
|
||||
git(r.dir, "checkout", "main")
|
||||
r.write("Aetheria/Old.md", "---\nkind: canon\n---\nv1\n")
|
||||
git(r.dir, "add", "-A")
|
||||
git(r.dir, "commit", "-m", "main adds Old")
|
||||
git(r.dir, "checkout", "pr")
|
||||
git(r.dir, "merge", "--no-edit", "main")
|
||||
git(r.dir, "checkout", "main")
|
||||
r.write("Aetheria/Old.md", "---\nkind: canon\n---\nv2\n")
|
||||
git(r.dir, "add", "-A")
|
||||
git(r.dir, "commit", "-m", "main edits Old")
|
||||
git(r.dir, "checkout", "pr")
|
||||
code, out = r.check()
|
||||
self.assertEqual(code, 0, out)
|
||||
|
||||
def test_an_unknown_base_ref_fails_loudly_without_a_traceback(self):
|
||||
r = Repo()
|
||||
p = subprocess.run(["python3", SCRIPT, "no-such-ref"], cwd=r.dir,
|
||||
capture_output=True, text=True)
|
||||
self.assertEqual(p.returncode, 1)
|
||||
self.assertNotIn("Traceback", p.stdout + p.stderr)
|
||||
self.assertIn("no-such-ref", p.stdout + p.stderr)
|
||||
|
||||
def test_pages_outside_aetheria_are_not_checked(self):
|
||||
code, out = self.run_pages({"README2.md": "---\nkind: canon\n---\nx\n"})
|
||||
self.assertEqual(code, 0, out)
|
||||
|
|
|
|||
Loading…
Reference in a new issue