Merge branch 'migrate-to-bookworm' into bookworm

This commit is contained in:
Alexandre Aubin 2024-07-03 17:27:09 +02:00
commit 8366e4b7c4
32 changed files with 1183 additions and 78 deletions

View file

@ -48,14 +48,17 @@
# reports: # reports:
# junit: report.xml # junit: report.xml
test-helpers: test-helpers2:
extends: .test-stage extends: .test-stage
script: script:
- cd tests - cd tests
- bash test_helpers.sh - bash test_helpers.sh
# only:
# changes: test-helpers2.1:
# - helpers/* extends: .test-stage
script:
- cd tests
- bash test_helpers.sh 2.1
test-domains: test-domains:
extends: .test-stage extends: .test-stage

21
debian/changelog vendored
View file

@ -4,6 +4,27 @@ yunohost (12.0.0) unstable; urgency=low
-- Alexandre Aubin <alex.aubin@mailoo.org> Thu, 04 May 2023 20:30:19 +0200 -- Alexandre Aubin <alex.aubin@mailoo.org> Thu, 04 May 2023 20:30:19 +0200
yunohost (11.2.20.1) stable; urgency=low
- helpers2.1: typo (1ed56952e)
- helpers2.1: add unit tests (92807afb1)
-- Alexandre Aubin <alex.aubin@mailoo.org> Mon, 01 Jul 2024 23:38:29 +0200
yunohost (11.2.20) stable; urgency=low
- helpers2.1: fix automigration of phpversion to php_version (3f973669)
- helpers2.1: change source patches location + raise an error instead of a warning when a patch fails to apply on CI (a48bfa67)
- helpers2.1: when using ynh_die, also return the error via YNH_STDRETURN such that it can be obtained from the python and displayed in the main error message, to increase the chance that people may read it and have something more useful than "An error happened in the script" (f2b5f0f2)
- helpers2.1: remove the ynh_clean_setup mechanism underused/useless.. (1c62960e)
- helpers2.1: switch to posisional args for ynh_multimedia_addaccess because that's what 99% of apps already do (ef622ffe)
- helpers2.1: add support for downloading .tar files ([#1889](http://github.com/YunoHost/yunohost/pull/1889))
- services/diagnosis: automatically ignore the service in diagnosis if it has been deactivated with the ynh cli ([#1886](http://github.com/YunoHost/yunohost/pull/1886))
Thanks to all contributors <3 ! (alexAubin, OniriCorpe, Sebastian Gumprich)
-- Alexandre Aubin <alex.aubin@mailoo.org> Mon, 01 Jul 2024 18:46:52 +0200
yunohost (11.2.19) stable; urgency=low yunohost (11.2.19) stable; urgency=low
- apps: tweaks to be more robust and prevent the stupid flood of 'sh: 0: getcwd() failed: No such file or directory' when running an app upgrade/remove from /var/www/$app, sometimes making it look like the upgrade failed when it didnt (a349fc03) - apps: tweaks to be more robust and prevent the stupid flood of 'sh: 0: getcwd() failed: No such file or directory' when running an app upgrade/remove from /var/www/$app, sometimes making it look like the upgrade failed when it didnt (a349fc03)

View file

@ -134,12 +134,12 @@ EOF
# Fake an install of those dependencies to see the errors # Fake an install of those dependencies to see the errors
# The sed command here is, Print only from 'Reading state info' to the end. # The sed command here is, Print only from 'Reading state info' to the end.
[[ -n "$problematic_dependencies" ]] && _ynh_apt_install $problematic_dependencies --dry-run 2>&1 | sed --quiet '/Reading state info/,$p' | grep -v "fix-broken\|Reading state info" >&2 [[ -n "$problematic_dependencies" ]] && _ynh_apt_install $problematic_dependencies --dry-run 2>&1 | sed --quiet '/Reading state info/,$p' | grep -v "fix-broken\|Reading state info" >&2
ynh_die "Unable to install dependencies" ynh_die "Unable to install apt dependencies"
} }
rm --recursive --force "$TMPDIR" # Remove the temp dir. rm --recursive --force "$TMPDIR" # Remove the temp dir.
# check if the package is actually installed # check if the package is actually installed
_ynh_apt_package_is_installed "${app_ynh_deps}" || ynh_die "Unable to install dependencies" _ynh_apt_package_is_installed "${app_ynh_deps}" || ynh_die "Unable to install apt dependencies"
# Specific tweak related to Postgresql # Specific tweak related to Postgresql
# -> trigger postgresql regenconf if we may have just installed postgresql # -> trigger postgresql regenconf if we may have just installed postgresql

View file

@ -4,7 +4,15 @@
# #
# usage: ynh_die "Some message" # usage: ynh_die "Some message"
ynh_die() { ynh_die() {
echo "$1" 1>&2 set +o xtrace # set +x
if [[ -n "${1:-}" ]]
then
if [[ -n "${YNH_STDRETURN:-}" ]]
then
python3 -c 'import yaml, sys; print(yaml.dump({"error": sys.stdin.read()}))' <<< "${1:-}" >>"$YNH_STDRETURN"
fi
echo "${1:-}" 1>&2
fi
exit 1 exit 1
} }

View file

@ -78,19 +78,12 @@ ynh_multimedia_addfolder() {
setfacl -RL -m m::rwx "$source_dir" setfacl -RL -m m::rwx "$source_dir"
} }
# Allow an user to have an write authorisation in multimedia directories # Add an user to the multimedia group, in turn having write permission in multimedia directories
# #
# usage: ynh_multimedia_addaccess user_name # usage: ynh_multimedia_addaccess user_name
# #
# | arg: --user_name= - The name of the user which gain this access. # | arg: user_name - The name of the user which gain this access.
ynh_multimedia_addaccess() { ynh_multimedia_addaccess() {
groupadd -f $MEDIA_GROUP
# ============ Argument parsing ============= usermod -a -G $MEDIA_GROUP $1
local -A args_array=([u]=user_name=)
local user_name
ynh_handle_getopts_args "$@"
# ===========================================
groupadd -f multimedia
usermod -a -G multimedia $user_name
} }

View file

@ -3,18 +3,6 @@
# (this is used in the apt helpers, big meh ...) # (this is used in the apt helpers, big meh ...)
readonly YNH_DEFAULT_PHP_VERSION=7.4 readonly YNH_DEFAULT_PHP_VERSION=7.4
# Legacy: auto-convert phpversion to php_version (for consistency with nodejs_version, ruby_version, ...)
if [[ -n "${app:-}" ]] && [[ -n "${phpversion:-}" ]]
then
if [[ -z "${php_version:-}" ]]
then
php_version=$phpversion
ynh_app_setting_set --key=php_version --value=$php_version
fi
ynh_app_setting_delete --key=phpversion
unset phpversion
fi
# Create a dedicated PHP-FPM config # Create a dedicated PHP-FPM config
# #
# usage: ynh_config_add_phpfpm # usage: ynh_config_add_phpfpm

View file

@ -122,3 +122,18 @@ else:
EOF EOF
eval "$xtrace_enable" eval "$xtrace_enable"
} }
# Legacy: auto-convert phpversion to php_version (for consistency with nodejs_version, ruby_version, ...)
# This has to be here and not in the "php" code file because ynh_app_setting_set/delete need to be defined @_@
if [[ -n "${app:-}" ]] && [[ -n "${phpversion:-}" ]]
then
if [[ -z "${php_version:-}" ]]
then
php_version=$phpversion
ynh_app_setting_set --key=php_version --value=$php_version
fi
ynh_app_setting_delete --key=phpversion
unset phpversion
fi

