Hi, in RHBZ, there was reported problem with xzgrep, we should exit 0 when
at lest one file contains matching string.  Grep behaves similarly.

Original bugreport:
https://bugzilla.redhat.com/show_bug.cgi?id=1108085

Patch is attached,
Pavel
>From f110a5a729a11de9b1e73699dfa94da409bd9d1a Mon Sep 17 00:00:00 2001
From: Pavel Raiskup <prais...@redhat.com>
Date: Wed, 11 Jun 2014 15:31:45 +0200
Subject: [PATCH] xz*grep: exit 0 when at least one file matches

Mimic the original grep behavior and return exit_success when at
least one xz compressed file matches given pattern.

Original bugreport:
https://bugzilla.redhat.com/show_bug.cgi?id=1108085
---
 src/scripts/xzgrep.in       | 15 +++++++++++++--
 tests/expected/test_scripts | 39 +++++++++++++++++++++++++++++++++++++++
 tests/output/.gitignore     |  1 +
 tests/test_scripts.sh       | 27 +++++++++++++++++++++++----
 4 files changed, 76 insertions(+), 6 deletions(-)
 create mode 100644 tests/expected/test_scripts
 create mode 100644 tests/output/.gitignore

diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in
index 951266b..6ed8325 100644
--- a/src/scripts/xzgrep.in
+++ b/src/scripts/xzgrep.in
@@ -147,7 +147,9 @@ if test $# -eq 0; then
 fi
 
 exec 3>&1
-res=0
+
+# res=1 means that no file matched yet
+res=1
 
 for i; do
   case $i in
@@ -195,8 +197,17 @@ for i; do
     fi >&3 5>&-
   )
   r=$?
+
+  # fail occured previously, nothing worse can happen
+  test $res -gt 1 && continue
+
   test "$xz_status" -eq 0 || test "$xz_status" -eq 2 \
       || test "$(kill -l "$xz_status" 2> /dev/null)" = "PIPE" || r=2
-  test $res -lt $r && res=$r
+
+  # still no match
+  test $r -eq 1 && continue
+
+  # 0 == match, >2 == fail
+  res=$r
 done
 exit $res
diff --git a/tests/expected/test_scripts b/tests/expected/test_scripts
new file mode 100644
index 0000000..0eb67b8
--- /dev/null
+++ b/tests/expected/test_scripts
@@ -0,0 +1,39 @@
+=> ../src/scripts/xzgrep  el <=
+./files/good-1-lzma2-1.xz:elit, sed do eiusmod tempor incididunt ut 
+./files/good-1-lzma2-1.xz:in voluptate velit esse cillum dolore eu 
+./files/good-2-lzma2.xz:Hello
+retval 0
+=> ../src/scripts/xzgrep -l el <=
+./files/good-1-lzma2-1.xz
+./files/good-2-lzma2.xz
+retval 0
+=> ../src/scripts/xzgrep -h el <=
+elit, sed do eiusmod tempor incididunt ut 
+in voluptate velit esse cillum dolore eu 
+Hello
+retval 0
+=> ../src/scripts/xzgrep -H el <=
+./files/good-1-lzma2-1.xz:elit, sed do eiusmod tempor incididunt ut 
+./files/good-1-lzma2-1.xz:in voluptate velit esse cillum dolore eu 
+./files/good-2-lzma2.xz:Hello
+retval 0
+=> ../src/scripts/xzgrep  Hello <=
+./files/good-2-lzma2.xz:Hello
+retval 0
+=> ../src/scripts/xzgrep -l Hello <=
+./files/good-2-lzma2.xz
+retval 0
+=> ../src/scripts/xzgrep -h Hello <=
+Hello
+retval 0
+=> ../src/scripts/xzgrep -H Hello <=
+./files/good-2-lzma2.xz:Hello
+retval 0
+=> ../src/scripts/xzgrep  NOMATCH <=
+retval 1
+=> ../src/scripts/xzgrep -l NOMATCH <=
+retval 1
+=> ../src/scripts/xzgrep -h NOMATCH <=
+retval 1
+=> ../src/scripts/xzgrep -H NOMATCH <=
+retval 1
diff --git a/tests/output/.gitignore b/tests/output/.gitignore
new file mode 100644
index 0000000..33662f5
--- /dev/null
+++ b/tests/output/.gitignore
@@ -0,0 +1 @@
+/*
diff --git a/tests/test_scripts.sh b/tests/test_scripts.sh
index 293929e..fcec3c0 100755
--- a/tests/test_scripts.sh
+++ b/tests/test_scripts.sh
@@ -12,16 +12,18 @@
 # If scripts weren't built, this test is skipped.
 XZ=../src/xz/xz
 XZDIFF=../src/scripts/xzdiff
-test -x "$XZ" || XZ=
-test -x "$XZDIFF" || XZDIFF=
-if test -z "$XZ" || test -z "$XZDIFF"; then
+XZGREP=../src/scripts/xzgrep
+
+for i in XZ XZDIFF XZGREP; do
+	eval test -x "\$$i" && continue
 	(exit 77)
 	exit 77
-fi
+done
 
 PATH=`pwd`/../src/xz:$PATH
 export PATH
 
+test -z "$srcdir" && srcdir=.
 preimage=$srcdir/files/good-1-check-crc32.xz
 samepostimage=$srcdir/files/good-1-check-crc64.xz
 otherpostimage=$srcdir/files/good-1-lzma2-1.xz
@@ -50,5 +52,22 @@ if test "$status" != 2 ; then
 	exit 1
 fi
 
+for pattern in el Hello NOMATCH; do
+	for opts in "" "-l" "-h" "-H"; do
+		echo "=> $XZGREP $opts $pattern <="
+		"$XZGREP" $opts $pattern \
+			"$srcdir/files/good-1-lzma2-1.xz" \
+			"$srcdir/files/good-2-lzma2.xz"
+		echo retval $?
+	done
+done > "$srcdir/output/test_scripts"
+
+diff -u "$srcdir/expected/test_scripts" "$srcdir/output/test_scripts"
+if test "$status" != 2 ; then
+	exho "unexpected output"
+	(exit 1)
+	exit 1
+fi
+
 (exit 0)
 exit 0
-- 
1.9.3

Reply via email to