Skip to content

Commit f236bc9

Browse files
author
ocaisa
committed
Merge pull request #17 from boegel/subtoolchain_searching
add test for EasyConfig comparison & hashing + add __ne__ definition
2 parents ceb8b07 + 35ef4aa commit f236bc9

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

easybuild/framework/easyconfig/easyconfig.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,10 +722,16 @@ def get(self, key, default=None):
722722
else:
723723
return default
724724

725+
# *both* __eq__ and __ne__ must be implemented for == and != comparisons to work correctly
726+
# see also https://docs.python.org/2/reference/datamodel.html#object.__eq__
725727
def __eq__(self, ec):
726728
"""Is this EasyConfig instance equivalent to the provided one?"""
727729
return self.asdict() == ec.asdict()
728730

731+
def __ne__(self, ec):
732+
"""Is this EasyConfig instance equivalent to the provided one?"""
733+
return self.asdict() != ec.asdict()
734+
729735
def __hash__(self):
730736
"""Return hash value for a hashable representation of this EasyConfig instance."""
731737
def make_hashable(val):
@@ -736,11 +742,12 @@ def make_hashable(val):
736742
val = tuple([(key, make_hashable(val)) for (key, val) in sorted(val.items())])
737743
return val
738744

739-
tup = ()
745+
lst = []
740746
for (key, val) in sorted(self.asdict().items()):
741-
tup += (key, make_hashable(val))
747+
lst.append((key, make_hashable(val)))
742748

743-
return hash(tup)
749+
# a list is not hashable, but a tuple is
750+
return hash(tuple(lst))
744751

745752
def asdict(self):
746753
"""

test/framework/easyconfig.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,26 @@ def test_param_value_type_checking(self):
15351535
error_msg_pattern = "Type checking of easyconfig parameter values failed: .*'name'.*'version'.*"
15361536
self.assertErrorRegex(EasyBuildError, error_msg_pattern, EasyConfig, ec_file)
15371537

1538+
def test_eq_hash(self):
1539+
"""Test comparing two EasyConfig instances."""
1540+
test_easyconfigs = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'easyconfigs')
1541+
ec1 = EasyConfig(os.path.join(test_easyconfigs, 'toy-0.0.eb'))
1542+
ec2 = EasyConfig(os.path.join(test_easyconfigs, 'toy-0.0.eb'))
1543+
1544+
# different instances, same parsed easyconfig
1545+
self.assertFalse(ec1 is ec2)
1546+
self.assertEqual(ec1, ec2)
1547+
self.assertTrue(ec1 == ec2)
1548+
self.assertFalse(ec1 != ec2)
1549+
1550+
# hashes should also be identical
1551+
self.assertEqual(hash(ec1), hash(ec2))
1552+
1553+
# other parsed easyconfig is not equal
1554+
ec3 = EasyConfig(os.path.join(test_easyconfigs, 'gzip-1.4.eb'))
1555+
self.assertFalse(ec1 == ec3)
1556+
self.assertTrue(ec1 != ec3)
1557+
15381558

15391559
def suite():
15401560
""" returns all the testcases in this module """

0 commit comments

Comments
 (0)