utils/file_length/tests/test_repo_exempt.py
Krzysztof kuhy Rudnicki 879265b966
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
file_length: repo-local .file-length-exempt list, fail-closed, package tested to 100%
A fork of a third-party project can carry data that is code only by
extension (TachiyomiSY's 6000-line exh/eh/tags/*.kt string tables). The
reason belongs in that repo next to the data, so the gate now reads a
.file-length-exempt of '<glob>  # <reason>' lines from its cwd. A glob
without a reason is a suppression and exits 2 instead of passing.

The package had no tests at all; it now has a suite at 100% line+branch,
wired into shared-gates-tests.yml with --cov-fail-under=100.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Yc4TiPjhbQLfH35QBPfrbP
2026-09-12 18:53:19 +02:00

67 lines
2.1 KiB
Python

"""`.file-length-exempt`: parsing, matching, and the fail-closed rule."""
from __future__ import annotations
from pathlib import Path
import pytest
from file_length.repo_exempt import (
EXEMPT_FILE,
ExemptFileError,
Exemption,
load,
parse,
repo_exempt_reason,
)
from file_length.tests.conftest import write
def test_parse_skips_blank_and_comment_lines() -> None:
text = "\n# a comment\n \napp/**/*.kt # data tables\n"
assert parse(text) == [Exemption("app/**/*.kt", "data tables")]
@pytest.mark.parametrize("line", ["app/x.kt", "app/x.kt #", "app/x.kt # ", "# r"])
def test_parse_rejects_entries_without_a_reason(line: str) -> None:
if line.startswith("#"):
assert parse(line) == [] # a pure comment is not an entry
return
with pytest.raises(ExemptFileError, match=f"{EXEMPT_FILE}:1"):
parse(line)
def test_parse_reports_the_offending_line_number() -> None:
with pytest.raises(ExemptFileError, match=f"{EXEMPT_FILE}:3"):
parse("a/*.kt # ok\n\nb/*.kt\n")
def test_load_without_file_is_empty(tmp_path: Path) -> None:
assert load(tmp_path) == ()
def test_load_reads_and_memoises(tmp_path: Path) -> None:
write(tmp_path, EXEMPT_FILE, "a/*.kt # one\n")
first = load(tmp_path)
write(tmp_path, EXEMPT_FILE, "b/*.kt # two\n")
assert load(tmp_path) is first
assert first == (Exemption("a/*.kt", "one"),)
def test_reason_none_when_repo_has_no_entries(tmp_path: Path) -> None:
assert repo_exempt_reason(tmp_path / "a" / "x.kt", tmp_path) is None
def test_reason_matches_glob_and_double_star(tmp_path: Path) -> None:
write(tmp_path, EXEMPT_FILE, "app/**/tags/*.kt # tag data tables\n")
hit = tmp_path / "app" / "src" / "exh" / "tags" / "Group.kt"
miss = tmp_path / "app" / "src" / "exh" / "Other.kt"
assert repo_exempt_reason(hit, tmp_path) == "repo exemption: tag data tables"
assert repo_exempt_reason(miss, tmp_path) is None
def test_reason_none_for_paths_outside_root(tmp_path: Path) -> None:
write(tmp_path, EXEMPT_FILE, "**/*.kt # everything\n")
outside = tmp_path.parent / "elsewhere.kt"
assert repo_exempt_reason(outside, tmp_path) is None