-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_tester.sh
executable file
·529 lines (431 loc) · 12.9 KB
/
my_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
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
#!/usr/bin/env bash
### STATS DECLARATION ###
TOTAL_TESTS=0
SUCCESS_TESTS=0
TEST_ID=$1
### CHECK FUNCTION ###
initialize_tests ()
{
printf "\n\033[1;30m----- INITIALIZE TESTS -----\033[0m\n\n"
touch random_file
mkdir random_folder
}
clean_tests ()
{
printf "\n\033[1;30m----- CLEAN TESTS -----\033[0m\n\n"
rm -rf random_file
rm -rf random_folder
}
reduce_length ()
{
if [ ${#1} -gt 150 ]
then
printf "\t\033[1;29m%s\n\033[0m" "${1:0:150}"
printf "\t\033[1;29m[...] (detail with ./run_tests $2)\n\033[0m"
else
printf "\t\033[1;29m%s\n\033[0m" "$1"
fi
}
compare ()
{
if [ "$1" = "$2" ] && [ "$3" = "$4" ]; then
printf "%d -\t\033[1;32m[SUCCESS]\033[0m (%s)\n" $TOTAL_TESTS "$5"
if [ $TEST_ID ]
then
printf "\t\033[1;29m./42sh output: \n\033[0m"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m%s\n\033[0m" "$1"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m./42sh return value %s\n\n\033[0m" "$3"
printf "\t\033[1;29mTcsh output:\n\033[0m"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m%s\n\033[0m" "$2"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29mTcsh return value %s \n\n\033[0m" "$4"
fi
SUCCESS_TESTS=$((SUCCESS_TESTS+1))
else
printf "%d -\t\033[1;31m[FAIL]\033[0m \033[4;31m(%s)\n\033[0m" $TOTAL_TESTS "$5"
printf "\t\033[1;29m./42sh output: \n\033[0m"
printf "\t\033[1;29m-----------------------\n\033[0m"
if [ $TEST_ID ]
then
printf "\t\033[1;29m%s\n\033[0m" "$1"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m./42sh return value %s\n\n\033[0m" "$3"
printf "\t\033[1;29mTcsh output:\n\033[0m"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m%s\n\033[0m" "$1"
else
reduce_length "$1" "$TOTAL_TESTS"
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29m./42sh return value %s\n\n\033[0m" "$3"
printf "\t\033[1;29mTcsh output:\n\033[0m"
printf "\t\033[1;29m-----------------------\n\033[0m"
reduce_length "$2" "$TOTAL_TESTS"
fi
printf "\t\033[1;29m-----------------------\n\033[0m"
printf "\t\033[1;29mTcsh return value %s \n\n\033[0m" "$4"
fi
TOTAL_TESTS=$((TOTAL_TESTS+1))
}
execute ()
{
if [ $TEST_ID ]
then
if [ $TEST_ID -ne $TOTAL_TESTS ]
then
TOTAL_TESTS=$((TOTAL_TESTS+1))
return
fi
fi
rm -rf /tmp/42sh
mkdir /tmp/42sh
MYSH=$(echo "$1"| ./42sh 2>&1)
RET_MYSH=$(echo $?)
rm -rf /tmp/42sh
mkdir /tmp/42sh
if [ "$2" = "1" ]; then
TCSH=$(echo "$1" | env -i tcsh 2>&1)
else
TCSH=$(echo "$1" | tcsh 2>&1)
fi
RET_TCSH=$(echo $?)
compare "$MYSH" "$TCSH" "$RET_MYSH" "$RET_TCSH" "$1"
}
printf "\033[1;36m----- MINISHELL FUNCTIONAL TESTS -----\033[0m\n\n"
## CHECK IF BINARY EXISTS ##
if [ ! -f 42sh ]; then
printf "\033[1;31m[ERROR]\033[0m 42sh binary not found\n"
printf "\033[1;33mPlease compile your project before running this script with make\033[0m\n\n"
printf "\033[1;36m----- MINISHELL FUNCTIONAL TESTS -----\033[0m\n\n"
exit 1
fi
initialize_tests
### BASICS TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL BASICS TESTS -----\033[0m\n\n"
fi
execute "" "0"
execute "a" "0"
execute "./" "0"
execute "../" "0"
execute "." "0"
execute ".." "0"
execute "ls -l" "0"
execute "exit" "0"
execute "cd ; pwd" "0"
execute "cat toto" "0"
execute "exitt" "0"
execute ".gitignore" "0"
execute "rjzjrjz ppekn" "0"
execute "touch /tmp/42sh/test.sh ; /tmp/42sh/test.sh" "1"
### ERROR TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL ERROR TESTS -----\033[0m\n\n"
fi
execute "./42sh-tester/tests/my_divzero" "0"
execute "./42sh-tester/tests/my_segfault" "0"
### SPACES AND TABS TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL SPACES AND TABS TESTS -----\033[0m\n\n"
fi
#only spaces
execute " " "0"
execute " ls -l -all " "0"
#only tabs
execute "\tls\t-l\t-all\t" "0"
#tabs and spaces
execute "\tls \t -l \t-all \t " "0"
### CD TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL CD TESTS -----\033[0m\n\n"
fi
execute "cd / ; pwd" "0"
execute "cd toto tata" "0"
execute "cd random_file" "0"
execute "cd random_folder ; pwd" "0"
execute "cd fail_dir" "0"
execute "unsetenv OLDPWD ; cd -" "1"
### ENV TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL ENV TESTS -----\033[0m\n\n"
fi
execute "setenv _toto tata" "0"
execute "setenv 8toto tata" "0"
execute "setenv toto tata titi" "0"
execute "setenv toto= tata" "0"
execute "setenv toto9 tata" "0"
execute "setenv Toto9" "0"
execute "setenv Toto" "0"
execute "setenv Abc Abc" "0"
execute "setenv Zbc Zbc" "0"
execute "setenv abc abc" "0"
execute "setenv zbc Zbc" "0"
execute "unsetenv" "0"
execute "unsetenv tototototototo" "0"
execute "unsetenv PATH" "0"
execute "unsetenv PATH HOME OLDPWD PWD LESS" "0"
### EXIT TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL EXIT TESTS -----\033[0m\n\n"
fi
#basics
execute "exit 0" "0"
execute "exit 1" "0"
execute "exit -1" "0"
execute "exit 999999" "0"
execute "exit -999999" "0"
execute "exit -abc" "0"
execute "exit abc" "0"
execute "exit -18abc" "0"
#advance
execute "exit 12; ls" "0"
execute "exit 12; exit 123" "0"
execute "exit 12; exit -a123" "0"
execute "exit -" "0"
### WITHOUT PATH TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL WITHOUT PATH TESTS -----\033[0m\n\n"
fi
execute "unsetenv PATH ; ls" "0"
execute "unsetenv HOME ; cd" "1"
### PERMISSIONS AND EXECUTION TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL PERMISSIONS AND EXECUTION TESTS -----\033[0m\n\n"
fi
execute "./42sh" "0"
execute "../bin/ls" "0"
execute "./bin/ls" "0"
execute "/bin/ls" "0"
execute "bin/ls" "0"
execute "./lib" "0"
### SEMICOLON TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL SEMICOLON TESTS -----\033[0m\n\n"
fi
execute ";" "1"
execute "ls ; ls ; ls ; ls ; ls ; ls ; ls" "0"
execute "ls;ls;ls;ls;ls;ls;ls" "0"
execute "ls;exit 34;ls" "0"
execute "aa ; ls" "0"
execute "ls ; aa ; ls" "0"
### PIPE TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL PIPE TESTS -----\033[0m\n\n"
fi
execute "|" "1"
execute "ls|ls" "0"
execute "ls -l | grep lib | cat -e" "0"
execute "ls / | wc -l | cat -e" "0"
execute "ls / | cat -e | cat -e | cat -e | cat -e | cat -e" "0"
execute "ls / | cat -e | cat -e" "0"
execute "ls /|cat -e|cat -e" "0"
execute "| ls | cat -e" "1"
execute "ls | | cat -e" "1"
execute "ls | sort -r | cut -b 1-1 | cat"
#with builtin (cd, exit, setenv, unsetenv, exit)
execute "setenv | grep PWD | cat -e" "0"
execute "cd /var/log | ls -la" "0"
execute "env | grep TERM" "0"
execute "unsetenv LANG | echo \"Hello, world!\"" "0"
execute "cd /usr/share | grep \"doc\" | wc -l | cat -e" "0"
### REDIRECTION TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL REDIRECTION TESTS -----\033[0m\n\n"
fi
execute "cat < >" "1"
execute "cat <" "1"
execute "cat >" "1"
execute "cat < > /tmp/42sh/hoho" "1"
execute "cat < /tmp/42sh/yoyo" "1"
execute "cat > /tmp/42sh/momo" "1"
execute "cat > /tmp/42sh/yuyu > /tmp/42sh/tata < /tmp/42sh/yuyu" "1"
execute "cat > /tmp/42sh/yuyu > /tmp/42sh/tata < /tmp/42sh/yuyu ; cat < /tmp/42sh/yuyu" "1"
## OUTPUT
# Simple
execute "ls -l > /tmp/42sh/toto ; cat /tmp/42sh/toto" "0"
# Double
execute "ls -l > /tmp/42sh/titi ; ls >> /tmp/42sh/titi ; cat /tmp/42sh/titi ; rm -rf /tmp/42sh/titi" "0"
execute "ls -l >> /tmp/42sh/kuku ; cat /tmp/42sh/kuku ; rm -rf /tmp/42sh/kuku" "0"
## INPUT
# Simple
execute "ls -l > /tmp/42sh/mama ; sort -r < /tmp/42sh/mama" "0"
### REDIRECTION && PIPE TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL REDIRECTION && PIPE TESTS -----\033[0m\n\n"
fi
## OUTPUT
# Simple
execute "ls -l > /tmp/42sh/papa ; cat /tmp/42sh/papa | grep lib" "0"
execute "ls -l > /tmp/42sh/lala ; cat /tmp/42sh/lala | grep lib | cat -e" "0"
# Double
execute "ls -l > /tmp/42sh/dada ; ls >> /tmp/42sh/dada ; cat /tmp/42sh/dada | grep lib ; rm -rf /tmp/42sh/dada" "0"
execute "ls -l > /tmp/42sh/kaka ; ls >> /tmp/42sh/kaka ; ls -l >> /tmp/42sh/kaka ; cat /tmp/42sh/kaka" "0"
## INPUT
# Simple
execute "ls -l > /tmp/42sh/fafa ; sort -r < /tmp/42sh/fafa | grep lib" "0"
execute "ls -l > /tmp/42sh/gaga ; sort -r < /tmp/42sh/gaga | grep lib | cat -e" "0"
### ECHO TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL ECHO TESTS -----\033[0m\n\n"
fi
# Simple
execute "Echo" "0"
execute "ECHO" "0"
execute "echo" "0"
execute "echo " "0"
execute "echo hello" "0"
execute "echo hello world" "0"
execute "echo hello world" "0"
execute "echo hello world" "0"
execute "echo hello world " "0"
# Quotes
execute "echo a '' b '' c '' d" "0"
execute "echo \"hello world\"" "0"
execute "echo 'hello world'" "0"
execute "echo 'hello world' 'hello world'" "0"
# Specials
execute "echo -nhello world" "0"
execute "echo a hello -nhello world" "0"
# Variables
execute "echo \$HOME" "0"
execute "echo \$AAA" "0"
execute "echo "\$HOME"" "0"
### VARIABLES ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL VARIABLES TESTS -----\033[0m\n\n"
fi
execute "\$PATH" "0"
execute "\$HOME" "0"
execute "\$SHTESTER" "0"
execute "set test = build/ ; ls \$test" "0"
execute "ls \$test" "0"
execute "set test = toto"
execute "set test=build/ ; ls \$test" "0"
execute "set test=build/" "0"
execute "set test = build tata ; ls \$test" "0"
execute "set bb aa cc dd ee ; echo \$bb ; echo \$aa ; echo \$cc" "0"
execute "set bb=1 aa=2 cc=3 dd=4 ; echo \$bb ; echo \$aa ; echo \$cc" "0"
### HISTORY ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL HISTORY TESTS -----\033[0m\n\n"
fi
# Command
execute "history -c" "0"
execute "history -c ; history" "0"
execute "history -c ; ls" "0"
execute "history -c ; ls ; history" "0"
# "!"
execute "ls ; !ls" "0"
execute "ls src ; !l | cat -e" "0"
execute "ls * 10 ; !100000000000000" "0"
execute "!5454" "1"
execute "history -c ; !10000000 | cat -e ; history" "0"
### WHERE / WHICH ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL WHERE / WHICH TESTS -----\033[0m\n\n"
fi
execute "where ls" "0"
execute "where ls toto" "0"
execute "where ./toto" "0"
execute "where 5" "0"
execute "where cd ls" "0"
execute "where" "0"
execute "which ls" "0"
execute "which ls toto" "0"
execute "which ./toto" "0"
execute "which 5" "0"
execute "which cd ls" "0"
execute "which" "0"
### OPERATORS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL OPERATORS TESTS -----\033[0m\n\n"
fi
# "&&"
execute "ls && ls" "0"
execute "ls && a && ls" "0"
# "||"
execute "ls || ls" "0"
execute "a || a || ls || ls" "0"
execute "ls || a && ls" "0"
execute "ls && a || ls" "0"
execute "&&" "1"
execute "||" "1"
execute "ls ||" "1"
execute "ls &&" "1"
execute "&& ls" "1"
execute "|| ls" "1"
### ALIAS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL ALIAS TESTS -----\033[0m\n\n"
fi
execute "alias" "0"
execute "alias popo" "0"
execute "alias bliblablou ls ; bliblablou" "0"
execute "alias blabla toto ; blabla" "0"
execute "alias bloblo ls ; bloblo ; alias" "0"
execute "alias aa ls -l includes ; aa" "0"
execute "alias bb ls -l random_dir ; bb" "0"
### BACKTICKS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL BACKTICKS TESTS -----\033[0m\n\n"
fi
execute "ls \`ls\`" "0"
execute "echo \`echo hello\`" "0"
execute "echo \`date\`" "0"
execute "echo \`echo hello \`echo world\`\`"
execute "echo \`echo \"hello world\"\`"
execute "echo \`echo 'hello world'\`"
### INHIBITOR ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- MINISHELL INHIBITOR TESTS -----\033[0m\n\n"
fi
execute "ls\ t" "0"
execute "ls valentin\ peron" "0"
execute "ls valentin\peron" "0"
### OTHER TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;33m----- OTHER TESTS -----\033[0m\n\n"
fi
execute "\$?" "0"
execute ";|" "1"
execute ";>>|><" "1"
execute "\0" "0"
execute "unsetenv PATH; setenv PATH /bin/ ; ls /" "0"
execute "./ls" "0"
execute "sleep 0.1" "0"
execute "ls /etc ; cat /etc/resolv.conf" "0"
### TESTS TA ###
execute "cat -e 42sh-tester/tests/bigfile.json | grep o" "0"
execute "setenv PATH / ; bin/ls" "0"
### RESULT TESTS ###
if [ ! $TEST_ID ]
then
printf "\n\033[1;34m----- MINISHELL RESULT TESTS -----\033[0m\n"
printf "\n\033[1;34mSuccess: %d/%d\n\033[0m" $SUCCESS_TESTS $TOTAL_TESTS
printf "\033[1;34mFailed: %d/%d\033[0m\n\n" $((TOTAL_TESTS-SUCCESS_TESTS)) $TOTAL_TESTS
fi
printf "\033[1;36m----- MINISHELL FUNCTIONAL TESTS -----\033[0m\n\n"
clean_tests