diff options
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 |