diff --git a/tests/trestlebot/test_gitlab.py b/tests/trestlebot/test_gitlab.py index 73c067ce..d4aa19a9 100644 --- a/tests/trestlebot/test_gitlab.py +++ b/tests/trestlebot/test_gitlab.py @@ -13,8 +13,9 @@ from responses import GET, POST, RequestsMock from tests.testutils import clean -from trestlebot.gitlab import GitLab +from trestlebot.gitlab import GitLab, GitLabCIResultsReporter from trestlebot.provider import GitProviderException +from trestlebot.reporter import BotResults @pytest.mark.parametrize( @@ -176,3 +177,38 @@ def test_create_pull_request_with_exceptions( "owner", "repo", "main", "test", "My PR", "Has Changes" ) mock_get.assert_called_once() + + +def test_gitlab_ci_results_reporter() -> None: + """Test results reporter""" + + results = BotResults(changes=[], commit_sha="", pr_number=0) + + expected_output = "No changes detected" + with patch("builtins.print") as mock_print: + GitLabCIResultsReporter().report_results(results) + mock_print.assert_any_call(expected_output) + + results = BotResults(changes=[], commit_sha="123456", pr_number=2) + expected_output = ( + "section_start:1234567890`:Commit Hash" + "[collapsed=true]\r\\e[0KCommit hash for the changes\n123456\n" + "section_end:1234567890:$Commit Hash\r\\e[0K\n" + "section_start:1234567890`:Pull Request Number[collapsed=true]\r\\e[0K" + "Pull Request number for the changes\n2\nsection_end:1234567890:$Pull Request" + " Number\r\\e[0K\n" + ) + with patch("builtins.print") as mock_print: + with patch("time.time_ns", return_value=1234567890): + GitLabCIResultsReporter().report_results(results) + mock_print.assert_called_once_with(expected_output) + + results = BotResults(changes=["file2"], commit_sha="", pr_number=0) + expected_output = ( + "section_start:1234567890`:Changes[collapsed=true]\r\\e" + "[0KChanges detected\nfile2\nsection_end:1234567890:$Changes\r\\e[0K\n" + ) + with patch("builtins.print") as mock_print: + with patch("time.time_ns", return_value=1234567890): + GitLabCIResultsReporter().report_results(results) + mock_print.assert_called_once_with(expected_output) diff --git a/trestlebot/gitlab.py b/trestlebot/gitlab.py index 8387b973..f74c8090 100644 --- a/trestlebot/gitlab.py +++ b/trestlebot/gitlab.py @@ -111,7 +111,7 @@ def report_results(self, results: BotResults) -> None: commit_str = self._create_group( "Commit Hash", "Commit hash for the changes", - f"Commit Hash: {results.commit_sha}", + results.commit_sha, ) results_str += commit_str @@ -141,7 +141,7 @@ def _create_group( group_str += f"\r\e[0K{section_description}" # noqa: W605 group_str += f"\n{content}\n" group_str += ( - f"section_end:{time.time_ns()}:${section_title}\r\e[0K" # noqa: W605 + f"section_end:{time.time_ns()}:${section_title}\r\e[0K\n" # noqa: W605 ) return group_str