def _validate_check(self, check): import reframe.utility as util name = type(check).__name__ checkfile = os.path.relpath(inspect.getfile(type(check))) required_attrs = [ 'sanity_patterns', 'valid_systems', 'valid_prog_environs' ] for attr in required_attrs: if getattr(check, attr) is None: getlogger().warning( f'{checkfile}: {attr!r} not defined for test {name!r}; ' f'skipping...') return False is_copyable = util.attr_validator(lambda obj: util.is_copyable(obj)) valid, attr = is_copyable(check) if not valid: getlogger().warning( f'{checkfile}: {attr!r} is not copyable; ' f'not copyable attributes are not ' f'allowed inside the __init__() method; ' f'consider setting them in a pipeline hook instead') return False return True
def test_attr_validator(): class C: def __init__(self): self.x = 3 self.y = [1, 2, 3] self.z = {'a': 1, 'b': 2} class D: def __init__(self): self.x = 1 self.y = C() has_no_str = util.attr_validator(lambda x: not isinstance(x, str)) d = D() assert has_no_str(d)[0] # Check when a list element does not validate d.y.y[1] = 'foo' assert has_no_str(d) == (False, 'D.y.y[1]') d.y.y[1] = 2 # Check when a dict element does not validate d.y.z['a'] = 'b' assert has_no_str(d) == (False, "D.y.z['a']") d.y.z['a'] = 1 # Check when an attribute does not validate d.x = 'foo' assert has_no_str(d) == (False, 'D.x') d.x = 1 # Check when an attribute does not validate d.y.x = 'foo' assert has_no_str(d) == (False, 'D.y.x') d.y.x = 3 # Check when an attribute does not validate against a custom type has_no_c = util.attr_validator(lambda x: not isinstance(x, C)) assert has_no_c(d) == (False, 'D.y')