Skip to content

Merges newly downloaded packages into existing index.json. #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/manage/install_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,26 @@
LOGGER.verbose("Install complete")


def _merge_existing_index(versions, index_json):
try:
with open(index_json, "r", encoding="utf-8") as f:
existing_index = json.load(f)
list(existing_index["versions"])
except FileNotFoundError:
pass
except (json.JSONDecodeError, KeyError, ValueError):
LOGGER.warn("Existing index file appeared invalid and was overwritten.")
LOGGER.debug("TRACEBACK", exc_info=True)
else:
LOGGER.debug("Merging into existing %s", index_json)
current = {i["url"].casefold() for i in versions}
added = []
for install in existing_index["versions"]:
if install.get("url", "").casefold() not in current:
LOGGER.debug("Merging %s", install.get("url", "<unspecified>"))
versions.append(install)


def _fatal_install_error(cmd, ex):
logfile = cmd.get_log_file()
if logfile:
Expand Down Expand Up @@ -753,6 +773,7 @@
return _fatal_install_error(cmd, ex)

if cmd.download:
_merge_existing_index(download_index["versions"], cmd.download / "index.json")

Check warning on line 776 in src/manage/install_command.py

View check run for this annotation

Codecov / codecov/patch

src/manage/install_command.py#L776

Added line #L776 was not covered by tests
with open(cmd.download / "index.json", "w", encoding="utf-8") as f:
json.dump(download_index, f, indent=2, default=str)
LOGGER.info("Offline index has been generated at !Y!%s!W!.", cmd.download)
Expand Down
58 changes: 58 additions & 0 deletions tests/test_install_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import pytest
import secrets
Expand Down Expand Up @@ -131,3 +132,60 @@ def get_installs(self):
assert_log(
assert_log.skip_until(".*Global shortcuts directory is not on PATH")
)


def test_merge_existing_index(tmp_path):
# This function is for multiple downloaded index.jsons, so it merges based
# on the url property, which should usually be a local file.
existing = tmp_path / "index.json"
with open(existing, "w", encoding="utf-8") as f:
json.dump({"versions": [
{"id": "test-1", "url": "test-file-1.zip"},
{"id": "test-2", "url": "test-file-2.zip"},
{"id": "test-3", "url": "test-file-3.zip"},
]}, f)

new = [
# Ensure new versions appear first
{"id": "test-4", "url": "test-file-4.zip"},
# Ensure matching ID doesn't result in overwrite
{"id": "test-1", "url": "test-file-1b.zip"},
# Ensure matching URL excludes original entry
{"id": "test-2b", "url": "test-file-2.zip"},
]

IC._merge_existing_index(new, existing)

assert new == [
{"id": "test-4", "url": "test-file-4.zip"},
{"id": "test-1", "url": "test-file-1b.zip"},
{"id": "test-2b", "url": "test-file-2.zip"},
{"id": "test-1", "url": "test-file-1.zip"},
{"id": "test-3", "url": "test-file-3.zip"},
]


def test_merge_existing_index_not_found(tmp_path):
existing = tmp_path / "index.json"
try:
existing.unlink()
except FileNotFoundError:
pass

# Expect no failure and no change
new = [1, 2, 3]
IC._merge_existing_index(new, existing)
assert new == [1, 2, 3]


def test_merge_existing_index_not_valid(tmp_path):
existing = tmp_path / "index.json"
with open(existing, "w", encoding="utf-8") as f:
print("It's not a list of installs", file=f)
print("But more importantly,", file=f)
print("it's not valid JSON!", file=f)

# Expect no failure and no change
new = [1, 2, 3]
IC._merge_existing_index(new, existing)
assert new == [1, 2, 3]