forked from threatgrid/declarative.bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-declarative
executable file
·104 lines (87 loc) · 2.18 KB
/
test-declarative
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
#!/bin/bash
# normally just ''source declarative.bash'', but here we want to be sure to
# test the current version
source ./declarative.bash
is_one_word() {
local varname value
varname=$1; value=${!varname}
[[ $varname && $varname != *[[:space:]]* ]]
}
is_multiple_words() {
local varname value
varname=$1; value=${!varname}
[[ $value && $value = *[[:space:]]* ]]
}
declare_assertions simple_var_A -- is_one_word
declare_assertions simple_var_B -- is_multiple_words
provides simple_provider_A simple_var_A
simple_provider_A() {
declare -g simple_var_A
simple_var_A="world"
}
provides simple_provider_B simple_var_B
simple_provider_B() {
declare -g simple_var_B
needs simple_var_A
simple_var_B="goodbye, cruel $simple_var_A"
}
provides simple_test test
simple_test() {
needs simple_var_B
[[ $simple_var_B = "goodbye, cruel world" ]] || {
echo "ERROR: Simple functionality not working" >&2
exit 1
}
}
provides loop_A lA
loop_A() {
declare -g lA
if [[ $lA ]]; then
(( ++lA ))
else
lA=1
fi
if (( lA >= 3 )); then
echo "FAILED: Loops not prevented" >&2
exit 1
fi
needs lA
}
provides loop_test test
loop_test() {
needs lA
}
provides leaky_func leaky
leaky_func() {
undeclared_variable=value
}
provides leak_test test leaks_detected
leak_test() {
local default_defn=$(declare -f _declarative_leak_detected)
_declarative_leak_detected() {
declare -g -A leaks_detected
: leaks_detected "$1" "$2"
leaks_detected[$1]+="$2 "
}
DECLARATIVE_TEST_LEAKS=1
needs leaky
[[ ${leaks_detected[leaky_func]% } = "undeclared_variable" ]] || {
echo "ERROR: Expected leaky function to have 'undeclared_variable' detected; found:"
echo " ${leaks_detected[leaky_func]% }" >&2
exit 1
}
# restore old state
unset DECLARATIVE_TEST_LEAKS
eval "$default_defn"
}
main() {
# we need all tests; run them in order.
(needs test 2>/dev/null) && {
echo "ERROR: Must refuse to satisfy dependencies within subshells" >&2
return 1
}
needs test
echo "SUCCESS"
}
# kick things off
main