Skip to content

Commit 6f5c719

Browse files
committed
fix(runner): --by-longname and --exclude-by-longname now take into account whether a name for the run was set via --name command line argument or in the name setting robot.toml
closes #248
1 parent bc97744 commit 6f5c719

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
1-
from typing import Union
1+
from typing import Optional, Union
22

33
from robot.api import SuiteVisitor
44
from robot.running import TestCase, TestSuite
55

66

7-
class ByLongName(SuiteVisitor):
8-
def __init__(self, *included: str) -> None:
7+
class _BaseSuiteVisitor(SuiteVisitor):
8+
def __init__(self, *included: str, root_name: Optional[str] = None) -> None:
99
super().__init__()
10-
self.included = included
10+
self.included = list(included)
11+
12+
self.root_name = root_name
13+
self.real_root_name: Optional[str] = None
1114

1215
def start_suite(self, suite: TestSuite) -> None:
13-
suite.tests = [t for t in suite.tests if self._is_included(t)]
16+
if self.real_root_name is None and self.root_name is not None:
17+
self.real_root_name = suite.longname
18+
new_included = []
19+
for i in self.included:
20+
if "." in i:
21+
root_name, rest = c if len(c := i.split(".", 1)) > 1 else (c, None)
22+
if root_name == self.root_name:
23+
if rest is not None:
24+
new_included.append(f"{self.real_root_name}.{rest}")
25+
else:
26+
new_included.append(f"{self.real_root_name}'")
27+
else:
28+
new_included.append(i)
29+
else:
30+
new_included.append(i)
31+
self.included = new_included
1432

1533
def _is_included(self, test: Union[TestCase, TestSuite]) -> bool:
1634
names = []
1735
names.append(test.longname)
1836
current = test.parent
1937
while current:
20-
names.append(current.longname)
38+
if current.parent is None:
39+
names.append(self.root_name if self.root_name is not None else current.longname)
40+
else:
41+
names.append(current.longname)
2142
current = current.parent
2243

2344
return any((s in names) for s in self.included)
@@ -26,23 +47,15 @@ def end_suite(self, suite: TestSuite) -> None:
2647
suite.suites = [s for s in suite.suites if s.test_count > 0]
2748

2849

29-
class ExcludedByLongName(SuiteVisitor):
30-
def __init__(self, *included: str) -> None:
31-
super().__init__()
32-
self.included = included
33-
50+
class ByLongName(_BaseSuiteVisitor):
3451
def start_suite(self, suite: TestSuite) -> None:
35-
suite.tests = [t for t in suite.tests if not self._is_included(t)]
52+
super().start_suite(suite)
3653

37-
def _is_included(self, test: Union[TestCase, TestSuite]) -> bool:
38-
names = []
39-
names.append(test.longname)
40-
current = test.parent
41-
while current:
42-
names.append(current.longname)
43-
current = current.parent
54+
suite.tests = [t for t in suite.tests if self._is_included(t)]
4455

45-
return any((s in names) for s in self.included)
4656

47-
def end_suite(self, suite: TestSuite) -> None:
48-
suite.suites = [s for s in suite.suites if s.test_count > 0]
57+
class ExcludedByLongName(_BaseSuiteVisitor):
58+
def start_suite(self, suite: TestSuite) -> None:
59+
super().start_suite(suite)
60+
61+
suite.tests = [t for t in suite.tests if not self._is_included(t)]

packages/runner/src/robotcode/runner/cli/discover/discover.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,7 @@ def handle_options(
462462
exclude_by_longname: Tuple[str, ...],
463463
robot_options_and_args: Tuple[str, ...],
464464
) -> Tuple[TestSuite, Collector, Optional[Dict[str, List[Diagnostic]]]]:
465-
root_folder, profile, cmd_options = handle_robot_options(
466-
app, by_longname, exclude_by_longname, robot_options_and_args
467-
)
465+
root_folder, profile, cmd_options = handle_robot_options(app, robot_options_and_args)
468466

469467
diagnostics_logger = DiagnosticsLogger()
470468
try:
@@ -479,6 +477,8 @@ def handle_options(
479477
),
480478
app.config.dry,
481479
root_folder,
480+
by_longname,
481+
exclude_by_longname,
482482
).parse_arguments((*cmd_options, "--runemptysuite", *robot_options_and_args))
483483

