Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Fix computing diff versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vknaisl committed Nov 14, 2023
1 parent f3045d1 commit cf35710
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
20 changes: 13 additions & 7 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ def test_compute_diff_versions_1(self):
# WHEN:
result = compute_diff_versions(versions)
# THEN:
self.assertEqual(result, expectation)
self.assertEqual(expectation, result)

def test_compute_diff_versions_2(self):
# GIVEN:
versions = [
(3, 20, 0),
(3, 21, 0),
(4, 0, 0),
]
expectation = [
('3.20.0', '3.21.0'),
('3.21.0', '4.0.0')
]
expectation = [('3.20.0', '3.21.0')]
# WHEN:
result = compute_diff_versions(versions)
# THEN:
self.assertEqual(result, expectation)
self.assertEqual(expectation, result)

def test_compute_diff_versions_3(self):
# GIVEN:
Expand All @@ -51,15 +55,14 @@ def test_compute_diff_versions_4(self):
]
expectation = [
('3.20.0', '3.21.0'),
('3.21.0', '3.22.0'),
('3.21.0', '3.21.1'),
('3.21.1', '3.21.2'),
('3.21.2', '3.22.0'),
]
# WHEN:
result = compute_diff_versions(versions)
# THEN:
self.assertEqual(result, expectation)
self.assertEqual(expectation, result)

def test_compute_diff_versions_5(self):
# GIVEN:
Expand All @@ -70,19 +73,22 @@ def test_compute_diff_versions_5(self):
(3, 21, 2),
(3, 22, 0),
(3, 22, 1),
(4, 0, 0),
(4, 1, 0),
]
expectation = [
('3.20.0', '3.21.0'),
('3.21.0', '3.22.0'),
('3.21.0', '3.21.1'),
('3.21.1', '3.21.2'),
('3.21.2', '3.22.0'),
('3.22.0', '3.22.1'),
('3.22.1', '4.0.0'),
('4.0.0', '4.1.0'),
]
# WHEN:
result = compute_diff_versions(versions)
# THEN:
self.assertEqual(result, expectation)
self.assertEqual(expectation, result)


if __name__ == '__main__':
Expand Down
53 changes: 32 additions & 21 deletions scripts/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,41 @@ def get_versions(specs_dir: pathlib.Path):


def compute_diff_versions(versions):
major = 3
diffs = []

minors = set()
for (_, minor, _) in versions:
minors.add(minor)
minors = list(minors)
for current_version in versions:
previous_version = get_previous_version(current_version, versions)
if previous_version is not None:
diffs.append((
f'{previous_version[0]}.{previous_version[1]}.{previous_version[2]}',
f'{current_version[0]}.{current_version[1]}.{current_version[2]}'
))

diffs = []
for minor in minors:
if (minor - 1) in minors:
diffs.append((f'{major}.{minor - 1}.0', f'{major}.{minor}.0'))
return diffs

for minor in minors:
patches = []
for (_, v_minor, v_patch) in versions:
if minor == v_minor:
patches.append(v_patch)

for patch in patches:
if (patch - 1) in patches:
diffs.append((f'{major}.{minor}.{patch - 1}', f'{major}.{minor}.{patch}'))
def get_previous_version(current_version: (int, int, int), versions: [(int, int, int)]) -> (int, int, int):
# 1. Define variables
(current_major, current_minor, current_patch) = current_version
minors = sorted([minor for (_, minor, _) in versions], reverse=True)
patches = sorted([patch for (_, _, patch) in versions], reverse=True)

last_patch = patches[-1]
if last_patch != 0 and (minor + 1) in minors:
diffs.append((f'{major}.{minor}.{last_patch}', f'{major}.{minor + 1}.0'))
# 2. Try decrease patch
previous_version = (current_major, current_minor, current_patch - 1)
if previous_version in versions:
return previous_version

return diffs
# 3. Try decrease minor
for patch in patches:
previous_version = (current_major, current_minor - 1, patch)
if previous_version in versions:
return previous_version

# 4. Try decrease major
for minor in minors:
for patch in patches:
previous_version = (current_major - 1, minor, patch)
if previous_version in versions:
return previous_version

return None

0 comments on commit cf35710

Please sign in to comment.