From fd477a8139330ffb4829a07e84e6b95793ad18be Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sat, 28 Oct 2023 00:14:14 +0200 Subject: [PATCH] start some tests on add command --- tests/add/file.sh | 84 +++++++++++++++++++++++++++++++++++++++++++++++ tests/main.sh | 34 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 tests/add/file.sh create mode 100755 tests/main.sh diff --git a/tests/add/file.sh b/tests/add/file.sh new file mode 100755 index 0000000..48e25bc --- /dev/null +++ b/tests/add/file.sh @@ -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 diff --git a/tests/main.sh b/tests/main.sh new file mode 100755 index 0000000..abe8593 --- /dev/null +++ b/tests/main.sh @@ -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)"