-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests
executable file
·180 lines (133 loc) · 5.93 KB
/
tests
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
#!/bin/bash
cd "$(dirname "$0")"
TEST_TOTAL=0 TEST_FAIL=0 TEST_SKIP=0
function test_record() { # message
if [[ $? = 0 ]]; then
(( TEST_TOTAL++ ))
echo -n .
else
(( TEST_TOTAL++, TEST_FAIL++ ))
echo $'x\n'"$1"
fi
}
function test_skip() { # count message
(( TEST_TOTAL += "$1", TEST_SKIP += "$1" ))
echo $'\n'"skipping $1 tests: $2"
return 1
}
function expect_equal() { # message expected actual
[[ "$3" = "$2" ]]; test_record "$1: expected '$2' but got '$3'"
}
function expect_not_equal() { # message unexpected actual
[[ "$3" != "$2" ]]; test_record "$1: unexpectedly got '$3'"
}
###################################################################################################
source "./aws-session"
echo "System information:"
echo -n "- uname: " && uname -a
echo "- bash: $BASH_VERSION"
have_cmd convert && echo -n "- convert: " && convert --version | head -1
have_cmd sips && echo -n "- sips: " && sips --help | head -1
###################################################################################################
### call
function _reply_return() { REPLY="$1"; return $2; }
function _reply_return_indirect { call REPLY=_reply_return "($1)" "$(( $2 + 1))"; }
function test_call() {
local foo=prev value=' " result * `date`'
call foo=_reply_return "$value" 23
expect_equal "return code" 23 $?
expect_equal "return value" "$value" "$foo"
call foo=_reply_return_indirect "text" 42
expect_equal "return code" 43 $?
expect_equal "return value" "(text)" "$foo"
}
test_call
### basenm / dirnm
function test_basename_dirname() {
local actual expected actual_rc expected_rc
expected="$(command basename "$1")"; expected_rc=$?
call actual=basenm "$1"; actual_rc=$?
expect_equal "basename '$1' output" "$expected" "$actual"
expect_equal "basename '$1' rc" "$expected_rc" "$actual_rc"
expected="$(command dirname "$1")"; expected_rc=$?
call actual=dirnm "$1"; actual_rc=$?
expect_equal "dirname '$1' output" "$expected" "$actual"
expect_equal "dirname '$1' rc" "$expected_rc" "$actual_rc"
}
for t in /aaa/bbb/ / // /aaa aaa aaa/ aaa/xxx aaa// a//b//c// ' a // b // c //' ''; do
test_basename_dirname "$t"
done
### normalize
function test_normalize() { # path expected
local actual rc; call actual=normalize "$1"; rc=$?
expect_equal "normalize($1) rc" 0 "$rc"
expect_equal "normalize($1)" "$2" "$actual"
}
test_normalize ./aws-session "$PWD/aws-session"
test_normalize fixtures/../aws-session "$PWD/aws-session"
### mktmpdir
mktmpdir test
expect_equal "mktmpdir rc" 0 $?
[[ -d "$REPLY" ]]; test_record "mktmpdir did not return a directory"
function mktemp() { return 100; }
mktmpdir test
expect_equal "mktmpdir rc [mktemp failure]" 100 $?
unset -f mktemp
### parse_utciso
function test_parse_utciso() {
parse_utciso "$1"
expect_equal "parse_utciso($1) return code" "$2" $?
expect_equal "parse_utciso($1) result" "$3" "$REPLY"
}
test_parse_utciso "1970-01-01T00:00:00Z" 0 0
test_parse_utciso "2005-03-18T01:58:31Z" 0 1111111111
test_parse_utciso "" 1
test_parse_utciso "yesterday" 1
### read_binary
expect_equal "read_binary ..." "BC-F" "$(echo ABCDEFG | { read_binary 1 2 '1/1 "%c"'; echo -n "-"; read_binary 2 1 '1/1 "%c"'; })"
### img_reorder_topdown
expect_equal "img_reorder_topdown(0)" $'c\nb\na' "$(echo $'a\nb\nc' | img_reorder_topdown 0)"
expect_equal "img_reorder_topdown(1)" $'a\nb\nc' "$(echo $'a\nb\nc' | img_reorder_topdown 1)"
### img_scale_x
expect_equal "img_scale_x(2)" 'abc' "$(echo 'aabbcc' | img_scale_x 2)"
expect_equal "img_scale_x(3)" '# #' "$(echo '### ###' | img_scale_x 3)"
expect_equal "img_scale_x(3, 2)" '## ##' "$(echo '### ###' | img_scale_x 3 2)"
### img_parse_bmp
expect_equal "img_parse_bmp(3x3-24-tdn.bmp)" $'# \n # \n## ' "$(img_parse_bmp <fixtures/3x3-24-tdn.bmp)"
expect_equal "img_parse_bmp(3x3-24-bup.bmp)" $'# \n # \n## ' "$(img_parse_bmp <fixtures/3x3-24-bup.bmp)"
expect_equal "img_parse_bmp(3x3-32-bup.bmp)" $'# \n # \n## ' "$(img_parse_bmp <fixtures/3x3-32-bup.bmp)"
### img_parse_png
if have_cmd convert || test_skip 2 "convert (ImageMagic) unavailable"; then
expect_equal "img_parse_png_convert(3x3.png)" $'# \n # \n## ' "$(img_parse_png_convert <fixtures/3x3.png)"
expect_equal "img_parse_png_convert(qr.png) | img_trim_qr" "$(cat fixtures/qr-trimmed.txt)" "$(img_parse_png_convert <fixtures/qr.png | img_trim_qr)"
fi
if have_cmd sips || test_skip 3 "sips unavailable"; then
expect_equal "img_parse_png_sips(3x3.png)" $'# \n # \n## ' "$(img_parse_png_sips <fixtures/3x3.png)"
expect_equal "img_parse_png_sips(qr.png) | img_trim_qr" "$(cat fixtures/qr-trimmed.txt)" "$(img_parse_png_sips <fixtures/qr.png | img_trim_qr)"
function mktemp() { return 100; }
img_parse_png_sips <fixtures/3x3.png >/dev/null
expect_equal "img_parse_png_sips rc [mktemp failure]" 100 $?
unset -f mktemp
fi
### img_trim_qr
expect_equal "img_trim_qr(qr.txt)" "$(cat fixtures/qr-trimmed.txt)" "$(img_trim_qr <fixtures/qr.txt)"
### aws_mfa_create
if have_cmd convert || have_cmd sips || test_skip 4 "no PNG parser available"; then
function aws() { # mock aws call
[[ "$1" = iam && "$2" = create-virtual-mfa-device ]] || return 100; shift 2
local outfile; while [[ $# -ge 2 ]]; do [[ "$1" = --outfile ]] && outfile="$2"; shift 2; done
[[ -n "$outfile" && -L "$outfile" ]] || return 101
cat fixtures/qr.png >"$outfile" && echo "mock-arn"
}
aws_mfa_create mock >/dev/null 2>/dev/null
expect_equal "aws_mfa_create(mock) rc" 0 $?
expect_equal "aws_mfa_create(mock) REPLY" mock-arn "$REPLY"
expect_equal "aws_mfa_create(mock) output" "$(img_ansi <fixtures/qr-trimmed.txt)" "$(aws_mfa_create mock)"
function mktemp() { return 100; }
aws_mfa_create mock >/dev/null 2>/dev/null
expect_equal "aws_mfa_create(mock) rc [mktemp failure]" 100 $?
unset -f aws mktemp
fi
###################################################################################################
echo $'\n'"Total tests: ${TEST_TOTAL} skipped: ${TEST_SKIP} failed: ${TEST_FAIL}"
[[ "$TEST_FAIL" -eq 0 ]]