start some tests on add command

This commit is contained in:
grimhilt 2023-10-28 00:14:14 +02:00
parent 559316e756
commit fd477a8139
2 changed files with 118 additions and 0 deletions

84
tests/add/file.sh Executable file
View File

@ -0,0 +1,84 @@
#!/bin/sh
get_exe() {
exe=$(pwd)
exe+="/../target/debug/nextsync"
if [ ! -f $exe ]; then
echo "No executable found, try to compile first"
exit 4
fi
}
setup_env() {
[ ! -v exe ] && get_exe
path=$(mktemp -d)
cd $path
}
add_cmp() {
res=$($exe status --nostyle)
diff <(echo -e "$2" ) <(echo -e "$res") 2> /dev/null > /dev/null
if [ $? -ne 0 ]; then
echo -e "$1: Output differ:"
diff <(echo -e "$2" ) <(echo -e "$res")
echo $path
exit 1
fi
}
add_test() {
setup_env
$exe init
touch $2
$exe add $3
add_cmp "$1" "$4"
}
add_basics() {
add_test "basic" "toto" "toto" "new: toto"
}
add_space() {
setup_env
$exe init
touch 'to to'
$exe add 'to to'
res=$($exe status --nostyle)
add_cmp "space" "new: to to"
}
add_multiple() {
add_test "multiple" "titi riri" "titi riri" "new: titi\nnew: riri"
}
add_regex() {
add_test "regex" "titi riri" "./*" "new: riri\nnew: titi"
}
add_subdir() {
setup_env
$exe init
mkdir dir
touch dir/toto
$exe add "./dir/toto"
res=$($exe status --nostyle)
add_cmp "subdir" "new: dir/toto"
}
add_subdir_regex() {
setup_env
$exe init
mkdir dir
touch dir/toto dir/roro
$exe add "./dir/*"
res=$($exe status --nostyle)
add_cmp "subdir_regex" "new: dir/roro\nnew: dir/toto"
}
add_basics
add_space
add_multiple
add_regex
add_subdir
#add_subdir_regex
exit 0

34
tests/main.sh Executable file
View File

@ -0,0 +1,34 @@
#!/bin/sh
# Getting all tests
TESTS=$(find -name "*.sh" -not -name "main.sh")
if [ $# -ne 0 ]; then
TESTS=$(find -name "*$1*" -not -name "main.sh")
tests=""
for obj in $TESTS; do
[ -d $obj ] && tests+=$(find -path "$obj/*.sh" -not -name "main.sh")
done;
TESTS=$tests
fi
# Executing tests
nb_tests=0
nb_success=0
for test in $TESTS; do
nb_tests=$((nb_tests + 1))
# run file
$test
exit_code=$?
if [ $exit_code -eq 0 ]; then
nb_success=$((nb_success + 1))
elif [ $exit_code -eq 4 ]; then
# not executable found, not need to try other tests
exit 1
else
echo "$test failed with exit code $exit_code"
fi
done;
echo -e "\nRan $nb_tests tests ($((nb_tests - nb_success)) Failed)"