diff --git a/tests/test_changelog_and_version.py b/tests/test_changelog_and_version.py index f2382f2d..80e3387f 100755 --- a/tests/test_changelog_and_version.py +++ b/tests/test_changelog_and_version.py @@ -5,20 +5,16 @@ import re import sys -import unittest from pathlib import Path from typing import TYPE_CHECKING, NamedTuple, TypeVar -import flake8_trio - -from .test_flake8_trio import ERROR_CODES - if TYPE_CHECKING: from collections.abc import Iterable ROOT_PATH = Path(__file__).parent.parent CHANGELOG = ROOT_PATH / "CHANGELOG.md" -README = CHANGELOG.parent / "README.md" +README = ROOT_PATH / "README.md" +INIT_FILE = ROOT_PATH / "flake8_trio" / "__init__.py" T = TypeVar("T", bound="Version") @@ -36,7 +32,10 @@ def __str__(self) -> str: return ".".join(map(str, self)) -VERSION = Version.from_string(flake8_trio.__version__) +for line in INIT_FILE.read_text().splitlines(): + if m := re.match(r'__version__ = "(\d*\.\d*\.\d*)"', line): + VERSION = Version.from_string(m.groups()[0]) + break def get_releases() -> Iterable[Version]: @@ -73,7 +72,7 @@ def ensure_tagged() -> None: last_version = next(iter(get_releases())) repo = Repo(ROOT_PATH) - if last_version not in repo.tags: + if str(last_version) not in iter(map(str, repo.tags)): # create_tag is partially unknown in pyright, which kinda looks like # https://github.com/gitpython-developers/GitPython/issues/1473 # which should be resolved? @@ -105,65 +104,3 @@ def update_version() -> None: update_version() if "--ensure-tag" in sys.argv: ensure_tagged() - - -# I wanted to move this test to a separate file, but that'd lead to merge conflicts, -# so will have to wait with that -IGNORED_CODES_REGEX = r"TRIO107|TRIO108|TRIO\d\d\d_.*" - - -class test_messages_documented(unittest.TestCase): - def runTest(self): - documented_errors: dict[str, set[str]] = {} - for path in (CHANGELOG, README): - with open(path, encoding="utf-8") as f: - lines = f.readlines() - filename = path.name - documented_errors[filename] = set() - for line in lines: - for error_msg in re.findall(r"TRIO\d\d\d", line): - documented_errors[filename].add(error_msg) - - documented_errors["flake8_trio.py"] = set(ERROR_CODES) - # check files for @error_class - - # get tested error codes from file names and from `INCLUDE` lines - documented_errors["eval_files"] = set() - p = Path(__file__).parent / "eval_files" - for file_path in p.iterdir(): - if not file_path.is_file(): - continue - - if m := re.search(r"trio\d\d\d", str(file_path)): - documented_errors["eval_files"].add(m.group().upper()) - - with open(file_path) as file: - for line in file: - if line.startswith("# ARG --enable"): - for m in re.findall(r"trio\d\d\d", line, re.IGNORECASE): - # pyright types m as `Any` (as it is in typeshed) - # mypy types it as Optional[Match[str]] - # but afaict it should be something like str|Tuple[str,...] - # depending on whether there's a group in the pattern or not. - # (or bytes, if both inputs are bytes) - documented_errors["eval_files"].add(m) # type: ignore - break - - for errset in documented_errors.values(): - errset.difference_update( - [c for c in errset if re.fullmatch(IGNORED_CODES_REGEX, c)] - ) - - unique_errors: dict[str, set[str]] = {} - missing_errors: dict[str, set[str]] = {} - for key, codes in documented_errors.items(): - unique_errors[key] = codes.copy() - missing_errors[key] = set() - - for other_key, other_codes in documented_errors.items(): - if key == other_key: - continue - unique_errors[key].difference_update(other_codes) - missing_errors[key].update(other_codes - codes) - - assert unique_errors == missing_errors diff --git a/tests/test_messages_documented.py b/tests/test_messages_documented.py new file mode 100644 index 00000000..e920aba5 --- /dev/null +++ b/tests/test_messages_documented.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +"""Tests for flake8-trio package metadata.""" + +from __future__ import annotations + +import re +from pathlib import Path +from typing import cast + +from .test_flake8_trio import ERROR_CODES + +ROOT_PATH = Path(__file__).parent.parent +CHANGELOG = ROOT_PATH / "CHANGELOG.md" +README = CHANGELOG.parent / "README.md" + +# 107 & 108 are removed (but still mentioned in changelog & readme) +# TRIOxxx_* are fake codes to get different error messages for the same code +IGNORED_CODES_REGEX = r"TRIO107|TRIO108|TRIO\d\d\d_.*" + + +def test_messages_documented(): + documented_errors: dict[str, set[str]] = {} + for path in (CHANGELOG, README): + with open(path, encoding="utf-8") as f: + lines = f.readlines() + filename = path.name + documented_errors[filename] = set() + for line in lines: + for error_msg in re.findall(r"TRIO\d\d\d", line): + documented_errors[filename].add(error_msg) + + documented_errors["flake8_trio.py"] = set(ERROR_CODES) + + # get tested error codes from file names and from `INCLUDE` lines + documented_errors["eval_files"] = set() + p = Path(__file__).parent / "eval_files" + for file_path in p.iterdir(): + if not file_path.is_file(): + continue + + if m := re.search(r"trio\d\d\d", str(file_path)): + documented_errors["eval_files"].add(m.group().upper()) + + with open(file_path) as file: + for line in file: + if line.startswith("# ARG --enable"): + for m in re.findall(r"trio\d\d\d", line, re.IGNORECASE): + # pyright types m as `Any` (as it is in typeshed) + # mypy types it as Optional[Match[str]] + # but afaict it should be something like str|Tuple[str,...] + # depending on whether there's a group in the pattern or not. + # (or bytes, if both inputs are bytes) + documented_errors["eval_files"].add(cast("str", m)) + break + + for errset in documented_errors.values(): + errset.difference_update( + [c for c in errset if re.fullmatch(IGNORED_CODES_REGEX, c)] + ) + + unique_errors: dict[str, set[str]] = {} + missing_errors: dict[str, set[str]] = {} + for key, codes in documented_errors.items(): + unique_errors[key] = codes.copy() + missing_errors[key] = set() + + for other_key, other_codes in documented_errors.items(): + if key == other_key: + continue + unique_errors[key].difference_update(other_codes) + missing_errors[key].update(other_codes - codes) + + assert unique_errors == missing_errors