* [PATCH] test: Use associative arrays to track external prereqs
@ 2012-11-28 4:54 Austin Clements
2012-11-28 8:00 ` Tomi Ollila
2012-11-29 13:26 ` David Bremner
0 siblings, 2 replies; 3+ messages in thread
From: Austin Clements @ 2012-11-28 4:54 UTC (permalink / raw)
To: notmuch
Previously, the test framework generated a variable name for each
external prereq as a poor man's associative array. Unfortunately,
prereqs names may not be legal variable names, leading to
unintelligible bash errors like
test_missing_external_prereq_emacsclient.emacs24_=t: command not found
Using proper associative arrays to track prereqs, in addition to being
much cleaner than generating variable names and using grep to
carefully construct unique string lists, removes restrictions on
prereq names.
---
test/test-lib.sh | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/test/test-lib.sh b/test/test-lib.sh
index 77063a4..f169785 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -625,18 +625,22 @@ test_have_prereq () {
esac
}
+declare -A test_missing_external_prereq_
+declare -A test_subtest_missing_external_prereq_
+
# declare prerequisite for the given external binary
test_declare_external_prereq () {
binary="$1"
test "$#" = 2 && name=$2 || name="$binary(1)"
- hash $binary 2>/dev/null || eval "
- test_missing_external_prereq_${binary}_=t
+ if ! hash $binary 2>/dev/null; then
+ test_missing_external_prereq_["${binary}"]=t
+ eval "
$binary () {
- echo -n \"\$test_subtest_missing_external_prereqs_ \" | grep -qe \" $name \" ||
- test_subtest_missing_external_prereqs_=\"\$test_subtest_missing_external_prereqs_ $name\"
+ test_subtest_missing_external_prereq_[\"${name}\"]=t
false
}"
+ fi
}
# Explicitly require external prerequisite. Useful when binary is
@@ -644,7 +648,7 @@ $binary () {
# Returns success if dependency is available, failure otherwise.
test_require_external_prereq () {
binary="$1"
- if [ "$(eval echo -n \$test_missing_external_prereq_${binary}_)" = t ]; then
+ if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
# dependency is missing, call the replacement function to note it
eval "$binary"
else
@@ -737,9 +741,9 @@ test_skip () {
}
test_check_missing_external_prereqs_ () {
- if test -n "$test_subtest_missing_external_prereqs_"; then
- say_color skip >&1 "missing prerequisites:"
- echo "$test_subtest_missing_external_prereqs_" >&1
+ if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
+ say_color skip >&1 "missing prerequisites: "
+ echo ${!test_subtest_missing_external_prereq_[@]} >&1
test_report_skip_ "$@"
else
false
@@ -1022,7 +1026,7 @@ test_python() {
# most others as /usr/bin/python. So first try python2, and fallback to
# python if python2 doesn't exist.
cmd=python2
- [[ "$test_missing_external_prereq_python2_" = t ]] && cmd=python
+ [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
(echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
| $cmd -
@@ -1064,7 +1068,7 @@ test_reset_state_ () {
test -z "$test_init_done_" && test_init_
test_subtest_known_broken_=
- test_subtest_missing_external_prereqs_=
+ test_subtest_missing_external_prereq_=()
}
# called once before the first subtest
--
1.7.10.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] test: Use associative arrays to track external prereqs
2012-11-28 4:54 [PATCH] test: Use associative arrays to track external prereqs Austin Clements
@ 2012-11-28 8:00 ` Tomi Ollila
2012-11-29 13:26 ` David Bremner
1 sibling, 0 replies; 3+ messages in thread
From: Tomi Ollila @ 2012-11-28 8:00 UTC (permalink / raw)
To: Austin Clements, notmuch
On Wed, Nov 28 2012, Austin Clements <amdragon@MIT.EDU> wrote:
> Previously, the test framework generated a variable name for each
> external prereq as a poor man's associative array. Unfortunately,
> prereqs names may not be legal variable names, leading to
> unintelligible bash errors like
> test_missing_external_prereq_emacsclient.emacs24_=t: command not found
>
> Using proper associative arrays to track prereqs, in addition to being
> much cleaner than generating variable names and using grep to
> carefully construct unique string lists, removes restrictions on
> prereq names.
> ---
That looks good and something like I once attempted
(at that time this would have make tests fail as this
speeds up the script execution so much that smtp-dummy
may not have time to start before it was used -- now
that is taken care by smtp-dummy --background feature :)
+1
Tomi
> test/test-lib.sh | 24 ++++++++++++++----------
> 1 file changed, 14 insertions(+), 10 deletions(-)
>
> diff --git a/test/test-lib.sh b/test/test-lib.sh
> index 77063a4..f169785 100644
> --- a/test/test-lib.sh
> +++ b/test/test-lib.sh
> @@ -625,18 +625,22 @@ test_have_prereq () {
> esac
> }
>
> +declare -A test_missing_external_prereq_
> +declare -A test_subtest_missing_external_prereq_
> +
> # declare prerequisite for the given external binary
> test_declare_external_prereq () {
> binary="$1"
> test "$#" = 2 && name=$2 || name="$binary(1)"
>
> - hash $binary 2>/dev/null || eval "
> - test_missing_external_prereq_${binary}_=t
> + if ! hash $binary 2>/dev/null; then
> + test_missing_external_prereq_["${binary}"]=t
> + eval "
> $binary () {
> - echo -n \"\$test_subtest_missing_external_prereqs_ \" | grep -qe \" $name \" ||
> - test_subtest_missing_external_prereqs_=\"\$test_subtest_missing_external_prereqs_ $name\"
> + test_subtest_missing_external_prereq_[\"${name}\"]=t
> false
> }"
> + fi
> }
>
> # Explicitly require external prerequisite. Useful when binary is
> @@ -644,7 +648,7 @@ $binary () {
> # Returns success if dependency is available, failure otherwise.
> test_require_external_prereq () {
> binary="$1"
> - if [ "$(eval echo -n \$test_missing_external_prereq_${binary}_)" = t ]; then
> + if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
> # dependency is missing, call the replacement function to note it
> eval "$binary"
> else
> @@ -737,9 +741,9 @@ test_skip () {
> }
>
> test_check_missing_external_prereqs_ () {
> - if test -n "$test_subtest_missing_external_prereqs_"; then
> - say_color skip >&1 "missing prerequisites:"
> - echo "$test_subtest_missing_external_prereqs_" >&1
> + if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
> + say_color skip >&1 "missing prerequisites: "
> + echo ${!test_subtest_missing_external_prereq_[@]} >&1
> test_report_skip_ "$@"
> else
> false
> @@ -1022,7 +1026,7 @@ test_python() {
> # most others as /usr/bin/python. So first try python2, and fallback to
> # python if python2 doesn't exist.
> cmd=python2
> - [[ "$test_missing_external_prereq_python2_" = t ]] && cmd=python
> + [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
>
> (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
> | $cmd -
> @@ -1064,7 +1068,7 @@ test_reset_state_ () {
> test -z "$test_init_done_" && test_init_
>
> test_subtest_known_broken_=
> - test_subtest_missing_external_prereqs_=
> + test_subtest_missing_external_prereq_=()
> }
>
> # called once before the first subtest
> --
> 1.7.10.4
>
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] test: Use associative arrays to track external prereqs
2012-11-28 4:54 [PATCH] test: Use associative arrays to track external prereqs Austin Clements
2012-11-28 8:00 ` Tomi Ollila
@ 2012-11-29 13:26 ` David Bremner
1 sibling, 0 replies; 3+ messages in thread
From: David Bremner @ 2012-11-29 13:26 UTC (permalink / raw)
To: Austin Clements, notmuch
Austin Clements <amdragon@MIT.EDU> writes:
> Using proper associative arrays to track prereqs, in addition to being
> much cleaner than generating variable names and using grep to
> carefully construct unique string lists, removes restrictions on
> prereq names.
pushed. FWIW, I think this is a sensible bash4-ism.
d
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-11-29 13:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-28 4:54 [PATCH] test: Use associative arrays to track external prereqs Austin Clements
2012-11-28 8:00 ` Tomi Ollila
2012-11-29 13:26 ` David Bremner
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).