Skip to content

Commit

Permalink
tests: add test of examples in docs
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Sep 21, 2023
1 parent 7b88dd3 commit 0297018
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions unit_test/validate_schema_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations

import re
from pathlib import Path

import pytest
Expand Down Expand Up @@ -54,3 +57,46 @@ def test_overrides_no_select():
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)


def test_docs_examples():
"""
Parse out all the configuration examples, build valid TOML out of them, and
make sure they pass.
"""

expr = re.compile(
r"""
!!! tab examples "pyproject.toml"
\s*
\s*```toml
([^`]*)
\s*```""",
re.MULTILINE,
)

txt = DIR.parent.joinpath("docs/options.md").read_text()

blocks: list[str] = []
for match in expr.finditer(txt):
lines = (line.strip() for line in match.group(1).strip().splitlines() if line.strip())
block: list[str] = []
header = ""
for line in lines:
if line.startswith(("[tool.cibuildwheel", "[[tool.cibuildwheel")):
header = line
elif line.startswith("#"):
if block:
blocks.append("\n".join([header, *block]))
block = []
elif " = " in line and any(x.startswith(line.partition(" = ")[0]) for x in block):
blocks.append("\n".join([header, *block]))
block = [line]
else:
block.append(line)
blocks.append("\n".join([header, *block]))

for example_txt in blocks:
example = tomllib.loads(example_txt)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None

0 comments on commit 0297018

Please sign in to comment.