Skip to content

Commit 31b48bc

Browse files
committed
Add deterministic sorting to versioned changes file
1 parent cfae785 commit 31b48bc

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

exasol/toolbox/util/release/changelog.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from datetime import datetime
44
from inspect import cleandoc
5-
from itertools import chain
65
from pathlib import Path
76

87
from exasol.toolbox.util.dependencies.poetry_dependencies import (
@@ -75,16 +74,12 @@ def _describe_dependency_changes(self) -> str:
7574
)
7675

7776
changes_by_group: list[str] = []
78-
# preserve order of keys from old group
79-
groups = list(
80-
dict.fromkeys(
81-
chain(
82-
previous_dependencies_in_groups.keys(),
83-
current_dependencies_in_groups.keys(),
84-
)
85-
)
77+
# dict.keys() returns a set
78+
all_groups = (
79+
previous_dependencies_in_groups.keys()
80+
| current_dependencies_in_groups.keys()
8681
)
87-
for group in groups:
82+
for group in self._sort_groups(all_groups):
8883
previous_dependencies = previous_dependencies_in_groups.get(group, {})
8984
current_dependencies = current_dependencies_in_groups.get(group, {})
9085
changes = DependencyChanges(
@@ -96,6 +91,21 @@ def _describe_dependency_changes(self) -> str:
9691
changes_by_group.append(f"\n### `{group}`\n{changes_str}\n")
9792
return "".join(changes_by_group)
9893

94+
@staticmethod
95+
def _sort_groups(groups: set[str]) -> list[str]:
96+
"""
97+
Prepare a deterministic sorting for groups shown in the versioned changes file:
98+
- `main` group should always be first
99+
- remaining groups are sorted alphabetically
100+
"""
101+
main = "main"
102+
if main not in groups:
103+
# sorted converts set to list
104+
return sorted(groups)
105+
remaining_groups = groups - {main}
106+
# sorted converts set to list
107+
return [main] + sorted(remaining_groups)
108+
99109
def _update_changelog_table_of_contents(self) -> None:
100110
"""
101111
Read in existing `changelog.md` and append to appropriate sections

test/unit/util/release/changelog_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ def test_describe_dependency_changes(changelogs, mock_dependencies):
143143
"* Added dependency `package2:0.2.0`\n"
144144
)
145145

146+
@staticmethod
147+
@pytest.mark.parametrize(
148+
"groups,expected",
149+
[
150+
pytest.param(
151+
{"dev", "abcd", "main"}, ["main", "abcd", "dev"], id="with_main"
152+
),
153+
pytest.param(
154+
{"dev", "abcd", "bacd"}, ["abcd", "bacd", "dev"], id="without_main"
155+
),
156+
],
157+
)
158+
def test_sort_groups(changelogs, groups, expected):
159+
result = changelogs._sort_groups(groups)
160+
assert result == expected
161+
146162
@staticmethod
147163
def test_update_changelog_table_of_contents(changelogs, changes_md):
148164
changelogs._update_changelog_table_of_contents()

0 commit comments

Comments
 (0)