Skip to content

Commit b498eb0

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

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

docs/functions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Then it goes through **all** other needs and checks if the value of their ``sour
3333
the ``target_option``.
3434
If this is the case, their IDs get stored and finally returned.
3535

36+
In case the ``source_option`` contains a ``*`` (e.g. ``sphinxcontrib.test_reports.*``), the function will check if the
37+
``target_option`` matches the ``source_option``. This is useful if one wants to link multiple test results to one test case.
38+
39+
3640
**Example**::
3741

3842
.. spec:: sphinxcontrib.test_reports.test_reports
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: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,101 @@ def test_tr_link_option_not_in_need():
55
"""
66
Return an empty string when the specified test option is missing from the need.
77
"""
8-
assert tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == ""
8+
assert (
9+
tr_link(app=None, need={}, needs={}, test_option="a", target_option="b") == ""
10+
)
11+
912

1013
def test_tr_link_no_target_option_in_needs():
1114
"""
1215
Return an empty list when the target option is missing in all items of needs.
1316
"""
14-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"id": "123"}}, test_option="a", target_option="b") == []
17+
assert (
18+
tr_link(
19+
app=None,
20+
need={"a": "1"},
21+
needs={"x": {"id": "123"}},
22+
test_option="a",
23+
target_option="b",
24+
)
25+
== []
26+
)
27+
1528

1629
def test_tr_link_no_match():
1730
"""
18-
Returns an empty list when no matching value for the test option is found in any of the target options within needs.
31+
Returns an empty list when no matching value for the test option is found
32+
in any of the target options within needs.
1933
"""
20-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "2", "id": "123"}}, test_option="a", target_option="b") == []
34+
assert (
35+
tr_link(
36+
app=None,
37+
need={"a": "1"},
38+
needs={"x": {"b": "2", "id": "123"}},
39+
test_option="a",
40+
target_option="b",
41+
)
42+
== []
43+
)
44+
2145

2246
def test_tr_link_match():
2347
"""
2448
Returns a list of ids when there is a matching value in both need and needs.
2549
"""
26-
assert tr_link(app=None, need={"a": "1"}, needs={"x": {"b": "1", "id": "123"}}, test_option="a", target_option="b") == ["123"]
50+
assert tr_link(
51+
app=None,
52+
need={"a": "1"},
53+
needs={"x": {"b": "1", "id": "123"}},
54+
test_option="a",
55+
target_option="b",
56+
) == ["123"]
57+
2758

2859
def test_tr_link_none_or_empty():
2960
"""
3061
'None' and empty string values are not considered as valid matches.
3162
"""
3263
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") == []
64+
needs = {
65+
"x": {"c": None, "id": "111"},
66+
"y": {"c": "valid", "id": "222"},
67+
"z": {"c": "", "id": "333"},
68+
}
69+
assert (
70+
tr_link(app=None, need=need, needs=needs, test_option="b", target_option="c")
71+
== []
72+
)
73+
assert (
74+
tr_link(app=None, need=need, needs=needs, test_option="a", target_option="c")
75+
== []
76+
)
77+
78+
79+
def test_tr_link_regex_match():
80+
"""
81+
Returns a list of ids when the test option value containing an asterisk (*)
82+
correctly matches target options using regular expression patterns.
83+
"""
84+
needs = {
85+
"x": {"b": "abc123", "id": "111"},
86+
"y": {"b": "def456", "id": "222"},
87+
"z": {"b": "ghi789", "id": "333"},
88+
}
89+
need = {"a": "abc.*"}
90+
assert tr_link(
91+
app=None, need=need, needs=needs, test_option="a", target_option="b"
92+
) == ["111"]
93+
94+
95+
def test_tr_link_regex_no_match():
96+
"""
97+
Returns an empty list when the test option value containing an asterisk (*)
98+
does not match any target options using regular expression patterns.
99+
"""
100+
needs = {"x": {"b": "abc123", "id": "111"}, "y": {"b": "def456", "id": "222"}}
101+
need = {"a": "xyz.*"}
102+
assert (
103+
tr_link(app=None, need=need, needs=needs, test_option="a", target_option="b")
104+
== []
105+
)

0 commit comments

Comments
 (0)