From 0ac57e5305e825a294b8ef04fe69222f42fe1cac Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 25 Mar 2021 18:26:46 +0100 Subject: [PATCH 1/8] 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 From 94beb52b0863724b15393d3e3a47d3d3d074d525 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 25 Mar 2021 18:30:20 +0100 Subject: [PATCH 2/8] Rename to .sh for syntax highlighting, add /bin/bash shebang --- .../{ynhtest_setup_source => ynhtest_setup_source.sh} | 0 tests/test_helpers.sh | 2 ++ 2 files changed, 2 insertions(+) rename tests/helpers.tests/{ynhtest_setup_source => ynhtest_setup_source.sh} (100%) diff --git a/tests/helpers.tests/ynhtest_setup_source b/tests/helpers.tests/ynhtest_setup_source.sh similarity index 100% rename from tests/helpers.tests/ynhtest_setup_source rename to tests/helpers.tests/ynhtest_setup_source.sh diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 1a33a2433..399345796 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -1,3 +1,5 @@ +#!/bin/bash + readonly NORMAL=$(printf '\033[0m') readonly BOLD=$(printf '\033[1m') readonly RED=$(printf '\033[31m') From 875fead9b57c4545b99cd15c58f10fec4cc659f4 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 15:47:56 +0200 Subject: [PATCH 3/8] Add test-helpers in gitlab CI pipeline --- .gitlab/ci/test.gitlab-ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) 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: From 4d6fcb1b8f8203836fdd1f62576d9dc99e5c3b8f Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 16:06:17 +0200 Subject: [PATCH 4/8] mv helpers.tests test_helpers.d, better naming consistency --- tests/{helpers.tests => test_helpers.d}/ynhtest_setup_source.sh | 0 tests/test_helpers.sh | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/{helpers.tests => test_helpers.d}/ynhtest_setup_source.sh (100%) diff --git a/tests/helpers.tests/ynhtest_setup_source.sh b/tests/test_helpers.d/ynhtest_setup_source.sh similarity index 100% rename from tests/helpers.tests/ynhtest_setup_source.sh rename to tests/test_helpers.d/ynhtest_setup_source.sh diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 399345796..830f6d14b 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -22,7 +22,7 @@ function log_failed() } source /usr/share/yunohost/helpers -for TEST_SUITE in $(ls helpers.tests/*) +for TEST_SUITE in $(ls test_helpers.d/*) do source $TEST_SUITE done From d782fe331dfbbf1c2cce88bd1dca1fd20131a374 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 16:06:33 +0200 Subject: [PATCH 5/8] Return exit code 1 when at least one test failed --- tests/test_helpers.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 830f6d14b..38bf010cf 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -29,6 +29,8 @@ done TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') +global_result=0 + for TEST in $TESTS do log_test $TEST @@ -41,5 +43,7 @@ do $TEST ) > ./test.log 2>&1 \ && log_passed \ - || { echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; } + || { echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; global_result=1; } done + +exit $global_result From d76f8d7969cd479f0ad33c17bab0dff056c09cc3 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 16:08:14 +0200 Subject: [PATCH 6/8] explaaaaain --- tests/test_helpers.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 38bf010cf..4c62d6275 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -21,12 +21,15 @@ function log_failed() echo "${BOLD}${RED}✘ Failed${NORMAL}" } +# ========================================================= + 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 From d11d26bdb4c96373da2e7e992d53b9a43080bc2a Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 18:02:08 +0200 Subject: [PATCH 7/8] Generate/server a dummy.tar.gz locally instead of hardcoding an external url --- tests/test_helpers.d/ynhtest_setup_source.sh | 72 +++++++++++--------- tests/test_helpers.sh | 36 ++++++++-- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/tests/test_helpers.d/ynhtest_setup_source.sh b/tests/test_helpers.d/ynhtest_setup_source.sh index 33d685844..cd28bc933 100644 --- a/tests/test_helpers.d/ynhtest_setup_source.sh +++ b/tests/test_helpers.d/ynhtest_setup_source.sh @@ -1,15 +1,28 @@ -EXAMPLE_SRC=" -SOURCE_URL=https://github.com/Hextris/hextris/archive/8872ec47d694628e2fe668ebaa90b13d5626d95f.tar.gz -SOURCE_SUM=67f3fbd54c405717a25fb1e6f71d2b46e94c7ac6971861dd99ae5e58f6609892 -" +_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() { - mkdir -p /tmp/var/www/ - final_path="$(mktemp -d -p /tmp/var/www)" - mkdir ../conf - echo "$EXAMPLE_SRC" > ../conf/test.src + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src - ynh_setup_source --dest_dir="$final_path" --source_id="test" + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" test -e "$final_path" test -e "$final_path/index.html" @@ -17,41 +30,36 @@ ynhtest_setup_source_nominal() { 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" + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src - test -e "$final_path" - test -e "$final_path/index.html" + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" + + test "$(cat $final_path/index.html)" == "Lorem Ipsum" + echo $? # Except index.html to get overwritten during next ynh_setup_source - echo "HELLOWORLD" > $final_path/index.html - test "$(cat $final_path/index.html)" == "HELLOWORLD" + echo "IEditedYou!" > $final_path/index.html + test "$(cat $final_path/index.html)" == "IEditedYou!" - ynh_setup_source --dest_dir="$final_path" --source_id="test" + ynh_setup_source --dest_dir="$final_path" --source_id="dummy" - test "$(cat $final_path/index.html)" != "HELLOWORLD" + test "$(cat $final_path/index.html)" == "Lorem Ipsum" } 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 + final_path="$(mktemp -d -p $VAR_WWW)" + _make_dummy_src > ../conf/dummy.src - echo "HELLOWORLD" > $final_path/index.html - echo "HELLOWORLD" > $final_path/test.conf.txt + echo "IEditedYou!" > $final_path/index.html + echo "IEditedYou!" > $final_path/test.txt - ynh_setup_source --dest_dir="$final_path" --source_id="test" --keep="index.html test.conf.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.conf.txt" - test "$(cat $final_path/index.html)" == "HELLOWORLD" - test "$(cat $final_path/test.conf.txt)" == "HELLOWORLD" + test -e "$final_path/test.txt" + test "$(cat $final_path/index.html)" == "IEditedYou!" + test "$(cat $final_path/test.txt)" == "IEditedYou!" } - diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 4c62d6275..15879a18a 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -21,6 +21,26 @@ 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" +python -m SimpleHTTPServer $HTTPSERVER_PORT &>/dev/null & +HTTPSERVER="$!" +popd + +VAR_WWW=$(mktemp -d)/var/www +mkdir -p $VAR_WWW # ========================================================= source /usr/share/yunohost/helpers @@ -34,19 +54,27 @@ TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') global_result=0 +echo $TESTS + for TEST in $TESTS do log_test $TEST cd $(mktemp -d) (app=ynhtest YNH_APP_ID=$app + mkdir conf mkdir scripts cd scripts - set -eu + set -eux $TEST - ) > ./test.log 2>&1 \ - && log_passed \ - || { echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; global_result=1; } + ) > ./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 From 4acfbdeade98124dffcff8f6d75a681aa98395fe Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 1 Apr 2021 18:26:18 +0200 Subject: [PATCH 8/8] Fix oopsies, fix and add test for ynh_setup_source with patch --- data/helpers.d/utils | 6 ++++-- tests/test_helpers.d/ynhtest_setup_source.sh | 20 ++++++++++++++++++-- tests/test_helpers.sh | 6 ++---- 3 files changed, 24 insertions(+), 8 deletions(-) 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 index cd28bc933..69edf8ac9 100644 --- a/tests/test_helpers.d/ynhtest_setup_source.sh +++ b/tests/test_helpers.d/ynhtest_setup_source.sh @@ -28,7 +28,6 @@ ynhtest_setup_source_nominal() { test -e "$final_path/index.html" } - ynhtest_setup_source_nominal_upgrade() { final_path="$(mktemp -d -p $VAR_WWW)" _make_dummy_src > ../conf/dummy.src @@ -36,7 +35,6 @@ ynhtest_setup_source_nominal_upgrade() { ynh_setup_source --dest_dir="$final_path" --source_id="dummy" test "$(cat $final_path/index.html)" == "Lorem Ipsum" - echo $? # Except index.html to get overwritten during next ynh_setup_source echo "IEditedYou!" > $final_path/index.html @@ -63,3 +61,21 @@ ynhtest_setup_source_with_keep() { 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 index 15879a18a..6a5f29bf1 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -34,10 +34,10 @@ trap cleanup EXIT SIGINT # Dummy http server, to serve archives for ynh_setup_source HTTPSERVER_DIR=$(mktemp -d) HTTPSERVER_PORT=1312 -pushd "$HTTPSERVER_DIR" +pushd "$HTTPSERVER_DIR" >/dev/null python -m SimpleHTTPServer $HTTPSERVER_PORT &>/dev/null & HTTPSERVER="$!" -popd +popd >/dev/null VAR_WWW=$(mktemp -d)/var/www mkdir -p $VAR_WWW @@ -54,8 +54,6 @@ TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') global_result=0 -echo $TESTS - for TEST in $TESTS do log_test $TEST