Update to 1.1:

- Added the shtk_config_run_hook function to invoke a hook in the context
  of a configuration file.
This commit is contained in:
jmmv 2013-03-08 17:45:13 +00:00
parent a1b7a04854
commit 3d01843ffa
3 changed files with 85 additions and 2 deletions

View file

@ -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

View file

@ -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"
}

View file

@ -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 <<EOF
ARGS=arg1 arg2
VAR1=first
VAR2=unset
VAR3=third
EOF
atf_check -o file:expout cat out
atf_check -o empty cat err
}
atf_test_case run_hook__fail
run_hook__fail_body() {
shtk_config_init VAR1
shtk_config_set VAR1 "first"
test_hook() {
echo "VAR1=${VAR1:-unset}"
false
}
(
if shtk_config_run_hook test_hook >out 2>err; then
atf_fail "Hook failure did not report an error"
fi
)
cat >expout <<EOF
VAR1=first
EOF
cat >experr <<EOF
EOF
atf_check -o file:expout cat out
grep "The hook test_hook returned an error" err >/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
}