From a2dae521c6af30577c175b7614825b7cd416fa93 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 16 Jan 2024 19:22:51 +0100 Subject: [PATCH] pyln: Add a version parser to pyln-testing We changed the way we configure CLN to run in developer mode, which promptly broke anything that wasn't v23.11 and later. So we parse the version, and make it comparable, so we can determine what to do on the fly, and maintain a better backwards compatibility. Changelog-Changed: pyln-testing: pyln-testing is now version-aware and can customize the run based on the version. --- contrib/pyln-testing/pyln/testing/version.py | 38 ++++++++++++++++++++ contrib/pyln-testing/tests/test_fixtures.py | 12 +++++++ 2 files changed, 50 insertions(+) create mode 100644 contrib/pyln-testing/pyln/testing/version.py create mode 100644 contrib/pyln-testing/tests/test_fixtures.py diff --git a/contrib/pyln-testing/pyln/testing/version.py b/contrib/pyln-testing/pyln/testing/version.py new file mode 100644 index 000000000000..2e31882a2ee7 --- /dev/null +++ b/contrib/pyln-testing/pyln/testing/version.py @@ -0,0 +1,38 @@ + +from dataclasses import dataclass + + +@dataclass +class Version: + year: int + month: int + patch: int = 0 + + def __lt__(self, other): + return [self.year, self.month, self.patch] < [other.year, other.month, other.patch] + + def __gt__(self, other): + return other < self + + def __le__(self, other): + return [self.year, self.month, self.patch] <= [other.year, other.month, other.patch] + + def __ge__(self, other): + return other <= self + + def __eq__(self, other): + return [self.year, self.month] == [other.year, other.month] + + @classmethod + def from_str(cls, s: str) -> "Version": + if s.startswith('v'): + s = s[1:] + + parts = [int(i) for i in s.split('.', 3)] + year, month = parts[0], parts[1] + if len(parts) == 3: + patch = parts[2] + else: + patch = 0 + + return Version(year=year, month=month, patch=patch) diff --git a/contrib/pyln-testing/tests/test_fixtures.py b/contrib/pyln-testing/tests/test_fixtures.py new file mode 100644 index 000000000000..60c8d3f771b9 --- /dev/null +++ b/contrib/pyln-testing/tests/test_fixtures.py @@ -0,0 +1,12 @@ +from pyln.testing.version import Version + + +def test_version_parsing(): + cases = [ + ("v24.02", Version(24, 2)), + ("v23.11.2", Version(23, 11, 2)), + ] + + for test_in, test_out in cases: + v = Version.from_str(test_in) + assert test_out == v