594167
From e7b657694adbd03403f2ebbe089a6d5baa58d7d5 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Tue, 14 Jun 2022 09:00:00 +0900
594167
Subject: [PATCH] test: introduce assert_not_in() helper function
594167
594167
This also silence assertions, and replace grep with bash's regular
594167
expression match.
594167
594167
(cherry picked from commit d170b47535e2acc8abd1af85ff8685107fdd490f)
594167
594167
Related: #2087652
594167
---
594167
 test/units/assert.sh       | 48 ++++++++++++++++++++++++--------------
594167
 test/units/testsuite-45.sh |  2 +-
594167
 2 files changed, 32 insertions(+), 18 deletions(-)
594167
594167
diff --git a/test/units/assert.sh b/test/units/assert.sh
594167
index db67dad268..66357ab688 100644
594167
--- a/test/units/assert.sh
594167
+++ b/test/units/assert.sh
594167
@@ -3,42 +3,56 @@
594167
 
594167
 # utility functions for shell tests
594167
 
594167
-assert_true() {
594167
+assert_true() {(
594167
     local rc
594167
 
594167
-    set +e
594167
+    set +ex
594167
+
594167
     "$@"
594167
     rc=$?
594167
-    set -e
594167
-    if [[ "$rc" != "0" ]]; then
594167
+    if [[ $rc -ne 0 ]]; then
594167
         echo "FAIL: command '$*' failed with exit code $rc" >&2
594167
         exit 1
594167
     fi
594167
-}
594167
+)}
594167
+
594167
 
594167
+assert_eq() {(
594167
+    set +ex
594167
 
594167
-assert_eq() {
594167
-    if [[ "$1" != "$2" ]]; then
594167
+    if [[ "${1?}" != "${2?}" ]]; then
594167
         echo "FAIL: expected: '$2' actual: '$1'" >&2
594167
         exit 1
594167
     fi
594167
-}
594167
+)}
594167
+
594167
+assert_in() {(
594167
+    set +ex
594167
 
594167
-assert_in() {
594167
-    if ! echo "$2" | grep -q "$1"; then
594167
+    if ! [[ "${2?}" =~ ${1?} ]]; then
594167
         echo "FAIL: '$1' not found in:" >&2
594167
         echo "$2" >&2
594167
         exit 1
594167
     fi
594167
-}
594167
+)}
594167
+
594167
+assert_not_in() {(
594167
+    set +ex
594167
+
594167
+    if [[ "${2?}" =~ ${1?} ]]; then
594167
+        echo "FAIL: '$1' found in:" >&2
594167
+        echo "$2" >&2
594167
+        exit 1
594167
+    fi
594167
+)}
594167
+
594167
+assert_rc() {(
594167
+    local rc exp="${1?}"
594167
+
594167
+    set +ex
594167
 
594167
-assert_rc() {
594167
-    local exp=$1
594167
-    local rc
594167
     shift
594167
-    set +e
594167
     "$@"
594167
     rc=$?
594167
-    set -e
594167
     assert_eq "$rc" "$exp"
594167
-}
594167
+)}
594167
diff --git a/test/units/testsuite-45.sh b/test/units/testsuite-45.sh
594167
index ac7860dccd..d0d1ef2eb4 100755
594167
--- a/test/units/testsuite-45.sh
594167
+++ b/test/units/testsuite-45.sh
594167
@@ -21,7 +21,7 @@ test_timezone() {
594167
     echo 'change timezone'
594167
     assert_eq "$(timedatectl --no-pager set-timezone Europe/Kiev 2>&1)" ""
594167
     assert_eq "$(readlink /etc/localtime | sed 's#^.*zoneinfo/##')" "Europe/Kiev"
594167
-    assert_in "Time.*zone: Europe/Kiev (EEST, +" "$(timedatectl --no-pager)"
594167
+    assert_in "Time zone: Europe/Kiev \(EEST, \+0[0-9]00\)" "$(timedatectl)"
594167
 
594167
     if [[ -n "$ORIG_TZ" ]]; then
594167
         echo 'reset timezone to original'