Skip to content

Commit 4de6ac1

Browse files
committed
Update tr_link to support pattern matching
1 parent 8526552 commit 4de6ac1

File tree

2 files changed

+114
-14
lines changed

2 files changed

+114
-14
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
import re
2+
3+
14
def tr_link(app, need, needs, test_option, target_option, *args, **kwargs):
25
if test_option not in need:
36
return ""
4-
test_opt = need[test_option]
7+
test_option_value = need[test_option]
58

69
links = []
710
for need_target in needs.values():
811
if target_option not in need_target:
912
continue
1013

11-
if (
12-
test_opt == need_target[target_option] and test_opt is not None and len(test_opt) > 0 # fmt: skip
13-
):
14-
links.append(need_target["id"])
14+
if test_option_value is not None and len(test_option_value) > 0:
15+
target_option_value = need_target[target_option]
16+
if target_option_value is not None and len(target_option_value) > 0:
17+
if test_option_value == target_option_value:
18+
links.append(need_target["id"])
19+
# if the test option value has a *, use regex matching
20+
elif "*" in test_option_value:
21+
if re.match(test_option_value, target_option_value):
22+
links.append(need_target["id"])
1523

1624
return links

tests/test_tr_link.py

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,127 @@
1-
from sphinxcontrib.test_reports.functions import tr_link
1+
from pathlib import Path
2+
3+
import importlib.util
4+
import sys
5+
6+
7+
def import_from_path(path, name):
8+
spec = importlib.util.spec_from_file_location(name, path)
9+
module = importlib.util.module_from_spec(spec)
10+
sys.modules[name] = module
11+
spec.loader.exec_module(module)
12+
return module
13+
14+
15+
# (!) We need to import the 'tr_link' method from the local module,
16+
# otherwise the method from the installed 'sphinxcontrib' package will be used.
17+
local_functions_module = import_from_path(
18+
Path(__file__).parent.parent.joinpath(
19+
"sphinxcontrib/test_reports/functions/__init__.py"
20+
),
21+
"functions",
22+
)
23+
tr_link = local_functions_module.tr_link
224

325

426
def test_tr_link_option_not_in_need():
527
"""
628
Return an empty string when the specified test option is missing from the need.
729
"""
8-
assert tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == ""
30+
assert (
31+
tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == ""
32+
)
33+
934

1035
def test_tr_link_no_target_option_in_needs():
1136
"""
1237
Return an empty list when the target option is missing in all items of needs.
1338
"""
14-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"id": "123"}}, test_option="a", target_option="b") == []
39+
assert (
40+
tr_link(
41+
app=None,
42+
need={"a": "1"},
43+
needs={"x": {"id": "123"}},
44+
test_option="a",
45+
target_option="b",
46+
)
47+
== []
48+
)
49+
1550

1651
def test_tr_link_no_match():
1752
"""
18-
Returns an empty list when no matching value for the test option is found in any of the target options within needs.
53+
Returns an empty list when no matching value for the test option is found
54+
in any of the target options within needs.
1955
"""
20-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "2", "id": "123"}}, test_option="a", target_option="b") == []
56+
assert (
57+
tr_link(
58+
app=None,
59+
need={"a": "1"},
60+
needs={"x": {"b": "2", "id": "123"}},
61+
test_option="a",
62+
target_option="b",
63+
)
64+
== []
65+
)
66+
2167

2268
def test_tr_link_match():
2369
"""
2470
Returns a list of ids when there is a matching value in both need and needs.
2571
"""
26-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "1", "id": "123"}}, test_option="a", target_option="b") == ["123"]
72+
assert tr_link(
73+
app=None,
74+
need={"a": "1"},
75+
needs={"x": {"b": "1", "id": "123"}},
76+
test_option="a",
77+
target_option="b",
78+
) == ["123"]
79+
2780

2881
def test_tr_link_none_or_empty():
2982
"""
3083
'None' and empty string values are not considered as valid matches.
3184
"""
3285
need = {"a": None, "b": ""}
33-
needs = {"x": {"c": None, "id": "111"}, "y": {"c": "valid", "id": "222"}, "z": {"c": "", "id": "333"}}
34-
assert tr_link(app=None, need=need, needs=needs, test_option="b", target_option="c") == []
35-
assert tr_link(app=None, need=need, needs=needs, test_option="a", target_option="c") == []
86+
needs = {
87+
"x": {"c": None, "id": "111"},
88+
"y": {"c": "valid", "id": "222"},
89+
"z": {"c": "", "id": "333"},
90+
}
91+
assert (
92+
tr_link(app=None, need=need, needs=needs, test_option="b", target_option="c")
93+
== []
94+
)
95+
assert (
96+
tr_link(app=None, need=need, needs=needs, test_option="a", target_option="c")
97+
== []
98+
)
99+
100+
101+
def test_tr_link_regex_match():
102+
"""
103+
Returns a list of ids when the test option value containing an asterisk (*)
104+
correctly matches target options using regular expression patterns.
105+
"""
106+
needs = {
107+
"x": {"b": "abc123", "id": "111"},
108+
"y": {"b": "def456", "id": "222"},
109+
"z": {"b": "ghi789", "id": "333"},
110+
}
111+
need = {"a": "abc.*"}
112+
assert tr_link(
113+
app=None, need=need, needs=needs, test_option="a", target_option="b"
114+
) == ["111"]
115+
116+
117+
def test_tr_link_regex_no_match():
118+
"""
119+
Returns an empty list when the test option value containing an asterisk (*)
120+
does not match any target options using regular expression patterns.
121+
"""
122+
needs = {"x": {"b": "abc123", "id": "111"}, "y": {"b": "def456", "id": "222"}}
123+
need = {"a": "xyz.*"}
124+
assert (
125+
tr_link(app=None, need=need, needs=needs, test_option="a", target_option="b")
126+
== []
127+
)

0 commit comments

Comments
 (0)