From 0402565f12623fb56da54e9a9a764edb39129186 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 17 May 2020 11:02:45 +0300 Subject: feat(template.sh): call cleanup functions if defined --- templates/bash/template.sh | 68 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/templates/bash/template.sh b/templates/bash/template.sh index c0a6e7e..bc6a51e 100644 --- a/templates/bash/template.sh +++ b/templates/bash/template.sh @@ -167,28 +167,62 @@ files_set "$filename" "$contents" ###################################### # Scenario: {{ scenario.title }} scenario_{{ loop.index }}() { - local _title _scendir _step _name _text + local title scendir step name text ret cleanups steps + declare -a cleanups + declare -a steps - _title="$(decode_base64 '{{ scenario.title | base64 }}')" - echo "scenario: $_title" + title="$(decode_base64 '{{ scenario.title | base64 }}')" + echo "scenario: $title" - _scendir="$(mktemp -d -p "$_datadir")" - cd "$_scendir" + scendir="$(mktemp -d -p "$_datadir")" + cd "$scendir" ctx_new + cleanups[0]='' + steps[0]='' + ret=0 + {% for step in scenario.steps %} - # Step: {{ step.text }} - _step="$(decode_base64 '{{ step.text | base64 }}')" - echo " step: {{ step.kind | lower }} $_step" - - cap_new - {% for part in step.parts %}{% if part.CapturedText is defined -%} - _name="$(decode_base64 '{{ part.CapturedText.name | base64 }}')" - _text="$(decode_base64 '{{ part.CapturedText.text | base64 }}')" - cap_set "$_name" "$_text" - {% endif -%} - {% endfor -%} - {{ step.function }} + if [ "$ret" = 0 ] + then + # Step: {{ step.text }} + step="{{ step.kind | lower }} $(decode_base64 '{{ step.text | base64 }}')" + echo " step: $step" + + cap_new + {% for part in step.parts %}{% if part.CapturedText is defined -%} + name="$(decode_base64 '{{ part.CapturedText.name | base64 }}')" + text="$(decode_base64 '{{ part.CapturedText.text | base64 }}')" + cap_set "$name" "$text" + {% endif -%} + {% endfor -%} + if {{ step.function }} + then + cleanup='{{ step.cleanup }}' + if [ "$cleanup" != "" ] + then + {% raw %} + i=${#cleanups} + cleanups[$i]="$cleanup" + steps[$i]="$step" + {% endraw %} + fi + else + ret=$? + fi + fi {% endfor %} + + {% raw %} + echo "${!cleanups[*]}" | tr ' ' '\n' | tac | while read i + do + step="${steps[$i]}" + func="${cleanups[$i]}" + echo " cleanup: $step" + $func + done + {% endraw %} + + return $ret } {% endfor %} -- cgit v1.2.1