Skip to content

Commit c3ebb0f

Browse files
authored
Merge pull request #152 from Tobias-Fischer/fix/sort-vinca-lists-adjacent-if-blocks
fix: close if-block before starting a new one in sort_vinca_lists
2 parents 435ebd6 + 26e9155 commit c3ebb0f

2 files changed

Lines changed: 113 additions & 3 deletions

File tree

vinca/sort_vinca_lists.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,21 @@ def sort_vinca_lists(path: Path) -> bool:
9797

9898
# Start of conditional block: " - if: ..."
9999
if RE_IF_BLOCK.match(line):
100-
if current_if_block is None:
101-
current_if_block = []
102-
current_if_block.append(line)
100+
# If a previous if-block is already open (no blank line or
101+
# comment separated it from this one), close it first.
102+
# Otherwise this new block's "then:" would be treated as a
103+
# continuation of the previous one's, and sorting would
104+
# pool both blocks' items together and redistribute them
105+
# across the block boundary.
106+
if current_if_block is not None and any(
107+
RE_IF_BLOCK.match(bl) for bl in current_if_block
108+
):
109+
if_blocks.append(current_if_block)
110+
current_if_block = [line]
111+
else:
112+
if current_if_block is None:
113+
current_if_block = []
114+
current_if_block.append(line)
103115
i += 1
104116
continue
105117

vinca/test_sort_vinca_lists.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Tests for the vinca.yaml list sorter."""
2+
3+
from vinca.sort_vinca_lists import sort_vinca_lists
4+
5+
BASE = """packages_select_by_deps:
6+
- alpha
7+
- charlie
8+
- bravo
9+
"""
10+
11+
12+
def test_sorts_simple_top_level_items(tmp_path):
13+
path = tmp_path / "vinca.yaml"
14+
path.write_text(BASE)
15+
16+
changed = sort_vinca_lists(path)
17+
18+
assert changed is True
19+
items = [
20+
line.strip()[2:]
21+
for line in path.read_text().splitlines()
22+
if line.startswith(" - ")
23+
]
24+
assert items == ["alpha", "bravo", "charlie"]
25+
26+
27+
def test_leaves_already_sorted_file_unchanged(tmp_path):
28+
path = tmp_path / "vinca.yaml"
29+
sorted_content = "packages_select_by_deps:\n - alpha\n - bravo\n - charlie\n\n"
30+
path.write_text(sorted_content)
31+
32+
changed = sort_vinca_lists(path)
33+
34+
assert changed is False
35+
assert path.read_text() == sorted_content
36+
37+
38+
def test_adjacent_if_blocks_without_separator_stay_isolated(tmp_path):
39+
# Regression test: two "- if:" blocks back-to-back with no blank line or
40+
# comment between them used to be parsed as a single block, so sorting
41+
# pooled both blocks' then-items together and could redistribute an item
42+
# from one block's platform condition into the other's.
43+
content = """packages_select_by_deps:
44+
- if: not wasm32 and not win
45+
then:
46+
- web_video_server
47+
- webots_ros2
48+
- yasmin
49+
- yasmin_ros
50+
- if: linux and not aarch64
51+
then:
52+
- somepkg
53+
- zed_msgs
54+
55+
patch_dir: patch
56+
"""
57+
path = tmp_path / "vinca.yaml"
58+
path.write_text(content)
59+
60+
sort_vinca_lists(path)
61+
62+
result = path.read_text()
63+
first_block, second_block = result.split("- if: not wasm32 and not win")[1].split(
64+
"- if: linux and not aarch64"
65+
)
66+
67+
assert "webots_ros2" in first_block
68+
assert "yasmin_ros" in first_block
69+
assert "webots_ros2" not in second_block
70+
assert "somepkg" in second_block
71+
assert "zed_msgs" in second_block
72+
73+
74+
def test_adjacent_if_blocks_separated_by_comment_stay_isolated(tmp_path):
75+
# The comment-separator workaround must keep working alongside the fix.
76+
content = """packages_select_by_deps:
77+
- if: not wasm32 and not win
78+
then:
79+
- web_video_server
80+
- yasmin
81+
- yasmin_ros
82+
83+
# webots_ros2 is linux-only
84+
- if: linux and not aarch64
85+
then:
86+
- webots_ros2
87+
- zed_msgs
88+
89+
patch_dir: patch
90+
"""
91+
path = tmp_path / "vinca.yaml"
92+
path.write_text(content)
93+
94+
sort_vinca_lists(path)
95+
96+
first_block, second_block = path.read_text().split("- if: linux and not aarch64")
97+
assert "- webots_ros2" not in first_block
98+
assert "- webots_ros2" in second_block

0 commit comments

Comments
 (0)