120 lines
3.3 KiB
Bash
Executable file
120 lines
3.3 KiB
Bash
Executable file
#!/bin/bash
|
|
source ./messages/message.sh
|
|
COMMAND=""
|
|
TEST_FILE=""
|
|
PWD=$(pwd)
|
|
DIR=""
|
|
|
|
process_args () {
|
|
while :; do
|
|
case $1 in
|
|
-h|-\?|--help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
v|--version)
|
|
show_version
|
|
exit 0
|
|
;;
|
|
-s|--script)
|
|
COMMAND=$2
|
|
shift
|
|
;;
|
|
-t|--test-file)
|
|
[[ ! -f ${PWD}/${2} ]] && error "Test file (${2}) not found"
|
|
TEST_FILE=${PWD}/${2}
|
|
shift
|
|
;;
|
|
-d|--directory)
|
|
[[ ! -d $2 ]] && error "Directory ${2} not found"
|
|
DIR=$2
|
|
shift
|
|
;;
|
|
*)
|
|
break
|
|
esac
|
|
shift
|
|
done
|
|
}
|
|
|
|
function usage (){
|
|
msg "\n**Script de test de code retour**\n"
|
|
msg "Ce script permet de comparer les codes retour attendu et ceux obtenus\n"
|
|
msg "Dans le cadre du Mooc bash de la licence ADSILLH\n"
|
|
msg "\n__USAGE__\n\n"
|
|
msg "$0 -d <rep> -s <script> -t <test>\n\n"
|
|
msg "-d\trépertoire ou se trouve le script à tester\n"
|
|
msg "-s\tnom du script à tester\n"
|
|
msg "-t\tfichier texte contenant les paramètres et codes retour attendus\n"
|
|
msg "\tCe fichier contient un test par lignes sous la forme\n"
|
|
msg "\t<arguments>**:**<code_retour_attendu>\n\n"
|
|
}
|
|
|
|
msg "**ADSILLH Bash return code test**\n"
|
|
process_args $@
|
|
[[ -z $COMMAND || -z $TEST_FILE ]] && error "You must specity a command and a test file"
|
|
msg "Test $COMMAND script with $TEST_FILE\n\n"
|
|
|
|
cd $DIR
|
|
[ ! -f $COMMAND ] && error "script **$COMMAND** not found"
|
|
|
|
while IFS='' read -r i || [[ -n "$line" ]]; do
|
|
# Create temp dir to store stderr
|
|
errfile=$(mktemp)
|
|
args=$(echo $i | cut -d: -f1)
|
|
e_code=$(echo $i | cut -d: -f2)
|
|
e_stdout=$(echo $i | cut -d: -f3)
|
|
e_stderr=$(echo $i | cut -d: -f4-)
|
|
|
|
msg "\nScript **\"${COMMAND} ${args}\"**\n"
|
|
ret_o=$(./$COMMAND $args 2>$errfile)
|
|
ret_c=$?
|
|
ret_e=$(< "$errfile")
|
|
rm "$errfile"
|
|
|
|
if [[ $ret_c == $e_code ]]
|
|
then
|
|
msg "green" "\tLe code de retour est celui attendu (${e_code})\n"
|
|
else
|
|
msg "yellow" "\tLe code de retour n'est pas correct script : ${ret_c} attendu : ${e_code}\n"
|
|
fi
|
|
|
|
if [[ ! -z $ret_o ]]
|
|
then
|
|
if [[ ! -z $e_stdout ]]
|
|
then
|
|
if [[ $ret_o == $e_stdout ]]
|
|
then
|
|
msg "blue" "\tSTDOUT : $e_stdout\n"
|
|
else
|
|
msg "red" "\tSTDOUT n'est pas celui attendu :\n"
|
|
msg "blue" "\t\tAttendu :\t$e_stdout\n"
|
|
msg "blue" "\t\tObtenu :\t$ret_o\n"
|
|
fi
|
|
else
|
|
msg "red" "STDOUT (non attendu) : $ret_o\n"
|
|
fi
|
|
fi
|
|
|
|
if [[ ! -z $ret_e ]]
|
|
then
|
|
# Script return STDERR
|
|
if [[ ! -z $e_stderr ]]
|
|
then
|
|
# We need stderr to validate test
|
|
if [[ $e_stderr == $ret_e ]]
|
|
then
|
|
msg "blue" "\tSTDERR : $ret_e\n"
|
|
else
|
|
msg "red" "\tSTDERR n'est pas celui attendu\n"
|
|
msg "blue" "\t\tAttendu :\t$e_stderr\n"
|
|
msg "blue" "\t\tObtenu :\t$ret_e\n"
|
|
fi
|
|
else
|
|
msg "red" "\tSDTERR (non attendu) : $ret_e\n"
|
|
fi
|
|
fi
|
|
|
|
done < "$TEST_FILE"
|
|
|
|
exit 0
|