Add drafty test system for helpers

This commit is contained in:
Alexandre Aubin 2021-03-25 18:26:46 +01:00
parent cd1f64383b
commit 0ac57e5305
2 changed files with 100 additions and 0 deletions

View file

@ -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"
}

43
tests/test_helpers.sh Normal file
View file

@ -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