[PATCH] tests: avoid subtle shell semantics bug

Jim Meyering jim at meyering.net
Mon May 9 19:56:32 UTC 2011


FYI,

>From 4e7ee40787b723700ce8f32ae3597b05f9182f5a Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Mon, 9 May 2011 21:55:53 +0200
Subject: [PATCH] tests: avoid subtle shell semantics bug

This bash command will always exit successfully,
i.e., the "return 1" will never happen:
  bash -c 'f(){ local f=$(false)||return 1; return 0; }; f'
That's because the assignment always succeeds, and that success
is what determines the return value, not the $(...) command.
This is very counter-intuitive, sigh.
The work-around is to separate the declaration and assignment, as in
local f; f=$(...) || ...
* t/replication: Do that.
* t/exercise (create_many): Likewise.
---
 t/exercise    |    2 +-
 t/replication |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/t/exercise b/t/exercise
index ca57145..83b6925 100755
--- a/t/exercise
+++ b/t/exercise
@@ -46,7 +46,7 @@ create_many()
   local nb=40

   # Use -j3 on a uniprocessor, else -j(3/2 N)
-  local n_procs=$(nproc || echo 2)
+  local n_procs; nprocs=$(nproc || echo 2)
   n_procs=$(expr $n_procs \* 3 / 2)

   for i in $(seq $nb); do
diff --git a/t/replication b/t/replication
index 30d761c..2a7a1ef 100644
--- a/t/replication
+++ b/t/replication
@@ -54,7 +54,7 @@ cleanup_() { kill -9 $mongo_pid; kill $iwhd_pid $iwhd_d_pid; }

 api=http://localhost:$port
 wait_for_repl() {
-	local n_req=$(curl -d op=rep_status $api) || return 1
+	local n_req; n_req=$(curl -d op=rep_status $api) || return 1
 	case $n_req in
 	  '0 requests') return 0 ;;
 	  *) return 1 ;;
--
1.7.5.1.354.g761178


More information about the iwhd-devel mailing list