|
1 | 1 | import os
|
2 | 2 | import re
|
3 | 3 | import sys
|
| 4 | +from collections import defaultdict |
4 | 5 | from dataclasses import dataclass
|
5 | 6 | from io import IOBase
|
6 | 7 | from pathlib import Path
|
@@ -177,6 +178,7 @@ def __init__(self) -> None:
|
177 | 178 | self._current = self.all
|
178 | 179 | self.suites: List[TestItem] = []
|
179 | 180 | self.tests: List[TestItem] = []
|
| 181 | + self.tags: Dict[str, List[TestItem]] = defaultdict(list) |
180 | 182 | self.statistics = Statistics()
|
181 | 183 |
|
182 | 184 | def visit_suite(self, suite: TestSuite) -> None:
|
@@ -228,6 +230,8 @@ def visit_test(self, test: TestCase) -> None:
|
228 | 230 | ),
|
229 | 231 | tags=list(test.tags) if test.tags else None,
|
230 | 232 | )
|
| 233 | + for tag in test.tags: |
| 234 | + self.tags[str(tag)].append(item) |
231 | 235 |
|
232 | 236 | self.tests.append(item)
|
233 | 237 | self._current.children.append(item)
|
@@ -533,3 +537,64 @@ def print(items: List[TestItem]) -> Iterable[str]:
|
533 | 537 |
|
534 | 538 | else:
|
535 | 539 | app.print_data(ResultItem(collector.suites, diagnostics), remove_defaults=True)
|
| 540 | + |
| 541 | + |
| 542 | +@dataclass |
| 543 | +class TagsResult: |
| 544 | + tags: Dict[str, List[TestItem]] |
| 545 | + |
| 546 | + |
| 547 | +@discover.command( |
| 548 | + context_settings={ |
| 549 | + "allow_extra_args": True, |
| 550 | + "ignore_unknown_options": True, |
| 551 | + }, |
| 552 | + add_help_option=True, |
| 553 | + epilog='Use "-- --help" to see `robot` help.', |
| 554 | +) |
| 555 | +@add_options(*ROBOT_OPTIONS) |
| 556 | +@pass_application |
| 557 | +def tags( |
| 558 | + app: Application, |
| 559 | + by_longname: Tuple[str, ...], |
| 560 | + exclude_by_longname: Tuple[str, ...], |
| 561 | + robot_options_and_args: Tuple[str, ...], |
| 562 | +) -> None: |
| 563 | + """\ |
| 564 | + Discover tags with the selected configuration, profiles, options and |
| 565 | + arguments. |
| 566 | +
|
| 567 | + \b |
| 568 | + Examples: |
| 569 | + ``` |
| 570 | + robotcode discover tags |
| 571 | + robotcode --profile regression discover tags |
| 572 | +
|
| 573 | + robotcode --profile regression discover tags -i wip |
| 574 | + ``` |
| 575 | + """ |
| 576 | + |
| 577 | + suite, diagnostics = handle_options(app, by_longname, exclude_by_longname, robot_options_and_args) |
| 578 | + |
| 579 | + collector = Collector() |
| 580 | + suite.visit(collector) |
| 581 | + |
| 582 | + if collector.all.children: |
| 583 | + if app.config.output_format is None or app.config.output_format == OutputFormat.TEXT: |
| 584 | + |
| 585 | + def print(tags: Dict[str, List[TestItem]]) -> Iterable[str]: |
| 586 | + for tag, items in tags.items(): |
| 587 | + yield f"{tag}{os.linesep}" |
| 588 | + # for item in items: |
| 589 | + # yield f" {item.longname}{os.linesep}" |
| 590 | + # if item.uri: |
| 591 | + # yield ( |
| 592 | + # f" ({Uri(item.uri).to_path()}{f':{item.range.start.line+1}' if item.range else ''})" |
| 593 | + # f"{os.linesep}" |
| 594 | + # ) |
| 595 | + |
| 596 | + if collector.suites: |
| 597 | + app.echo_via_pager(print(collector.tags)) |
| 598 | + |
| 599 | + else: |
| 600 | + app.print_data(TagsResult(collector.tags), remove_defaults=True) |
0 commit comments