From: Eugene Crosser Date: Mon, 11 Jul 2022 10:10:31 +0000 (+0200) Subject: test: more robust version check for tools (black) X-Git-Tag: 1.90~31 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=5c3e8e8f28d8dcb72e9212ddd88f73f4bf71cea2 test: more robust version check for tools (black) --- diff --git a/test/__init__.py b/test/__init__.py index e69de29..b4ddd2e 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -0,0 +1,25 @@ +from typing import Callable, Tuple + + +__all__ = ("no_less_than",) + + +def no_less_than(base: str) -> Callable[[str], bool]: + def _no_less_than(base: str, what: str) -> bool: + bl, wl = ( + tuple((int(el) for el in s.split("."))) for s in (base, what) + ) + + def __no_less_than(bl: Tuple[int, ...], wl: Tuple[int, ...]) -> bool: + if len(bl) == 0 and len(wl) == 0: + return True + if (len(bl) == 0) != (len(wl) == 0): + return len(bl) < len(wl) + # At this point both lists are non-empty + if bl[0] == wl[0]: + return __no_less_than(bl[1:], wl[1:]) + return bl[0] < wl[0] + + return __no_less_than(bl, wl) + + return lambda arg: _no_less_than(base, arg) diff --git a/test/test_black.py b/test/test_black.py index 4922749..76275af 100644 --- a/test/test_black.py +++ b/test/test_black.py @@ -5,17 +5,24 @@ from subprocess import run from shutil import which from unittest import main, TestCase, skipUnless -black_version = 0.0 +from . import no_less_than + +is_acceptable_verison = no_less_than("21.1") + +black_version = "0.0" try: vermatch = match("[\.\d]*", get_distribution("black").version) if vermatch is not None: - black_version = float(vermatch.group()) + black_version = vermatch.group() except DistributionNotFound: pass class BlackFormatter(TestCase): - @skipUnless(black_version >= 21.1, "Do not trust earlier black versions") + @skipUnless( + is_acceptable_verison(black_version), + "Do not trust earlier black versions", + ) def test_black(self) -> None: if not which("black"): self.fail(f"black not installed.")