summaryrefslogtreecommitdiff
path: root/templates/bash/template.sh
blob: bc6a51e222799923e29015cdcb124543d591c2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env bash

set -eu -o pipefail

#############################################################################
# Functions that implement steps. From {{ functions_filename }}.

{{ functions }}


#############################################################################
# Helper code generated by Subplot.


# Simple dictionary abstraction. All values are stored in files so
# they can more easily be inspected.

dict_new() {
    local name="$1"
    rm -rf "$name"
    mkdir "$name"
}

dict_has() {
    local name="$1"
    local key="$2"
    local f="$name/$key"
    test -e "$f"
}

dict_get() {
    local name="$1"
    local key="$2"
    local f="$name/$key"
    cat "$f"
}

dict_get_default() {
    local name="$1"
    local key="$2"
    local default="$3"
    local f="$name/$key"
    if [ -e "$f" ]
    then
        cat "$f"
    else
        echo "$default"
    fi
}

dict_set() {
    local name="$1"
    local key="$2"
    local value="$3"
    local f="$name/$key"
    echo "$value" > "$f"
}

dict_keys() {
    local name="$1"
    ls -1 "$name"
}


# A context abstraction using dictionaries.

ctx_new() {
    dict_new _ctx
}

ctx_set()
{
    dict_set _ctx "$1" "$2"
}

ctx_get()
{
    dict_get _ctx "$1"
}


# Store step captures for calling the corresponding functions.

cap_new() {
    dict_new _cap
}

cap_set()
{
    dict_set _cap "$1" "$2"
}

cap_get()
{
    dict_get _cap "$1"
}


# Store files embedded in the markdown input.

files_new() {
    dict_new _files
}

files_set() {
    dict_set _files "$1" "$2"
}

files_get() {
    dict_get _files "$1"
}


# Decode a base64 encoded string.

decode_base64() {
    echo "$1" | base64 -d
}

# Check two values for equality and give error if they are not equal
assert_eq() {
    if ! diff -u <(echo "$1") <(echo "$2")
    then
        echo "expected values to be identical, but they're not"
        exit 1
    fi
}

# Check first value contains second value.
assert_contains() {
    if ! echo "$1" | grep -F "$2" > /dev/null
    then
        echo "expected first value to contain second value"
        exit 1
    fi
}

# Remember where we started from. The step functions may need to refer
# to files there.
srcdir="$(pwd)"
echo "srcdir $srcdir"

# Create a new temporary directory and chdir there. This allows step
# functions to create new files in the current working directory
# without having to be so careful.
_datadir="$(mktemp -d)"
echo "datadir $_datadir"
cd "$_datadir"


# Store test data files that were embedded in the source document.
# Base64 encoding is used to allow arbitrary data.

files_new
{% for file in files %}
# {{ file.filename }}
filename="$(decode_base64 '{{ file.filename | base64 }}')"
contents="$(decode_base64 '{{ file.contents | base64 }}')"
files_set "$filename" "$contents"
{% endfor %}


#############################################################################
# Code to implement the scenarios.

{% for scenario in scenarios %}
######################################
# Scenario: {{ scenario.title }}
scenario_{{ loop.index }}() {
    local title scendir step name text ret cleanups steps
    declare -a cleanups
    declare -a steps

    title="$(decode_base64 '{{ scenario.title | base64 }}')"
    echo "scenario: $title"

    scendir="$(mktemp -d -p "$_datadir")"
    cd "$scendir"
    ctx_new
    cleanups[0]=''
    steps[0]=''
    ret=0
    
    {% for step in scenario.steps %}
    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 %}

#############################################################################
# Run the scenarios.

if [ "$#" = 0 ]
then {% for scenario in scenarios %}
   scenario_{{ loop.index }}{% endfor %}
else


    for pattern in "$@"
    do
	pattern="$(echo "$pattern" | tr A-Z a-z)"
{% for scenario in scenarios %}
        if echo "{{ scenario.title | lower }}" | grep -F -e "$pattern" > /dev/null
	then
            scenario_{{ loop.index }}
	fi
{% endfor %}
    done
fi


#############################################################################
# Clean up temporary directory and report success.

rm -rf "$_datadir"
echo "OK, all scenarios finished successfully"