| 1 |
""" |
| 2 |
|
| 3 |
Test that the docs are up to date. |
| 4 |
|
| 5 |
""" |
| 6 |
|
| 7 |
import re |
| 8 |
from collections.abc import Sequence |
| 9 |
from itertools import islice |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from black.mode import UNSTABLE_FEATURES, Preview |
| 15 |
|
| 16 |
DOCS_PATH = Path("docs/the_black_code_style/future_style.md") |
| 17 |
|
| 18 |
|
| 19 |
def check_feature_list( |
| 20 |
lines: Sequence[str], expected_feature_names: set[str], label: str |
| 21 |
) -> str | None: |
| 22 |
start_index = lines.index(f"(labels/{label}-features)=\n") |
| 23 |
if start_index == -1: |
| 24 |
return ( |
| 25 |
f"Could not find the {label} features list in {DOCS_PATH}. Ensure the" |
| 26 |
" preview-features label is present." |
| 27 |
) |
| 28 |
num_blank_lines_seen = 0 |
| 29 |
seen_preview_feature_names = set() |
| 30 |
for line in islice(lines, start_index + 1, None): |
| 31 |
if not line.strip(): |
| 32 |
num_blank_lines_seen += 1 |
| 33 |
if num_blank_lines_seen == 3: |
| 34 |
break |
| 35 |
continue |
| 36 |
if line.startswith("- "): |
| 37 |
match = re.search(r"^- `([a-z\d_]+)`", line) |
| 38 |
if match: |
| 39 |
seen_preview_feature_names.add(match.group(1)) |
| 40 |
|
| 41 |
if seen_preview_feature_names - expected_feature_names: |
| 42 |
extra = ", ".join(sorted(seen_preview_feature_names - expected_feature_names)) |
| 43 |
return ( |
| 44 |
f"The following features should not be in the list of {label} features:" |
| 45 |
f" {extra}. Please remove them from the {label}-features label in" |
| 46 |
f" {DOCS_PATH}" |
| 47 |
) |
| 48 |
elif expected_feature_names - seen_preview_feature_names: |
| 49 |
missing = ", ".join(sorted(expected_feature_names - seen_preview_feature_names)) |
| 50 |
return ( |
| 51 |
f"The following features are missing from the list of {label} features:" |
| 52 |
f" {missing}. Please document them under the {label}-features label in" |
| 53 |
f" {DOCS_PATH}" |
| 54 |
) |
| 55 |
else: |
| 56 |
return None |
| 57 |
|
| 58 |
|
| 59 |
def test_feature_lists_are_up_to_date() -> None: |
| 60 |
repo_root = Path(__file__).parent.parent |
| 61 |
if not (repo_root / "docs").exists(): |
| 62 |
pytest.skip("docs not found") |
| 63 |
with (repo_root / DOCS_PATH).open(encoding="utf-8") as f: |
| 64 |
future_style = f.readlines() |
| 65 |
preview_error = check_feature_list( |
| 66 |
future_style, |
| 67 |
{feature.name for feature in set(Preview) - UNSTABLE_FEATURES}, |
| 68 |
"preview", |
| 69 |
) |
| 70 |
assert preview_error is None, preview_error |
| 71 |
unstable_error = check_feature_list( |
| 72 |
future_style, {feature.name for feature in UNSTABLE_FEATURES}, "unstable" |
| 73 |
) |
| 74 |
assert unstable_error is None, unstable_error |