Skip to content

Commit

Permalink
Docs: Fixed sorting issue in release notes index
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Schwarz committed Oct 4, 2023
1 parent a4ebb43 commit 9d5c877
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tools/generate_release_notes_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions tools/test_index_generator.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9d5c877

Please sign in to comment.