Skip to content

Commit

Permalink
test: adds unit tests for GitLabCIResultsReporter class
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Mar 25, 2024
1 parent 85373df commit 24d5ac9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
38 changes: 37 additions & 1 deletion tests/trestlebot/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions trestlebot/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 24d5ac9

Please sign in to comment.