From 647db7a81965ab4a9aefbb35aedbfe5ca2e4c006 Mon Sep 17 00:00:00 2001 From: mytestopia <147036941+mytestopia@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:58:02 +0400 Subject: [PATCH] support custom statuses for --show-paths (#7) --- setup.cfg | 2 +- setup.py | 2 +- tests/_utils.py | 2 +- tests/test_gitlab_reporter.py | 108 +++++++++++++++++++++- vedro_gitlab_reporter/__init__.py | 2 +- vedro_gitlab_reporter/_gitlab_reporter.py | 28 ++++-- 6 files changed, 127 insertions(+), 17 deletions(-) diff --git a/setup.cfg b/setup.cfg index 428bbe1..103703f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 2.1.3 +current_version = 2.1.4 message = bump version → {new_version} commit = True tag = True diff --git a/setup.py b/setup.py index e229f58..560d436 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def find_dev_required(): setup( name="vedro-gitlab-reporter", - version="2.1.3", + version="2.1.4", description="GitLab reporter with collapsable sections for Vedro framework", long_description=open("README.md").read(), long_description_content_type="text/markdown", diff --git a/tests/_utils.py b/tests/_utils.py index 4ee55e8..08664e6 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -57,7 +57,7 @@ async def fire_arg_parsed_event(dispatcher: Dispatcher, *, GitlabReporter.tb_show_internal_calls, tb_show_locals: bool = GitlabReporter.tb_show_locals, - show_paths: bool = + show_paths: Optional[list] = GitlabReporter.show_paths) -> None: await dispatcher.fire(ConfigLoadedEvent(Path(), Config)) diff --git a/tests/test_gitlab_reporter.py b/tests/test_gitlab_reporter.py index fe3f6de..d00b69b 100644 --- a/tests/test_gitlab_reporter.py +++ b/tests/test_gitlab_reporter.py @@ -145,9 +145,60 @@ async def test_scenario_passed_with_extra_details(*, dispatcher: Dispatcher, pri @pytest.mark.usefixtures(gitlab_reporter.__name__) -async def test_scenario_passed_with_show_path(*, dispatcher: Dispatcher, printer_: Mock): +async def test_scenario_passed_with_default_show_path(*, dispatcher: Dispatcher, printer_: Mock): with given: - await fire_arg_parsed_event(dispatcher, show_paths=True) + await fire_arg_parsed_event(dispatcher) + + scenario_result = make_scenario_result().mark_passed() + await dispatcher.fire(ScenarioPassedEvent(scenario_result)) + + aggregated_result = make_aggregated_result(scenario_result) + event = ScenarioReportedEvent(aggregated_result) + + with when: + await dispatcher.fire(event) + + with then: + assert printer_.mock_calls == [ + call.print_scenario_subject(aggregated_result.scenario.subject, + ScenarioStatus.PASSED, + elapsed=aggregated_result.elapsed, + prefix=" "), + call.print_scenario_extra_details([f"{aggregated_result.scenario.path.name}"], + prefix=" " * 3) + ] + + +@pytest.mark.usefixtures(gitlab_reporter.__name__) +async def test_scenario_passed_with_none_show_path(*, dispatcher: Dispatcher, printer_: Mock): + with given: + await fire_arg_parsed_event(dispatcher, show_paths=None) + + scenario_result = make_scenario_result().mark_passed() + await dispatcher.fire(ScenarioPassedEvent(scenario_result)) + + aggregated_result = make_aggregated_result(scenario_result) + event = ScenarioReportedEvent(aggregated_result) + + with when: + await dispatcher.fire(event) + + with then: + # no printed path of scenario info + assert printer_.mock_calls == [ + call.print_scenario_subject(aggregated_result.scenario.subject, + ScenarioStatus.PASSED, + elapsed=aggregated_result.elapsed, + prefix=" "), + ] + + +@pytest.mark.usefixtures(gitlab_reporter.__name__) +async def test_scenario_passed_with_specified_show_path_included( + *, dispatcher: Dispatcher, printer_: Mock +): + with given: + await fire_arg_parsed_event(dispatcher, show_paths=[ScenarioStatus.PASSED.value]) scenario_result = make_scenario_result().mark_passed() await dispatcher.fire(ScenarioPassedEvent(scenario_result)) @@ -169,6 +220,31 @@ async def test_scenario_passed_with_show_path(*, dispatcher: Dispatcher, printer ] +@pytest.mark.usefixtures(gitlab_reporter.__name__) +async def test_scenario_passed_with_specified_show_path_not_included( + *, dispatcher: Dispatcher, printer_: Mock +): + with given: + await fire_arg_parsed_event(dispatcher, show_paths=[ScenarioStatus.FAILED.value]) + + scenario_result = make_scenario_result().mark_passed() + await dispatcher.fire(ScenarioPassedEvent(scenario_result)) + + aggregated_result = make_aggregated_result(scenario_result) + event = ScenarioReportedEvent(aggregated_result) + + with when: + await dispatcher.fire(event) + + with then: + assert printer_.mock_calls == [ + call.print_scenario_subject(aggregated_result.scenario.subject, + ScenarioStatus.PASSED, + elapsed=aggregated_result.elapsed, + prefix=" "), + ] + + @pytest.mark.usefixtures(gitlab_reporter.__name__) async def test_scenario_failed_with_extra_details(*, dispatcher: Dispatcher, printer_: Mock): with given: @@ -215,9 +291,9 @@ async def test_scenario_failed(*, dispatcher: Dispatcher, printer_: Mock): @pytest.mark.usefixtures(gitlab_reporter.__name__) -async def test_scenario_failed_with_show_paths(*, dispatcher: Dispatcher, printer_: Mock): +async def test_scenario_failed_with_default_show_paths(*, dispatcher: Dispatcher, printer_: Mock): with given: - await fire_arg_parsed_event(dispatcher, show_paths=True) + await fire_arg_parsed_event(dispatcher) scenario_result = make_scenario_result().mark_failed() await dispatcher.fire(ScenarioFailedEvent(scenario_result)) @@ -239,6 +315,30 @@ async def test_scenario_failed_with_show_paths(*, dispatcher: Dispatcher, printe ] +@pytest.mark.usefixtures(gitlab_reporter.__name__) +async def test_scenario_failed_with_none_show_paths(*, dispatcher: Dispatcher, printer_: Mock): + with given: + await fire_arg_parsed_event(dispatcher, show_paths=None) + + scenario_result = make_scenario_result().mark_failed() + await dispatcher.fire(ScenarioFailedEvent(scenario_result)) + + aggregated_result = make_aggregated_result(scenario_result) + event = ScenarioReportedEvent(aggregated_result) + + with when: + await dispatcher.fire(event) + + with then: + # no printed path of scenario info + assert printer_.mock_calls == [ + call.print_scenario_subject(aggregated_result.scenario.subject, + ScenarioStatus.FAILED, + elapsed=aggregated_result.elapsed, + prefix=" "), + ] + + @pytest.mark.usefixtures(gitlab_reporter.__name__) async def test_scenario_passed_aggregated_result(*, dispatcher: Dispatcher, printer_: Mock): with given: diff --git a/vedro_gitlab_reporter/__init__.py b/vedro_gitlab_reporter/__init__.py index 91368b1..4685457 100644 --- a/vedro_gitlab_reporter/__init__.py +++ b/vedro_gitlab_reporter/__init__.py @@ -1,5 +1,5 @@ from ._collapsable_mode import GitlabCollapsableMode from ._gitlab_reporter import GitlabReporter, GitlabReporterPlugin -__version__ = "2.1.3" +__version__ = "2.1.4" __all__ = ("GitlabReporter", "GitlabReporterPlugin", "GitlabCollapsableMode",) diff --git a/vedro_gitlab_reporter/_gitlab_reporter.py b/vedro_gitlab_reporter/_gitlab_reporter.py index 7377900..ecc4e49 100644 --- a/vedro_gitlab_reporter/_gitlab_reporter.py +++ b/vedro_gitlab_reporter/_gitlab_reporter.py @@ -4,7 +4,7 @@ from typing import Any, Callable, Dict, List, Set, Type, Union import vedro -from vedro.core import Dispatcher, PluginConfig, ScenarioResult +from vedro.core import Dispatcher, PluginConfig, ScenarioResult, ScenarioStatus from vedro.events import ( ArgParsedEvent, ArgParseEvent, @@ -34,7 +34,7 @@ def __init__(self, config: Type["GitlabReporter"], *, self._tb_show_internal_calls = config.tb_show_internal_calls self._tb_show_locals = config.tb_show_locals self._tb_max_frames = config.tb_max_frames - self._show_paths = config.show_paths + self._show_paths = config.show_paths if not False else [] self._collapsable_mode: Union[GitlabCollapsableMode, None] = None self._namespace: Union[str, None] = None @@ -74,15 +74,25 @@ def on_arg_parse(self, event: ArgParseEvent) -> None: default=self._tb_show_locals, help="Show local variables in the traceback output") group.add_argument("--gitlab-show-paths", - action="store_true", - default=self._show_paths, - help="Show the relative path of each passed scenario") + default=None, + nargs="*", + type=str.upper, + choices=[ScenarioStatus.FAILED.value, ScenarioStatus.PASSED.value], + help="Show the relative path of scenario in status (failed or passed)." + "--gitlab-show-paths - show all paths of scenarios; " + "--gitlab-show-paths failed passed - show all paths of scenarios;" + "--gitlab-show-paths failed - show all failed paths of scenarios;") def on_arg_parsed(self, event: ArgParsedEvent) -> None: self._collapsable_mode = event.args.gitlab_collapsable self._tb_show_internal_calls = event.args.gitlab_tb_show_internal_calls self._tb_show_locals = event.args.gitlab_tb_show_locals - self._show_paths = event.args.gitlab_show_paths + + # --gitlab-show-path -> default values (all) + if event.args.gitlab_show_paths == []: + self._show_paths = [ScenarioStatus.FAILED, ScenarioStatus.PASSED] + elif event.args.gitlab_show_paths is not None: + self._show_paths = [ScenarioStatus(value) for value in event.args.gitlab_show_paths] def on_startup(self, event: StartupEvent) -> None: self._printer.print_header() @@ -260,7 +270,7 @@ def _print_scope_val(self, val: Any) -> None: self._printer.print_scope_val(val) def _add_extra_details(self, scenario_result: ScenarioResult) -> None: - if self._show_paths: + if self._show_paths and scenario_result.status in self._show_paths: scenario_result.add_extra_details(f"{scenario_result.scenario.rel_path}") @@ -276,5 +286,5 @@ class GitlabReporter(PluginConfig): # Max stack trace entries to show (min=4) tb_max_frames: int = 8 - # Show the relative path of each passed scenario - show_paths: bool = False + # Show the relative path of scenario in status + show_paths: List[ScenarioStatus] = []