feat: testsuite summary and enhanced tests look

This commit is contained in:
Gu://em_ 2026-01-21 22:59:33 +01:00
parent 402b1614b5
commit ed6e761f9c

112
tests/functional/run-tests.sh Normal file → Executable file
View file

@ -1,20 +1,20 @@
#!/bin/bash
echo WIP: vous pourriez regretter de lancer ce script
exit 2
###################
# Variables #
###################
executable="../../src/42sh"
errors_count=0
total_tests=0
ref_executable="dash"
tmp_script="/tmp/test_script.sh"
output="/tmp/42sh_tests.output"
ref_output="/tmp/42sh_tests_ref.output"
total_tests=0
errors_count=0
timeouts_count=0
##################
@ -113,19 +113,25 @@ check_result() {
test_failed=0
# Check return code
if [[ "$actual_code" -ne "$ref_code" ]]; then
echo $BRed "FAILED" $Color_off
echo $Blue ' ' "on '$2'" $Color_off
echo ' ' "Expected code $ref_code but got $actual_code"
if [[ "$actual_code" -eq 124 ]]; then
echo -e $BRed "TIMEOUT" $Color_Off
echo -e ' ' "on '$command'"
echo -e ' ' "Expected code $ref_code but got $actual_code"
((timeouts_count++))
test_failed=1
elif [[ "$actual_code" -ne "$ref_code" ]]; then
echo -e $BRed "FAILED" $Color_Off
echo -e ' ' "on '$command'"
echo -e ' ' "Expected code $ref_code but got $actual_code"
test_failed=1
# Check output
elif [ diff $output $ref_output > /dev/null ]; then
echo $BRed "FAILED" $Color_off
echo $Blue ' ' "on '$2'" $Color_off
echo ' ' "Output is not the one expected"
elif diff $output $ref_output > /dev/null; then
echo -e $BRed "FAILED" $Color_Off
echo -e ' ' "on '$command'"
echo -e ' ' "Output is not the one expected"
test_failed=1
else
echo $Blue OK $Colors_off
echo -e $BGreen OK $Color_Off
fi
if [[ "$test_failed" -eq 1 ]]; then
@ -141,17 +147,17 @@ test_str() {
# Check input
if [[ -z "$1" || -z "$2" ]]; then
echo $BRed "\n\n" "Issue in the testsuite: test_str: One or more argument is empty" $Color_off
echo -e $BRed "\n\n" "Issue in the testsuite: test_str: One or more argument is empty" $Color_Off
exit 2
fi
echo $BBlue "=== $1 ===" $Color_off
echo -e $BBlue "================== $1 ==================" $Color_Off
echo "$2" > $tmp_script
echo -e "$2" > $tmp_script
# Arg
echo -n "\n= [ARG] "
$executable -c "$2" &> $output
echo -e -n $Blue "= [ARG] " $Color_Off
timeout 2 $executable -c "$2" &> $output
actual_code=$?
$ref_executable -c "$2" &> $ref_output
ref_code=$?
@ -159,8 +165,8 @@ test_str() {
check_result "$2" "$actual_code" "$ref_code"
# Script
echo -n "\n= [SCRIPT] "
$executable "$tmp_script" &> $output
echo -e -n $Blue "= [SCRIPT]" $Color_Off
timeout 2 $executable "$tmp_script" &> $output
actual_code=$?
$ref_executable "$tmp_script" &> $ref_output
ref_code=$?
@ -168,15 +174,16 @@ test_str() {
check_result "$2" "$actual_code" "$ref_code"
# Stdin
echo -n "\n= [STDIN] "
$executable < "$tmp_script" &> $output
echo -e -n $Blue "= [STDIN] " $Color_Off
timeout 2 $executable < "$tmp_script" &> $output
actual_code=$?
$ref_executable < "$2" &> $ref_output
$ref_executable < "$tmp_script" &> $ref_output
ref_code=$?
((total_tests++))
check_result "$tmp_script" "$actual_code" "$ref_code"
check_result "$2" "$actual_code" "$ref_code"
echo $BBlue "===================" $Color_off "\n"
echo -e "\n"
# echo -e $BBlue "===========================================" $Color_Off "\n"
}
# @arg test name
@ -185,19 +192,19 @@ test_script() {
# Check input
if [[ -z "$1" || -z "$2" ]]; then
echo $BRed "\n\n" "Issue in the testsuite: test_script: One or more argument is empty" $Color_off
echo -e $BRed "\n\n" "Issue in the testsuite: test_script: One or more argument is empty" $Color_Off
exit 2
fi
if [[ ! -f "$2" ]]; then
echo $BRed "\n\n" "Issue in the testsuite: test_script: Second argument is not a file" $Color_off
echo -e $BRed "\n\n" "Issue in the testsuite: test_script: Second argument is not a file" $Color_Off
exit 2
fi
echo $BBlue "=== $1 ===" $Color_off
echo -e $BBlue "================== $1 ==================" $Color_Off
# Script
echo -n "\n= [SCRIPT] "
$executable "$tmp_script" &> $output
echo -e -n "= [SCRIPT] "
timeout 2 $executable "$tmp_script" &> $output
actual_code=$?
$ref_executable "$tmp_script" &> $ref_output
ref_code=$?
@ -205,15 +212,38 @@ test_script() {
check_result "$2" "$actual_code" "$ref_code"
# Stdin
echo -n "\n= [STDIN] "
$executable < "$tmp_script" &> $output
echo -e -n "= [STDIN] "
timeout 2 $executable < "$tmp_script" &> $output
actual_code=$?
$ref_executable < "$2" &> $ref_output
ref_code=$?
((total_tests++))
check_result "$tmp_script" "$actual_code" "$ref_code"
echo $BBlue "===================" $Color_off "\n"
echo -e "\n"
# echo -e $BBlue "===========================================" $Color_Off "\n"
}
summarize() {
(( passed_tests = $total_tests - $errors_count ))
(( tests_percentage = 100 * $passed_tests / $total_tests ))
if [[ tests_percentage -gt 80 ]]; then
coverage_color=$BGreen
elif [[ tests_percentage -gt 50 ]]; then
coverage_color=$BYellow
elif [[ tests_percentage -gt 30 ]]; then
coverage_color=$BOrange
else
coverage_color=$BRed
fi
echo -e $BWhite "\n\n""===========" $UWhite"Summary"$Color_Off "\n"
echo -e " Passed $coverage_color$passed_tests/$total_tests$Color_Off tests ($coverage_color$tests_percentage$Color_Off%)"
echo -e " Got $BRed$timeouts_count timeout(s)$Color_Off"
echo
}
# ***********************************************************
@ -221,4 +251,18 @@ test_script() {
# Tests #
#################
echo -e "\n\n""===$BGreen TestsuitatorX Ultra Pro Max+ 365 Premium Gris Sidéral" "\n\n"$Color_Off
# echo
test_str "Hello" "echo Hello there"
test_str "Hello;" "echo Hello there;"
test_str "'Hello'" "echo 'Hello there'"
test_str "Hello;Hello" "echo Hello; echo Wesh attends quoi; echo pouquoi je suis une ligne en dessous"
# programs
test_str "LS" "ls"
test_str "Le SS" "lss"
summarize