Skip to content

Commit

Permalink
Improve error when checksum dict has no entry for a file
Browse files Browse the repository at this point in the history
When the dict didn't contain the filename EB will crash with
> Invalid checksum spec 'None', should be a string (MD5) or 2-tuple (type, value).

This will now raise a more descriptive error
  • Loading branch information
Flamefire committed May 11, 2023
1 parent 2436f4d commit aaf6564
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 3 additions & 6 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,14 +1265,11 @@ def verify_checksum(path, checksums):

for checksum in checksums:
if isinstance(checksum, dict):
if filename in checksum:
try:
# Set this to a string-type checksum
checksum = checksum[filename]
elif build_option('enforce_checksums'):
raise EasyBuildError("Missing checksum for %s", filename)
else:
# Set to None and allow to fail elsewhere
checksum = None
except KeyError:
raise EasyBuildError("Missing checksum for %s in %s", filename, checksum)

if isinstance(checksum, string_type):
# if no checksum type is specified, it is assumed to be MD5 (32 characters) or SHA256 (64 characters)
Expand Down
10 changes: 10 additions & 0 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ def test_checksums(self):
alt_checksums = ('7167b64b1ca062b9674ffef46f9325db7167b64b1ca062b9674ffef46f9325db', broken_checksums['sha256'])
self.assertFalse(ft.verify_checksum(fp, alt_checksums))

# Check dictionary
alt_checksums = (known_checksums['sha256'],)
self.assertTrue(ft.verify_checksum(fp, {os.path.basename(fp): known_checksums['sha256']}))
faulty_dict = {'wrong-name': known_checksums['sha256']}
self.assertErrorRegex(EasyBuildError,
"Missing checksum for " + os.path.basename(fp) + " in .*wrong-name.*",
ft.verify_checksum, fp, faulty_dict)

# check whether missing checksums are enforced
build_options = {
'enforce_checksums': True,
Expand All @@ -362,6 +370,8 @@ def test_checksums(self):
for checksum in [known_checksums[x] for x in ('md5', 'sha256')]:
dict_checksum = {os.path.basename(fp): checksum, 'foo': 'baa'}
self.assertTrue(ft.verify_checksum(fp, dict_checksum))
del dict_checksum[os.path.basename(fp)]
self.assertErrorRegex(EasyBuildError, "Missing checksum for", ft.verify_checksum, fp, dict_checksum)

def test_common_path_prefix(self):
"""Test get common path prefix for a list of paths."""
Expand Down

0 comments on commit aaf6564

Please sign in to comment.