From 9d5c87756f88718f7edbe2a94df37d6a61721dc8 Mon Sep 17 00:00:00 2001 From: Anton Schwarz Date: Wed, 4 Oct 2023 10:22:36 +0000 Subject: [PATCH] Docs: Fixed sorting issue in release notes index --- tools/generate_release_notes_index.py | 34 +++++++++++++---- tools/test_index_generator.py | 53 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 tools/test_index_generator.py diff --git a/tools/generate_release_notes_index.py b/tools/generate_release_notes_index.py index 8f9ff0722b3..301e8e12312 100755 --- a/tools/generate_release_notes_index.py +++ b/tools/generate_release_notes_index.py @@ -9,8 +9,9 @@ ONEMINOR = re.compile(r"\d+\.\d+\.(\d+)") SEMANTICMINOR = re.compile(r"\d+\.(\d+).+") +SEMANTICPATCH = re.compile(r"\d+\.\d+\.(\d+).*") POST = re.compile(r".+post(\d+)") -RC = re.compile(r".+0rc(\d+)") +RC = re.compile(r".+rc(\d+)") BIGNUM = 999 @@ -105,24 +106,41 @@ def sort_func(a: str, b: str) -> int: class Version: def __init__(self, s: str) -> None: if s.startswith("1."): + self.semantic = False self.major = int(s.split(".")[1]) self.minor = int(ONEMINOR.match(s).group(1)) # type: ignore - self.post = int(POST.search(s).group(1)) if POST.search(s) else -BIGNUM # type: ignore - self.rc = int(RC.search(s).group(1)) if RC.search(s) else BIGNUM # type: ignore + self.patch = 0 + self.post = int(POST.search(s).group(1)) if POST.search(s) else 0 # type: ignore + self.rc = int(RC.search(s).group(1)) if RC.search(s) else 0 # type: ignore else: + self.semantic = True self.major = int(s.split(".")[0]) self.minor = int(SEMANTICMINOR.match(s).group(1)) # type: ignore - self.post = int(POST.search(s).group(1)) if POST.search(s) else -BIGNUM # type: ignore - self.rc = int(RC.search(s).group(1)) if RC.search(s) else BIGNUM # type: ignore + self.patch = int(SEMANTICPATCH.match(s).group(1)) # type: ignore + self.post = int(POST.search(s).group(1)) if POST.search(s) else 0 # type: ignore + self.rc = int(RC.search(s).group(1)) if RC.search(s) else 0 # type: ignore + + def combined_post_rc(self) -> int: + # assuming that post and rc are never both non-zero + if self.rc != 0: + # we know that post is 0, rc can be zero + return self.rc - BIGNUM + else: + # we know that rc is 0, post can be zero + return self.post A, B = Version(a), Version(b) if A.major == B.major: if A.minor == B.minor: - if A.minor == 0: - return A.rc - B.rc + # need to treat semantic and non-semantic differently + if A.semantic: + if A.patch == B.patch: + return A.combined_post_rc() - B.combined_post_rc() + else: + return A.patch - B.patch else: - return A.post - B.post + return A.combined_post_rc() - B.combined_post_rc() else: return A.minor - B.minor else: diff --git a/tools/test_index_generator.py b/tools/test_index_generator.py new file mode 100644 index 00000000000..abe65c12513 --- /dev/null +++ b/tools/test_index_generator.py @@ -0,0 +1,53 @@ +from generate_release_notes_index import sort_func as sf + + +class TestSemantic: + def test_simple(self): + # major + assert sf("33.0.0", "32.2.0") > 0 + assert sf("32.0.0", "33.0.0") < 0 + # minor + assert sf("32.1.0", "32.2.0") < 0 + # patch + assert sf("32.3.0", "32.2.0") > 0 + + def test_rc_patchzero(self): + assert sf("33.0.0rc1", "33.0.0") < 0 + assert sf("33.0.0rc1", "33.0.0rc2") < 0 + assert sf("33.0.0rc1", "32.0.0rc2") > 0 + + def test_rc_patchnonzero(self): + assert sf("33.0.1rc1", "33.0.1") < 0 + assert sf("32.0.2rc1", "32.0.2rc2") < 0 + + def test_post(self): + assert sf("32.1.2.post1", "32.1.2") > 0 + assert sf("32.1.2.post1", "32.1.2.post2") < 0 + assert sf("33.0.0.post1", "32.0.0.post2") > 0 + + def test_mixed_rc_post(self): + assert sf("32.1.2.post1", "32.1.2rc1") > 0 + + +class TestLegacy: + def test_simple(self): + # major + assert sf("1.26.0", "1.25.0") > 0 + # minor + assert sf("1.25.1", "1.25.0") > 0 + + def test_rc(self): + assert sf("1.25.0rc1", "1.25.0") < 0 + assert sf("1.25.0rc1", "1.25.0rc2") < 0 + assert sf("1.25.0rc1", "1.24.0rc2") > 0 + # nonzero minor + assert sf("1.25.1rc1", "1.25.1rc2") < 0 + + def test_post(self): + assert sf("1.25.0.post1", "1.25.0") > 0 + assert sf("1.25.0.post1", "1.25.0.post2") < 0 + assert sf("1.26.0.post1", "1.25.0.post2") > 0 + + def test_mixed_rc_post(self): + assert sf("1.25.0.post1", "1.25.0rc1") > 0 + assert sf("1.25.1.post1", "1.25.1rc1") > 0