diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 20:18:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 20:18:22 +0000 |
commit | 590e10ceccc9835d3d28d7857b544fcfe1f13183 (patch) | |
tree | 87ad585203bcf185e77de28d9399eddbcfe628cb /tests/lint_tests/lint_tutil.py | |
parent | Adding debian version 0.1.27.1. (diff) | |
download | debputy-590e10ceccc9835d3d28d7857b544fcfe1f13183.tar.xz debputy-590e10ceccc9835d3d28d7857b544fcfe1f13183.zip |
Merging upstream version 0.1.28.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/lint_tests/lint_tutil.py')
-rw-r--r-- | tests/lint_tests/lint_tutil.py | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/tests/lint_tests/lint_tutil.py b/tests/lint_tests/lint_tutil.py index d4f654c..83b69fd 100644 --- a/tests/lint_tests/lint_tutil.py +++ b/tests/lint_tests/lint_tutil.py @@ -1,9 +1,16 @@ import collections -from typing import List, Optional, Mapping, Any +from typing import List, Optional, Mapping, Any, Callable import pytest -from debputy.linting.lint_util import LinterImpl, LinterPositionCodec +from debputy.linting.lint_util import ( + LinterImpl, + LinterPositionCodec, + LintStateImpl, + LintState, +) +from debputy.packages import DctrlParser +from debputy.plugin.api.feature_set import PluginProvidedFeatureSet try: from lsprotocol.types import Diagnostic, DiagnosticSeverity @@ -22,13 +29,48 @@ except ImportError: LINTER_POSITION_CODEC = LinterPositionCodec() +class LintWrapper: + + def __init__( + self, + path: str, + handler: Callable[[LintState], Optional[List[Diagnostic]]], + debputy_plugin_feature_set: PluginProvidedFeatureSet, + dctrl_parser: DctrlParser, + ) -> None: + self._debputy_plugin_feature_set = debputy_plugin_feature_set + self._handler = handler + self.dctrl_lines: Optional[List[str]] = None + self.path = path + self._dctrl_parser = dctrl_parser + + def __call__(self, lines: List[str]) -> Optional[List["Diagnostic"]]: + source_package = None + binary_packages = None + dctrl_lines = self.dctrl_lines + if dctrl_lines is not None: + source_package, binary_packages = ( + self._dctrl_parser.parse_source_debian_control( + dctrl_lines, ignore_errors=True + ) + ) + state = LintStateImpl( + self._debputy_plugin_feature_set, + self.path, + lines, + source_package, + binary_packages, + ) + return check_diagnostics(self._handler(state)) + + def requires_levenshtein(func: Any) -> Any: return pytest.mark.skipif( not HAS_LEVENSHTEIN, reason="Missing python3-levenshtein" )(func) -def _check_diagnostics( +def check_diagnostics( diagnostics: Optional[List["Diagnostic"]], ) -> Optional[List["Diagnostic"]]: if diagnostics: @@ -37,18 +79,6 @@ def _check_diagnostics( return diagnostics -def run_linter( - path: str, lines: List[str], linter: LinterImpl -) -> Optional[List["Diagnostic"]]: - uri = f"file://{path}" - return _check_diagnostics(linter(uri, path, lines, LINTER_POSITION_CODEC)) - - -def exactly_one_diagnostic(diagnostics: Optional[List["Diagnostic"]]) -> "Diagnostic": - assert diagnostics and len(diagnostics) == 1 - return diagnostics[0] - - def by_range_sort_key(diagnostic: Diagnostic) -> Any: start_pos = diagnostic.range.start end_pos = diagnostic.range.end |