View file

@ -8,11 +8,7 @@
# | arg: --keep= - Space-separated list of files/folders that will be backup/restored in $dest_dir, such as a config file you don't want to overwrite. For example 'conf.json secrets.json logs' (no trailing `/` for folders) # | arg: --keep= - Space-separated list of files/folders that will be backup/restored in $dest_dir, such as a config file you don't want to overwrite. For example 'conf.json secrets.json logs' (no trailing `/` for folders)
# | arg: --full_replace= - Remove previous sources before installing new sources (can be 1 or 0, default to 0) # | arg: --full_replace= - Remove previous sources before installing new sources (can be 1 or 0, default to 0)
# #
# ##### New 'sources' resources # This helper will read infos from the 'sources' resources in the `manifest.toml` of the app
#
# (See also the resources documentation which may be more complete?)
#
# This helper will read infos from the 'sources' resources in the manifest.toml of the app
# and expect a structure like: # and expect a structure like:
# #
# ```toml # ```toml
@ -22,10 +18,12 @@
# sha256 = "0123456789abcdef" # The sha256 sum of the asset obtained from the URL # sha256 = "0123456789abcdef" # The sha256 sum of the asset obtained from the URL
# ``` # ```
# #
# ##### Optional flags # (See also the resources documentation which may be more complete?)
#
# ##### Optional flags in the 'sources' resource
# #
# ```text # ```text
# format = "tar.gz"/xz/bz2 # automatically guessed from the extension of the URL, but can be set explicitly. Will use `tar` to extract # format = "tar.gz"/xz/bz2/tar # automatically guessed from the extension of the URL, but can be set explicitly. Will use `tar` to extract
# "zip" # automatically guessed from the extension of the URL, but can be set explicitly. Will use `unzip` to extract # "zip" # automatically guessed from the extension of the URL, but can be set explicitly. Will use `unzip` to extract
# "docker" # useful to extract files from an already-built docker image (instead of rebuilding them locally). Will use `docker-image-extract` to extract # "docker" # useful to extract files from an already-built docker image (instead of rebuilding them locally). Will use `docker-image-extract` to extract
# "whatever" # an arbitrary value, not really meaningful except to imply that the file won't be extracted # "whatever" # an arbitrary value, not really meaningful except to imply that the file won't be extracted
@ -60,7 +58,8 @@
# - Uncompress the archive to `$dest_dir`. # - Uncompress the archive to `$dest_dir`.
# - If `in_subdir` is true, the first level directory of the archive will be removed. # - If `in_subdir` is true, the first level directory of the archive will be removed.
# - If `in_subdir` is a numeric value, the N first level directories will be removed. # - If `in_subdir` is a numeric value, the N first level directories will be removed.
# - Patches named `patches/${src_id}-*.patch` will be applied to `$dest_dir` # - Patches named `patches/${src_id}/*.patch` will be applied to `$dest_dir`
# - Apply sane default permissions (see _ynh_apply_default_permissions)
ynh_setup_source() { ynh_setup_source() {
# ============ Argument parsing ============= # ============ Argument parsing =============
local -A args_array=([d]=dest_dir= [s]=source_id= [k]=keep= [r]=full_replace) local -A args_array=([d]=dest_dir= [s]=source_id= [k]=keep= [r]=full_replace)
@ -108,6 +107,9 @@ ynh_setup_source() {
elif [[ "$src_url" =~ ^.*\.tar\.bz2$ ]] elif [[ "$src_url" =~ ^.*\.tar\.bz2$ ]]
then then
src_format="tar.bz2" src_format="tar.bz2"
elif [[ "$src_url" =~ ^.*\.tar$ ]]
then
src_format="tar"
elif [[ -z "$src_extract" ]] elif [[ -z "$src_extract" ]]
then then
src_extract="false" src_extract="false"
@ -213,7 +215,7 @@ ynh_setup_source() {
fi fi
strip="--strip-components $sub_dirs" strip="--strip-components $sub_dirs"
fi fi
if [[ "$src_format" =~ ^tar.gz|tar.bz2|tar.xz$ ]]; then if [[ "$src_format" =~ ^tar.gz|tar.bz2|tar.xz|tar$ ]]; then
tar --extract --file=$src_filename --directory="$dest_dir" $strip tar --extract --file=$src_filename --directory="$dest_dir" $strip
else else
ynh_die "Archive format unrecognized." ynh_die "Archive format unrecognized."
@ -222,18 +224,21 @@ ynh_setup_source() {
fi fi
# Apply patches # Apply patches
if [ -d "$YNH_APP_BASEDIR/patches/" ]; then local patches_folder=$(realpath "$YNH_APP_BASEDIR/patches/$source_id")
local patches_folder=$(realpath $YNH_APP_BASEDIR/patches/) if [ -d "$patches_folder" ]; then
# Check if any file matching the pattern exists, cf https://stackoverflow.com/a/34195247
if compgen -G "$patches_folder/${source_id}-*.patch" >/dev/null; then
pushd "$dest_dir" pushd "$dest_dir"
for p in $patches_folder/${source_id}-*.patch; do for patchfile in "$patches_folder/"*.patch; do
echo $p echo "Applying $patchfile"
patch --strip=1 <$p || ynh_print_warn "Packagers /!\\ patch $p failed to apply" if ! patch --strip=1 < "$patchfile"; then
if ynh_in_ci_tests; then
ynh_die "Patch $patchfile failed to apply!"
else
ynh_print_warn "Warn your packagers /!\\ Patch $patchfile failed to apply"
fi
fi
done done
popd popd
fi fi
fi
# Keep files to be backup/restored at the end of the helper # Keep files to be backup/restored at the end of the helper
# Assuming $dest_dir already exists # Assuming $dest_dir already exists

View file

@ -6,17 +6,6 @@ YNH_APP_BASEDIR=${YNH_APP_BASEDIR:-$(realpath ..)}
# #
# [internal] # [internal]
# #
# usage:
# ynh_exit_properly is used only by the helper ynh_abort_if_errors.
# You should not use it directly.
# Instead, add to your script:
# ynh_clean_setup () {
# instructions...
# }
#
# This function provide a way to clean some residual of installation that not managed by remove script.
#
# It prints a warning to inform that the script was failed, and execute the ynh_clean_setup function if used in the app script
ynh_exit_properly() { ynh_exit_properly() {
local exit_code=$? local exit_code=$?
@ -37,10 +26,6 @@ ynh_exit_properly() {
# Small tempo to avoid the next message being mixed up with other DEBUG messages # Small tempo to avoid the next message being mixed up with other DEBUG messages
sleep 0.5 sleep 0.5
if type -t ynh_clean_setup >/dev/null; then # Check if the function exist in the app script.
ynh_clean_setup # Call the function to do specific cleaning for the app.
fi
# Exit with error status # Exit with error status
# We don't call ynh_die basically to avoid unecessary 10-ish # We don't call ynh_die basically to avoid unecessary 10-ish
# debug lines about parsing args and stuff just to exit 1.. # debug lines about parsing args and stuff just to exit 1..
@ -55,7 +40,6 @@ ynh_exit_properly() {
# #
# This configure the rest of the script execution such that, if an error occurs # This configure the rest of the script execution such that, if an error occurs
# or if an empty variable is used, the execution of the script stops immediately # or if an empty variable is used, the execution of the script stops immediately
# and a call to `ynh_clean_setup` is triggered if it has been defined by your script.
ynh_abort_if_errors() { ynh_abort_if_errors() {
set -o errexit # set -e; Exit if a command fail set -o errexit # set -e; Exit if a command fail
set -o nounset # set -u; And if a variable is used unset set -o nounset # set -u; And if a variable is used unset
@ -272,7 +256,7 @@ _ynh_apply_default_permissions() {
fi fi
# Files inside should be owned by $app with rw-r----- (+x for folders or files that already have +x) # Files inside should be owned by $app with rw-r----- (+x for folders or files that already have +x)
# The group needs read/dirtraversal (in particular if it's www-data) # The group needs read/dirtraversal (in particular if it's www-data)
chmod -R u=rwX,g=r-X,o=--- "$target" chmod -R u=rwX,g=rX,o=--- "$target"
chown -R "$app:$group" "$target" chown -R "$app:$group" "$target"
return return
fi fi

View file

@ -1994,6 +1994,6 @@ diagnosis:
api: PUT /diagnosis/unignore api: PUT /diagnosis/unignore
arguments: arguments:
--filter: --filter:
help: Remove a filter (it should be an existing filter as listed with --list) help: Remove a filter (it should be an existing filter as listed with "ignore --list")
nargs: "*" nargs: "*"
metavar: CRITERIA metavar: CRITERIA

View file

@ -304,12 +304,14 @@ def _diagnosis_ignore(add_filter=None, remove_filter=None, list=False):
configuration["ignore_filters"][category] = [] configuration["ignore_filters"][category] = []
if criterias in configuration["ignore_filters"][category]: if criterias in configuration["ignore_filters"][category]:
logger.warning("This filter already exists.") logger.warning(
f"(There is already a diagnosis {category} filter with these criterias)"
)
return return
configuration["ignore_filters"][category].append(criterias) configuration["ignore_filters"][category].append(criterias)
_diagnosis_write_configuration(configuration) _diagnosis_write_configuration(configuration)
logger.success("Filter added") logger.success(f"Added a {category} diagnosis filter")
return return
if remove_filter: if remove_filter:
@ -322,11 +324,14 @@ def _diagnosis_ignore(add_filter=None, remove_filter=None, list=False):
configuration["ignore_filters"][category] = [] configuration["ignore_filters"][category] = []
if criterias not in configuration["ignore_filters"][category]: if criterias not in configuration["ignore_filters"][category]:
raise YunohostValidationError("This filter does not exists.") logger.warning(
f"(There is no such diagnosis {category} filter with these criterias to remove)"
)
return
configuration["ignore_filters"][category].remove(criterias) configuration["ignore_filters"][category].remove(criterias)
_diagnosis_write_configuration(configuration) _diagnosis_write_configuration(configuration)
logger.success("Filter removed") logger.success(f"Removed a {category} diagnosis filter")
return return

View file

@ -540,6 +540,9 @@ def hook_exec_with_script_debug_if_failure(*args, **kwargs):
failed = True if retcode != 0 else False failed = True if retcode != 0 else False
if failed: if failed:
error = error_message_if_script_failed error = error_message_if_script_failed
# check more specific error message added by ynh_die in $YNH_STDRETURN
if isinstance(retpayload, dict) and "error" in retpayload:
error += " : " + retpayload["error"].strip()
logger.error(error_message_if_failed(error)) logger.error(error_message_if_failed(error))
failure_message_with_debug_instructions = operation_logger.error(error) failure_message_with_debug_instructions = operation_logger.error(error)
if Moulinette.interface.type != "api": if Moulinette.interface.type != "api":

View file

@ -26,6 +26,7 @@ from glob import glob
from datetime import datetime from datetime import datetime
from moulinette import m18n from moulinette import m18n
from yunohost.diagnosis import diagnosis_ignore, diagnosis_unignore
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
from moulinette.utils.filesystem import ( from moulinette.utils.filesystem import (
@ -295,6 +296,7 @@ def service_enable(names):
names = [names] names = [names]
for name in names: for name in names:
if _run_service_command("enable", name): if _run_service_command("enable", name):
diagnosis_unignore(["services", f"service={name}"])
logger.success(m18n.n("service_enabled", service=name)) logger.success(m18n.n("service_enabled", service=name))
else: else:
raise YunohostError( raise YunohostError(
@ -314,6 +316,7 @@ def service_disable(names):
names = [names] names = [names]
for name in names: for name in names:
if _run_service_command("disable", name): if _run_service_command("disable", name):
diagnosis_ignore(["services", f"service={name}"])
logger.success(m18n.n("service_disabled", service=name)) logger.success(m18n.n("service_disabled", service=name))
else: else:
raise YunohostError( raise YunohostError(

View file

@ -144,6 +144,7 @@ def tools_postinstall(
force_diskspace=False, force_diskspace=False,
overwrite_root_password=True, overwrite_root_password=True,
): ):
from yunohost.service import _run_service_command
from yunohost.dyndns import _dyndns_available, dyndns_unsubscribe from yunohost.dyndns import _dyndns_available, dyndns_unsubscribe
from yunohost.utils.dns import is_yunohost_dyndns_domain from yunohost.utils.dns import is_yunohost_dyndns_domain
from yunohost.utils.password import ( from yunohost.utils.password import (
@ -259,8 +260,8 @@ def tools_postinstall(
os.system("touch /etc/yunohost/installed") os.system("touch /etc/yunohost/installed")
# Enable and start YunoHost firewall at boot time # Enable and start YunoHost firewall at boot time
service_enable("yunohost-firewall") _run_service_command("enable", "yunohost-firewall")
service_start("yunohost-firewall") _run_service_command("start", "yunohost-firewall")
tools_regen_conf(names=["ssh"], force=True) tools_regen_conf(names=["ssh"], force=True)

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
VERSION=${1:-2}
readonly NORMAL=$(printf '\033[0m') readonly NORMAL=$(printf '\033[0m')
readonly BOLD=$(printf '\033[1m') readonly BOLD=$(printf '\033[1m')
readonly RED=$(printf '\033[31m') readonly RED=$(printf '\033[31m')
@ -47,7 +49,7 @@ getent passwd ynhtest &>/dev/null || useradd --system ynhtest
# ========================================================= # =========================================================
for TEST_SUITE in $(ls test_helpers.d/*) for TEST_SUITE in $(ls test_helpers.v$VERSION.d/*)
do do
source $TEST_SUITE source $TEST_SUITE
done done
@ -64,6 +66,7 @@ do
(mkdir conf (mkdir conf
mkdir scripts mkdir scripts
cd scripts cd scripts
export YNH_HELPERS_VERSION=$VERSION
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
app=ynhtest app=ynhtest
YNH_APP_ID=$app YNH_APP_ID=$app

View file

@ -0,0 +1,28 @@
ynhtest_apt_install_apt_deps_regular() {
cat << EOF > ../manifest.toml
packaging_format = 2
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
! _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_package_is_installed "$app-ynh-deps"
! _ynh_apt_package_is_installed "nyancat"
! _ynh_apt_package_is_installed "sl"
}

View file

@ -0,0 +1,662 @@
#################
# _ __ _ _ #
# | '_ \| | | | #
# | |_) | |_| | #
# | .__/ \__, | #
# | | __/ | #
# |_| |___/ #
# #
#################
_read_py() {
local file="$1"
local key="$2"
python3 -c "exec(open('$file').read()); print($key)"
}
ynhtest_config_read_py() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.py"
cat << EOF > $dummy_dir/dummy.py
# Some comment
FOO = None
ENABLED = False
# TITLE = "Old title"
TITLE = "Lorem Ipsum"
THEME = "colib'ris"
EMAIL = "root@example.com" # This is a comment without quotes
PORT = 1234 # This is a comment without quotes
URL = 'https://yunohost.org'
DICT = {}
DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org"
DICT['ldap_conf'] = {}
DICT['ldap_conf']['user'] = "camille"
# YNH_ICI
DICT['TITLE'] = "Hello world"
EOF
test "$(_read_py "$file" "FOO")" == "None"
test "$(ynh_read_var_in_file --file="$file" --key="FOO")" == "None"
test "$(_read_py "$file" "ENABLED")" == "False"
test "$(ynh_read_var_in_file --file="$file" --key="ENABLED")" == "False"
test "$(_read_py "$file" "TITLE")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="TITLE")" == "Lorem Ipsum"
test "$(_read_py "$file" "THEME")" == "colib'ris"
test "$(ynh_read_var_in_file --file="$file" --key="THEME")" == "colib'ris"
test "$(_read_py "$file" "EMAIL")" == "root@example.com"
test "$(ynh_read_var_in_file --file="$file" --key="EMAIL")" == "root@example.com"
test "$(_read_py "$file" "PORT")" == "1234"
test "$(ynh_read_var_in_file --file="$file" --key="PORT")" == "1234"
test "$(_read_py "$file" "URL")" == "https://yunohost.org"
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"
test "$(ynh_read_var_in_file --file="$file" --key="NONEXISTENT")" == "YNH_NULL"
! _read_py "$file" "ENABLE"
test "$(ynh_read_var_in_file --file="$file" --key="ENABLE")" == "YNH_NULL"
}
ynhtest_config_write_py() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.py"
cat << EOF > $dummy_dir/dummy.py
# Some comment
FOO = None
ENABLED = False
# TITLE = "Old title"
TITLE = "Lorem Ipsum"
THEME = "colib'ris"
EMAIL = "root@example.com" # This is a comment without quotes
PORT = 1234 # This is a comment without quotes
URL = 'https://yunohost.org'
DICT = {}
DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org"
# YNH_ICI
DICT['TITLE'] = "Hello world"
EOF
ynh_write_var_in_file --file="$file" --key="FOO" --value="bar"
test "$(_read_py "$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"
test "$(_read_py "$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"
test "$(_read_py "$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"
test "$(_read_py "$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"
test "$(_read_py "$file" "EMAIL")" == "sam@domain.tld"
test "$(ynh_read_var_in_file --file="$file" --key="EMAIL")" == "sam@domain.tld"
ynh_write_var_in_file --file="$file" --key="PORT" --value="5678"
test "$(_read_py "$file" "PORT")" == "5678"
test "$(ynh_read_var_in_file --file="$file" --key="PORT")" == "5678"
ynh_write_var_in_file --file="$file" --key="URL" --value="https://domain.tld/foobar"
test "$(_read_py "$file" "URL")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file --file="$file" --key="URL")" == "https://domain.tld/foobar"
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"
! ynh_write_var_in_file --file="$file" --key="NONEXISTENT" --value="foobar"
! _read_py "$file" "NONEXISTENT"
test "$(ynh_read_var_in_file --file="$file" --key="NONEXISTENT")" == "YNH_NULL"
! ynh_write_var_in_file --file="$file" --key="ENABLE" --value="foobar"
! _read_py "$file" "ENABLE"
test "$(ynh_read_var_in_file --file="$file" --key="ENABLE")" == "YNH_NULL"
}
###############
# _ _ #
# (_) (_) #
# _ _ __ _ #
# | | '_ \| | #
# | | | | | | #
# |_|_| |_|_| #
# #
###############
_read_ini() {
local file="$1"
local key="$2"
python3 -c "import configparser; c = configparser.ConfigParser(); c.read('$file'); print(c['main']['$key'])"
}
ynhtest_config_read_ini() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.ini"
cat << EOF > $file
# Some comment
; Another comment
[main]
foo = null
enabled = False
# title = Old title
title = Lorem Ipsum
theme = colib'ris
email = root@example.com ; This is a comment without quotes
port = 1234 ; This is a comment without quotes
url = https://yunohost.org
[dict]
ldap_base = ou=users,dc=yunohost,dc=org
EOF
test "$(_read_ini "$file" "foo")" == "null"
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "null"
test "$(_read_ini "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "False"
test "$(_read_ini "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Lorem Ipsum"
test "$(_read_ini "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file --file="$file" --key="theme")" == "colib'ris"
#test "$(_read_ini "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "root@example.com"
#test "$(_read_ini "$file" "port")" == "1234"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "1234"
test "$(_read_ini "$file" "url")" == "https://yunohost.org"
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"
! _read_ini "$file" "nonexistent"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! _read_ini "$file" "enable"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
}
ynhtest_config_write_ini() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.ini"
cat << EOF > $file
# Some comment
; Another comment
[main]
foo = null
enabled = False
# title = Old title
title = Lorem Ipsum
theme = colib'ris
email = root@example.com # This is a comment without quotes
port = 1234 # This is a comment without quotes
url = https://yunohost.org
[dict]
ldap_base = ou=users,dc=yunohost,dc=org
EOF
ynh_write_var_in_file --file="$file" --key="foo" --value="bar"
test "$(_read_ini "$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"
test "$(_read_ini "$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"
test "$(_read_ini "$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"
test "$(_read_ini "$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"
test "$(_read_ini "$file" "email")" == "sam@domain.tld # This is a comment without quotes"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "sam@domain.tld"
ynh_write_var_in_file --file="$file" --key="port" --value="5678"
test "$(_read_ini "$file" "port")" == "5678 # This is a comment without quotes"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "5678"
ynh_write_var_in_file --file="$file" --key="url" --value="https://domain.tld/foobar"
test "$(_read_ini "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file --file="$file" --key="url")" == "https://domain.tld/foobar"
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="nonexistent" "foobar"
! _read_ini "$file" "nonexistent"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file --file="$file" --key="enable" "foobar"
! _read_ini "$file" "enable"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
}
#############################
# _ #
# | | #
# _ _ __ _ _ __ ___ | | #
# | | | |/ _` | '_ ` _ \| | #
# | |_| | (_| | | | | | | | #
# \__, |\__,_|_| |_| |_|_| #
# __/ | #
# |___/ #
# #
#############################
_read_yaml() {
local file="$1"
local key="$2"
python3 -c "import yaml; print(yaml.safe_load(open('$file'))['$key'])"
}
ynhtest_config_read_yaml() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.yml"
cat << EOF > $file
# Some comment
foo:
enabled: false
# title: old title
title: Lorem Ipsum
theme: colib'ris
email: root@example.com # This is a comment without quotes
port: 1234 # This is a comment without quotes
url: https://yunohost.org
dict:
ldap_base: ou=users,dc=yunohost,dc=org
EOF
test "$(_read_yaml "$file" "foo")" == "None"
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == ""
test "$(_read_yaml "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "false"
test "$(_read_yaml "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Lorem Ipsum"
test "$(_read_yaml "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file --file="$file" --key="theme")" == "colib'ris"
test "$(_read_yaml "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "root@example.com"
test "$(_read_yaml "$file" "port")" == "1234"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "1234"
test "$(_read_yaml "$file" "url")" == "https://yunohost.org"
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"
! _read_yaml "$file" "nonexistent"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! _read_yaml "$file" "enable"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
}
ynhtest_config_write_yaml() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.yml"
cat << EOF > $file
# Some comment
foo:
enabled: false
# title: old title
title: Lorem Ipsum
theme: colib'ris
email: root@example.com # This is a comment without quotes
port: 1234 # This is a comment without quotes
url: https://yunohost.org
dict:
ldap_base: ou=users,dc=yunohost,dc=org
EOF
ynh_write_var_in_file --file="$file" --key="foo" --value="bar"
# cat $dummy_dir/dummy.yml # to debug
! test "$(_read_yaml "$file" "foo")" == "bar" # writing broke the yaml syntax... "foo:bar" (no space aftr :)
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "bar"
ynh_write_var_in_file --file="$file" --key="enabled" --value="true"
test "$(_read_yaml "$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"
test "$(_read_yaml "$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"
test "$(_read_yaml "$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"
test "$(_read_yaml "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "sam@domain.tld"
ynh_write_var_in_file --file="$file" --key="port" --value="5678"
test "$(_read_yaml "$file" "port")" == "5678"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "5678"
ynh_write_var_in_file --file="$file" --key="url" --value="https://domain.tld/foobar"
test "$(_read_yaml "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file --file="$file" --key="url")" == "https://domain.tld/foobar"
ynh_write_var_in_file --file="$file" --key="ldap_base" --value="ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file --file="$file" --key="ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file --file="$file" --key="nonexistent" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file --file="$file" --key="enable" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "true"
}
#########################
# _ #
# (_) #
# _ ___ ___ _ __ #
# | / __|/ _ \| '_ \ #
# | \__ \ (_) | | | | #
# | |___/\___/|_| |_| #
# _/ | #
# |__/ #
# #
#########################
_read_json() {
local file="$1"
local key="$2"
python3 -c "import json; print(json.load(open('$file'))['$key'])"
}
ynhtest_config_read_json() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.json"
cat << EOF > $file
{
"foo": null,
"enabled": false,
"title": "Lorem Ipsum",
"theme": "colib'ris",
"email": "root@example.com",
"port": 1234,
"url": "https://yunohost.org",
"dict": {
"ldap_base": "ou=users,dc=yunohost,dc=org"
}
}
EOF
test "$(_read_json "$file" "foo")" == "None"
test "$(ynh_read_var_in_file --file="$file" --key="foo")" == "null"
test "$(_read_json "$file" "enabled")" == "False"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "false"
test "$(_read_json "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Lorem Ipsum"
test "$(_read_json "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file --file="$file" --key="theme")" == "colib'ris"
test "$(_read_json "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "root@example.com"
test "$(_read_json "$file" "port")" == "1234"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "1234"
test "$(_read_json "$file" "url")" == "https://yunohost.org"
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"
! _read_json "$file" "nonexistent"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! _read_json "$file" "enable"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
}
ynhtest_config_write_json() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.json"
cat << EOF > $file
{
"foo": null,
"enabled": false,
"title": "Lorem Ipsum",
"theme": "colib'ris",
"email": "root@example.com",
"port": 1234,
"url": "https://yunohost.org",
"dict": {
"ldap_base": "ou=users,dc=yunohost,dc=org"
}
}
EOF
ynh_write_var_in_file --file="$file" --key="foo" --value="bar"
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
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
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
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
test "$(_read_json "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "sam@domain.tld"
ynh_write_var_in_file --file="$file" --key="port" --value="5678"
test "$(_read_json "$file" "port")" == "5678"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "5678"
ynh_write_var_in_file --file="$file" --key="url" --value="https://domain.tld/foobar"
test "$(_read_json "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file --file="$file" --key="url")" == "https://domain.tld/foobar"
ynh_write_var_in_file --file="$file" --key="ldap_base" --value="ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file --file="$file" --key="ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file --file="$file" --key="nonexistent" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file --file="$file" --key="enable" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "true"
}
#######################
# _ #
# | | #
# _ __ | |__ _ __ #
# | '_ \| '_ \| '_ \ #
# | |_) | | | | |_) | #
# | .__/|_| |_| .__/ #
# | | | | #
# |_| |_| #
# #
#######################
_read_php() {
local file="$1"
local key="$2"
php -r "include '$file'; echo var_export(\$$key);" | sed "s/^'//g" | sed "s/'$//g"
}
ynhtest_config_read_php() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.php"
cat << EOF > $file
<?php
// Some comment
\$foo = NULL;
\$enabled = false;
// \$title = "old title";
\$title = "Lorem Ipsum";
\$theme = "colib'ris";
\$email = "root@example.com"; // This is a comment without quotes
\$port = 1234; // This is a second comment without quotes
\$url = "https://yunohost.org";
\$dict = [
'ldap_base' => "ou=users,dc=yunohost,dc=org",
'ldap_conf' => []
];
\$dict['ldap_conf']['user'] = 'camille';
const DB_HOST = 'localhost';
?>
EOF
test "$(_read_php "$file" "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 "$(_read_php "$file" "title")" == "Lorem Ipsum"
test "$(ynh_read_var_in_file --file="$file" --key="title")" == "Lorem Ipsum"
test "$(_read_php "$file" "theme")" == "colib\\'ris"
test "$(ynh_read_var_in_file --file="$file" --key="theme")" == "colib'ris"
test "$(_read_php "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "root@example.com"
test "$(_read_php "$file" "port")" == "1234"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "1234"
test "$(_read_php "$file" "url")" == "https://yunohost.org"
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"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! _read_php "$file" "enable"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
}
ynhtest_config_write_php() {
local dummy_dir="$(mktemp -d -p $VAR_WWW)"
file="$dummy_dir/dummy.php"
cat << EOF > $file
<?php
// Some comment
\$foo = NULL;
\$enabled = false;
// \$title = "old title";
\$title = "Lorem Ipsum";
\$theme = "colib'ris";
\$email = "root@example.com"; // This is a comment without quotes
\$port = 1234; // This is a comment without quotes
\$url = "https://yunohost.org";
\$dict = [
'ldap_base' => "ou=users,dc=yunohost,dc=org",
];
?>
EOF
ynh_write_var_in_file --file="$file" --key="foo" --value="bar"
test "$(_read_php "$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"
test "$(_read_php "$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
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
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="sam@domain.tld"
cat $file
test "$(_read_php "$file" "email")" == "sam@domain.tld"
test "$(ynh_read_var_in_file --file="$file" --key="email")" == "sam@domain.tld"
ynh_write_var_in_file --file="$file" --key="port" --value="5678"
test "$(_read_php "$file" "port")" == "5678"
test "$(ynh_read_var_in_file --file="$file" --key="port")" == "5678"
ynh_write_var_in_file --file="$file" --key="url" --value="https://domain.tld/foobar"
test "$(_read_php "$file" "url")" == "https://domain.tld/foobar"
test "$(ynh_read_var_in_file --file="$file" --key="url")" == "https://domain.tld/foobar"
ynh_write_var_in_file --file="$file" --key="ldap_base" --value="ou=foobar,dc=domain,dc=tld"
test "$(ynh_read_var_in_file --file="$file" --key="ldap_base")" == "ou=foobar,dc=domain,dc=tld"
! ynh_write_var_in_file --file="$file" --key="nonexistent" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="nonexistent")" == "YNH_NULL"
! ynh_write_var_in_file --file="$file" --key="enable" --value="foobar"
test "$(ynh_read_var_in_file --file="$file" --key="enable")" == "YNH_NULL"
test "$(ynh_read_var_in_file --file="$file" --key="enabled")" == "true"
}

View file

@ -0,0 +1,51 @@
ynhtest_exec_warn_less() {
FOO='foo'
bar=""
BAR='$bar'
FOOBAR="foo bar"
# These looks like stupid edge case
# but in fact happens when dealing with passwords
# (which could also contain bash chars like [], {}, ...)
# or urls containing &, ...
FOOANDBAR="foo&bar"
FOO1QUOTEBAR="foo'bar"
FOO2QUOTEBAR="foo\"bar"
ynh_hide_warnings uptime
test ! -e $FOO
ynh_hide_warnings touch $FOO
test -e $FOO
rm $FOO
test ! -e $FOO1QUOTEBAR
ynh_hide_warnings touch $FOO1QUOTEBAR
test -e $FOO1QUOTEBAR
rm $FOO1QUOTEBAR
test ! -e $FOO2QUOTEBAR
ynh_hide_warnings touch $FOO2QUOTEBAR
test -e $FOO2QUOTEBAR
rm $FOO2QUOTEBAR
test ! -e $BAR
ynh_hide_warnings touch $BAR
test -e $BAR
rm $BAR
test ! -e "$FOOBAR"
ynh_hide_warnings touch "$FOOBAR"
test -e "$FOOBAR"
rm "$FOOBAR"
test ! -e "$FOOANDBAR"
ynh_hide_warnings touch $FOOANDBAR
test -e "$FOOANDBAR"
rm "$FOOANDBAR"
test ! -e $FOO
! ynh_hide_warnings "touch $FOO"
! test -e $FOO
}

View file

@ -0,0 +1,71 @@
ynhtest_acceptable_path_to_delete() {
mkdir -p /home/someuser
mkdir -p /home/$app
mkdir -p /home/yunohost.app/$app
mkdir -p /var/www/$app
touch /var/www/$app/bar
touch /etc/cron.d/$app
! _acceptable_path_to_delete /
! _acceptable_path_to_delete ////
! _acceptable_path_to_delete " //// "
! _acceptable_path_to_delete /var
! _acceptable_path_to_delete /var/www
! _acceptable_path_to_delete /var/cache
! _acceptable_path_to_delete /usr
! _acceptable_path_to_delete /usr/bin
! _acceptable_path_to_delete /home
! _acceptable_path_to_delete /home/yunohost.backup
! _acceptable_path_to_delete /home/yunohost.app
! _acceptable_path_to_delete /home/yunohost.app/
! _acceptable_path_to_delete ///home///yunohost.app///
! _acceptable_path_to_delete /home/yunohost.app/$app/..
! _acceptable_path_to_delete ///home///yunohost.app///$app///..//
! _acceptable_path_to_delete /home/yunohost.app/../$app/..
! _acceptable_path_to_delete /home/someuser
! _acceptable_path_to_delete /home/yunohost.app//../../$app
! _acceptable_path_to_delete " /home/yunohost.app/// "
! _acceptable_path_to_delete /etc/cron.d/
! _acceptable_path_to_delete /etc/yunohost/
_acceptable_path_to_delete /home/yunohost.app/$app
_acceptable_path_to_delete /home/yunohost.app/$app/bar
_acceptable_path_to_delete /etc/cron.d/$app
_acceptable_path_to_delete /var/www/$app/bar
_acceptable_path_to_delete /var/www/$app
rm /var/www/$app/bar
rm /etc/cron.d/$app
rmdir /home/yunohost.app/$app
rmdir /home/$app
rmdir /home/someuser
rmdir /var/www/$app
}
ynhtest_safe_rm() {
mkdir -p /home/someuser
mkdir -p /home/yunohost.app/$app
mkdir -p /var/www/$app
mkdir -p /var/whatever
touch /var/www/$app/bar
touch /etc/cron.d/$app
! ynh_safe_rm "/home/someuser"
! ynh_safe_rm "/home/yunohost.app/"
! ynh_safe_rm "/var/whatever"
ynh_safe_rm "/home/yunohost.app/$app"
ynh_safe_rm "/var/www/$app"
ynh_safe_rm "/etc/cron.d/$app"
test -e /home/someuser
test -e /home/yunohost.app
test -e /var/whatever
! test -e /home/yunohost.app/$app
! test -e /var/www/$app
! test -e /etc/cron.d/$app
rmdir /home/someuser
rmdir /var/whatever
}

View file

@ -0,0 +1,58 @@
ynhtest_settings() {
test -n "$app"
mkdir -p "/etc/yunohost/apps/$app"
echo "label: $app" > "/etc/yunohost/apps/$app/settings.yml"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
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"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "$(ynh_app_setting_get --key="bar")"
rm -rf "/etc/yunohost/apps/$app"
}
ynhtest_setting_set_default() {
test -n "$app"
mkdir -p "/etc/yunohost/apps/$app"
echo "label: $app" > "/etc/yunohost/apps/$app/settings.yml"
test -z "$(ynh_app_setting_get --key="foo")"
test -z "${foo:-}"
ynh_app_setting_set_default --key="foo" --value="foovalue"
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"
test -z "$(ynh_app_setting_get --key="foo")"
ynh_app_setting_set_default --key="foo" --value="bar"
# Hmmm debatable ? But that's how it works right now because the var still exists
test "${foo:-}" == "foovalue"
test -z "$(ynh_app_setting_get --key="foo")"
rm -rf "/etc/yunohost/apps/$app"
}

View file

@ -0,0 +1,114 @@
_make_dummy_manifest() {
if [ ! -e $HTTPSERVER_DIR/dummy.tar.gz ]
then
pushd "$HTTPSERVER_DIR" >/dev/null
mkdir dummy
pushd dummy >/dev/null
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 >/dev/null
tar -czf dummy.tar.gz dummy >/dev/null
popd >/dev/null
fi
cat << EOF
packaging_format = 2
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}')"
[resources.install_dir]
group = "www-data:r-x"
EOF
}
ynhtest_setup_source_nominal() {
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"
test -e "$install_dir/index.html"
test -e "$install_dir/assets"
ls -ld "$install_dir" | grep -q "drwxr-x--- . $app www-data"
ls -l "$install_dir/index.html" | grep -q "\-rw-r----- . $app www-data"
ls -ld "$install_dir/assets" | grep -q "drwxr-x--- . $app www-data"
}
ynhtest_setup_source_no_group_in_manifest() {
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"
test -e "$install_dir/index.html"
ls -ld "$install_dir" | grep -q "drwxr-x--- . $app $app"
ls -l "$install_dir/index.html" | grep -q "\-rw-r----- . $app $app"
ls -ld "$install_dir/assets" | grep -q "drwxr-x--- . $app $app"
}
ynhtest_setup_source_nominal_upgrade() {
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"
# Except index.html to get overwritten during next ynh_setup_source
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"
}
ynhtest_setup_source_with_keep() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
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!"
}
ynhtest_setup_source_with_patch() {
install_dir="$(mktemp -d -p $VAR_WWW)"
_make_dummy_manifest > ../manifest.toml
mkdir -p ../patches/dummy/
cat > ../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="$install_dir" --source_id="dummy"
test "$(cat $install_dir/index.html)" == "Lorem Ipsum dolor sit amet"
}

View file

@ -0,0 +1,64 @@
ynhtest_simple_template_app_config() {
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
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"
}
ynhtest_simple_template_system_config() {
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
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')"
test "$(ls -l /etc/cron.d/ynhtest_config | cut -d' ' -f1-4)" == "-r-------- 1 root root"
rm -f /etc/cron.d/ynhtest_config
}
ynhtest_jinja_template_app_config() {
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
app={{ app }}
{% if foo == "bar" %}foo=true{% endif %}
EOF
foo="bar"
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"
}

View file

@ -0,0 +1,25 @@
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"
! ynh_system_user_exists --username="$username"
}
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
ynh_system_user_delete --username="$username"
}