|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import pytest
|
3 | 4 | import secrets
|
@@ -131,3 +132,60 @@ def get_installs(self):
|
131 | 132 | assert_log(
|
132 | 133 | assert_log.skip_until(".*Global shortcuts directory is not on PATH")
|
133 | 134 | )
|
| 135 | + |
| 136 | + |
| 137 | +def test_merge_existing_index(tmp_path): |
| 138 | + # This function is for multiple downloaded index.jsons, so it merges based |
| 139 | + # on the url property, which should usually be a local file. |
| 140 | + existing = tmp_path / "index.json" |
| 141 | + with open(existing, "w", encoding="utf-8") as f: |
| 142 | + json.dump({"versions": [ |
| 143 | + {"id": "test-1", "url": "test-file-1.zip"}, |
| 144 | + {"id": "test-2", "url": "test-file-2.zip"}, |
| 145 | + {"id": "test-3", "url": "test-file-3.zip"}, |
| 146 | + ]}, f) |
| 147 | + |
| 148 | + new = [ |
| 149 | + # Ensure new versions appear first |
| 150 | + {"id": "test-4", "url": "test-file-4.zip"}, |
| 151 | + # Ensure matching ID doesn't result in overwrite |
| 152 | + {"id": "test-1", "url": "test-file-1b.zip"}, |
| 153 | + # Ensure matching URL excludes original entry |
| 154 | + {"id": "test-2b", "url": "test-file-2.zip"}, |
| 155 | + ] |
| 156 | + |
| 157 | + IC._merge_existing_index(new, existing) |
| 158 | + |
| 159 | + assert new == [ |
| 160 | + {"id": "test-4", "url": "test-file-4.zip"}, |
| 161 | + {"id": "test-1", "url": "test-file-1b.zip"}, |
| 162 | + {"id": "test-2b", "url": "test-file-2.zip"}, |
| 163 | + {"id": "test-1", "url": "test-file-1.zip"}, |
| 164 | + {"id": "test-3", "url": "test-file-3.zip"}, |
| 165 | + ] |
| 166 | + |
| 167 | + |
| 168 | +def test_merge_existing_index_not_found(tmp_path): |
| 169 | + existing = tmp_path / "index.json" |
| 170 | + try: |
| 171 | + existing.unlink() |
| 172 | + except FileNotFoundError: |
| 173 | + pass |
| 174 | + |
| 175 | + # Expect no failure and no change |
| 176 | + new = [1, 2, 3] |
| 177 | + IC._merge_existing_index(new, existing) |
| 178 | + assert new == [1, 2, 3] |
| 179 | + |
| 180 | + |
| 181 | +def test_merge_existing_index_not_valid(tmp_path): |
| 182 | + existing = tmp_path / "index.json" |
| 183 | + with open(existing, "w", encoding="utf-8") as f: |
| 184 | + print("It's not a list of installs", file=f) |
| 185 | + print("But more importantly,", file=f) |
| 186 | + print("it's not valid JSON!", file=f) |
| 187 | + |
| 188 | + # Expect no failure and no change |
| 189 | + new = [1, 2, 3] |
| 190 | + IC._merge_existing_index(new, existing) |
| 191 | + assert new == [1, 2, 3] |
0 commit comments