Skip to content

Commit 9b018f3

Browse files
committed
Fix the checksum type check
The `None` case was missed and due to the unrestricted `tuple` elem_type it may return valid for actually invalid entries. So restrict that beeing overly cautious so it may wrongly return invalid. But in that case the conversion function will be called which can do more elaborate verification. Add test checking for None in checksums.
1 parent fcab3eb commit 9b018f3

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

easybuild/framework/easyconfig/types.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,19 +608,33 @@ def ensure_iterable_license_specs(specs):
608608
}))
609609
# checksums is a list of checksums, one entry per file (source/patch)
610610
# each entry can be:
611+
# None
611612
# a single checksum value (string)
612613
# a single checksum value of a specified type (2-tuple, 1st element is checksum type, 2nd element is checksum)
613614
# a list of checksums (of different types, perhaps different formats), which should *all* be valid
614-
# a dictionary with a mapping from filename to checksum value
615-
CHECKSUM_LIST = (list, as_hashable({'elem_types': [str, tuple, STRING_DICT]}))
616-
CHECKSUMS = (list, as_hashable({'elem_types': [str, tuple, STRING_DICT, CHECKSUM_LIST]}))
615+
# a tuple of checksums (of different types, perhaps different formats), where one should be valid
616+
# a dictionary with a mapping from filename to checksum (None, value, type&value, alternatives)
617617

618-
CHECKABLE_TYPES = [CHECKSUM_LIST, CHECKSUMS, DEPENDENCIES, DEPENDENCY_DICT, LIST_OF_STRINGS,
618+
# Type & value, value may be an int for type "size"
619+
# This is a bit too permissive as it allows the first element to be an int and doesn't restrict the number of elements
620+
CHECKSUM_TUPLE = (tuple, as_hashable({'elem_types': [str, int]}))
621+
CHECKSUM_DICT = (dict, as_hashable(
622+
{
623+
'elem_types': [type(None), str, CHECKSUM_TUPLE],
624+
'key_types': [str],
625+
}
626+
))
627+
CHECKSUM_LIST = (list, as_hashable({'elem_types': [str, CHECKSUM_TUPLE, CHECKSUM_DICT]}))
628+
629+
CHECKSUMS = (list, as_hashable({'elem_types': [type(None), str, CHECKSUM_LIST, CHECKSUM_TUPLE, CHECKSUM_DICT]}))
630+
631+
CHECKABLE_TYPES = [CHECKSUM_DICT, CHECKSUM_LIST, CHECKSUM_TUPLE, CHECKSUMS,
632+
DEPENDENCIES, DEPENDENCY_DICT, LIST_OF_STRINGS,
619633
SANITY_CHECK_PATHS_DICT, SANITY_CHECK_PATHS_ENTRY, STRING_DICT, STRING_OR_TUPLE_LIST,
620634
STRING_OR_TUPLE_DICT, STRING_OR_TUPLE_OR_DICT_LIST, TOOLCHAIN_DICT, TUPLE_OF_STRINGS]
621635

622636
# easy types, that can be verified with isinstance
623-
EASY_TYPES = [string_type, bool, dict, int, list, str, tuple]
637+
EASY_TYPES = [string_type, bool, dict, int, list, str, tuple, type(None)]
624638

625639
# type checking is skipped for easyconfig parameters names not listed in PARAMETER_TYPES
626640
PARAMETER_TYPES = {

test/framework/type_checking.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,24 @@ def test_check_type_of_param_value_checksums(self):
221221
{'foo.txt': sha256_checksum1, 'bar.txt': sha256_checksum2},
222222
# 3 alternative checksums for a single file, one match is sufficient
223223
(sha256_checksum1, sha256_checksum2, sha256_checksum3),
224-
]
224+
# two alternative checksums for a single file (not to be confused by checksum-type & -value tuple)
225+
(sha256_checksum1, md5_checksum),
226+
# three alternative checksums for a single file of different types
227+
(sha256_checksum1, ('md5', md5_checksum), {'foo.txt': sha256_checksum1}),
228+
# alternative checksums in dicts are also allowed
229+
{'foo.txt': (sha256_checksum2, sha256_checksum3), 'bar.txt': (sha256_checksum1, md5_checksum)},
230+
# Same but with lists -> all must match for each file
231+
{'foo.txt': [sha256_checksum2, sha256_checksum3], 'bar.txt': [sha256_checksum1, md5_checksum]},
232+
],
233+
# None is allowed, meaning skip the checksum
234+
[
235+
None,
236+
# Also in mappings
237+
{'foo.txt': sha256_checksum1, 'bar.txt': None},
238+
],
225239
]
226240
for inp in inputs:
227-
self.assertEqual(check_type_of_param_value('checksums', inp), (True, inp))
241+
self.assertTrue(check_type_of_param_value('checksums', inp), 'Failed for ' + str(inp))
228242

229243
def test_check_type_of_param_value_patches(self):
230244
"""Test check_type_of_param_value function for patches."""

0 commit comments

Comments
 (0)