Skip to content

Commit

Permalink
tests/: fix shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamandar committed Dec 16, 2024
1 parent 303d992 commit d971def
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 170 deletions.
71 changes: 44 additions & 27 deletions tests/test_helpers.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/bash
#!/usr/bin/env bash

VERSION=${1:-2}

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()
Expand All @@ -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

Expand All @@ -42,43 +50,52 @@ 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_
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

Expand Down
22 changes: 15 additions & 7 deletions tests/test_helpers.v2.1.d/ynhtest_apt.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
71 changes: 36 additions & 35 deletions tests/test_helpers.v2.1.d/ynhtest_config.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash

#################
# _ __ _ _ #
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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="[email protected]"
cat $file
cat "$file"
test "$(_read_json "$file" "email")" == "[email protected]"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "[email protected]"

Expand Down Expand Up @@ -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"
<?php
// Some comment
\$foo = NULL;
Expand All @@ -564,10 +565,10 @@ ynhtest_config_read_php() {
EOF

test "$(_read_php "$file" "foo")" == "NULL"
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "NULL"
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "NULL"

test "$(_read_php "$file" "enabled")" == "false"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "false"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "false"

test "$(_read_php "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Lorem Ipsum"
Expand All @@ -585,9 +586,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="DB_HOST")" == "localhost"

! _read_php "$file" "nonexistent"
Expand All @@ -599,10 +600,10 @@ EOF


ynhtest_config_write_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"
<?php
// Some comment
\$foo = NULL;
Expand All @@ -628,17 +629,17 @@ EOF
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_php "$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_php "$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="[email protected]"
cat $file
cat "$file"
test "$(_read_php "$file" "email")" == "[email protected]"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "[email protected]"

Expand Down
Loading

0 comments on commit d971def

Please sign in to comment.