teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0211-test-introduce-assert_not_in-helper-function.patch

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