Re: [PATCH 1/2] test-lib: mark function variables as local

2020-05-08 Thread Daniel Kahn Gillmor
On Thu 2020-05-07 10:31:38 +0300, Tomi Ollila wrote:
> Good stuff
>
> robustness comment IMO:
>
> There is slight difference when writing
>
> local foo=`false`
>
> and
>
> local foo; foo=`false`
>
>
> former does not "fail"; latter does,

thanks for pointing this out.  On IRC, jindraj pointed me to
http://tldp.org/LDP/abs/html/localvar.html   this is an unusual and
confusing set of behaviors i had not understood about how local
interacts with return codes, etc.

I'll send around a revised set of patches that uses "local foo" instead
of "local foo=".

 --dkg


signature.asc
Description: PGP signature
___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH 1/2] test-lib: mark function variables as local

2020-05-07 Thread Tomi Ollila
On Wed, May 06 2020, Daniel Kahn Gillmor wrote:

> Several functions in test/test-lib.sh used variable names that are
> also used outside of those functions (e.g. $output and $expected are
> used in many of the test scripts), but they are not expected to
> communicate via those variables.
>
> We mark those variables "local" within test-lib.sh so that they do not
> get clobbered when used outside test-lib.


Good stuff

robustness comment IMO:

There is slight difference when writing

local foo=`false`

and

local foo; foo=`false`


former does not "fail"; latter does,


Although there is (currently!) no difference in our test code
(we don't have `set -e` there, IMO the former serves as a bad
example for anyone looking the code. 

(same applies to export foo=`bar`, readonly foo=`bar` and so
on, for anyone curious...)

IMO better declare all local variables in one line separately,
e.g.

local output expected

and then either

output=$1
expected=$2
or
output=$1 expected=$2

( FYI: exection latter in shell differs in a way one could do
  output=$expected expected=$output ) (IIRC, did not test >;)

(add double quotes around $1 and $2 if you desire =D)


well, when doing change just add the `local` line, smaller diff :)


Tomi

>
> Signed-off-by: Daniel Kahn Gillmor 
> ---
>  test/test-lib.sh | 44 ++--
>  1 file changed, 22 insertions(+), 22 deletions(-)
>
> diff --git a/test/test-lib.sh b/test/test-lib.sh
> index 5c8eab7c..e8feab3b 100644
> --- a/test/test-lib.sh
> +++ b/test/test-lib.sh
> @@ -109,7 +109,6 @@ unset ALTERNATE_EDITOR
>  
>  add_gnupg_home ()
>  {
> -local output
>  [ -e "${GNUPGHOME}/gpg.conf" ] && return
>  _gnupg_exit () { gpgconf --kill all 2>/dev/null || true; }
>  at_exit_function _gnupg_exit
> @@ -427,7 +426,7 @@ emacs_fcc_message ()
>  # number of messages.
>  add_email_corpus ()
>  {
> -corpus=${1:-default}
> +local corpus=${1:-default}
>  
___
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch


[PATCH 1/2] test-lib: mark function variables as local

2020-05-06 Thread Daniel Kahn Gillmor
Several functions in test/test-lib.sh used variable names that are
also used outside of those functions (e.g. $output and $expected are
used in many of the test scripts), but they are not expected to
communicate via those variables.

We mark those variables "local" within test-lib.sh so that they do not
get clobbered when used outside test-lib.

Signed-off-by: Daniel Kahn Gillmor 
---
 test/test-lib.sh | 44 ++--
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/test/test-lib.sh b/test/test-lib.sh
index 5c8eab7c..e8feab3b 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -109,7 +109,6 @@ unset ALTERNATE_EDITOR
 
 add_gnupg_home ()
 {
-local output
 [ -e "${GNUPGHOME}/gpg.conf" ] && return
 _gnupg_exit () { gpgconf --kill all 2>/dev/null || true; }
 at_exit_function _gnupg_exit
@@ -427,7 +426,7 @@ emacs_fcc_message ()
 # number of messages.
 add_email_corpus ()
 {
-corpus=${1:-default}
+local corpus=${1:-default}
 
 rm -rf ${MAIL_DIR}
 cp -a $NOTMUCH_SRCDIR/test/corpora/$corpus ${MAIL_DIR}
@@ -465,14 +464,14 @@ test_expect_equal ()
test "$#" = 2 ||
error "bug in the test script: not 2 parameters to test_expect_equal"
 
-   output="$1"
-   expected="$2"
+   local output="$1"
+   local expected="$2"
if ! test_skip "$test_subtest_name"
then
if [ "$output" = "$expected" ]; then
test_ok_
else
-   testname=$this_test.$test_count
+   local testname=$this_test.$test_count
echo "$expected" > $testname.expected
echo "$output" > $testname.output
test_failure_ "$(diff -u $testname.expected 
$testname.output)"
@@ -491,16 +490,16 @@ test_expect_equal_file ()
test "$#" = 2 ||
error "bug in the test script: not 2 parameters to 
test_expect_equal_file"
 
-   file1="$1"
-   file2="$2"
+   local file1="$1"
+   local file2="$2"
if ! test_skip "$test_subtest_name"
then
if diff -q "$file1" "$file2" >/dev/null ; then
test_ok_
else
-   testname=$this_test.$test_count
-   basename1=`basename "$file1"`
-   basename2=`basename "$file2"`
+   local testname=$this_test.$test_count
+   local basename1=`basename "$file1"`
+   local basename2=`basename "$file2"`
cp "$file1" "$testname.$basename1"
cp "$file2" "$testname.$basename2"
test_failure_ "$(diff -u "$testname.$basename1" 
"$testname.$basename2")"
@@ -516,9 +515,9 @@ test_expect_equal_json () {
 # decode stdin as ASCII.  We need to read JSON in UTF-8, so
 # override Python's stdio encoding defaults.
 local script='import json, sys; json.dump(json.load(sys.stdin), 
sys.stdout, sort_keys=True, indent=4)'
-output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c "$script" \
+local output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c 
"$script" \
 || echo "$1")
-expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c "$script" 
\
+local expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c 
"$script" \
 || echo "$2")
 shift 2
 test_expect_equal "$output" "$expected" "$@"
@@ -540,6 +539,7 @@ test_sort_json () {
 # read the source of test/json_check_nodes.py (or the output when
 # invoking it without arguments) for an explanation of the syntax.
 test_json_nodes () {
+local output
 exec 1>&6 2>&7 # Restore stdout and stderr
if [ -z "$inside_subtest" ]; then
error "bug in the test script: test_json_eval without 
test_begin_subtest"
@@ -577,7 +577,7 @@ test_emacs_expect_t () {
inside_subtest=
 
# Report success/failure.
-   result=$(cat OUTPUT)
+   local result=$(cat OUTPUT)
if [ "$result" = t ]
then
test_ok_
@@ -717,7 +717,7 @@ declare -A test_subtest_missing_external_prereq_
 
 # declare prerequisite for the given external binary
 test_declare_external_prereq () {
-   binary="$1"
+   local binary="$1"
test "$#" = 2 && name=$2 || name="$binary(1)"
 
if ! hash $binary 2>/dev/null; then
@@ -734,7 +734,7 @@ $binary () {
 # called indirectly (e.g. from emacs).
 # Returns success if dependency is available, failure otherwise.
 test_require_external_prereq () {
-   binary="$1"
+   local binary="$1"
if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
# dependency is missing, call the replacement function to note 
it
eval "$binary"
@@ -1075,8 +1075,8 @@ test_ruby() {
 }
 
 test_C