From ee8a18abe6d5ecbbcdb5347099312ad8492be993 Mon Sep 17 00:00:00 2001 From: jmmv Date: Fri, 8 Mar 2013 17:45:13 +0000 Subject: Update to 1.1: - Added the shtk_config_run_hook function to invoke a hook in the context of a configuration file. --- devel/shtk/Makefile | 4 +-- devel/shtk/files/config.subr | 26 +++++++++++++++++++ devel/shtk/files/config_test.sh | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) (limited to 'devel') diff --git a/devel/shtk/Makefile b/devel/shtk/Makefile index 5943f8c68a9..ced350d843a 100644 --- a/devel/shtk/Makefile +++ b/devel/shtk/Makefile @@ -1,6 +1,6 @@ -# $NetBSD: Makefile,v 1.2 2012/10/31 11:19:46 asau Exp $ +# $NetBSD: Makefile,v 1.3 2013/03/08 17:45:13 jmmv Exp $ -DISTNAME= shtk-1.0 +DISTNAME= shtk-1.1 CATEGORIES= devel MASTER_SITES= # empty DISTFILES= # empty diff --git a/devel/shtk/files/config.subr b/devel/shtk/files/config.subr index f62ec8a678e..ee92521c234 100644 --- a/devel/shtk/files/config.subr +++ b/devel/shtk/files/config.subr @@ -299,3 +299,29 @@ shtk_config_override() { _Shtk_ConfigOverrides="${_Shtk_ConfigOverrides} unset:${var}" fi } + + +# Executes a hook in the context of a configuration file. +# +# The caller should not invoke the hook directly because then the hook would +# not have access to the configuration variables defined in the same file. +# Requiring the hook to use the various shtk_config_* methods would be weird. +# +# \post Any errors in the execution of the hook terminate the script. +# +# \param hook Name of the function to invoke. +# \param ... Arguments to pass to the hook. +shtk_config_run_hook() { + local hook="${1}"; shift + + ( + for var in ${_Shtk_ConfigVars}; do + if shtk_config_has "${var}"; then + eval "${var}"=\"$(shtk_config_get "${var}")\" + else + unset "${var}" + fi + done + "${hook}" "${@}" + ) || shtk_cli_error "The hook ${hook} returned an error" +} diff --git a/devel/shtk/files/config_test.sh b/devel/shtk/files/config_test.sh index 2a5b9a873c3..194f7e23d77 100644 --- a/devel/shtk/files/config_test.sh +++ b/devel/shtk/files/config_test.sh @@ -383,6 +383,60 @@ override__unknown_variable_body() { } +atf_test_case run_hook__ok +run_hook__ok_body() { + shtk_config_init VAR1 VAR2 VAR3 + shtk_config_set VAR1 "first" + shtk_config_set VAR3 "third" + + test_hook() { + echo "ARGS=${*}" + echo "VAR1=${VAR1:-unset}" + echo "VAR2=${VAR2:-unset}" + echo "VAR3=${VAR3:-unset}" + } + + VAR1=ignore-this; VAR2=ignore-this; VAR3=ignore-this + shtk_config_run_hook test_hook arg1 arg2 >out 2>err + + cat >expout <out 2>err; then + atf_fail "Hook failure did not report an error" + fi + ) + + cat >expout <experr </dev/null \ + || atf_fail "Expected error message not found" +} + + atf_init_test_cases() { atf_add_test_case is_valid__true atf_add_test_case is_valid__false @@ -421,4 +475,7 @@ atf_init_test_cases() { atf_add_test_case override__not_ok_after_load atf_add_test_case override__invalid_format atf_add_test_case override__unknown_variable + + atf_add_test_case run_hook__ok + atf_add_test_case run_hook__fail } -- cgit v1.2.3