Skip to content

Commit

Permalink
Ignore invalid pre-release specifiers when checking for updates (#121)
Browse files Browse the repository at this point in the history
* add test to reproduce issue120

* ignore invalid pre-release specifiers (fixes #120)
  • Loading branch information
dennisvang authored Mar 7, 2024
1 parent b0ae976 commit cbf8226
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/tufup/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ def check_for_updates(
If `patch` is `False`, a full update is enforced.
"""
included = {None: '', '': '', 'a': 'abrc', 'b': 'brc', 'rc': 'rc'}
# invalid pre-release specifiers are ignored, with a warning
pre_map = dict(a='abrc', b='brc', rc='rc')
prereleases = pre_map.get(pre, '')
if pre and not prereleases:
logger.warning(f'ignoring invalid pre-release specifier: "{pre}"')
# refresh top-level metadata (root -> timestamp -> snapshot -> targets)
try:
self.refresh()
Expand All @@ -177,7 +181,7 @@ def check_for_updates(
item
for item in all_new_targets.items()
if item[0].is_archive
and (not item[0].version.pre or item[0].version.pre[0] in included[pre])
and (not item[0].version.pre or item[0].version.pre[0] in prereleases)
)
new_archive_meta = None
if new_archives:
Expand Down
8 changes: 5 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,18 @@ def test_download_and_apply_update(self):

def test_check_for_updates(self):
# expectations (based on targets in tests/data/repository):
# - pre=None: only full releases are included, so finds 2.0 patch
# - pre=None, '', or 'invalid': only full releases are included, finds 2.0 patch
# - pre='a': finds all, but total patch size exceeds archive size
# - pre='b': there is no 'b' release, so this finds same as 'rc'
# - pre='rc': finds 2.0 and 3.0rc0, total patch size smaller than archive
client = self.get_refreshed_client()
with patch.object(client, 'refresh', Mock()):
for pre, expected in [(None, 1), ('a', 1), ('b', 2), ('rc', 2)]:
for pre, expected in [
(None, 1), ('', 1), ('a', 1), ('b', 2), ('rc', 2), ('invalid', 1)
]:
with self.subTest(msg=pre):
target_meta = client.check_for_updates(pre=pre)
self.assertTrue(target_meta)
self.assertTrue(expected and target_meta)
self.assertEqual(expected, len(client.new_targets))
if pre == 'a':
self.assertTrue(
Expand Down

0 comments on commit cbf8226

Please sign in to comment.