484484
settings = RobotSettings(options)

packages/runner/src/robotcode/runner/cli/robot.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from robot.run import USAGE, RobotFramework
99
from robot.version import get_full_version
1010

11+
import robotcode.modifiers
1112
from robotcode.plugin import Application, pass_application
1213
from robotcode.plugin.click_helper.aliases import AliasedCommand
1314
from robotcode.plugin.click_helper.types import add_options
@@ -25,13 +26,17 @@ def __init__(
2526
paths: List[str],
2627
dry: bool,
2728
root_folder: Optional[Path],
29+
by_longname: Tuple[str, ...] = (),
30+
exclude_by_longname: Tuple[str, ...] = (),
2831
) -> None:
2932
super().__init__()
3033
self.app = app
3134
self.paths = paths
3235
self.dry = dry
3336
self.root_folder = root_folder
3437
self._orig_cwd = Path.cwd()
38+
self.by_longname = by_longname
39+
self.exclude_by_longname = exclude_by_longname
3540

3641
def parse_arguments(self, cli_args: Any) -> Any:
3742
if self.root_folder is not None and Path.cwd() != self.root_folder:
@@ -61,6 +66,18 @@ def parse_arguments(self, cli_args: Any) -> Any:
6166
f'{line_end.join((*(f"{k} = {v!r}" for k, v in options.items()), *arguments))}'
6267
)
6368

69+
modifiers = []
70+
root_name = options.get("name", None)
71+
72+
if self.by_longname:
73+
modifiers.append(robotcode.modifiers.ByLongName(*self.by_longname, root_name=root_name))
74+
75+
if self.exclude_by_longname:
76+
modifiers.append(robotcode.modifiers.ExcludedByLongName(*self.exclude_by_longname, root_name=root_name))
77+
78+
if modifiers:
79+
options["prerunmodifier"] = options.get("prerunmodifier", []) + modifiers
80+
6481
return options, arguments
6582

6683

@@ -90,10 +107,7 @@ def parse_arguments(self, cli_args: Any) -> Any:
90107

91108

92109
def handle_robot_options(
93-
app: Application,
94-
by_longname: Tuple[str, ...],
95-
exclude_by_longname: Tuple[str, ...],
96-
robot_options_and_args: Tuple[str, ...],
110+
app: Application, robot_options_and_args: Tuple[str, ...]
97111
) -> Tuple[Optional[Path], RobotBaseProfile, List[str]]:
98112
robot_arguments: Optional[List[Union[str, Path]]] = None
99113
old_sys_path = sys.path.copy()
@@ -118,20 +132,6 @@ def handle_robot_options(
118132

119133
cmd_options = profile.build_command_line()
120134

121-
if by_longname:
122-
sep = ";" if any(True for l in by_longname if ":" in l) else ":"
123-
cmd_options += (
124-
"--prerunmodifier",
125-
f"robotcode.modifiers.ByLongName{sep}{sep.join(by_longname)}",
126-
)
127-
128-
if exclude_by_longname:
129-
sep = ";" if any(True for l in exclude_by_longname if ":" in l) else ":"
130-
cmd_options += (
131-
"--prerunmodifier",
132-
f"robotcode.modifiers.ExcludedByLongName{sep}{sep.join(exclude_by_longname)}",
133-
)
134-
135135
app.verbose(
136136
lambda: "Executing robot with following options:\n "
137137
+ " ".join(f'"{o}"' for o in (cmd_options + list(robot_options_and_args)))
@@ -170,9 +170,7 @@ def robot(
170170
```
171171
"""
172172

173-
root_folder, profile, cmd_options = handle_robot_options(
174-
app, by_longname, exclude_by_longname, robot_options_and_args
175-
)
173+
root_folder, profile, cmd_options = handle_robot_options(app, robot_options_and_args)
176174

177175
app.exit(
178176
cast(
@@ -186,6 +184,8 @@ def robot(
186184
),
187185
app.config.dry,
188186
root_folder,
187+
by_longname,
188+
exclude_by_longname,
189189
).execute_cli((*cmd_options, *robot_options_and_args), exit=False),
190190
)
191191
)

0 commit comments

Comments
 (0)