diff --git a/pyproject.toml b/pyproject.toml index 85ba2a611..deb45caa5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ dev = [ "pytest<9", 'click-type-test==1.1.0;python_version>="3.10"', "coverage<8", + "identify>=2.6.9", "pytest-xdist<4", "responses==0.25.6", ] diff --git a/tests/acceptance/test_hook_file_matches.py b/tests/acceptance/test_hook_file_matches.py index b48f6dfca..918ed315b 100644 --- a/tests/acceptance/test_hook_file_matches.py +++ b/tests/acceptance/test_hook_file_matches.py @@ -5,6 +5,7 @@ import pytest import ruamel.yaml +from identify.identify import tags_from_filename yaml = ruamel.yaml.YAML(typ="safe") @@ -29,6 +30,22 @@ def check_pattern_match( pytest.fail(f"'{pattern}' matched '{value}' (expected no match)") +def check_types_match( + types: list[str], + types_or: list[str], + path: str, +) -> None: + __tracebackhide__ = True + + tags = tags_from_filename(path) + + if types and not tags.issuperset(types): + pytest.fail(f"types={types} did not match '{path}' (expected match)") + + if types_or and not tags.intersection(types_or): + pytest.fail(f"types_or={types_or} did not match '{path}' (expected match)") + + def get_hook_config(hookid): config_file = HERE.parent.parent / ".pre-commit-hooks.yaml" with open(config_file) as fp: @@ -182,6 +199,7 @@ def get_hook_config(hookid): def test_hook_matches_known_good_paths(hookid, filepath): config = get_hook_config(hookid) check_pattern_match(config["files"], filepath) + check_types_match(config.get("types", []), config.get("types_or", []), filepath) @pytest.mark.parametrize(