-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimist.test.sh
executable file
·216 lines (187 loc) · 4.5 KB
/
minimist.test.sh
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
#!/bin/bash
set -e
# Config
VERBOSE=${VERBOSE:-n}
# ===== Helpers =====
ERROR_COUNT=0
function assert() {
if [[ $VERBOSE == 'y' ]]; then
echo "$(caller): ${FUNCNAME[1]} asserted '$1' == '$2'"
fi
if [[ "$1" != "$2" ]]; then
echo "$(caller) Assertion failed: '$1' != '$2'" >&2
ERROR_COUNT=$((ERROR_COUNT+1))
fi
}
HAD_ERROR=N
function __test_on_error() {
HAD_ERROR=Y
}
function runtest() {
HAD_ERROR=N
echo "Running test: $1"
$1
}
export AOPT_EXIT_ON_ERROR=n
export AOPT_ON_ERROR=__test_on_error
# ===== Tests =====
function test_basic() {
source minimist.sh -a --long -Abc abc 123
assert $HAD_ERROR N
assert $ARG_a y
assert $ARG_LONG y
assert $ARG_A y
assert $ARG_b y
assert $ARG_c y
assert "$ARG_V" "abc 123"
assert "$ARG_0" "./minimist.test.sh"
}
function test_positional() {
AOPT_POSITIONAL_OVER_FLAG=n
source minimist.sh abc -a bq --key val -- -ab --cd 123
assert $HAD_ERROR N
assert $ARG_KEY val
assert "$ARG_V" "abc bq"
assert "$ARG0" ""
}
function test_positional_override() {
AOPT_POSITIONAL_OVER_FLAG=y
source minimist.sh --abc 123
assert $HAD_ERROR N
assert $ARG_ABC y
assert $ARG_V 123
}
function test_duplicate_definitions() {
source minimist.sh -aa 2>/dev/null || true
assert $HAD_ERROR Y
assert $ARG_a y
}
function test_empty_equals() {
source minimist.sh --abc=
assert $HAD_ERROR N
assert $ARG_ABC ""
}
function test_full_equals() {
source minimist.sh --abc=qef "--def=this is long" --double=a=b
assert $HAD_ERROR N
assert $ARG_ABC qef
assert "$ARG_DEF" "this is long"
assert "$ARG_DOUBLE_A" "a=b"
}
function test_no_export() {
source minimist.sh "--abc=123"
assert $HAD_ERROR N
assert $ARG_ABC 123
FOUND=$(env | grep ARG_ || echo)
assert $FOUND ""
}
function test_export() {
AOPT_DECLARE_FLAGS=-rx
source minimist.sh "--abc=456"
assert $HAD_ERROR N
assert $ARG_ABC 456
FOUND=$(env | grep ARG_ABC || echo)
assert $FOUND "ARG_ABC=456"
}
function test_truthy_override() {
AOPT_TRUTHY=1
source minimist.sh -ab --charlie
assert $HAD_ERROR N
assert $ARG_a 1
assert $ARG_b 1
assert $ARG_CHARLIE 1
AOPT_TRUTHY=y
}
function test_invalid_key() {
source minimist.sh '-wz -f'
assert $HAD_ERROR Y
assert $ARG_w "y"
assert $ARG_z "y"
assert $ARG_f "y"
}
function test_spaces_in_key() {
source minimist.sh "--test thing=bla"
assert $HAD_ERROR N
assert "$ARG_TEST_THING" "bla"
}
function test_spaces_in_value() {
source minimist.sh "--test=bla bla"
assert $HAD_ERROR N
assert "$ARG_TEST" "bla bla"
}
function test_sanitizing_key() {
source minimist.sh "--test-bla=bla"
assert $HAD_ERROR N
assert "$ARG_TEST_BLA" "bla"
}
function test_sanitizing_non_alphanum() {
source minimist.sh "--test#123-*=abc @@23"
assert $HAD_ERROR N
assert "$ARG_TEST_123__" "abc @@23"
}
function test_argument_validate_succeeds() {
AOPT_VALID_ARGS=("verbose")
source minimist.sh --verbose
assert $HAD_ERROR N
unset AOPT_VALID_ARGS
}
function test_argument_validate_succeeds_case() {
AOPT_VALID_ARGS=("verbose")
source minimist.sh --VeRbose
assert $HAD_ERROR N
unset AOPT_VALID_ARGS
}
function test_argument_validate_fails() {
AOPT_VALID_ARGS=("verbose")
source minimist.sh --bla
assert $HAD_ERROR Y
unset AOPT_VALID_ARGS
}
function test_flags_validate_succeeds() {
AOPT_VALID_ARGS=("a" "b" "c")
source minimist.sh -ab -c
assert $HAD_ERROR N
unset AOPT_VALID_ARGS
}
function test_flags_validate_fails_missing() {
AOPT_VALID_ARGS=("a" "b" "c")
source minimist.sh -abq -c
assert $HAD_ERROR Y
unset AOPT_VALID_ARGS
}
function test_flags_validate_fails_case() {
AOPT_VALID_ARGS=("a" "b" "c")
source minimist.sh -ab -C
assert $HAD_ERROR Y
unset AOPT_VALID_ARGS
}
if [[ -z $1 ]]; then
runtest test_basic
runtest test_positional
runtest test_positional_override
runtest test_duplicate_definitions
runtest test_empty_equals
runtest test_full_equals
runtest test_no_export
runtest test_export
runtest test_truthy_override
runtest test_invalid_key
runtest test_spaces_in_key
runtest test_spaces_in_value
runtest test_sanitizing_key
runtest test_sanitizing_non_alphanum
runtest test_argument_validate_succeeds
runtest test_argument_validate_succeeds_case
runtest test_argument_validate_fails
runtest test_flags_validate_succeeds
runtest test_flags_validate_fails_missing
runtest test_flags_validate_fails_case
else
eval "runtest $1"
fi
if [[ $ERROR_COUNT -gt 0 ]]; then
echo "There were $ERROR_COUNT errors"
exit 1
else
echo "Tests finished successfully"
fi