-
Notifications
You must be signed in to change notification settings - Fork 0
/
ee.test.bats
executable file
·335 lines (272 loc) · 11.5 KB
/
ee.test.bats
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env bats
# smoke tests
@test 'smoke test > dc is installed' {
dc --version >/dev/null
}
@test 'smoke test > GNU grep is installed' {
grep --version | grep 'GNU' >/dev/null
}
# generic tests
@test 'ee.sh > exists' {
[[ -f 'ee.sh' ]]
}
@test 'ee.sh > is executable' {
[[ -x 'ee.sh' ]]
}
@test 'ee.sh > returns EXIT_SUCCESS by default' {
./ee.sh >/dev/null
}
# shellcheck disable=SC2016
@test 'ee.sh > runs the given command, performing evaluation internally' {
TEST_STDOUT="$(./ee.sh 'echo "${BASH_SOURCE[0]}"' || :)"
echo "$TEST_STDOUT" | grep './ee.sh' >/dev/null
}
##### BASE CASE #####
export BASE_CASE="dc -e '1 2 + p'"
export BASE_CASE_CMD='dc'
export BASE_CASE_ARG1='-e'
export BASE_CASE_ARG2="'1 2 + p'"
export BASE_CASE_RESULT='3'
# base case > command
@test 'ee.sh > base case > command > is printed' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep "$BASE_CASE_CMD" >/dev/null
}
@test 'ee.sh > base case > command > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP "$BASE_CASE_CMD" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > base case > command > is printed on first line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | head -n 1 | grep "$BASE_CASE_CMD" >/dev/null
}
@test 'ee.sh > base case > command > is right after shell prompt' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P "[\$#]+[ \t]+$BASE_CASE_CMD" >/dev/null
}
@test 'ee.sh > base case > command > is with arguments' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep "$BASE_CASE" >/dev/null
}
# base case > parameters
@test 'ee.sh > base case > parameters > arg1 is printed' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -- "$BASE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > base case > parameters > arg1 is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP -- "$BASE_CASE_ARG1" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > base case > parameters > arg1 is printed on first line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -- "$BASE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > base case > parameters > arg1 is right after command' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -- "$BASE_CASE_CMD $BASE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > base case > parameters > arg2 is printed' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -- "$BASE_CASE_ARG2" >/dev/null
}
@test 'ee.sh > base case > parameters > arg2 is printed on first line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -- "$BASE_CASE_ARG2" >/dev/null
}
@test 'ee.sh > base case > parameters > arg2 is right after arg1' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -- "$BASE_CASE_ARG1 $BASE_CASE_ARG2" >/dev/null
}
# base case > result
@test 'ee.sh > base case > result > does not write to STDERR' {
./ee.sh "$BASE_CASE" >/dev/null 2>test.out || :
TEST_STDERR="$(cat test.out)"
rm test.out
[[ "$(printf '%s' "$TEST_STDERR" | wc -c)" == '0' ]]
}
@test 'ee.sh > base case > result > is printed' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep "$BASE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > base case > result > is printed alone on line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P "^$BASE_CASE_RESULT\$" >/dev/null
}
@test 'ee.sh > base case > result > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP "$BASE_CASE_RESULT" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > base case > result > is printed on last line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | tail -n 1 | grep "$BASE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > base case > result > is printed on second line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | head -n 2 | tail -n 1 | grep "$BASE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > base case > result > returns EXIT_SUCCESS' {
./ee.sh "$BASE_CASE" >/dev/null
}
@test 'ee.sh > base case > result > writes to STDOUT' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
[[ "$(printf '%s' "$TEST_STDOUT" | wc -c)" != '0' ]]
}
# base case > shell prompt
@test 'ee.sh > base case > shell prompt > exists' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P '[$#]' >/dev/null
}
@test 'ee.sh > base case > shell prompt > has no prefix' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P '^[$#]' >/dev/null
}
@test 'ee.sh > base case > shell prompt > is followed by exactly one space' {
TEST_STDOUT="$(./ee.sh || :)"
echo "$TEST_STDOUT" | grep -P '[$#]+ $' >/dev/null
}
@test 'ee.sh > base case > shell prompt > is on first line' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -P '[$#]' >/dev/null
}
@test 'ee.sh > base case > shell prompt > is one character' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P '^[^$#]*[$#][^$#]*$' >/dev/null
}
@test 'ee.sh > base case > shell prompt > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP '[$#]+' | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > base case > shell prompt > uses dollar sign' {
TEST_STDOUT="$(./ee.sh "$BASE_CASE" || :)"
echo "$TEST_STDOUT" | grep -P '^[^$#]*[$]+[^$#]*$' >/dev/null
}
##### FAILURE CASE #####
export FAILURE_CASE='grep pattern imaginary.txt'
export FAILURE_CASE_CMD='grep'
export FAILURE_CASE_ARG1='pattern'
export FAILURE_CASE_ARG2='imaginary.txt'
export FAILURE_CASE_RESULT='grep: imaginary.txt: No such file or directory'
export FAILURE_CASE_EXIT_STATUS='2'
# failure case > command
@test 'ee.sh > failure case > command > is printed' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep "$FAILURE_CASE_CMD" >/dev/null
}
@test 'ee.sh > failure case > command > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP "$FAILURE_CASE_CMD" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > failure case > command > is printed on first line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | head -n 1 | grep "$FAILURE_CASE_CMD" >/dev/null
}
@test 'ee.sh > failure case > command > is right after shell prompt' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P "[\$#]+[ \t]+$FAILURE_CASE_CMD" >/dev/null
}
@test 'ee.sh > failure case > command > is with arguments' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep "$FAILURE_CASE" >/dev/null
}
# failure case > parameters
@test 'ee.sh > failure case > parameters > arg1 is printed' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -- "$FAILURE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > failure case > parameters > arg1 is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP -- "$FAILURE_CASE_ARG1" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > failure case > parameters > arg1 is printed on first line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -- "$FAILURE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > failure case > parameters > arg1 is right after command' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -- "$FAILURE_CASE_CMD $FAILURE_CASE_ARG1" >/dev/null
}
@test 'ee.sh > failure case > parameters > arg2 is printed' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -- "$FAILURE_CASE_ARG2" >/dev/null
}
@test 'ee.sh > failure case > parameters > arg2 is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP -- "$FAILURE_CASE_ARG2" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > failure case > parameters > arg2 is printed on first line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -- "$FAILURE_CASE_ARG2" >/dev/null
}
@test 'ee.sh > failure case > parameters > arg2 is right after arg1' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -- "$FAILURE_CASE_ARG1 $FAILURE_CASE_ARG2" >/dev/null
}
# failure case > result
@test 'ee.sh > failure case > result > is printed' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>&1 || :)"
echo "$TEST_STDOUT" | grep "$FAILURE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > failure case > result > is printed alone on line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>&1 || :)"
echo "$TEST_STDOUT" | grep -P "^$FAILURE_CASE_RESULT\$" >/dev/null
}
@test 'ee.sh > failure case > result > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>&1 || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP "$FAILURE_CASE_RESULT" | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > failure case > result > is printed on last line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>&1 || :)"
echo "$TEST_STDOUT" | tail -n 1 | grep "$FAILURE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > failure case > result > is printed on second line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>&1 || :)"
echo "$TEST_STDOUT" | head -n 2 | tail -n 1 | grep "$FAILURE_CASE_RESULT" >/dev/null
}
@test 'ee.sh > failure case > result > returns EXIT_FAILURE' {
./ee.sh "$FAILURE_CASE" >/dev/null 2>&1 && false || true
}
@test 'ee.sh > failure case > result > returns exit status from command' {
./ee.sh "$FAILURE_CASE" >/dev/null 2>&1 && false || [[ "$?" == 2 ]]
}
@test 'ee.sh > failure case > result > writes to STDOUT' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
[[ "$(printf '%s' "$TEST_STDOUT" | wc -c)" != '0' ]]
}
# failure case > shell prompt
@test 'ee.sh > failure case > shell prompt > exists' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P '[$#]' >/dev/null
}
@test 'ee.sh > failure case > shell prompt > has no prefix' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P '^[$#]' >/dev/null
}
@test 'ee.sh > failure case > shell prompt > is followed by exactly one space' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P '[$#]+ [^ \t]' >/dev/null
}
@test 'ee.sh > failure case > shell prompt > is on first line' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | head -n 1 | grep -P '[$#]' >/dev/null
}
@test 'ee.sh > failure case > shell prompt > is one character' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P '^[^$#]*[$#][^$#]*$' >/dev/null
}
@test 'ee.sh > failure case > shell prompt > is printed exactly once' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
COUNT="$(echo "$TEST_STDOUT" | grep -oP '[$#]+' | wc -l)"
[[ "$COUNT" == '1' ]]
}
@test 'ee.sh > failure case > shell prompt > uses dollar sign' {
TEST_STDOUT="$(./ee.sh "$FAILURE_CASE" 2>/dev/null || :)"
echo "$TEST_STDOUT" | grep -P '^[^$#]*[$]+[^$#]*$' >/dev/null
}