summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-05-16 10:19:39 +0300
committerLars Wirzenius <liw@liw.fi>2020-05-17 12:11:02 +0300
commit385777c09a4f70402f10daaf79e0aa602e4e4071 (patch)
tree1dda05f51b3dc574e8cc9c0da493871971b76737
parent6fe21a9836eb85fbda8cf26bdabc6b56131f79aa (diff)
downloadsubplot-385777c09a4f70402f10daaf79e0aa602e4e4071.tar.gz
feat(template.py): call cleanup functions if defined
If a binding has a cleanup function defined, call it at the end of the scenario, even if a step in the scenario failed.
-rw-r--r--templates/python/template.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/templates/python/template.py b/templates/python/template.py
index 86396ff..76d8239 100644
--- a/templates/python/template.py
+++ b/templates/python/template.py
@@ -90,6 +90,7 @@ class Step:
self._text = None
self._args = {}
self._function = None
+ self._cleanup = None
def set_kind(self, kind):
self._kind = kind
@@ -103,11 +104,22 @@ class Step:
def set_function(self, function):
self._function = function
+ def set_cleanup(self, cleanup):
+ self._cleanup = cleanup
+
def do(self, ctx):
print(' step: {} {}'.format(self._kind, self._text))
logging.info(' step: {} {}'.format(self._kind, self._text))
self._function(ctx, **self._args)
+ def cleanup(self, ctx):
+ if self._cleanup:
+ print(' cleanup: {} {}'.format(self._kind, self._text))
+ logging.info(' cleanup: {} {}'.format(self._kind, self._text))
+ self._cleanup(ctx)
+ else:
+ logging.info(' no cleanup defined: {} {}'.format(self._kind, self._text))
+
class Scenario:
@@ -132,8 +144,18 @@ class Scenario:
os.chdir(scendir)
ctx = Context()
- for step in self._steps:
- step.do(ctx)
+ done = []
+ try:
+ for step in self._steps:
+ step.do(ctx)
+ done.append(step)
+ except Exception as e:
+ logging.error(str(e), exc_info=True)
+ for step in reversed(done):
+ step.cleanup(ctx)
+ raise
+ for step in reversed(done):
+ step.cleanup(ctx)
{% for scenario in scenarios %}
@@ -149,6 +171,8 @@ class Scenario_{{ loop.index }}():
step.set_kind('{{ step.kind | lower }}')
step.set_text(decode_str('{{ step.text | base64 }}'))
step.set_function({{ step.function }})
+ if '{{ step.cleanup }}':
+ step.set_cleanup({{ step.cleanup }})
self._scenario.append_step(step)
{% for part in step.parts %}{% if part.CapturedText is defined -%}
name = decode_str('{{ part.CapturedText.name | base64 }}')