commit:     586a56440594d8cde2ceb75e90cc0f4a19d8a8f5
Author:     Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq <DOT> in>
AuthorDate: Tue Jan 23 15:24:35 2024 +0000
Commit:     Anna Vyalkova <cyber+gentoo <AT> sysrq <DOT> in>
CommitDate: Wed Jan 24 18:43:00 2024 +0000
URL:        https://gitweb.gentoo.org/repo/proj/guru.git/commit/?id=586a5644

databases.eclass: new --add-deps helper

Signed-off-by: Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq.in>

 eclass/databases.eclass   | 101 +++++++++++++++++++++++++++++-----------------
 eclass/tests/databases.sh |  86 ++++++++++++++++++++++++++++++---------
 2 files changed, 131 insertions(+), 56 deletions(-)

diff --git a/eclass/databases.eclass b/eclass/databases.eclass
index 9c2457e93e..1c640d3ee9 100644
--- a/eclass/databases.eclass
+++ b/eclass/databases.eclass
@@ -28,6 +28,11 @@
 #
 # @SUBSECTION Helper usage
 #
+# --add-deps <use>
+#
+#      Adds the server package to build-time dependencies under the given USE 
flag
+#      (IUSE will be set automatically).
+#
 # --die [msg]
 #
 #      Prints the path to the server's log file to the console and aborts the
@@ -79,10 +84,10 @@
 #
 # ...
 #
-# BDEPEND="test? ( ${DATABASES_DEPEND[postgres]} )"
-#
 # distutils_enable_tests pytest
 #
+# epostgres --add-deps test
+#
 # src_test() {
 #      epostgres --start 65432
 #      distutils-r1_src_test
@@ -118,39 +123,41 @@ declare -Ag DATABASES_DEPEND=()
 
 # @FUNCTION: _databases_set_globals
 # @INTERNAL
