diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 78299f59c0..8f084b722a 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash VERSION=${1:-2} @@ -6,6 +6,7 @@ readonly NORMAL=$(printf '\033[0m') readonly BOLD=$(printf '\033[1m') readonly RED=$(printf '\033[31m') readonly GREEN=$(printf '\033[32m') +# shellcheck disable=SC2034 readonly ORANGE=$(printf '\033[33m') function log_test() @@ -23,11 +24,18 @@ function log_failed() echo "${BOLD}${RED}✘ Failed${NORMAL}" } +# shellcheck disable=SC2317 function cleanup() { - [ -n "$HTTPSERVER" ] && kill "$HTTPSERVER" - [ -d "$HTTPSERVER_DIR" ] && rm -rf "$HTTPSERVER_DIR" - [ -d "$VAR_WWW" ] && rm -rf "$VAR_WWW" + if [ -n "$HTTPSERVER" ]; then + kill "$HTTPSERVER" + fi + if [ -d "$HTTPSERVER_DIR" ]; then + rm -rf "$HTTPSERVER_DIR" + fi + if [ -d "$VAR_WWW" ]; then + rm -rf "$VAR_WWW" + fi } trap cleanup EXIT SIGINT @@ -42,16 +50,16 @@ HTTPSERVER="$!" popd >/dev/null VAR_WWW=$(mktemp -d)/var/www -mkdir -p $VAR_WWW +mkdir -p "$VAR_WWW" # Needed to check the permission behavior in ynh_add_config x_x getent passwd ynhtest &>/dev/null || useradd --system ynhtest # ========================================================= -for TEST_SUITE in $(ls test_helpers.v$VERSION.d/*) -do - source $TEST_SUITE +for TEST_SUITE in "test_helpers.v$VERSION.d"/*; do + # shellcheck disable=SC1090 + source "$TEST_SUITE" done # Hack to list all known function, keep only those starting by ynhtest_ @@ -59,26 +67,35 @@ TESTS=$(declare -F | grep ' ynhtest_' | awk '{print $3}') global_result=0 -for TEST in $TESTS -do - log_test $TEST - cd $(mktemp -d) - (mkdir conf - mkdir scripts - cd scripts - export YNH_HELPERS_VERSION=$VERSION - source /usr/share/yunohost/helpers - app=ynhtest - YNH_APP_ID=$app - set -eux - $TEST - ) > ./test.log 2>&1 - - if [[ $? == 0 ]] - then - set +x; log_passed; +run_test() { + ( + test=$1 + pushd "$(mktemp -d)" + mkdir conf + mkdir scripts + cd scripts + export YNH_HELPERS_VERSION=$VERSION + # shellcheck disable=SC1091 + source /usr/share/yunohost/helpers + app=ynhtest + # shellcheck disable=SC2034 + YNH_APP_ID=$app + set -eux + "$test" + ) +} + +for TEST in $TESTS; do + log_test "$TEST" + + if run_test "$TEST" > ./test.log 2>&1; then + log_passed else - set +x; echo -e "\n----------"; cat ./test.log; echo -e "----------"; log_failed; global_result=1; + echo -e "\n----------" + cat ./test.log + echo -e "----------" + log_failed + global_result=1 fi done diff --git a/tests/test_helpers.v2.1.d/ynhtest_apt.sh b/tests/test_helpers.v2.1.d/ynhtest_apt.sh index 1fcf3b1853..a50c7195af 100644 --- a/tests/test_helpers.v2.1.d/ynhtest_apt.sh +++ b/tests/test_helpers.v2.1.d/ynhtest_apt.sh @@ -1,26 +1,34 @@ +#!/usr/bin/env bash + ynhtest_apt_install_apt_deps_regular() { cat << EOF > ../manifest.toml packaging_format = 2 -id = "$app" +id = "${app:?}" version = "0.1~ynh2" EOF - dpkg --list | grep -q "ii *$app-ynh-deps" && apt remove $app-ynh-deps --assume-yes || true - dpkg --list | grep -q 'ii *nyancat' && apt remove nyancat --assume-yes || true - dpkg --list | grep -q 'ii *sl' && apt remove sl --assume-yes || true + if dpkg --list | grep -q "ii *$app-ynh-deps "; then + apt remove "$app-ynh-deps" --assume-yes + fi + if dpkg --list | grep -q 'ii *nyancat '; then + apt remove nyancat --assume-yes + fi + if dpkg --list | grep -q 'ii *sl '; then + apt remove sl --assume-yes + fi ! _ynh_apt_package_is_installed "$app-ynh-deps" ! _ynh_apt_package_is_installed "nyancat" ! _ynh_apt_package_is_installed "sl" - + ynh_apt_install_dependencies "nyancat sl" _ynh_apt_package_is_installed "$app-ynh-deps" _ynh_apt_package_is_installed "nyancat" _ynh_apt_package_is_installed "sl" - - ynh_apt_remove_dependencies + + ynh_apt_remove_dependencies ! _ynh_apt_package_is_installed "$app-ynh-deps" ! _ynh_apt_package_is_installed "nyancat" diff --git a/tests/test_helpers.v2.1.d/ynhtest_config.sh b/tests/test_helpers.v2.1.d/ynhtest_config.sh index 5448d41c3b..b8fd96e387 100644 --- a/tests/test_helpers.v2.1.d/ynhtest_config.sh +++ b/tests/test_helpers.v2.1.d/ynhtest_config.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash ################# # _ __ _ _ # @@ -17,10 +18,10 @@ _read_py() { ynhtest_config_read_py() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.py" - cat << EOF > $dummy_dir/dummy.py + cat << EOF > "$dummy_dir/dummy.py" # Some comment FOO = None ENABLED = False @@ -60,9 +61,9 @@ EOF test "$(ynh_read_var_in_file --file="$file" --key="URL")" == "https://yunohost.org" test "$(ynh_read_var_in_file --file="$file" --key="ldap_base")" == "ou=users,dc=yunohost,dc=org" - + test "$(ynh_read_var_in_file --file="$file" --key="user")" == "camille" - + test "$(ynh_read_var_in_file --file="$file" --key="TITLE" --after="YNH_ICI")" == "Hello world" ! _read_py "$file" "NONEXISTENT" @@ -73,10 +74,10 @@ EOF } ynhtest_config_write_py() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.py" - cat << EOF > $dummy_dir/dummy.py + cat << EOF > "$dummy_dir/dummy.py" # Some comment FOO = None ENABLED = False @@ -122,7 +123,7 @@ EOF ynh_write_var_in_file --file="$file" --key="ldap_base" --value="ou=users,dc=yunohost,dc=org" test "$(ynh_read_var_in_file --file="$file" --key="ldap_base")" == "ou=users,dc=yunohost,dc=org" - + ynh_write_var_in_file --file="$file" --key="TITLE" --value="YOLO" --after="YNH_ICI" test "$(ynh_read_var_in_file --file="$file" --key="TITLE" --after="YNH_ICI")" == "YOLO" @@ -153,10 +154,10 @@ _read_ini() { } ynhtest_config_read_ini() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.ini" - cat << EOF > $file + cat << EOF > "$file" # Some comment ; Another comment [main] @@ -204,10 +205,10 @@ EOF } ynhtest_config_write_ini() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.ini" - cat << EOF > $file + cat << EOF > "$file" # Some comment ; Another comment [main] @@ -283,10 +284,10 @@ _read_yaml() { } ynhtest_config_read_yaml() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.yml" - cat << EOF > $file + cat << EOF > "$file" # Some comment foo: enabled: false @@ -332,10 +333,10 @@ EOF ynhtest_config_write_yaml() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.yml" - cat << EOF > $file + cat << EOF > "$file" # Some comment foo: enabled: false @@ -408,10 +409,10 @@ _read_json() { } ynhtest_config_read_json() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.json" - cat << EOF > $file + cat << EOF > "$file" { "foo": null, "enabled": false, @@ -459,10 +460,10 @@ EOF ynhtest_config_write_json() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.json" - cat << EOF > $file + cat << EOF > "$file" { "foo": null, "enabled": false, @@ -478,27 +479,27 @@ ynhtest_config_write_json() { EOF ynh_write_var_in_file --file="$file" --key="foo" --value="bar" - cat $file + cat "$file" test "$(_read_json "$file" "foo")" == "bar" test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "bar" ynh_write_var_in_file --file="$file" --key="enabled" --value="true" - cat $file + cat "$file" test "$(_read_json "$file" "enabled")" == "true" test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "true" ynh_write_var_in_file --file="$file" --key="title" --value="Foo Bar" - cat $file + cat "$file" test "$(_read_json "$file" "title")" == "Foo Bar" test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Foo Bar" ynh_write_var_in_file --file="$file" --key="theme" --value="super-awesome-theme" - cat $file + cat "$file" test "$(_read_json "$file" "theme")" == "super-awesome-theme" test "$(ynh_read_var_in_file --file="$file" --key="theme")" == "super-awesome-theme" ynh_write_var_in_file --file="$file" --key="email" --value="sam@domain.tld" - cat $file + cat "$file" test "$(_read_json "$file" "email")" == "sam@domain.tld" test "$(ynh_read_var_in_file --file="$file" --key="email")" == "sam@domain.tld" @@ -540,10 +541,10 @@ _read_php() { } ynhtest_config_read_php() { - local dummy_dir="$(mktemp -d -p $VAR_WWW)" + local dummy_dir="$(mktemp -d -p "$VAR_WWW")" file="$dummy_dir/dummy.php" - cat << EOF > $file + cat << EOF > "$file" $file + cat << EOF > "$file" "/etc/yunohost/apps/$app/settings.yml" @@ -10,10 +12,10 @@ ynhtest_settings() { ynh_app_setting_set --key="foo" --value="foovalue" ynh_app_setting_set --app="$app" --key="bar" --value="barvalue" - + test "$(ynh_app_setting_get --key="foo")" == "foovalue" test "$(ynh_app_setting_get --key="bar")" == "barvalue" - + ynh_app_setting_delete --key="foo" ynh_app_setting_delete --app="$app" --key="bar" @@ -37,12 +39,12 @@ ynhtest_setting_set_default() { test "${foo:-}" == "foovalue" test "$(ynh_app_setting_get --key="foo")" == "foovalue" - + ynh_app_setting_set_default --key="foo" --value="bar" test "${foo:-}" == "foovalue" test "$(ynh_app_setting_get --key="foo")" == "foovalue" - + ynh_app_setting_delete --key="foo" test "${foo:-}" == "foovalue" diff --git a/tests/test_helpers.v2.1.d/ynhtest_setup_source.sh b/tests/test_helpers.v2.1.d/ynhtest_setup_source.sh index a8e0a59472..caac5184ac 100644 --- a/tests/test_helpers.v2.1.d/ynhtest_setup_source.sh +++ b/tests/test_helpers.v2.1.d/ynhtest_setup_source.sh @@ -1,6 +1,8 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2164,SC2010 + _make_dummy_manifest() { - if [ ! -e $HTTPSERVER_DIR/dummy.tar.gz ] - then + if [ ! -e "$HTTPSERVER_DIR/dummy.tar.gz" ]; then pushd "$HTTPSERVER_DIR" >/dev/null mkdir dummy pushd dummy >/dev/null @@ -16,13 +18,13 @@ _make_dummy_manifest() { cat << EOF packaging_format = 2 -id = "$app" +id = "${app:?}" version = "0.1~ynh2" [resources] [resources.sources.dummy] url = "http://127.0.0.1:$HTTPSERVER_PORT/dummy.tar.gz" - sha256 = "$(sha256sum $HTTPSERVER_DIR/dummy.tar.gz | awk '{print $1}')" + sha256 = "$(sha256sum "$HTTPSERVER_DIR/dummy.tar.gz" | awk '{print $1}')" [resources.install_dir] group = "www-data:r-x" @@ -31,9 +33,9 @@ EOF } ynhtest_setup_source_nominal() { - install_dir="$(mktemp -d -p $VAR_WWW)" + install_dir="$(mktemp -d -p "$VAR_WWW")" _make_dummy_manifest > ../manifest.toml - + ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" test -e "$install_dir" @@ -46,10 +48,10 @@ ynhtest_setup_source_nominal() { } ynhtest_setup_source_no_group_in_manifest() { - install_dir="$(mktemp -d -p $VAR_WWW)" + install_dir="$(mktemp -d -p "$VAR_WWW")" _make_dummy_manifest > ../manifest.toml sed '/www-data/d' -i ../manifest.toml - + ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" test -e "$install_dir" @@ -62,41 +64,41 @@ ynhtest_setup_source_no_group_in_manifest() { ynhtest_setup_source_nominal_upgrade() { - install_dir="$(mktemp -d -p $VAR_WWW)" + install_dir="$(mktemp -d -p "$VAR_WWW")" _make_dummy_manifest > ../manifest.toml ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" - test "$(cat $install_dir/index.html)" == "Lorem Ipsum" - + test "$(cat "$install_dir/index.html")" == "Lorem Ipsum" + # Except index.html to get overwritten during next ynh_setup_source - echo "IEditedYou!" > $install_dir/index.html - test "$(cat $install_dir/index.html)" == "IEditedYou!" + echo "IEditedYou!" > "$install_dir/index.html" + test "$(cat "$install_dir/index.html")" == "IEditedYou!" ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" - test "$(cat $install_dir/index.html)" == "Lorem Ipsum" + test "$(cat "$install_dir/index.html")" == "Lorem Ipsum" } ynhtest_setup_source_with_keep() { - install_dir="$(mktemp -d -p $VAR_WWW)" + install_dir="$(mktemp -d -p "$VAR_WWW")" _make_dummy_manifest > ../manifest.toml - echo "IEditedYou!" > $install_dir/index.html - echo "IEditedYou!" > $install_dir/test.txt + echo "IEditedYou!" > "$install_dir/index.html" + echo "IEditedYou!" > "$install_dir/test.txt" ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" --keep="index.html test.txt" test -e "$install_dir" test -e "$install_dir/index.html" test -e "$install_dir/test.txt" - test "$(cat $install_dir/index.html)" == "IEditedYou!" - test "$(cat $install_dir/test.txt)" == "IEditedYou!" + test "$(cat "$install_dir/index.html")" == "IEditedYou!" + test "$(cat "$install_dir/test.txt")" == "IEditedYou!" } ynhtest_setup_source_with_patch() { - install_dir="$(mktemp -d -p $VAR_WWW)" + install_dir="$(mktemp -d -p "$VAR_WWW")" _make_dummy_manifest > ../manifest.toml mkdir -p ../patches/dummy/ @@ -110,5 +112,5 @@ EOF ynh_setup_source --dest_dir="$install_dir" --source_id="dummy" - test "$(cat $install_dir/index.html)" == "Lorem Ipsum dolor sit amet" + test "$(cat "$install_dir/index.html")" == "Lorem Ipsum dolor sit amet" } diff --git a/tests/test_helpers.v2.1.d/ynhtest_templating.sh b/tests/test_helpers.v2.1.d/ynhtest_templating.sh index b8cea07707..77825d4b8b 100644 --- a/tests/test_helpers.v2.1.d/ynhtest_templating.sh +++ b/tests/test_helpers.v2.1.d/ynhtest_templating.sh @@ -1,41 +1,45 @@ +#!/usr/bin/env bash + ynhtest_simple_template_app_config() { - mkdir -p /etc/yunohost/apps/$app/ - echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + mkdir -p "/etc/yunohost/apps/${app:?}/" + echo "id: $app" > "/etc/yunohost/apps/$app/settings.yml" - template="$(mktemp -d -p $VAR_WWW)/template.txt" - cat << EOF > $template + template="$(mktemp -d -p "$VAR_WWW")/template.txt" + cat << EOF > "$template" app=__APP__ foo=__FOO__ EOF foo="bar" install_dir="$VAR_WWW" - + ynh_config_add --template="$template" --destination="$VAR_WWW/config.txt" - test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=bar')" - test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw------- 1 ynhtest ynhtest" + test "$(cat "$VAR_WWW/config.txt")" == "$(echo -ne 'app=ynhtest\nfoo=bar')" + # shellcheck disable=SC2012 + test "$(ls -l "$VAR_WWW/config.txt" | cut -d' ' -f1-4)" == "-rw------- 1 ynhtest ynhtest" } ynhtest_simple_template_system_config() { - mkdir -p /etc/yunohost/apps/$app/ - echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + mkdir -p "/etc/yunohost/apps/$app/" + echo "id: $app" > "/etc/yunohost/apps/$app/settings.yml" rm -f /etc/cron.d/ynhtest_config - template="$(mktemp -d -p $VAR_WWW)/template.txt" - cat << EOF > $template + template="$(mktemp -d -p "$VAR_WWW")/template.txt" + cat << EOF > "$template" app=__APP__ foo=__FOO__ EOF foo="bar" - + ynh_config_add --template="$template" --destination="/etc/cron.d/ynhtest_config" test "$(cat /etc/cron.d/ynhtest_config)" == "$(echo -ne 'app=ynhtest\nfoo=bar')" + # shellcheck disable=SC2012 test "$(ls -l /etc/cron.d/ynhtest_config | cut -d' ' -f1-4)" == "-r-------- 1 root root" rm -f /etc/cron.d/ynhtest_config @@ -43,22 +47,23 @@ EOF ynhtest_jinja_template_app_config() { - mkdir -p /etc/yunohost/apps/$app/ - echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + mkdir -p "/etc/yunohost/apps/$app/" + echo "id: $app" > "/etc/yunohost/apps/$app/settings.yml" - template="$(mktemp -d -p $VAR_WWW)/template.txt" - cat << EOF > $template + template="$(mktemp -d -p "$VAR_WWW")/template.txt" + cat << EOF > "$template" app={{ app }} {% if foo == "bar" %}foo=true{% endif %} EOF + # shellcheck disable=SC2034 foo="bar" + # shellcheck disable=SC2034 install_dir="$VAR_WWW" - + ynh_config_add --template="$template" --destination="$VAR_WWW/config.txt" --jinja - test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=true')" - test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw------- 1 ynhtest ynhtest" + test "$(cat "$VAR_WWW/config.txt")" == "$(echo -ne 'app=ynhtest\nfoo=true')" + # shellcheck disable=SC2012 + test "$(ls -l "$VAR_WWW/config.txt" | cut -d' ' -f1-4)" == "-rw------- 1 ynhtest ynhtest" } - - diff --git a/tests/test_helpers.v2.1.d/ynhtest_user.sh b/tests/test_helpers.v2.1.d/ynhtest_user.sh index 46f2a0cd6f..c805a0dff3 100644 --- a/tests/test_helpers.v2.1.d/ynhtest_user.sh +++ b/tests/test_helpers.v2.1.d/ynhtest_user.sh @@ -1,11 +1,12 @@ +#!/usr/bin/env bash ynhtest_system_user_create() { username=$(head -c 12 /dev/urandom | md5sum | head -c 12) - + ! ynh_system_user_exists --username="$username" - + ynh_system_user_create --username="$username" - + ynh_system_user_exists --username="$username" ynh_system_user_delete --username="$username" @@ -15,9 +16,9 @@ ynhtest_system_user_create() { ynhtest_system_user_with_group() { username=$(head -c 12 /dev/urandom | md5sum | head -c 12) - + ynh_system_user_create --username="$username" --groups="ssl-cert,ssh.app" - + grep -q "^ssl-cert:.*$username" /etc/group grep -q "^ssh.app:.*$username" /etc/group