From 7801ad02922eea85ecddf3a32f7f39e2e40bcbd6 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Tue, 2 Apr 2019 13:59:33 +1300 Subject: [PATCH] Fix bug when an AttributeError is raised, and add a unit test for that too --- lib/cylc/config.py | 15 ++++++++------- lib/cylc/tests/test_config.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/cylc/config.py b/lib/cylc/config.py index ae0d076d964..2f54682465f 100644 --- a/lib/cylc/config.py +++ b/lib/cylc/config.py @@ -2103,13 +2103,14 @@ def load_graph(self): else: try: if not callable(get_func(xtrig.func_name, self.fdir)): - raise SuiteConfigError("ERROR, xtrigger function " - "not callable: " - "%s" % xtrig.func_name) - except ImportError: - raise SuiteConfigError("ERROR, xtrigger function " - "not found: " - "%s" % xtrig.func_name) + raise SuiteConfigError( + f"ERROR, " + f"xtrigger function not callable: " + f"{xtrig.func_name}") + except (ImportError, AttributeError): + raise SuiteConfigError( + f"ERROR, " + f"xtrigger function not found: {xtrig.func_name}") self.xtrigger_mgr.add_trig(label, xtrig) self.taskdefs[task_name].xtrig_labels.add(label) diff --git a/lib/cylc/tests/test_config.py b/lib/cylc/tests/test_config.py index 8bcafdbb97d..0088636199c 100644 --- a/lib/cylc/tests/test_config.py +++ b/lib/cylc/tests/test_config.py @@ -76,6 +76,32 @@ def test_xfunction_import_error(self): SuiteConfig(suite="caiman_suite", fpath=f.name) assert "not found" in str(excinfo.value) + def test_xfunction_attribute_error(self): + """Test for error when a xtrigger function cannot be imported.""" + with TemporaryDirectory() as temp_dir: + python_dir = Path(os.path.join(temp_dir, "lib", "python")) + python_dir.mkdir(parents=True) + capybara_file = python_dir / "capybara.py" + with capybara_file.open(mode="w") as f: + # NB: we are not returning a lambda, instead we have a scalar + f.write("""toucan = lambda: True""") + f.flush() + suite_rc = Path(temp_dir, "suite.rc") + with suite_rc.open(mode="w") as f: + f.write(""" + [scheduling] + initial cycle point = 2018-01-01 + [[xtriggers]] + oopsie = capybara() + [[dependencies]] + [[[R1]]] + graph = '@oopsie => qux' + """) + f.flush() + with pytest.raises(SuiteConfigError) as excinfo: + SuiteConfig(suite="capybara_suite", fpath=f.name) + assert "not found" in str(excinfo.value) + def test_xfunction_not_callable(self): """Test for error when a xtrigger function is not callable.""" with TemporaryDirectory() as temp_dir: