test/infamy: fix xpath_to_uri() to handle multiple predicates

The xpath_to_uri() method only processed the first predicate in XPath
expressions with multiple [key='value'] patterns.  Each re.sub() call
was performed on the original xpath instead of the result the previous
substitutions, causing subsequent predicates to be ignored.

Example XPath that would fail:

    /infix-firewall:firewall/zone[name='untrusted']/interface[.='e2']

Would incorrectly convert to:

    /infix-firewall:firewall/zone[name='untrusted']/interface=e2

Instead of the correct RESTCONF URL:

    /infix-firewall:firewall/zone=untrusted/interface=e2

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-09-28 22:02:58 +02:00
parent 9d9e099cdb
commit a0a4051c8a
+2 -4
View File
@@ -26,16 +26,14 @@ class Location:
def xpath_to_uri(xpath, extra=None):
"""Convert xpath to HTTP URI"""
# If the xpath has a
pattern = r'\[(.*?)=["\'](.*?)["\']\]'
matches = re.findall(pattern, xpath)
uri_path = xpath
if matches:
for key, value in matches:
# replace [key=value] with =value
uri_path = re.sub(rf'\[{re.escape(key)}=["\']{re.escape(value)}["\']\]', f'={value}', xpath)
else:
uri_path = xpath
uri_path = re.sub(rf'\[{re.escape(key)}=["\']{re.escape(value)}["\']\]', f'={value}', uri_path)
# Append extra if provided
if extra is not None: