diff --git a/tests/conftest.py b/tests/conftest.py index 729bc63..229c5a2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 -"""Shared fixtures and helpers: plain-text screen reading, and fixed path rendering. +"""Shared fixtures and helpers: plain-text screen reading, fixed path rendering, project files. :func:`plain` is the only way that a test reads a rendered screen. Each screen test must first remove the ANSI colour escapes and join the rendered lines. Only then can it make @@ -18,13 +18,18 @@ powerline pass explicit modes, or they patch the widget's own switch with and keeps attributes. This silently makes each colour assertion in the suite useless (a shell that an agent harness runs sets it). The autouse fixture below deletes it. Thus the tests always see the colours that the app really sends. + +:func:`not_ignored` keeps a test that walks a folder to the files of the project. Git +ignores the other files in the checkout, and these files are not ours to examine. """ from __future__ import annotations import os import re +import subprocess from collections.abc import Callable, Iterable, Iterator +from pathlib import Path import pytest @@ -47,6 +52,39 @@ def plain(rendered: str | Iterable[str]) -> str: return _ANSI.sub("", rendered) +#: The root of the checkout. +_ROOT = Path(__file__).resolve().parent.parent + + +def not_ignored(paths: Iterable[Path]) -> list[Path]: + """The paths in ``paths`` that git does not ignore, in their order. + + A test that walks a folder of the checkout must examine only the files of the project. + A folder can also hold files that git ignores. An example is the MeshCore checkout that + ``build-firmware.sh`` makes in ``scripts/``, with its third-party code and its build + tree. Git never ignores a tracked file. When git is not available (for example, in an + unpacked source archive), the function returns all the paths, and the test examines + each file, as it did before. + """ + paths = list(paths) + relative = [path.resolve().relative_to(_ROOT).as_posix() for path in paths] + try: + result = subprocess.run( + ["git", "check-ignore", "--stdin", "-z"], + cwd=_ROOT, + input="\0".join(relative), + capture_output=True, + encoding="utf-8", + check=False, + ) + except OSError: + return paths + if result.returncode not in (0, 1): # 1 is "nothing ignored", 128 is "not a repository" + return paths + ignored = set(result.stdout.split("\0")) + return [path for path, name in zip(paths, relative, strict=True) if name not in ignored] + + @pytest.fixture(autouse=True) def _plain_path_rendering(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: monkeypatch.setenv("MESHTERM_POWERLINE", "0") diff --git a/tests/test_spdx.py b/tests/test_spdx.py index f61c41f..4b4f0b7 100644 --- a/tests/test_spdx.py +++ b/tests/test_spdx.py @@ -10,6 +10,8 @@ identifier. Thus this gate makes sure that no file has no marking. from pathlib import Path +from tests.conftest import not_ignored + def test_spdx_headers(): """Each Python file must have an SPDX-License-Identifier header.""" @@ -33,7 +35,7 @@ def test_spdx_headers(): python_files.append(spi_bridge) missing_spdx = [] - for filepath in sorted(python_files): + for filepath in not_ignored(sorted(python_files)): # A file that MeshTerm cannot read as UTF-8 is a finding. The test must not skip it. # Each source file here is UTF-8, and a skip with no message lets a file with no # marking pass. diff --git a/tests/test_writing_style.py b/tests/test_writing_style.py index 59cf142..5f94ed6 100644 --- a/tests/test_writing_style.py +++ b/tests/test_writing_style.py @@ -20,6 +20,8 @@ import re import tokenize from pathlib import Path +from tests.conftest import not_ignored + ROOT = Path(__file__).resolve().parent.parent #: The folders whose Python files are examined. @@ -81,7 +83,7 @@ def _blank_quotes(text: str) -> str: def _python_files() -> list[Path]: files = [p for d in PYTHON_DIRS for p in (ROOT / d).rglob("*.py")] files += [ROOT / p for p in PYTHON_EXTRA] - return sorted(p for p in files if p.exists() and "__pycache__" not in p.parts) + return not_ignored(sorted(p for p in files if p.exists() and "__pycache__" not in p.parts)) def _python_prose(path: Path) -> list[tuple[int, str]]: @@ -125,7 +127,7 @@ def _documents() -> list[Path]: for name in DOCUMENTS: path = ROOT / name files += sorted(path.rglob("*.md")) if path.is_dir() else [path] - return files + return not_ignored(files) def _markdown_prose(path: Path) -> list[tuple[int, str]]: