mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-01 07:10:12 +01:00
clients/tests: fix regular expression match in Util.replace_text()
Seems the previous code did not work properly:
With python36-3.6.8-38.module+el8.5.0+12207+5c5719bc.x86_6 on rhel-8.6:
Traceback (most recent call last):
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1157, in f
func(self)
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1724, in test_offline
replace_stdout=replace_uuids,
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 797, in call_nmcli
frame,
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 997, in _call_nmcli
self.async_start(wait_all=sync_barrier)
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1032, in async_start
async_job.wait_and_complete()
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 670, in wait_and_complete
self._complete_cb(self, return_code, stdout, stderr)
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 919, in complete_cb
stdout = Util.replace_text(stdout, replace_stdout)
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 362, in replace_text
if Util.is_regex_pattern(v_search):
File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 208, in is_regex_pattern
t = re.Pattern
AttributeError: module 're' has no attribute 'Pattern'
On this python version, re.compile() give an object of type
_sre.SRE_Pattern.
# python -c 'import re; print(type(re.compile("a")))'
<class '_sre.SRE_Pattern'>
Fixes: beebde9e56 ('client/test: allow matching and replacing regex-es in nmcli output')
This commit is contained in:
parent
e95482dfd0
commit
a3038d4f5a
1 changed files with 7 additions and 11 deletions
|
|
@ -202,14 +202,6 @@ 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 = []
|
||||
|
|
@ -342,6 +334,10 @@ class Util:
|
|||
except:
|
||||
return None
|
||||
|
||||
class ReplaceTextUsingRegex:
|
||||
def __init__(self, pattern):
|
||||
self.pattern = re.compile(pattern)
|
||||
|
||||
@staticmethod
|
||||
def replace_text(text, replace_arr):
|
||||
if not replace_arr:
|
||||
|
|
@ -359,10 +355,10 @@ class Util:
|
|||
v_replace = replace[1]
|
||||
v_replace = v_replace.encode("utf-8")
|
||||
|
||||
if Util.is_regex_pattern(v_search):
|
||||
if isinstance(v_search, Util.ReplaceTextUsingRegex):
|
||||
text2 = []
|
||||
for t in text:
|
||||
text2.append(v_search.sub(v_replace, t))
|
||||
text2.append(v_search.pattern.sub(v_replace, t))
|
||||
text = text2
|
||||
continue
|
||||
|
||||
|
|
@ -1713,7 +1709,7 @@ class TestNmcli(NmTestBase):
|
|||
|
||||
replace_uuids = [
|
||||
(
|
||||
re.compile(b"uuid=.*"),
|
||||
Util.ReplaceTextUsingRegex(b"\\buuid=[-a-f0-9]+\\b"),
|
||||
"uuid=UUID-WAS-HERE-BUT-IS-NO-MORE-SADLY",
|
||||
)
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue