|
| 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