-
Notifications
You must be signed in to change notification settings - Fork 7
/
tester.sh
executable file
·491 lines (472 loc) · 14.4 KB
/
tester.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
#!/bin/bash
# Change if you store the tester in another PATH
export MINISHELL_PATH=./
export EXECUTABLE=minishell
RUNDIR=$HOME/42_minishell_tester
NL=$'\n'
TAB=$'\t'
TEST_COUNT=0
TEST_KO_OUT=0
TEST_KO_ERR=0
TEST_KO_EXIT=0
TEST_OK=0
FAILED=0
ONE=0
TWO=0
THREE=0
GOOD_TEST=0
LEAKS=0
TESTFILES=""
COMMAND=$1
main() {
while [ -n "$2" ]
do
case $2 in
"builtins" | "b")
TESTFILES+="${RUNDIR}/cmds/mand/1_builtins.sh"
;;
"parsing" | "pa")
TESTFILES+=" ${RUNDIR}/cmds/mand/0_compare_parsing.sh"
TESTFILES+=" ${RUNDIR}/cmds/mand/10_parsing_hell.sh"
;;
"redirections" | "r")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_redirs.sh"
;;
"pipelines" | "pi")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_pipeline.sh"
;;
"cmds" | "c")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_scmd.sh"
;;
"variables" | "v")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_variables.sh"
;;
"corrections" | "co")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_corrections.sh"
;;
"path")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_path_check.sh"
;;
"syntax" | "s")
TESTFILES+=" ${RUNDIR}/cmds/mand/1_syntax_errors.sh"
;;
esac
shift
done
set "$COMMAND"
if [[ ! -f $MINISHELL_PATH/$EXECUTABLE ]] ; then
echo -e "\033[1;31m# **************************************************************************** #"
echo "# MINISHELL NOT COMPILED #"
echo "# TRY TO COMPILE ... #"
echo -e "# **************************************************************************** #\033[m"
make -C $MINISHELL_PATH
if [[ ! -f $MINISHELL_PATH/$EXECUTABLE ]] ; then
echo -e "\033[1;31mCOMPILING FAILED\033[m" && exit 1
fi
fi
if [[ $1 == "m" ]] ; then
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
echo -e " 🚀 \033[1;34mMANDATORY\033[m 🚀"
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
test_mandatory
elif [[ $1 == "vm" ]] ; then
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
echo -e " 🚀 \033[1;34mMANDATORY_LEAKS\033[m 🚀"
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
test_mandatory_leaks
elif [[ $1 == "ne" ]] ; then
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
echo -e " 🚀 \033[1;34mNO_ENV\033[m 🚀"
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
test_no_env
elif [[ $1 == "b" ]] ; then
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
echo -e " 🚀 \033[1;34mBONUS\033[m 🚀"
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
test_bonus
elif [[ $1 == "a" ]] ; then
test_mandatory
test_bonus
elif [[ $1 == "d" ]] ; then
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
echo -e " 🚀 \033[1;34mMINI_DEATH\033[m 🚀"
echo " 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀"
test_mini_death
elif [[ $1 == "-f" ]] ; then
[[ ! -f $2 ]] && echo "\"$2\" FILE NOT FOUND"
[[ -f $2 ]] && test_from_file $2
else
echo "usage: mstest [m,vm,ne,b,a] {builtins,b,parsing,pa,redirections,r,pipelines,pi,cmds,c,variables,v,corrections,co,path,syntax,s}..."
echo "m: mandatory tests"
echo "vm: mandatory tests with valgrind"
echo "ne: tests without environment"
echo "b: bonus tests"
echo "a: mandatory and bonus tests"
echo "d: mandatory pipe segfault test (BRUTAL)"
echo "Starting from the second argument, their can be any number of argument specified between brackets."
echo "You can test a specific part of your minishell by writing mstest vm builtins redirections"
echo "If the part list is empty, everything will be tested."
fi
if [[ $TEST_COUNT -gt 0 ]] ; then
print_stats
fi
# \_o_/ this is my ananas.jpeg \_o_/
rm -rf test
}
test_no_env() {
FILES="${RUNDIR}/cmds/no_env/*"
for file in $FILES
do
if [[ $TESTFILES =~ $file ]] || [ -z $TESTFILES ]
then
test_without_env $file
fi
done
}
test_mandatory_leaks() {
FILES="${RUNDIR}/cmds/mand/*"
for file in $FILES
do
if [[ $TESTFILES =~ $file ]] || [ -z $TESTFILES ]
then
test_leaks $file
fi
done
}
test_mandatory() {
FILES="${RUNDIR}/cmds/mand/*"
for file in $FILES
do
if [[ $TESTFILES =~ $file ]] || [ -z $TESTFILES ]
then
test_from_file $file
fi
done
}
test_mini_death() {
FILES="${RUNDIR}/cmds/mini_death/*"
for file in $FILES
do
if [[ $TESTFILES =~ $file ]] || [ -z $TESTFILES ]
then
test_from_file $file
fi
done
}
test_bonus() {
FILES="${RUNDIR}/cmds/bonus/*"
for file in $FILES
do
if [[ $TESTFILES =~ $file ]] || [ -z $TESTFILES ]
then
test_from_file $file
fi
done
}
print_stats() {
echo "🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁"
echo -e "🏁 \033[1;31mRESULT\033[m 🏁"
echo "🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁"
printf "\033[1;35m%-4s\033[m" " TOTAL TEST COUNT: $TEST_COUNT "
printf "\033[1;32m TESTS PASSED: $GOOD_TEST\033[m "
if [[ $LEAKS == 0 ]] ; then
printf "\033[1;32m LEAKING: $LEAKS\033[m "
else
printf "\033[1;31m LEAKING: $LEAKS\033[m "
fi
echo ""
echo -ne "\033[1;34m STD_OUT:\033[m "
if [[ $TEST_KO_OUT == 0 ]] ; then
echo -ne "\033[1;32m✓ \033[m "
else
echo -ne "\033[1;31m$TEST_KO_OUT\033[m "
fi
echo -ne "\033[1;36mSTD_ERR:\033[m "
if [[ $TEST_KO_ERR == 0 ]] ; then
echo -ne "\033[1;32m✓ \033[m "
else
echo -ne "\033[1;31m$TEST_KO_ERR\033[m "
fi
echo -ne "\033[1;36mEXIT_CODE:\033[m "
if [[ $TEST_KO_EXIT == 0 ]] ; then
echo -ne "\033[1;32m✓ \033[m "
else
echo -ne "\033[1;31m$TEST_KO_EXIT\033[m "
fi
echo ""
echo -e "\033[1;33m TOTAL FAILED AND PASSED CASES:"
echo -e "\033[1;31m ❌ $FAILED \033[m "
echo -ne "\033[1;32m ✅ $TEST_OK \033[m "
echo ""
}
test_from_file() {
IFS=''
i=1
end_of_file=0
line_count=0
while [[ $end_of_file == 0 ]] ;
do
read -r line
end_of_file=$?
((line_count++))
if [[ $line == \#* ]] || [[ $line == "" ]] ; then
# if [[ $line == "###"[[:blank:]]*[[:blank:]]"###" ]] ; then
# echo -e "\033[1;33m$line\033[m"
if [[ $line == "#"[[:blank:]]*[[:blank:]]"#" ]] ; then
echo -e "\033[1;33m $line\033[m" | tr '\t' ' '
fi
continue
else
printf "\033[1;35m%-4s\033[m" " $i: "
tmp_line_count=$line_count
while [[ $end_of_file == 0 ]] && [[ $line != \#* ]] && [[ $line != "" ]] ;
do
INPUT+="$line$NL"
read -r line
end_of_file=$?
((line_count++))
done
# INPUT=${INPUT%?}
echo -n "$INPUT" | $MINISHELL_PATH/$EXECUTABLE 2>tmp_err_minishell >tmp_out_minishell
exit_minishell=$?
echo -n "enable -n .$NL$INPUT" | bash 2>tmp_err_bash >tmp_out_bash
exit_bash=$?
echo -ne "\033[1;34mSTD_OUT:\033[m "
if ! diff -q tmp_out_minishell tmp_out_bash >/dev/null ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_OUT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((ONE++))
fi
echo -ne "\033[1;33mSTD_ERR:\033[m "
if [[ -s tmp_err_minishell && ! -s tmp_err_bash ]] || [[ ! -s tmp_err_minishell && -s tmp_err_bash ]] ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_ERR++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((TWO++))
fi
echo -ne "\033[1;36mEXIT_CODE:\033[m "
if [[ $exit_minishell != $exit_bash ]] ;
then
echo -ne "❌\033[1;31m [ minishell($exit_minishell) bash($exit_bash) ]\033[m " | tr '\n' ' '
((TEST_KO_EXIT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((THREE++))
fi
INPUT=""
((i++))
((TEST_COUNT++))
echo -e "\033[0;90m$1:$tmp_line_count\033[m "
if [[ $ONE == 1 && $TWO == 1 && $THREE == 1 ]] ;
then
((GOOD_TEST++))
((ONE--))
((TWO--))
((THREE--))
else
ONE=0
TWO=0
THREE=0
fi
fi
done < "$1"
}
test_leaks() {
IFS=''
i=1
end_of_file=0
line_count=0
while [[ $end_of_file == 0 ]] ;
do
read -r line
end_of_file=$?
((line_count++))
if [[ $line == \#* ]] || [[ $line == "" ]] ; then
# if [[ $line == "###"[[:blank:]]*[[:blank:]]"###" ]] ; then
# echo -e "\033[1;33m$line\033[m"
if [[ $line == "#"[[:blank:]]*[[:blank:]]"#" ]] ; then
echo -e "\033[1;33m $line\033[m" | tr '\t' ' '
fi
continue
else
printf "\033[0;35m%-4s\033[m" " $i: "
tmp_line_count=$line_count
while [[ $end_of_file == 0 ]] && [[ $line != \#* ]] && [[ $line != "" ]] ;
do
INPUT+="$line$NL"
read -r line
end_of_file=$?
((line_count++))
done
# INPUT=${INPUT%?}
echo -n "$INPUT" | $MINISHELL_PATH/$EXECUTABLE 2>tmp_err_minishell >tmp_out_minishell
exit_minishell=$?
echo -n "enable -n .$NL$INPUT" | bash 2>tmp_err_bash >tmp_out_bash
exit_bash=$?
echo -ne "\033[1;34mSTD_OUT:\033[m "
if ! diff -q tmp_out_minishell tmp_out_bash >/dev/null ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_OUT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((ONE++))
fi
echo -ne "\033[1;36mSTD_ERR:\033[m "
if [[ -s tmp_err_minishell && ! -s tmp_err_bash ]] || [[ ! -s tmp_err_minishell && -s tmp_err_bash ]] ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_ERR++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((TWO++))
fi
echo -ne "\033[1;36mEXIT_CODE:\033[m "
if [[ $exit_minishell != $exit_bash ]] ;
then
echo -ne "❌\033[1;31m [ minishell($exit_minishell) bash($exit_bash) ]\033[m " | tr '\n' ' '
((TEST_KO_EXIT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((THREE++))
fi
echo -ne "\033[1;36mLEAKS:\033[m "
echo -n "$INPUT" | valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=tmp_valgrind-out.txt $MINISHELL_PATH/$EXECUTABLE 2>/dev/null >/dev/null
# Get the number of bytes lost
definitely_lost=$(cat tmp_valgrind-out.txt | grep "definitely lost:" | awk 'END{print $4}')
possibly_lost=$(cat tmp_valgrind-out.txt | grep "possibly lost:" | awk 'END{print $4}')
indirectly_lost=$(cat tmp_valgrind-out.txt | grep "indirectly lost:" | awk 'END{print $4}')
all_blocks_freed=$(cat tmp_valgrind-out.txt | grep "All heap blocks were freed -- no leaks are possible")
# echo "$definitely_lost"
# echo "$possibly_lost"
# echo "$indirectly_lost"
# Check if any bytes were lost
if [ "$definitely_lost" != "0" ] || [ "$possibly_lost" != "0" ] || [ "$indirectly_lost" != "0" ] && [[ -z "$all_blocks_freed" ]];
then
echo -ne "❌ "
((LEAKS++))
else
echo -ne "✅ "
fi
INPUT=""
((i++))
((TEST_COUNT++))
echo -e "\033[0;90m$1:$tmp_line_count\033[m "
if [[ $ONE == 1 && $TWO == 1 && $THREE == 1 ]] ;
then
((GOOD_TEST++))
((ONE--))
((TWO--))
((THREE--))
else
ONE=0
TWO=0
THREE=0
fi
fi
done < "$1"
}
test_without_env() {
IFS=''
i=1
end_of_file=0
line_count=0
while [[ $end_of_file == 0 ]] ;
do
read -r line
end_of_file=$?
((line_count++))
if [[ $line == \#* ]] || [[ $line == "" ]] ; then
# if [[ $line == "###"[[:blank:]]*[[:blank:]]"###" ]] ; then
# echo -e "\033[1;33m$line\033[m"
if [[ $line == "#"[[:blank:]]*[[:blank:]]"#" ]] ; then
echo -e "\033[1;33m $line\033[m" | tr '\t' ' '
fi
continue
else
printf "\033[0;35m%-4s\033[m" " $i: "
tmp_line_count=$line_count
while [[ $end_of_file == 0 ]] && [[ $line != \#* ]] && [[ $line != "" ]] ;
do
INPUT+="$line$NL"
read -r line
end_of_file=$?
((line_count++))
done
# INPUT=${INPUT%?}
echo -n "$INPUT" | env -i $MINISHELL_PATH/$EXECUTABLE 2>tmp_err_minishell >tmp_out_minishell
exit_minishell=$?
echo -n "enable -n .$NL$INPUT" | env -i bash 2>tmp_err_bash >tmp_out_bash
exit_bash=$?
echo -ne "\033[1;34mSTD_OUT:\033[m "
if ! diff -q tmp_out_minishell tmp_out_bash >/dev/null ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_OUT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((ONE++))
fi
echo -ne "\033[1;36mSTD_ERR:\033[m "
if [[ -s tmp_err_minishell && ! -s tmp_err_bash ]] || [[ ! -s tmp_err_minishell && -s tmp_err_bash ]] ;
then
echo -ne "❌ " | tr '\n' ' '
((TEST_KO_ERR++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((TWO++))
fi
echo -ne "\033[1;36mEXIT_CODE:\033[m "
if [[ $exit_minishell != $exit_bash ]] ;
then
echo -ne "❌\033[1;31m [ minishell($exit_minishell) bash($exit_bash) ]\033[m " | tr '\n' ' '
((TEST_KO_EXIT++))
((FAILED++))
else
echo -ne "✅ "
((TEST_OK++))
((THREE++))
fi
INPUT=""
((i++))
((TEST_COUNT++))
echo -e "\033[0;90m$1:$tmp_line_count\033[m "
if [[ $ONE == 1 && $TWO == 1 && $THREE == 1 ]] ;
then
((GOOD_TEST++))
((ONE--))
((TWO--))
((THREE--))
else
ONE=0
TWO=0
THREE=0
fi
fi
done < "$1"
}
# Start the tester
main "$@"
# Clean all tmp files
[[ $1 != "-f" ]] && rm -f tmp_*