From 0ac57e5305e825a294b8ef04fe69222f42fe1cac Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 25 Mar 2021 18:26:46 +0100 Subject: [PATCH] Add drafty test system for helpers --- tests/helpers.tests/ynhtest_setup_source | 57 ++++++++++++++++++++++++ tests/test_helpers.sh | 43 ++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tests/helpers.tests/ynhtest_setup_source create mode 100644 tests/test_helpers.sh diff --git a/tests/helpers.tests/ynhtest_setup_source b/tests/helpers.tests/ynhtest_setup_source new file mode 100644 index 000000000..33d685844 --- /dev/null +++ b/tests/helpers.tests/ynhtest_setup_source @@ -0,0 +1,57 @@ +EXAMPLE_SRC=" +SOURCE_URL=https://github.com/Hextris/hextris/archive/8872ec47d694628e2fe668ebaa90b13d5626d95f.tar.gz +SOURCE_SUM=67f3fbd54c405717a25fb1e6f71d2b46e94c7ac6971861dd99ae5e58f6609892 +" + +ynhtest_setup_source_nominal() { + mkdir -p /tmp/var/www/ + final_path="$(mktemp -d -p /tmp/var/www)" + mkdir ../conf + echo "$EXAMPLE_SRC" > ../conf/test.src + + ynh_setup_source --dest_dir="$final_path" --source_id="test" + + test -e "$final_path" + test -e "$final_path/index.html" +} + + +ynhtest_setup_source_nominal_upgrade() { + mkdir -p /tmp/var/www/ + final_path="$(mktemp -d -p /tmp/var/www)" + mkdir ../conf + echo "$EXAMPLE_SRC" > ../conf/test.src + + ynh_setup_source --dest_dir="$final_path" --source_id="test" + + test -e "$final_path" + test -e "$final_path/index.html" + + # Except index.html to get overwritten during next ynh_setup_source + echo "HELLOWORLD" > $final_path/index.html + test "$(cat $final_path/index.html)" == "HELLOWORLD" + + ynh_setup_source --dest_dir="$final_path" --source_id="test" + + test "$(cat $final_path/index.html)" != "HELLOWORLD" +} + + +ynhtest_setup_source_with_keep() { + mkdir -p /tmp/var/www/ + final_path="$(mktemp -d -p /tmp/var/www)" + mkdir ../conf + echo "$EXAMPLE_SRC" > ../conf/test.src + + echo "HELLOWORLD" > $final_path/index.html + echo "HELLOWORLD" > $final_path/test.conf.txt + + ynh_setup_source --dest_dir="$final_path" --source_id="test" --keep="index.html test.conf.txt" + + test -e "$final_path" + test -e "$final_path/index.html" + test -e "$final_path/test.conf.txt" + test "$(cat $final_path/index.html)" == "HELLOWORLD" + test "$(cat $final_path/test.conf.txt)" == "HELLOWORLD" +} + diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh new file mode 100644 index 000000000..1a33a2433 --- /dev/null +++ b/tests/test_helpers.sh @@ -0,0 +1,43 @@ +readonly NORMAL=$(printf '\033[0m') +readonly BOLD=$(printf '\033[1m') +readonly RED=$(printf '\033[31m') +readonly GREEN=$(printf '\033[32m') +readonly ORANGE=$(printf '\033[33m') + +function log_test() +{ + echo -n "${BOLD}$1${NORMAL} ... " +} + +function log_passed() +{ + echo "${BOLD}${GREEN}✔ Passed${NORMAL}" +} + +function log_failed() +{ + echo "${BOLD}${RED}✘ Failed${NORMAL}" +} + +source /usr/share/yunohost/helpers +for TEST_SUITE in $(ls helpers.tests/*) +do + source $TEST_SUITE +done + +TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') + +for TEST in $TESTS +do + log_test $TEST + cd $(mktemp -d) + (app=ynhtest + YNH_APP_ID=$app + mkdir scripts + cd scripts + set -eu + $TEST + ) > ./test.log 2>&1 \ + && log_passed \ + || { echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; } +done