Skip to content

Commit

Permalink
Fix bug when an AttributeError is raised, and add a unit test for tha…
Browse files Browse the repository at this point in the history
…t too
  • Loading branch information
Bruno P. Kinoshita committed Apr 3, 2019
1 parent 48f5514 commit 7801ad0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/cylc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions lib/cylc/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7801ad0

Please sign in to comment.