diff --git a/.gitlab/ci/test.gitlab-ci.yml b/.gitlab/ci/test.gitlab-ci.yml index a4ec77ee8..308701475 100644 --- a/.gitlab/ci/test.gitlab-ci.yml +++ b/.gitlab/ci/test.gitlab-ci.yml @@ -53,6 +53,12 @@ root-tests: script: - python3 -m pytest tests +test-helpers: + extends: .test-stage + script: + - cd tests + - bash test_helpers.sh + test-apps: extends: .test-stage script: diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 942f6b34d..e651a5f77 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -217,11 +217,13 @@ ynh_setup_source () { fi # Apply patches - if (( $(find $YNH_APP_BASEDIR/sources/patches/ -type f -name "${source_id}-*.patch" 2> /dev/null | wc --lines) > "0" )) + local patches_folder=$(realpath $YNH_APP_BASEDIR/sources/patches/) + if (( $(find $patches_folder -type f -name "${source_id}-*.patch" 2> /dev/null | wc --lines) > "0" )) then (cd "$dest_dir" - for p in $YNH_APP_BASEDIR/sources/patches/${source_id}-*.patch + for p in $patches_folder/${source_id}-*.patch do + echo $p patch --strip=1 < $p done) || ynh_die --message="Unable to apply patches" fi diff --git a/tests/test_helpers.d/ynhtest_setup_source.sh b/tests/test_helpers.d/ynhtest_setup_source.sh new file mode 100644 index 000000000..69edf8ac9 --- /dev/null +++ b/tests/test_helpers.d/ynhtest_setup_source.sh @@ -0,0 +1,81 @@ +_make_dummy_src() { +echo "test coucou" + if [ ! -e $HTTPSERVER_DIR/dummy.tar.gz ] + then + pushd "$HTTPSERVER_DIR" + mkdir dummy + pushd dummy + echo "Lorem Ipsum" > index.html + echo '{"foo": "bar"}' > conf.json + mkdir assets + echo '.some.css { }' > assets/main.css + echo 'var some="js";' > assets/main.js + popd + tar -czf dummy.tar.gz dummy + popd + fi + echo "SOURCE_URL=http://127.0.0.1:$HTTPSERVER_PORT/dummy.tar.gz" + echo "SOURCE_SUM=$(sha256sum $HTTPSERVER_DIR/dummy.tar.gz | awk '{print $1}')" +} + +ynhtest_setup_source_nominal() { + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src + + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" + + test -e "$final_path" + test -e "$final_path/index.html" +} + +ynhtest_setup_source_nominal_upgrade() { + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src + + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" + + test "$(cat $final_path/index.html)" == "Lorem Ipsum" + + # Except index.html to get overwritten during next ynh_setup_source + echo "IEditedYou!" > $final_path/index.html + test "$(cat $final_path/index.html)" == "IEditedYou!" + + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" + + test "$(cat $final_path/index.html)" == "Lorem Ipsum" +} + + +ynhtest_setup_source_with_keep() { + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src + + echo "IEditedYou!" > $final_path/index.html + echo "IEditedYou!" > $final_path/test.txt + + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" --keep="index.html test.txt" + + test -e "$final_path" + test -e "$final_path/index.html" + test -e "$final_path/test.txt" + test "$(cat $final_path/index.html)" == "IEditedYou!" + test "$(cat $final_path/test.txt)" == "IEditedYou!" +} + +ynhtest_setup_source_with_patch() { + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src + + mkdir -p ../sources/patches + cat > ../sources/patches/dummy-index.html.patch << EOF +--- a/index.html ++++ b/index.html +@@ -1 +1,1 @@ +-Lorem Ipsum ++Lorem Ipsum dolor sit amet +EOF + + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" + + test "$(cat $final_path/index.html)" == "Lorem Ipsum dolor sit amet" +} diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh new file mode 100644 index 000000000..6a5f29bf1 --- /dev/null +++ b/tests/test_helpers.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +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}" +} + +function cleanup() +{ + [ -n "$HTTPSERVER" ] && kill "$HTTPSERVER" + [ -d "$HTTPSERVER_DIR" ] && rm -rf "$HTTPSERVER_DIR" + [ -d "$VAR_WWW" ] && rm -rf "$VAR_WWW" +} +trap cleanup EXIT SIGINT + +# ========================================================= + +# Dummy http server, to serve archives for ynh_setup_source +HTTPSERVER_DIR=$(mktemp -d) +HTTPSERVER_PORT=1312 +pushd "$HTTPSERVER_DIR" >/dev/null +python -m SimpleHTTPServer $HTTPSERVER_PORT &>/dev/null & +HTTPSERVER="$!" +popd >/dev/null + +VAR_WWW=$(mktemp -d)/var/www +mkdir -p $VAR_WWW +# ========================================================= + +source /usr/share/yunohost/helpers +for TEST_SUITE in $(ls test_helpers.d/*) +do + source $TEST_SUITE +done + +# Hack to list all known function, keep only those starting by ynhtest_ +TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') + +global_result=0 + +for TEST in $TESTS +do + log_test $TEST + cd $(mktemp -d) + (app=ynhtest + YNH_APP_ID=$app + mkdir conf + mkdir scripts + cd scripts + set -eux + $TEST + ) > ./test.log 2>&1 + + if [[ $? == 0 ]] + then + set +x; log_passed; + else + set +x; echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; global_result=1; + fi +done + +exit $global_result