From fa0404ff97145ceb17eb7afc17892d7c71e9b6ad Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 5 Oct 2023 12:04:42 -0500 Subject: [PATCH] test/attr_tests: fix tests with setattr Since Python objects will dynamically add an attribute if one does not exist when calling setattr(), we need to check that the attribute actually exists before setting it. Otherwise, tests will pass even if the attribute is not defined in the object being tested. Signed-off-by: David Lechner --- test/attr_tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/attr_tests.py b/test/attr_tests.py index f86cf6d9b..a3fa0ab6f 100644 --- a/test/attr_tests.py +++ b/test/attr_tests.py @@ -83,6 +83,10 @@ def attribute_single_value_boolean(uri, classname, attr, value): Value to write and read back from attribute """ bi = eval(classname + "(uri='" + uri + "')") + + if not hasattr(bi, attr): + raise AttributeError(f"no attribute named: {attr}") + setattr(bi, attr, value) rval = getattr(bi, attr) del bi @@ -258,6 +262,10 @@ def attribute_write_only_str(uri, classname, attr, value): Value to write into attr property """ sdr = eval(classname + "(uri='" + uri + "')") + + if not hasattr(sdr, attr): + raise AttributeError(f"no attribute named: {attr}") + try: setattr(sdr, attr, value) del sdr @@ -284,6 +292,14 @@ def attribute_write_only_str_with_depends(uri, classname, attr, value, depends): are properties and values are values to be written """ sdr = eval(classname + "(uri='" + uri + "')") + + for p in depends: + if not hasattr(sdr, p): + raise AttributeError(f"no attribute named: {p}") + + if not hasattr(sdr, attr): + raise AttributeError(f"no attribute named: {attr}") + for p in depends: setattr(sdr, p, depends[p]) try: