From beebde9e56c3bb8e72bbe4234cace411bc7670f7 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Wed, 6 Apr 2022 11:59:21 +0200 Subject: [PATCH] client/test: allow matching and replacing regex-es in nmcli output This allows us to sanitize unpredictable UUIDs in client output in --offline mode (where we can't just ask the mock service about the actual UUID). --- src/tests/client/test-client.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/tests/client/test-client.py b/src/tests/client/test-client.py index d025d12ead..bb99b8cd30 100755 --- a/src/tests/client/test-client.py +++ b/src/tests/client/test-client.py @@ -202,6 +202,14 @@ class Util: t = basestring return isinstance(s, t) + @staticmethod + def is_regex_pattern(s): + if Util.python_has_version(3): + t = re.Pattern + else: + t = re._pattern_type + return isinstance(s, t) + @staticmethod def memoize_nullary(nullary_func): result = [] @@ -347,12 +355,21 @@ class Util: v_search = replace[0]() except TypeError: v_search = replace[0] + + v_replace = replace[1] + v_replace = v_replace.encode("utf-8") + + if Util.is_regex_pattern(v_search): + text2 = [] + for t in text: + text2.append(v_search.sub(v_replace, t)) + text = text2 + continue + assert v_search is None or Util.is_string(v_search) if not v_search: continue - v_replace = replace[1] v_search = v_search.encode("utf-8") - v_replace = v_replace.encode("utf-8") text2 = [] for t in text: if isinstance(t, tuple):