-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutest.sh
executable file
·2098 lines (1817 loc) · 65.6 KB
/
utest.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
#!/usr/bin/env bash
VERSION="1.9.5"
IFS=$'\n'
log_container=" [[ UTEST version ${VERSION} ]] \n\n"
flag_log=false
flag_never_rm=false
function log {
if [[ "$flag_log" = "true" ]]; then
time=$(date "+%H:%M:%S")
message="[${time}] ${1}"
log_container="${log_container}\n${message}"
fi
}
function flushlog {
if [[ "$flag_log" = "true" ]]; then
log "Flush log to the file."
printf "$log_container" > utest.log
fi
}
# Dependencies
#
#
# YAML parsing in bash
# Based on https://gist.github.com/pkuczynski/8665367
# And on https://gist.github.com/epiloque/8cf512c6d64641bde388
#
#
#
# Usage: parse_yaml
# <file>
# [optional prefix]
# [optional string "true" -> then it sets all values to ""]
# [optional string "true" -> use IFS=|next|]
#
parse_yaml() {
log "Load YAML file \"${1}\""
#unset IFS
IFS=$'\n'
local prefix=$2
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_]*'
fs="$(echo @|tr @ '\034')"
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
if [[ "$3" = "true" ]]; then
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'"$prefix"'",vn, $2, "");
}
}' | sed 's/_=/+=/g'
else
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=(\"%s%s\")\n", "'"$prefix"'",vn, $2, $3, "");
}
}' | sed 's/_=/+=/g'
fi
#IFS=$'\n'
}
# Helper function to generate short universal name
# for tested program
function shortname {
short_name=$(basename "$1" | tr . _)
printf "$short_name"
}
#
#
# Code based on spinner.sh by Tasos Latsas
#
#
# Author: Tasos Latsas
# spinner.sh
#
# Display an awesome 'spinner' while running your long shell commands
#
# Do *NOT* call _spinner function directly.
# Use {start,stop}_spinner wrapper functions
# usage:
# 1. source this script in your's
# 2. start the spinner:
# start_spinner [display-message-here]
# 3. run your command
# 4. stop the spinner:
# stop_spinner [your command's exit status]
#
# Also see: test.sh
function _spinner() {
# $1 start/stop
#
# on start: $2 display message
# on stop : $2 process exit status
# $3 spinner function pid (supplied from stop_spinner)
local on_success=""
local on_fail=""
local white="\e[1;37m"
local green="\e[1;32m"
local red="\e[1;31m"
local nc="\e[0m"
case $1 in
start)
if [[ -z ${3} ]]; then
let column=1
# start spinner
i=1
sp='\|/-'
delay=${SPINNER_DELAY:-0.15}
printf "\n "
while :
do
printf "\b${sp:i++%${#sp}:1}"
sleep $delay
done
else
sleep 0
fi
;;
stop)
if [[ -z ${3} ]]; then
sleep 0
else
kill -9 $3 > /dev/null 2>&1
while kill -0 $3 2>/dev/null; do sleep 0.005; done
sleep 0.005
printf "\b\b\b \b\b\b "
fi
;;
*)
echo "invalid argument, try {start/stop}"
exit 1
;;
esac
}
# Disable spinner
spinner_is_running=false
flag_use_spinner=true
function start_spinner {
if [[ "${flag_use_spinner}|${flag_no_builtin_outputs}" = "true|false" ]]; then
if [[ "$spinner_is_running" = "false" ]]; then
spinner_is_running=true
# $1 : msg to display
_spinner "start" "${1}" &
# set global spinner pid
_sp_pid=$!
disown
fi
fi
}
function stop_spinner {
if [[ "$spinner_is_running" = "true" ]]; then
spinner_is_running=false
# $1 : command exit status
_spinner "stop" $1 $_sp_pid
unset _sp_pid
fi
}
function sbusy {
start_spinner "$1"
}
function sready {
stop_spinner 0
}
sbusy ""
#
#
# UTEST.SH
#
#
#
# General purpose awesome testing-script
# Used to test program with given .in/.err files
# Or selected script
#
# Usage: type test.sh --help to get some info
#
#
# @Piotr Styczy?ski 2017
#
flag_formating=term
flag_out_path=./out
flag_err_path=./out
flag_force=false
flag_auto_test_creation=false
flag_skip_ok=false
flag_always_continue=true
flag_skip_summary=false
flag_minimal=false
flag_very_minimal=false
flag_extreamely_minimalistic=false
flag_always_need_good_err=false
flag_good_err_path=
input_prog_flag_acc=
flag_default_require_err_emptyness=false
flag_always_ignore_stderr=false
flag_test_out_script=
flag_test_err_script=
flag_out_path=./test_out_temp
flag_err_path=./test_out_temp
flag_err_temp=true
flag_out_temp=true
file_count=0
flag_tools=
flag_no_builtin_outputs="false"
flag_testing_programs=
flag_additional_test_name_info=
flag_pipe_input=()
flag_pipe_output=()
flag_pipe_err_output=()
param_cwd=""
flag_hook_init=()
flag_hook_deinit=()
flag_hook_test_case_start=()
flag_hook_test_case_finish=()
flag_hook_test_case_fail_err=()
flag_hook_test_case_fail_out=()
flag_hook_test_case_fail=()
flag_hook_test_case_success=()
# Should be changed
flag_no_pipes="false"
flag_full_in_path_in_desc="false"
flag_override_good_out_file=
flag_override_good_err_file=
C_RED=$(printf "\e[1;31m")
C_GREEN=$(printf "\e[1;32m")
C_BLUE=$(printf "\e[1;34m")
C_CYAN=$(printf "\e[1;36m")
C_PURPLE=$(printf "\e[1;35m")
C_YELLOW=$(printf "\e[1;33m")
C_GRAY=$(printf "\e[1;30m")
C_NORMAL=$(printf "\e[0m")
C_BOLD=$(printf "\e[1m")
B_DEBUG=
E_DEBUG=
B_ERR=
E_ERR=
B_INFO=
E_INFO=
B_WARN=
E_WARN=
B_BOLD=
E_BOLD=
B_OK=
E_OK=
bdebug="\${B_DEBUG}"
edebug="\${E_DEBUG}"
berr="\${B_ERR}"
eerr="\${E_ERR}"
binfo="\${B_INFO}"
einfo="\${E_INFO}"
bwarn="\${B_WARN}"
ewarn="\${E_WARN}"
bbold="\${B_BOLD}"
ebold="\${E_BOLD}"
bok="\${B_OK}"
eok="\${E_OK}"
TEXT_OK="OK"
# TODO REMOVE
#sleep 1
#sready
#exit 22
function stdout {
if [[ "$flag_no_builtin_outputs" = "false" ]]; then
printf $@
fi
}
function stdoutplain {
if [[ "$flag_no_builtin_outputs" = "false" ]]; then
echo -en "$@"
fi
}
function clean_temp_content {
log "Clean temp content..."
if [[ "$flag_never_rm" = "false" ]]; then
if [[ ${flag_out_temp} = 'true' ]]; then
log "Clean temp out:\n rm -f -r $flag_out_path/*"
if [[ ! "$flag_out_path" = "" ]]; then
rm -f -r $flag_out_path/*.piped
rm -f -r $flag_out_path/*.out
rm -f -r $flag_out_path/*.err
else
log "ERROR Try to remove empty flag out path recursively! :(("
fi
fi
if [[ ${flag_err_temp} = 'true' ]]; then
log "Clean temp err:\n rm -f -r $flag_err_path/*"
if [[ ! "$flag_err_path" = "" ]]; then
rm -f -r $flag_err_path/*.piped
rm -f -r $flag_err_path/*.out
rm -f -r $flag_err_path/*.err
else
log "ERROR Try to remove empty flag err path recursively! :(("
fi
fi
else
log "Cleanup blocked (never rm flag is set)"
fi
log "Cleanup done."
}
function clean_temp {
log "Clean temp files..."
if [[ "$flag_never_rm" = "false" ]]; then
if [[ ${flag_out_temp} = 'true' ]]; then
log "Clean temp out:\n rm -f -r $flag_out_path"
if [[ ! "$flag_out_path" = "" ]]; then
if [[ ! "$flag_out_path" = "/" ]]; then
rm -f -r $flag_out_path
fi
fi
fi
if [[ ${flag_err_temp} = 'true' ]]; then
log "Clean temp err:\n rm -f -r $flag_err_path"
if [[ ! "$flag_err_path" = "" ]]; then
if [[ ! "$flag_out_path" = "/" ]]; then
rm -f -r $flag_err_path
fi
fi
fi
else
log "Cleanup blocked (never rm flag is set)"
fi
log "Cleanup done."
}
function close {
run_hook "deinit"
sready $1
log "Close (status=${1})"
flushlog
exit $1
}
function print_help {
sready
log "Display help."
echo -e "--- utest.sh VERSION ${VERSION}v ---\n\n"
printf "General purpose awesome testing-script v. $VERSION\n\n"
printf "Usage:\n"
printf " test [test_flags] <prog> <dir> [prog_flags]\n"
printf " <prog> is path to the executable, you want to test\n"
printf " <dir> is the path to folder containing .in/.out files\n"
printf " [prog_flags] are optional conmmand line argument passed to program <prog>\n"
printf " [test_flags] are optional flags for test script\n"
printf " All available [test_flags] are:\n"
printf " --tdebug - Turns debug mode ON.\n"
printf " In debug mode no files are ever deleted!\n"
printf " Also logging with --tlog is enabled.\n"
printf " --tlog - Enable logging to the utest.log file.\n"
printf " --tsilent - Outputs nothing except for the hooks messages.\n"
printf " --ttools <tools> - set additional debug tools\n"
printf " Tools is the coma-separated array of tools names. Tools names can be as the following:\n"
printf " * size - prints the size of input file in bytes.\n"
printf " * time - prints time statistic using Unix time command.\n"
printf " * stime - measures time using bash date command (not as precise as time tool).\n"
printf " * vmemcheck - uses valgrind memcheck tools to search for application leaks.\n"
printf " * vmassif - uses valgrind massif and prints peak memory usage.\n"
printf " --tscript <script> - set output testing command as <script>\n"
printf " Script path is path to the executed script/program.\n"
printf " There exists few built-in testing scripts:\n"
printf " Use '--tscript ignore' to always assume output is OK.\n"
printf " --tscript-err <script> - set stderr output testing command as <script>\n"
printf " Script path is path to the executed script/program.\n"
printf " There exists few built-in testing scripts:\n"
printf " Use '--tscript-err ignore' to always assume sterr output is OK.\n"
printf " --tpipe-in <prog> - Run program input through additional middleware program.\n"
printf " You can add more than one piping program.\n"
printf " They are executed in order from left to the right.\n"
printf " Each program has ability to use \$input and \$output variables.\n"
printf " And should read input from \$input file and save it to \$output file.\n"
printf " For example --tpipe-in 'cp \$input \$output' to do nothing important.\n"
printf " --tpipe-out <prog> - Run program output through additional middleware program.\n"
printf " See --tpipe-in.\n"
printf " --tpipe-out-err <prog> - Run program std err output through additional middleware program.\n"
printf " See --tpipe-in.\n"
printf " --tflags - enable --t* flags interpreting at any place among command line arguments (by default flags after dir are expected to be program flags)\n"
printf " --tsty-format - use !error!, !info! etc. output format\n"
printf " --tterm-format - use (default) term color formatting\n"
printf " --tno-spinner - display no spinner\n"
printf " --tc, --tnone-format - use clean character output\n"
printf " --ts - Skip oks\n"
printf " --tierr - Always ignore stderr output.\n"
printf " --tgout <dir> - set (good) .out input directory (default is the same as dir/inputs will be still found in dir location/use when .out and .in are in separate locations)\n"
printf " --tgerr <dir> same as --tgout but says where to find good .err files (by default nonempty .err file means error)\n"
printf " --terr <dir> - set .err output directory (default is /out)\n"
printf " --tout <dir> set output .out file directory (default is /out)\n"
printf " --tf - proceed even if directories do not exists etc.\n"
printf " --tneed-err, --tnerr - Always need .err files (by default missing good .err files are ignored)\n"
printf " If --tnerr flag is used and --tgerr not specified the good .err files are beeing searched in <dir> folder.\n"
printf " --te, --tdefault-no-err - If the .err file not exists (ignored by default) require stderr to be empty\n"
printf " --tt - automatically create missing .out files using program output\n"
printf " --tn - skip after-testing summary\n"
printf " --ta - abort after +5 errors\n"
printf " -help - display this help\n"
printf " --tm - use minimalistic mode (less output)\n"
printf " --tmm - use very minimalistic mode (even less output)\n"
printf " --tmmm - use the most minimialistic mode (only file names are shown)\n"
printf " Wherever -help,--help flags are placed the script always displays its help info.\n"
printf "\n"
printf " About minimalistic modes:\n"
printf " In --tm mode OKs are not printed / erors are atill full with diff\n"
printf " In --tmm mode errors are only generally descripted / OK at output on success\n"
printf " In --tmmm only names of error files are printed / Nothing at output on success\n"
printf "\n"
}
function update_loc {
filename="$1"
folder_loc="$filename"
es_param_dir=$(echo "${param_dir}" | sed 's:/:\\\/:g')
es_folder_loc=$(echo "${folder_loc}" | sed 's:/:\\\/:g')
#printf "es_param_dir = ${es_param_dir}\n"
#printf "es_folder_loc = ${es_folder_loc}\n"
#printf " flag_good_err_path = ${flag_good_err_path}\n"
#printf " flag_good_out_path = ${flag_good_out_path}\n"
#flag_good_err_path=$(echo "${flag_good_err_path}" | sed -e 's/${es_param_dir}/'$es_folder_loc'/g')
#flag_good_out_path=$(echo "${flag_good_out_path}" | sed -e 's/${es_param_dir}/'$es_folder_loc'/g')
flag_good_err_path="${flag_good_err_path//$es_param_dir/$es_folder_loc}"
flag_good_out_path="${flag_good_out_path//$es_param_dir/$es_folder_loc}"
#printf "after: flag_good_err_path = ${flag_good_err_path}\n"
#printf "after: flag_good_out_path = ${flag_good_out_path}\n"
#printf " NOW OK\n\n"
param_dir="$filename"
log "Update locations.\n Good error path points to \"${flag_good_err_path}\"\n Good output path points to \"${flag_good_out_path}\"\n Param directory points to \"${param_dir}\""
}
function prepare_input {
log "Prepare input files..."
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
if [[ ! "${param_prog}" = "" ]]; then
if [[ "${param_dir}" = "" ]]; then
if [[ $param_prog =~ $regex ]]
then
param_dir="$param_prog"
param_prog=""
log "URL detected.\n Set param directory to point to \"${param_dir}\"."
fi
fi
fi
if [[ $param_dir =~ $regex ]]
then
log "Try to download file from \"${param_dir}\"..."
# Link is valid URL so try to download file
sready
stdout "${B_INFO}Trying to download data from provided url...${E_INFO}\n"
filename=$(curl -sI $param_dir | grep -o -E 'filename=.*$' | sed -e 's/filename=//')
if [[ "$filename" = "" ]]; then
log "Cannot obtain file name. Use default generated."
filename="downloaded_tests.zip"
fi
if [[ -f $filename ]]; then
sready
log "Downloaded file is present. Skip download."
stdout "${B_INFO}File already present. Skipping.${E_INFO}\n"
update_loc "$filename"
else
sready
stdout "${B_INFO}Download into \"${filename}\"${E_INFO}\n"
log "Download URL into path \"${filename}\"."
curl -f -L -o "$filename" $param_dir
curl_status=$?
if [ "$curl_status" -eq 0 ]; then
update_loc "$filename"
else
sready
log "Failed to download the requested file.\n Curl exit status is ${curl_status}"
stdout "${B_ERR}Could not download requested file. :(${E_ERR}\n"
close 22
fi
fi
fi
if [[ -f $param_dir ]]; then
log "Detected param directory is a file."
folder_loc=${param_dir%%.*}
if [[ ! -d "$folder_loc" ]]; then
sready
log "Try to unzip file \"${folder_loc}\"."
stdout "${B_INFO}Test input is zip file -- needs unzipping...${E_INFO}\n"
stdout "${B_INFO}This may take a while...${E_INFO}\n"
mkdir "$folder_loc"
unzip -q "$param_dir" -d "$folder_loc"
unpackage_status=$?
if [[ ! "$unpackage_status" = "0" ]]; then
log "Try to unrar file \"${folder_loc}\"."
unrar "$param_dir" "$folder_loc"
unpackage_status=$?
fi
if [[ ! "$unpackage_status" = "0" ]]; then
log "Try to unpack file (using unp) \"${folder_loc}\"."
unp "$param_dir" "$folder_loc"
unpackage_status=$?
fi
fi
update_loc "$folder_loc"
# USE AUTOFIND
best_test_dir=$(autofind_tests "$folder_loc")
if [[ ${best_test_dir} != '' ]]; then
log "Autodected ${best_test_dir} as best testing directory.\n Using it. :)"
sready
stdout "${B_DEBUG}Autodected \'$best_test_dir\' as best test directory. Using it.${E_DEBUG}\n"
update_loc "$best_test_dir"
else
update_loc "$folder_loc"
fi
fi
# Look for utest.yaml inside test directory
log "Look for utest.yaml inside test directory..."
if [[ -f "${param_dir}/utest.yaml" ]]; then
if [[ -f "./utest.yaml" ]]; then
log "Found utest.yaml but it's already present in cwd so skipping!"
stdout "${B_DEBUG}Found utest.yaml inside tests folder. But yaml file is already present.${E_DEBUG}\n"
else
log "Found utest.yaml so copy it :)"
stdout "${B_DEBUG}Found utest.yaml inside tests folder. Copy it.${E_DEBUG}\n"
cp -n "${param_dir}/utest.yaml" "./utest.yaml"
load_global_configuration_file
fi
else
log "Did not found utest.yaml in the test folder (\"${param_dir}/utest.yaml\")"
fi
}
function autofind_tests {
# USE AUTOFIND
log "Try to use autofind functionality..."
best_test_dir=$(find "$1" -maxdepth 3 -type f -name "**.in" -printf '%h\n' | sort | uniq -c | sort -k 1 -r | awk '{print $2}' | head -n 1 | tr -d "[:cntrl:]")
log "Autofind selected:\n ${best_test_dir}"
printf "$best_test_dir"
}
function verify_args {
log "Veryfying provided parameters"
if [[ ${flag_force} = 'false' ]]; then
prog_use_autodetect=false
prog_autodetect_rel_path=.
if [[ $param_prog = '' ]]; then
log "Param prog is empty trigger AUTODETECTION."
prog_use_autodetect=true
fi
if [[ -d $param_prog ]]; then
log "Param prog is directory trigger AUTODETECTION with hint \"${param_prog}\"."
prog_use_autodetect=true
prog_autodetect_rel_path="$param_prog"
fi
if [[ $prog_use_autodetect = 'true' ]]; then
log "Run executable autodetection..."
possible_executables=$(find "$prog_autodetect_rel_path" -perm /u=x,g=x,o=x -type f -printf "%d %p\n" | sort -n| head -n 3 | awk '{print $2}')
log "Possible executables are:\n ${possible_executables}"
#possible_executables=$(while read -r line; do
# stat -c '%Y %n' "$line"
#done <<< "$possible_executables" | sort -n | cut -d ' ' -f2)
if [[ $param_prog = '' ]]; then
log "Print possible executables to the output."
sready
stdout "${B_ERR}Tested program name was not given. (parameter <prog> is missing)${E_ERR}\n"
stdout "${B_DEBUG}Possible executables to test:\n\n$possible_executables"
stdout "\n\n${B_ERR}Usage: test <prog> <input_dir> [flags]${E_ERR}\n"
stdout "${B_DEBUG}Use -f option to forcefully proceed.${E_DEBUG}\n"
clean_temp
close 1
else
log "Automatically proceed to testing autodetected programs..."
param_prog=$(echo "$possible_executables" | head -n 1)
log "Automatically using program \"${param_prog}\"..."
sready
stdout "${B_DEBUG}Autodected \'$param_prog\' as best test program. Using it.${E_DEBUG}\n"
fi
fi
if [[ $param_dir = '' ]]; then
# USE AUTOFIND
log "Param directory was not given so use AUTODETECTION."
best_test_dir=$(autofind_tests ".")
log "Best testing directory was determined to be \"${best_test_dir}\"."
if [[ ${best_test_dir} = '' ]]; then
log "AUTODETECTION returned nothing so exit."
sready
stdout "${B_ERR}Input directory was not given. (parameter <input_dir> is missing)${E_ERR}\n"
stdout "${B_ERR}Usage: test <prog> <input_dir> [flags]${E_ERR}\n"
stdout "${B_DEBUG}Use -f option to forcefully proceed.${E_DEBUG}\n"
clean_temp
close 1
else
log "AUTODETECTION returned some result so proceed."
sready
#printf "${B_WARN}Input directory was not given. (parameter <input_dir> is missing)${E_WARN}\n"
stdout "${B_DEBUG}Autodected \'$best_test_dir\' as best test directory. Using it.${E_DEBUG}\n"
param_dir="$best_test_dir"
if [[ "$flag_good_out_path" = "" ]]; then
flag_good_out_path="$param_dir"
fi
log "Setup autodetected paths.\n Now param directory points to \"${param_dir}\".\n And good output path points to \"${flag_good_out_path}\""
fi
#sready
#printf "${B_ERR}Input directory was not given. (parameter <input_dir> is missing)${E_ERR}\n"
#printf "${B_ERR}Usage: test <prog> <input_dir> [flags]${E_ERR}\n"
#printf "${B_DEBUG}Use -f option to forcefully proceed.${E_DEBUG}\n"
#clean_temp
#close 1
fi
if [[ -d $param_dir ]]; then
echo -en ""
else
log "Param directory is not a directory! :("
sready
log "Interpret directory as regex"
regex_file_list=$(find -regextype posix-extended -regex "${param_dir}" -print0)
if [[ "$regex_file_list" != "" ]]; then
echo -en ""
log "Regex returned results :)"
else
log "Regex failed to return anything"
log "Interpret directory as globbing pattern"
regex_file_list=$(ls -v $param_dir 2> /dev/null)
if [[ "$regex_file_list" != "" ]]; then
echo -en ""
log "Globbing returned results :)"
else
log "Globbing failed."
log "No more directory interpretations. FAIL"
stdout "${B_ERR}Input directory \"$param_dir\" does not exists.${E_ERR}\n"
stdout "${B_DEBUG}Use -f option to forcefully proceed.${E_DEBUG}\n"
clean_temp
close 1
fi
fi
fi
fi
if [[ ${flag_always_need_good_err} = 'true' ]]; then
log "Need good error is set to true so inherit good err path from param directory."
if [[ ${flag_good_err_path} = '' ]]; then
flag_good_err_path=$param_dir
fi
fi
}
function set_format {
log "Set output format to { ${flag_formating} }."
if [[ ${flag_formating} = 'sty' ]]; then
B_DEBUG="!debug!"
E_DEBUG="!normal!"
B_ERR="!error!"
E_ERR="!normal!"
B_INFO="!info!"
E_INFO="!normal!"
B_WARN="!warn!"
E_WARN="!normal!"
B_BOLD="!bold!"
E_BOLD="!normal!"
B_OK="!ok!"
E_OK="!normal!"
fi
if [[ ${flag_formating} = 'term' ]]; then
B_DEBUG=$C_GRAY
E_DEBUG=$C_NORMAL
B_ERR=$C_RED
E_ERR=$C_NORMAL
B_INFO=$C_BLUE
E_INFO=$C_NORMAL
B_WARN=$C_YELLOW
E_WARN=$C_NORMAL
B_BOLD=$C_BOLD
E_BOLD=$C_NORMAL
B_OK=$C_GREEN
E_OK=$C_NORMAL
fi
}
function clean_out_err_paths {
log "Clean output error paths."
log " mkdir $flag_out_path"
log " mkdir $flag_err_path"
log " rm -f -r $flag_out_path/*"
log " rm -f -r $flag_err_path/*"
mkdir -p $flag_out_path
mkdir -p $flag_err_path
if [[ "$flag_never_rm" = "false" ]]; then
rm -f -r $flag_out_path/*
rm -f -r $flag_err_path/*
else
log "Removal blocked (flag never rm is set up)."
fi
}
function collect_testing_programs {
log "Collecting testing programs..."
testing_programs_list_str="$param_prog"
IFS=','
flag_testing_programs_len=0
for testing_prog in $testing_programs_list_str
do
param_prog="$testing_prog"
find_testing_program
log "Collected \"${testing_prog}\" as \"${param_prog}\""
flag_testing_programs[${flag_testing_programs_len}]=$param_prog
flag_testing_programs_len=$((flag_testing_programs_len+1))
done
unset IFS
IFS=$'\n'
log "Collected all programs."
param_prog="$testing_programs_list_str"
}
function find_testing_program {
log "Find testing program with hint \"${param_prog}\"..."
command -v "$param_prog" >/dev/null 2>&1
if [ "$?" != "0" ]; then
log "Program is not a valid command"
command -v "./$param_prog" >/dev/null 2>&1
if [ "$?" != "0" ]; then
log "Program is not a valid \"./NAME\" style command"
command -v "./$param_prog.exe" >/dev/null 2>&1
if [ "$?" != "0" ]; then
log "Program is not a valid \"./NAME.exe\" style command"
command -v "./$param_prog.app" >/dev/null 2>&1
if [ "$?" != "0" ]; then
log "Program is not a valid \"./NAME.app\" style command"
command -v "./$param_prog.sh" >/dev/null 2>&1
if [ "$?" != "0" ]; then
log "Program is not a valid \"./NAME.sh\" style command"
log "No more options end testing and override no settings. :("
#sready
#printf "${B_ERR}Invalid program name: ${param_prog}. Program not found.${E_ERR}\n";
#printf "${B_ERR}Please verify if the executable name is correct.${E_ERR}"
#clean_temp
#close 1
nothingthere=""
else
param_prog=./$param_prog.sh
fi
else
param_prog=./$param_prog.app
fi
else
param_prog=./$param_prog.exe
fi
else
param_prog=./$param_prog
fi
fi
}
function count_input_files {
log "Try to count input files..."
# Count input files
unset IFS
file_count=0
for input_file_path in $input_file_list
do
file_count=$((file_count+1))
done
log "Counted ${file_count} files."
IFS=$'\n'
}
function find_input_files {
log "Try to find input files for \"${param_dir}\"..."
regex_file_matching="false"
#
# input file list sorted by ascending file size
#
#input_file_list=`ls -vhS $param_dir/*.in | tr ' ' '\n'|tac|tr '\n' ' '`
log "Find ${param_dir}/*.in"
input_file_list=$(ls -v $param_dir/*.in 2> /dev/null)
input_file_list_err_code=$?
if [[ "$input_file_list_err_code" != "0" ]]; then
#
# Obtain file list by using globs
#
log "Find ${param_dir}"
input_file_list=$(ls -v $param_dir 2> /dev/null)
input_file_list_err_code=$?
regex_file_matching="true"
fi
if [[ "$input_file_list_err_code" != "0" ]]; then
#
# Obtain file list by using regexes
#
log "Find regex ${param_dir}"
input_file_list=$(find -regextype posix-extended -regex "${param_dir}" -printf "%p ")
input_file_list_err_code=$?
regex_file_matching="true"
fi
}
function run_testing {
log "Run testing..."
sbusy
file_index=1
err_index=0
ok_index=0
warn_index=0
not_exists_index=0
not_exists_but_created_index=0
tooling_additional_test_info=
unset IFS
for input_file_path in $input_file_list
do
prog_iter=0
IFS=$'\n'
while [ $prog_iter -lt $flag_testing_programs_len ];
do
sbusy
prog=${flag_testing_programs[${prog_iter}]}
log "Tested input ${input_file_path} for program ${prog}"
#echo "|===> Prog ${prog}"
if [ $flag_testing_programs_len -gt 1 ]; then
flag_additional_test_name_info="${B_INFO} ${prog} ${E_INFO}"
else
flag_additional_test_name_info=""
fi
#printf "Evaluated prog is -> $param_prog_eval\n"
if [[ -e $input_file_path ]]; then
#
# When we use regexes
#
if [[ "$regex_file_matching" = "true" ]]; then
if [[ "$flag_good_out_path" = "$param_dir" ]]; then
flag_good_out_path=$(dirname "$input_file_path")
fi
if [[ "$flag_good_err_path" = "$param_dir" ]]; then
flag_good_err_path=$(dirname "$input_file_path")
fi
fi
param_prog="$prog"
#TEST_RESULTS
input_file=$(basename $input_file_path)
input_file_name=${input_file/.in/}
input_file_folder=$(dirname "$input_file_path")
# If input file name does not contain .in extension
if [[ "${input_file/.in/}" = "$input_file" ]]; then
input_file_name=${input_file}
fi
#
# Parse dynamic paths
#
flag_good_out_path_unparsed=$flag_good_out_path
flag_good_err_path_unparsed=$flag_good_err_path
flag_good_out_path=$(evalspecplain "$flag_good_out_path")
flag_good_err_path=$(evalspecplain "$flag_good_err_path")
if [[ "$flag_good_out_path" != "$flag_good_out_path_unparsed" ]]; then
good_out_path="$flag_good_out_path"
else
good_out_path=$flag_good_out_path_unparsed
fi
if [[ "$flag_good_err_path" != "$flag_good_err_path_unparsed" ]]; then
good_err_path="$flag_good_err_path"
else
good_err_path=$flag_good_err_path_unparsed
fi
if [[ ! -f "$good_out_path" ]]; then
good_out_path=$flag_good_out_path/${input_file/.in/.out}
fi
if [[ ! -f "$good_err_path" ]]; then
good_err_path=$flag_good_err_path/${input_file/.in/.err}
fi
if [[ ! -f "$out_path" ]]; then
out_path=$flag_out_path/${input_file/.in/.out}
fi
if [[ ! -f "$err_path" ]]; then
err_path=$flag_err_path/${input_file/.in/.err}
fi
single_test_configuration_file_path=${input_file_path/.in/.config.yaml}
# If input file name does not contain .in extension
if [[ "${input_file/.in/}" = "$input_file" ]]; then
if [[ ! -f "$good_out_path" ]]; then
good_out_path=$flag_good_out_path/${input_file}.out
fi
if [[ ! -f "$good_err_path" ]]; then
good_err_path=$flag_good_err_path/${input_file}.err
fi
if [[ ! -f "$out_path" ]]; then
out_path=$flag_out_path/${input_file}.out
fi
if [[ ! -f "$err_path" ]]; then
err_path=$flag_err_path/${input_file}.err
fi
single_test_configuration_file_path=${input_file_path}.config.yaml
fi
param_prog="$prog"
param_prog_eval=$(evalspecplain "$param_prog")
param_prog="$param_prog_eval"
# Load test configuration
return_buffer=""