Skip to content

Commit 6889b79

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 7666088 commit 6889b79

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
@@ -613,19 +613,33 @@ def ensure_iterable_license_specs(specs):
613613
}))
614614
# checksums is a list of checksums, one entry per file (source/patch)
615615
# each entry can be:
616+
# None
616617
# a single checksum value (string)
617618
# a single checksum value of a specified type (2-tuple, 1st element is checksum type, 2nd element is checksum)
618619
# a list of checksums (of different types, perhaps different formats), which should *all* be valid
619-
# a dictionary with a mapping from filename to checksum value
620-
CHECKSUM_LIST = (list, as_hashable({'elem_types': [str, tuple, STRING_DICT]}))
621-
CHECKSUMS = (list, as_hashable({'elem_types': [str, tuple, STRING_DICT, CHECKSUM_LIST]}))
620+
# a tuple of checksums (of different types, perhaps different formats), where one should be valid
621+
# a dictionary with a mapping from filename to checksum (None, value, type&value, alternatives)
622622

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

627641
# easy types, that can be verified with isinstance
628-
EASY_TYPES = [str, bool, dict, int, list, str, tuple]
642+
EASY_TYPES = [str, bool, dict, int, list, str, tuple, type(None)]
629643

630644
# type checking is skipped for easyconfig parameters names not listed in PARAMETER_TYPES
631645
PARAMETER_TYPES = {

test/framework/type_checking.py

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

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

0 commit comments

Comments
 (0)