-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.sh
executable file
·1991 lines (1733 loc) · 45.6 KB
/
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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright 2013-2017 Stuart Shelton
# Portions Copyright 2016 Daniele Borsaro
# Distributed under the terms of the GNU General Public License v2
#
# test.sh - Basic functionality checks for stdlib.sh
#
# TODO: Migrate to a bash unit-testing framework (github.com/pgrange/bash_unit?)
# Expand coverage to all non-trivial fucntions
#
export STDLIB_WANT_COLOUR=1
# {{{
# stdlib.sh should be in /usr/local/lib/stdlib.sh, which can be found as
# follows by scripts located in /usr/local/{,s}bin/...
type -pf 'dirname' >/dev/null 2>&1 || function dirname() { : ; }
declare std_LIB="stdlib.sh"
for std_LIBPATH in \
"$( dirname -- "${BASH_SOURCE:-${0:-.}}" )" \
"." \
"$( dirname -- "$( type -pf "${std_LIB}" 2>/dev/null )" )" \
"$( dirname -- "${BASH_SOURCE:-${0:-.}}" )/../lib" \
"/usr/local/lib" \
${FPATH:+${FPATH//:/ }} \
${PATH:+${PATH//:/ }}
do
if [[ -r "${std_LIBPATH}/${std_LIB}" ]]; then
break
fi
done
unset -f dirname
#
# We want the non if-then-else functionality here - the third element should be
# executed if either of the first two fail...
#
# N.B. The shellcheck 'source' option is only valid with shellcheck 0.4.0 and
# later...
#
# shellcheck disable=SC1091,SC2015
# shellcheck source=stdlib.sh
[[ -r "${std_LIBPATH}/${std_LIB}" ]] && source "${std_LIBPATH}/${std_LIB}" || {
echo >&2 "FATAL: Unable to source ${std_LIB} functions"
exit 1
}
# }}}
# Defined in stdlib.sh
# shellcheck disable=SC2154
(( std_TRACE )) && set -o xtrace
###############################################################################
#
# stdlib.sh - Code tests - Confirm correct operation of more complex functions
#
########################################################################### {{{
# N.B.: These functions are not versioned, as they aren't intended for general
# use. However, functions are free to interrogate the API version and
# may still perform version-specific tests.
#
function emktemp::test() { # {{{
local tempfile
local -i result=0
local func="std::emktemp"
# Test 1 # {{{
(
local check="Test 1"
local -i rc=0
std::emktemp tempfile
debug "${func} created file '${tempfile:-}' with no arguments"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 2 # {{{
(
local check="Test 2"
local -i rc=0
std::emktemp tempfile "${$}"
debug "${func} created file '${tempfile:-}' with template '${$}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 3 # {{{
(
local check="Test 3"
local -i rc=0
std::emktemp -var tempfile -suffix "${$}"
debug "${func} created file '${tempfile:-}' with suffix '${$}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 4 # {{{
(
local check="Test 4"
local -i rc=0
mkdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot mkdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
std::emktemp -var tempfile -tmpdir "${TMPDIR:-/tmp}/stdlib-${NAME}" -suffix "${$}"
debug "${func} created file '${tempfile:-}' with tmpdir '${TMPDIR:-/tmp}/stdlib-${NAME}' and suffix '${$}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
rmdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot rmdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 5 # {{{
(
local check="Test 5"
local -i rc=0
std::emktemp -var tempfile -suffix "${$}" -directory
debug "${func} created directory '${tempfile:-}' with suffix '${$}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no directory-name"
rc=1
else
if ! [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "Temporary directory '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "std::cleanup failed to remove directory '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 6 # {{{
(
local check="Test 6"
local -i rc=0
mkdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot mkdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
std::emktemp -var tempfile -tmpdir "${TMPDIR:-/tmp}/stdlib-${NAME}" -suffix "${$}" -directory
debug "${func} created directory '${tempfile:-}' with tmpdir '${TMPDIR:-/tmp}/stdlib-${NAME}' and suffix '${$}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no directory-name"
rc=1
else
if ! [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "Temporary directory '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "std::cleanup failed to remove directory '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
rmdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot rmdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
exit ${rc}
)
(( result += ${?} ))
# }}}
if (( result )); then
error "${FUNCNAME[0]##*_} failed test-suite"
else
info "${FUNCNAME[0]##*_} passed test-suite"
fi
# Don't stomp std_ERRNO
return ${result}
} # emktemp::test # }}}
function http::test() { # {{{
local -i ic=0 rc=0 code=0 result=0
(( STDLIB_HAVE_BASH_4 )) || {
# Defined in stdlib.sh
# shellcheck disable=SC2154
(( std_DEBUG )) && error "${FUNCNAME[0]##*_} requires bash-4 associative arrays"
std_ERRNO=$( errsymbol ENOEXE )
return 1
}
local -A codes
# 1xx Informational
codes[100]='Continue'
codes[101]='Switching Protocols'
# Non-RFC2616 Status Codes
codes[102]='Processing' # RFC2518; WebDAV
# 2xx Successful
codes[200]='OK'
codes[201]='Created'
codes[202]='Accepted'
codes[203]='Non-Authoritative Information' # Since HTTP/1.1
codes[204]='No Content'
codes[205]='Reset Content'
codes[206]='Partial Content'
# Non-RFC2616 Status Codes
codes[207]='Multi-Status' # RFC4918; WebDAV
codes[208]='Already Reported' # RFC5842; WebDAV
codes[226]='IM Used' # RFC3229; WebDAV
# 3xx Redirection
codes[300]='Multiple Choices'
codes[301]='Moved Permanently'
codes[302]='Found'
codes[303]='See Other' # Since HTTP/1.1
codes[304]='Not Modified'
codes[305]='Use Proxy' # Since HTTP/1.1
codes[306]='(Switch Proxy - Unused)'
codes[307]='Temporary Redirect'
# Non-RFC2616 Status Codes
codes[308]='Permanent Redirect' # RFC7238; Experimental
# 4xx Client Error
codes[400]='Bad Request'
codes[401]='Unauthorized'
codes[402]='Payment Required'
codes[403]='Forbidden'
codes[404]='Not Found'
codes[405]='Method Not Allowed'
codes[406]='Not Acceptable'
codes[407]='Proxy Authentication Required'
codes[408]='Request Timeout'
codes[409]='Conflict'
codes[410]='Gone'
codes[411]='Length Required'
codes[412]='Precondition Failed'
codes[413]='Request Entity Too Large'
codes[414]='Request-URI Too Long'
codes[415]='Unsupported Media Type'
codes[416]='Requested Range Not Satisfiable'
codes[417]='Expectation Failed'
# Non-RFC2616 Status Codes
codes[418]="I'm a teapot" # RFC2324
codes[419]='Authentication Timeout' # *Not* in RFC2616
codes[420]='Method Failure/Enhance Your Calm' # Spring; Deprecated/Twitter API v1.0
codes[421]='Expectation Failed'
codes[422]='Unprocessable Entity' # RFC4918; WebDAV
codes[423]='Locked' # RFC4918; WebDAV
codes[424]='Failed Dependency' # RFC4918; WebDAV
codes[426]='Upgrade Required'
codes[428]='Precondition Required' # RFC6585
codes[429]='Too Many Requests' # RFC6585
codes[431]='Request Header Fields Too Large' # RFC6585
codes[440]='Login Timeout' # Microsoft
codes[444]='No Response' # nginx
codes[449]='Retry With' # Microsoft
codes[450]='Blocked by Windows Parental Controls' # Microsoft
codes[451]='Unavailable For Legal Reasons/Redirect' # Draft/Microsoft
codes[494]='Request Header Too Large' # nginx
codes[495]='Cert Error' # nginx
codes[496]='No Cert' # nginx
codes[497]='HTTP to HTTPS' # nginx
codes[498]='Token expired/invalid' # ersi
codes[499]='Client Closed Request/Token Required' # nginx/ersi
# 5xx Server Error
codes[500]='Internal Server Error'
codes[501]='Not Implemented'
codes[502]='Bad Gateway'
codes[503]='Service Unavailable'
codes[504]='Gateway Timeout'
codes[505]='HTTP Version Not Supported'
# Non-RFC2616 Status Codes
codes[506]='Variant Also Negotiates' # RFC2295
codes[507]='Insufficient Storage' # RFC4918; WebDAV
codes[508]='Loop Detected' # RFC5842; WebDAV
codes[509]='Bandwidth Limit Exceeded' # Apache
codes[510]='Not Extended' # RFC2774
codes[511]='Network Authentication Required' # RFC6585
codes[520]='Origin Error' # CloudFlare
codes[521]='Web server is down' # CloudFlare
codes[522]='Connection timed out' # CloudFlare
codes[523]='Proxy Declined Request' # CloudFlare
codes[524]='A timeout occurred' # CloudFlare
codes[598]='Network read timeout error' # Microsoft
codes[599]='Network connect timeout error' # Microsoft
output "Testing HTTP-to-shell response-code mappings\n"
output "N.B.: non-RFC2616 status codes are expected failures\n"
std_ERRNO=0
# 'ic' and 'code' are numeric, and therefore not subject to word-splitting
# shellcheck disable=SC2086
for code in $( for ic in "${!codes[@]}"; do echo ${ic}; done | sort -n ); do
std::http::squash ${code} 2>/dev/null ; ic=${?}
rc=$( std::http::expand ${ic} )
if (( code == rc )); then
info "${code} '${codes[${code}]}' -> ${ic} -> ${rc} : Okay"
else
warn "${code} '${codes[${code}]}' -> ${ic} -> ${rc} : FAIL"
result=1
std_ERRNO=$( errsymbol EERROR )
fi
done
# Don't stomp std_ERRNO
return ${result}
} # http::test # }}}
function mktemp::test() { # {{{
local tempfile
local -i result=0
local func="std::mktemp"
if [[ -e "${TMPDIR:-/tmp}"/stdlib-"${NAME}" ]]; then
error "Cannot run test-suite if filesystem object '${TMPDIR:-/tmp}/stdlib-${NAME}' exists"
return 1
fi
# Test 1 # {{{
(
local check="Test 1"
local -i rc=0
tempfile="$( std::mktemp )"
debug "${func} created file '${tempfile:-}' with no arguments"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 2 # {{{
(
local check="Test 2"
local -i rc=0
tempfile="$( std::mktemp "stdlib-${NAME}" )"
debug "${func} created file '${tempfile:-}' with template 'stdlib-${NAME}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 3 # {{{
(
local check="Test 3"
local -i rc=0
tempfile="$( std::mktemp -suffix "${$}" "stdlib-${NAME}" )"
debug "${func} created file '${tempfile:-}' with suffix '${$}' and template 'stdlib-${NAME}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 4 # {{{
(
local check="Test 4"
local -i rc=0
mkdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot mkdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
tempfile="$( std::mktemp -tmpdir "${TMPDIR:-/tmp}/stdlib-${NAME}" -suffix "${$}" "stdlib-${NAME}" )"
debug "${func} created file '${tempfile:-}' with tmpdir ${TMPDIR:-/tmp}/stdlib-${NAME}', suffix '${$}' and template 'stdlib-${NAME}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no file-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "Temporary file '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -f "${tempfile}" ]]; then
error "std::cleanup failed to remove file '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
rmdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot rmdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 5 # {{{
(
local check="Test 5"
local -i rc=0
#local -i std_debug="${std_DEBUG}"
#std_DEBUG=2
tempfile="$( std::mktemp -directory -suffix "${$}" "stdlib-${NAME}" )"
#std_DEBUG="${std_debug}"
debug "${func} created directory '${tempfile:-}' with suffix '${$}' and template 'stdlib-${NAME}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no directory-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "Temporary directory '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "std::cleanup failed to remove directory '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
exit ${rc}
)
(( result += ${?} ))
# }}}
# Test 6 # {{{
(
local check="Test 6"
local -i rc=0
mkdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot mkdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
tempfile="$( std::mktemp -directory -tmpdir "${TMPDIR:-/tmp}/stdlib-${NAME}" -suffix "${$}" "stdlib-${NAME}" )"
debug "${func} created directory '${tempfile:-}' with tmpdir '${TMPDIR:-/tmp}/stdlib-${NAME}', suffix '${$}' and template 'stdlib-${NAME}'"
if ! [[ -n "${tempfile:-}" ]]; then
error "${func} returned no directory-name"
rc=1
else
std::garbagecollect "${tempfile}"
if ! [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "Temporary directory '${tempfile}' does not exst"
rc=1
else
(
#export std_DEBUG=2
std::cleanup 0
)
rc+=${?}
if [[ -e "${tempfile}" && -d "${tempfile}" ]]; then
error "std::cleanup failed to remove directory '${tempfile}'"
(( rc++ ))
else
info "${func} ${check}: okay"
fi
fi
fi
rmdir "${TMPDIR:-/tmp}"/stdlib-"${NAME}" || die "Cannot rmdir('${TMPDIR:-/tmp}/stdlib-${NAME}')"
exit ${rc}
)
(( result += ${?} ))
# }}}
if (( result )); then
error "${FUNCNAME[0]##*_} failed test-suite"
else
info "${FUNCNAME[0]##*_} passed test-suite"
fi
# Don't stomp std_ERRNO
return ${result}
} # mktemp::test # }}}
function output::test() { # {{{
local -i debug=${std_DEBUG:-0}
output "Colourisation test:\n"
std::colour "Message"
std::colour "Multi-word message"
output
std::colour "WARN: Warning"
std::colour "warning message"
std::colour "info information"
std::colour "information: info"
std::colour "NOTICE: Notice"
std::colour "note: Notice"
std::colour "debug debug"
std::colour "warn warn"
std::colour "warning warning"
std::colour "error error"
output
std::colour Red "Text in red specified inline"
output
std::colour -colour black "Black"
std::colour -colour red "Red"
std::colour -colour green "Green"
std::colour -colour yellow "Yellow"
std::colour -colour blue "Blue"
std::colour -colour magenta "Magenta"
std::colour -colour cyan "Cyan"
std::colour -colour white "White"
std::colour -colour $(( ( 1 << 16 ) + 0 )) "Bright Black"
std::colour -colour $(( ( 1 << 16 ) + 1 )) "Bright Red"
std::colour -colour $(( ( 1 << 16 ) + 2 )) "Bright Green"
std::colour -colour $(( ( 1 << 16 ) + 3 )) "Bright Yellow"
std::colour -colour $(( ( 1 << 16 ) + 4 )) "Bright Blue"
std::colour -colour $(( ( 1 << 16 ) + 5 )) "Bright Magenta"
std::colour -colour $(( ( 1 << 16 ) + 6 )) "Bright Cyan"
std::colour -colour $(( ( 1 << 16 ) + 7 )) "Bright White"
std::colour -colour $(( ( 4 << 16 ) + ( 3 << 8 ) + 4 )) "Underlined blue on yellow"
output
std::colour -type debug "This is debug"
std::colour -type error "This is error"
std::colour -type exec "This is exec"
std::colour -type fail "This is fail"
std::colour -type fatal "This is fatal"
std::colour -type info "This is info"
std::colour -type note "This is note"
std::colour -type notice "This is notice"
std::colour -type okay "This is okay"
std::colour -type ok "This is ok"
std::colour -type warn "This is warn"
output
std::colour -type fatal -colour yellow "!!! Yellow text following red fatal prefix"
std::colour -type okay -colour magenta "!!! Magenta text following green ok prefix"
output
std::colour -colour $(( ( 1 << 16 ) + 2 )) "Bright green"
std::colour -colour $(( 2 )) "Dim green"
output
info "Info"
notice "Notice"
note "Note"
std_DEBUG=0 debug "This should not be displayed..."
std_DEBUG=1 debug "Debug"
std_DEBUG=${debug}
warn "Warning"
error "Error"
( die "Die" )
output "\nSpacing test:"
output " 11111111112"
output "12345678901234567890"
std::colour -colour white "1 "
std::colour -colour white " 5 "
std::colour -colour white "1 5 "
std::colour -colour white " 9 "
std::colour -colour white "1 9 "
std::colour -colour white " 5 9 "
std::colour -colour white "1 5 9 "
std::colour -colour white "1 5 9 * "
std::colour -colour white "1 5 9 * + "
echo " $( std::colour -colour white "2 4 6 8 - * + ! @ #" )x"
echo -n ' ' ; echo -n "$( std::colour -colour white "2 4 6 8 - * + ! @ #" )" ; echo 'x'
echo -n "$( std::colour -colour white "1 3 5 7 9 # @ ! + *" )" ; echo ' x'
output "\nColourisation test complete"
# Defined in stdlib.sh
# shellcheck disable=SC2034
std_ERRNO=0
return 0
} # output::test # }}}
function parseargs::test() { # {{{
local response expected item1 item2 item3 unknown
local -i std_PARSEARGS_parsed=0 result=0
local -a args
# Clear any arguments we were called with...
while [[ -n "${1:-}" || -n "${*:-}" ]]; do
shift
done
local func="std::parseargs"
# Test 1 # {{{
(
local check="Test 1"
local -i rc=0 testresult=0
args=( -item1 a -item2 b -item3 c )
(( std_DEBUG )) && info "Stripping arguments from '${args[*]}' ..."
(( std_DEBUG )) && debug "$( std::parseargs --strip -- "${args[@]}" )"
eval "set -- '$( std::parseargs --strip -- "${args[@]}" )'"
response="$(
echo "x"
echo "${item1[*]:-}"
echo "${item2[*]:-}"
echo "${item3[*]:-}"
echo "${*:-}"
echo "x"
)"
rc=${?}
if (( std_DEBUG )); then
echo "item1 is '${item1:-}', '${item1[*]:-}'"
echo "item2 is '${item2:-}', '${item2[*]:-}'"
echo "item3 is '${item3:-}', '${item3[*]:-}'"
echo "unknown is '${unknown:-}', '${unknown[*]:-}'"
fi
if ! [[ -n "${std_PARSEARGS_parsed:-}" ]]; then
warn "std_PARSEARGS_parsed not set!"
else
(( std_DEBUG )) && output "std_PARSEARGS_parsed='${std_PARSEARGS_parsed}'"
fi
# Doesn't apply when stripping arguments
#(( rc )) || rc=$(( ! std_PARSEARGS_parsed ))
if (( rc )); then
error "${func} ${check} failed: ${rc}"
testresult=1
else
std::define expected <<'EOF'
x
a
b
c
x
EOF
if [[ "${response:-}" == "${expected}" ]]; then
info "${func} ${check}: okay"
else
error "${func} ${check}: failed"
info "Expected:"
output "$( grep -v '^x$' <<<"${expected}" )"
info "Received:"
output "$( grep -v '^x$' <<<"${response}" )"
testresult=1
fi
fi
exit ${testresult}
)
(( result += ${?} ))
# }}}
#output "\n"
# Test 2 # {{{
(
local check="Test 2"
local -i rc=0 testresult=0
args=( -item1 a b -item2 c d -item3 e )
(( std_DEBUG )) && info "Stripping arguments from '${args[*]}' ..."
(( std_DEBUG )) && debug "$( std::parseargs --strip -- "${args[@]}" )"
eval "set -- '$( std::parseargs --strip -- "${args[@]}" )'"
response="$(
echo "x"
echo "${item1[*]:-}"
echo "${item2[*]:-}"
echo "${item3[*]:-}"
echo "${*:-}"
echo "x"
)"
rc=${?}
if (( std_DEBUG )); then
echo "item1 is '${item1:-}', '${item1[*]:-}'"
echo "item2 is '${item2:-}', '${item2[*]:-}'"
echo "item3 is '${item3:-}', '${item3[*]:-}'"
echo "unknown is '${unknown:-}', '${unknown[*]:-}'"
fi
if ! [[ -n "${std_PARSEARGS_parsed:-}" ]]; then
warn "std_PARSEARGS_parsed not set!"
else
(( std_DEBUG )) && output "std_PARSEARGS_parsed='${std_PARSEARGS_parsed}'"
fi
# Doesn't apply when stripping arguments
#(( rc )) || rc=$(( ! std_PARSEARGS_parsed ))
if (( rc )); then
error "${func} ${check} failed: ${rc}"
testresult=1
else
std::define expected <<'EOF'
x
a
b
c
d
e
x
EOF
if [[ "${response:-}" == "${expected}" ]]; then
info "${func} ${check}: okay"
else
error "${func} ${check}: failed"
info "Expected:"
output "$( grep -v '^x$' <<<"${expected}" )"
info "Received:"
output "$( grep -v '^x$' <<<"${response}" )"
testresult=1
fi
fi
exit ${testresult}
)
(( result += ${?} ))
# }}}
#output "\n"
# Test 3 # {{{
(
local check="Test 3"
local -i rc=0 testresult=0
args=( -item1 a -item2 b -item3 c )
(( std_DEBUG )) && info "Argument-parsing '${args[*]}' ..."
(( std_DEBUG )) && debug "$( std::parseargs --single --permissive --var unknown -- "${args[@]}" )"
eval "$( std::parseargs --single --permissive --var unknown -- "${args[@]}" )"
response="$(
echo "x"
echo "${item1[*]:-}"
echo "${item2[*]:-}"
echo "${item3[*]:-}"
echo "${unknown[*]:-}"
echo "x"
)"
rc=${?}
if (( std_DEBUG )); then
echo "item1 is '${item1:-}', '${item1[*]:-}'"
echo "item2 is '${item2:-}', '${item2[*]:-}'"
echo "item3 is '${item3:-}', '${item3[*]:-}'"
echo "unknown is '${unknown:-}', '${unknown[*]:-}'"
fi
if ! [[ -n "${std_PARSEARGS_parsed:-}" ]]; then
warn "std_PARSEARGS_parsed not set!"
else
(( std_DEBUG )) && output "std_PARSEARGS_parsed='${std_PARSEARGS_parsed}'"
fi
(( rc )) || rc=$(( ! std_PARSEARGS_parsed ))
if (( rc )); then
error "${func} ${check} failed: ${rc}"
testresult=1
else
std::define expected <<'EOF'
x
a
b
c
x
EOF
if [[ "${response:-}" == "${expected}" ]]; then
info "${func} ${check}: okay"
else
error "${func} ${check}: failed"
info "Expected:"
output "$( grep -v '^x$' <<<"${expected}" )"
info "Received:"
output "$( grep -v '^x$' <<<"${response}" )"
testresult=1
fi
fi
exit ${testresult}
)
(( result += ${?} ))
# }}}
#output "\n"
# Test 4 # {{{
(
local check="Test 4"
local -i rc=0 testresult=0
args=( -item1 a b -item2 c d -item3 e )
(( std_DEBUG )) && info "Argument-parsing '${args[*]}' with --single ..."
(( std_DEBUG )) && debug "$( std::parseargs --single --permissive --var unknown -- "${args[@]}" )"
eval "$( std::parseargs --single --permissive --var unknown -- "${args[@]}" )"
response="$(
echo "x"
echo "${item1[*]:-}"
echo "${item2[*]:-}"
echo "${item3[*]:-}"
echo "${unknown[*]:-}"
echo "x"
)"
rc=${?}
if (( std_DEBUG )); then
echo "item1 is '${item1:-}', '${item1[*]:-}'"
echo "item2 is '${item2:-}', '${item2[*]:-}'"
echo "item3 is '${item3:-}', '${item3[*]:-}'"
echo "unknown is '${unknown:-}', '${unknown[*]:-}'"
fi
if ! [[ -n "${std_PARSEARGS_parsed:-}" ]]; then