|
| 1 | +# This file is part of craft_application. |
| 2 | +# |
| 3 | +# Copyright 2025 Canonical Ltd. |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU Lesser General Public License version 3, as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | +# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 11 | +# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | +# See the GNU Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public License along |
| 15 | +# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +"""Integration tests for the LinterService.""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from typing import TYPE_CHECKING |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from collections.abc import Iterator |
| 24 | + |
| 25 | +import pytest |
| 26 | +from craft_application.lint.base import AbstractLinter |
| 27 | +from craft_application.lint.types import ( |
| 28 | + ExitCode, |
| 29 | + LintContext, |
| 30 | + LinterIssue, |
| 31 | + Severity, |
| 32 | + Stage, |
| 33 | +) |
| 34 | +from craft_application.services.linter import LinterService |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def restore_linter_registry() -> Iterator[None]: |
| 39 | + """Snapshot and restore the linter registry around a test.""" |
| 40 | + saved = { |
| 41 | + stage: list(classes) for stage, classes in LinterService._class_registry.items() |
| 42 | + } |
| 43 | + try: |
| 44 | + yield |
| 45 | + finally: |
| 46 | + for stage, classes in saved.items(): |
| 47 | + LinterService._class_registry[stage] = list(classes) |
| 48 | + |
| 49 | + |
| 50 | +def test_issue_then_ignore( |
| 51 | + fake_services, |
| 52 | + project_path, |
| 53 | + fake_project, |
| 54 | + restore_linter_registry, |
| 55 | +) -> None: |
| 56 | + class _FailingPreLinter(AbstractLinter): |
| 57 | + name = "integration.failing_pre" |
| 58 | + stage = Stage.PRE |
| 59 | + |
| 60 | + def run(self, ctx: LintContext): |
| 61 | + target = ctx.project_dir / "README.md" |
| 62 | + target.write_text("content") |
| 63 | + yield LinterIssue( |
| 64 | + id="INT001", |
| 65 | + message="integration failure", |
| 66 | + severity=Severity.ERROR, |
| 67 | + filename=str(target), |
| 68 | + ) |
| 69 | + |
| 70 | + LinterService.register(_FailingPreLinter) |
| 71 | + |
| 72 | + project_service = fake_services.get("project") |
| 73 | + project_service.set(fake_project) # type: ignore[reportAttributeAccessIssue] |
| 74 | + project_dir = project_service.resolve_project_file_path().parent |
| 75 | + project_dir.mkdir(parents=True, exist_ok=True) |
| 76 | + |
| 77 | + linter_service = fake_services.get("linter") |
| 78 | + ctx = LintContext(project_dir=project_dir, artifact_dirs=[]) |
| 79 | + |
| 80 | + linter_service.load_ignore_config(project_dir=project_dir) |
| 81 | + issues = list(linter_service.run(Stage.PRE, ctx)) |
| 82 | + assert [i.id for i in issues] == ["INT001"] |
| 83 | + assert linter_service.summary() == ExitCode.ERROR |
| 84 | + |
| 85 | + ignore_file = project_dir / "craft-lint.yaml" |
| 86 | + ignore_file.write_text(f"{_FailingPreLinter.name}:\n ids: ['INT001']\n") |
| 87 | + |
| 88 | + linter_service.load_ignore_config(project_dir=project_dir) |
| 89 | + rerun = list(linter_service.run(Stage.PRE, ctx)) |
| 90 | + assert rerun == [] |
| 91 | + assert linter_service.summary() == ExitCode.OK |
0 commit comments