+# @DESCRIPTION:
+# Set the DATABASES_DEPEND variable.
 _databases_set_globals() {
-               local -A db_pkgs=(
-                               [memcached]="net-misc/memcached"
-                               [mongod]="dev-db/mongodb"
-                               [mysql]="virtual/mysql"
-                               [postgres]="dev-db/postgresql"
-                               [redis]="dev-db/redis"
-               )
-
-               local -A db_useflags=(
-                               [mysql]="server"
-                               [postgres]="server"
-               )
-               
-               if declare -p DATABASES_REQ_USE &>/dev/null; then
-                               [[ $(declare -p DATABASES_REQ_USE) == "declare 
-A"* ]] || \
-                                               die "DATABASES_REQ_USE must be 
declared as an associative array"
-               fi
-
-               local name dep usestr
-               for name in "${!db_pkgs[@]}"; do
-                               dep=${db_pkgs[${name}]?}
-                               usestr=${db_useflags[${name}]}
-                               usestr+=",${DATABASES_REQ_USE[${name}]}"
-                               # strip leading/trailing commas
-                               usestr=${usestr#,}
-                               usestr=${usestr%,}
-
-                               [[ ${usestr} ]] && usestr="[${usestr}]"
-                               DATABASES_DEPEND[${name?}]="${dep?}${usestr}"
-               done
-
-               readonly DATABASES_DEPEND
+       local -A db_pkgs=(
+               [memcached]="net-misc/memcached"
+               [mongod]="dev-db/mongodb"
+               [mysql]="virtual/mysql"
+               [postgres]="dev-db/postgresql"
+               [redis]="dev-db/redis"
+       )
+
+       local -A db_useflags=(
+               [mysql]="server"
+               [postgres]="server"
+       )
+
+       if declare -p DATABASES_REQ_USE &>/dev/null; then
+               [[ $(declare -p DATABASES_REQ_USE) == "declare -A"* ]] || \
+                       die "DATABASES_REQ_USE must be declared as an 
associative array"
+       fi
+
+       local name dep usestr
+       for name in "${!db_pkgs[@]}"; do
+               dep=${db_pkgs[${name}]?}
+               usestr=${db_useflags[${name}]}
+               usestr+=",${DATABASES_REQ_USE[${name}]}"
+               # strip leading/trailing commas
+               usestr=${usestr#,}
+               usestr=${usestr%,}
+
+               [[ ${usestr} ]] && usestr="[${usestr}]"
+               DATABASES_DEPEND[${name?}]="${dep?}${usestr}"
+       done
+
+       readonly DATABASES_DEPEND
 }
 _databases_set_globals
 unset -f _databases_set_globals
@@ -176,6 +183,23 @@ _databases_die() {
        die -n "${@}"
 }
 
+# @FUNCTION: _databases_add_deps
+# @USAGE: <funcname> <use>
+# @INTERNAL
+# @DESCRIPTION:
+# Set the BDEPEND, IUSE and RESTRICT variables.
+_databases_add_deps() {
+       local funcname=${1?}
+       local useflag=${2?}
+
+       BDEPEND="${useflag}? ( ${DATABASES_DEPEND[${funcname:1}]} )"
+       IUSE="${useflag}"
+       [[ ${useflag} == "test" ]] &&
+               RESTRICT="!test? ( test )"
+
+       return 0
+}
+
 # @FUNCTION: _databases_stop_service
 # @USAGE: <funcname>
 # @INTERNAL
@@ -200,19 +224,22 @@ _databases_stop_service() {
 # @DESCRIPTION:
 # Process the given command with its options.
 #
-# If "--start" command is used, `_${funcname}_start` function must be defined.
+# If "--start" command is used, "_${funcname}_start" function must be defined.
 # Note that directories will be created automatically.
 #
-# If `_${funcname}_stop` function is not declared, the internal
+# If "_${funcname}_stop" function is not declared, the internal
 # `_databases_stop_service` function will be used instead.
 #
-# No `--get` function can be overloaded.
+# "--get-*" and "-add-deps" helpers cannot be overloaded.
 _databases_dispatch() {
        local funcname=${1?}
        local cmd=${2?}
        shift; shift
 
        case ${cmd} in
+               --add-deps)
+                       _databases_add_deps ${funcname} "${@}"
+                       ;;
                --die)
                        _databases_die ${funcname} "${@}"
                        ;;

diff --git a/eclass/tests/databases.sh b/eclass/tests/databases.sh
index 92cbbd13bf..08dd75472f 100755
--- a/eclass/tests/databases.sh
+++ b/eclass/tests/databases.sh
@@ -9,36 +9,70 @@ source "${GENTOO_REPO}"/eclass/tests/tests-common.sh || exit
 TESTS_ECLASS_SEARCH_PATHS+=( "${GENTOO_REPO}"/eclass )
 
 declare -A DATABASES_REQ_USE=(
-               [mongod]="ssl"
-               [postgres]="xml"
+       [mongod]="ssl"
+       [postgres]="xml"
 )
 
 inherit databases
 
+HELPERS=( ememcached emongod emysql epostgres eredis )
+
 test_depend() {
-               tbegin "if \${DATABASES_DEPEND} is defined"
-               declare -p DATABASES_DEPEND &>/dev/null
-               tend $?
+       tbegin "if \${DATABASES_DEPEND} is defined"
+       declare -p DATABASES_DEPEND &>/dev/null
+       tend $?
 
-               tbegin "\${DATABASES_DEPEND[memcached]}"
-               test "${DATABASES_DEPEND[memcached]}" == "net-misc/memcached"
-               tend $?
+       tbegin "\${DATABASES_DEPEND[memcached]}"
+       test "${DATABASES_DEPEND[memcached]}" == "net-misc/memcached"
+       tend $?
 
-               tbegin "\${DATABASES_DEPEND[mongod]}"
-               test "${DATABASES_DEPEND[mongod]}" == "dev-db/mongodb[ssl]"
-               tend $?
+       tbegin "\${DATABASES_DEPEND[mongod]}"
+       test "${DATABASES_DEPEND[mongod]}" == "dev-db/mongodb[ssl]"
+       tend $?
 
-               tbegin "\${DATABASES_DEPEND[mysql]}"
-               test "${DATABASES_DEPEND[mysql]}" == "virtual/mysql[server]"
-               tend $?
+       tbegin "\${DATABASES_DEPEND[mysql]}"
+       test "${DATABASES_DEPEND[mysql]}" == "virtual/mysql[server]"
+       tend $?
 
-               tbegin "\${DATABASES_DEPEND[postgres]}"
-               test "${DATABASES_DEPEND[postgres]}" == 
"dev-db/postgresql[server,xml]"
-               tend $?
+       tbegin "\${DATABASES_DEPEND[postgres]}"
+       test "${DATABASES_DEPEND[postgres]}" == "dev-db/postgresql[server,xml]"
+       tend $?
+
+       tbegin "\${DATABASES_DEPEND[redis]}"
+       test "${DATABASES_DEPEND[redis]}" == "dev-db/redis"
+       tend $?
+}
+
+test_add_deps() {
+       local IUSE= RESTRICT= BDEPEND=
+       local helper=${1?}
+
+       IUSE= RESTRICT= BDEPEND=
+       tbegin "'${helper} --add-deps test'"
+       ${helper} --add-deps test && [[ ${BDEPEND} && ${IUSE} && ${RESTRICT} ]]
+       tend $?
 
-               tbegin "\${DATABASES_DEPEND[redis]}"
-               test "${DATABASES_DEPEND[redis]}" == "dev-db/redis"
+       IUSE= RESTRICT= BDEPEND=
+       tbegin "'${helper} --add-deps test-db'"
+       ${helper} --add-deps test-db && [[ ${BDEPEND} && ${IUSE} && ! 
${RESTRICT} ]]
+       tend $?
+}
+
+test_getters() {
+       local helper=${1?}
+       local getters=(
+               --get-dbpath
+               --get-logfile
+               --get-pidfile
+               --get-sockdir
+               --get-sockfile
+       )
+
+       for getter in "${getters[@]}"; do
+               tbegin "'${helper} ${getter}'"
+               test -n "$(${helper} ${getter})"
                tend $?
+       done
 }
 
 einfo "Testing dependency strings"
@@ -46,4 +80,18 @@ eindent
 test_depend
 eoutdent
 
+einfo "Testing --add-deps helper"
+eindent
+for helper in "${HELPERS[@]}"; do
+       test_add_deps ${helper}
+done
+eoutdent
+
+einfo "Testing --get-* helpers"
+eindent
+for helper in "${HELPERS[@]}"; do
+       test_getters ${helper}
+done
+eoutdent
+
 texit

Reply via email to