Skip to content

Commit 241868e

Browse files
authored
Merge pull request #4150 from Flamefire/fix-checksum-check
Improve error when checksum dict has no entry for a file
2 parents f1cd377 + f60d2c6 commit 241868e

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

easybuild/tools/filetools.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,14 +1264,11 @@ def verify_checksum(path, checksums):
12641264

12651265
for checksum in checksums:
12661266
if isinstance(checksum, dict):
1267-
if filename in checksum:
1267+
try:
12681268
# Set this to a string-type checksum
12691269
checksum = checksum[filename]
1270-
elif build_option('enforce_checksums'):
1271-
raise EasyBuildError("Missing checksum for %s", filename)
1272-
else:
1273-
# Set to None and allow to fail elsewhere
1274-
checksum = None
1270+
except KeyError:
1271+
raise EasyBuildError("Missing checksum for %s in %s", filename, checksum)
12751272

12761273
if isinstance(checksum, string_type):
12771274
# if no checksum type is specified, it is assumed to be MD5 (32 characters) or SHA256 (64 characters)
@@ -1301,7 +1298,8 @@ def verify_checksum(path, checksums):
13011298
# no matching checksums
13021299
return False
13031300
else:
1304-
raise EasyBuildError("Invalid checksum spec '%s', should be a string (MD5) or 2-tuple (type, value).",
1301+
raise EasyBuildError("Invalid checksum spec '%s': should be a string (MD5 or SHA256), "
1302+
"2-tuple (type, value), or tuple of alternative checksum specs.",
13051303
checksum)
13061304

13071305
actual_checksum = compute_checksum(path, typ)

test/framework/filetools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ def test_checksums(self):
349349
alt_checksums = ('7167b64b1ca062b9674ffef46f9325db7167b64b1ca062b9674ffef46f9325db', broken_checksums['sha256'])
350350
self.assertFalse(ft.verify_checksum(fp, alt_checksums))
351351

352+
# Check dictionary
353+
alt_checksums = (known_checksums['sha256'],)
354+
self.assertTrue(ft.verify_checksum(fp, {os.path.basename(fp): known_checksums['sha256']}))
355+
faulty_dict = {'wrong-name': known_checksums['sha256']}
356+
self.assertErrorRegex(EasyBuildError,
357+
"Missing checksum for " + os.path.basename(fp) + " in .*wrong-name.*",
358+
ft.verify_checksum, fp, faulty_dict)
359+
352360
# check whether missing checksums are enforced
353361
build_options = {
354362
'enforce_checksums': True,
@@ -363,6 +371,8 @@ def test_checksums(self):
363371
for checksum in [known_checksums[x] for x in ('md5', 'sha256')]:
364372
dict_checksum = {os.path.basename(fp): checksum, 'foo': 'baa'}
365373
self.assertTrue(ft.verify_checksum(fp, dict_checksum))
374+
del dict_checksum[os.path.basename(fp)]
375+
self.assertErrorRegex(EasyBuildError, "Missing checksum for", ft.verify_checksum, fp, dict_checksum)
366376

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

0 commit comments

Comments
 (0)