Skip to content

Commit

Permalink
fix: fixes sequences used for GitLabCI groups and tests
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 24d5ac9 commit 34f6345
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
16 changes: 8 additions & 8 deletions tests/trestlebot/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ def test_gitlab_ci_results_reporter() -> None:

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"
"\x1b[0Ksection_start:1234567890:Commit Hash"
"[collapsed=true]\r\x1b[0KCommit hash for the changes\n123456\n"
"\x1b[0Ksection_end:1234567890:Commit Hash\r\x1b[0K\n"
"\x1b[0Ksection_start:1234567890:Pull Request Number[collapsed=true]\r\x1b[0K"
"Pull Request number for the changes\n2\n\x1b[0Ksection_end:1234567890:Pull Request"
" Number\r\x1b[0K\n"
)
with patch("builtins.print") as mock_print:
with patch("time.time_ns", return_value=1234567890):
Expand All @@ -205,8 +205,8 @@ def test_gitlab_ci_results_reporter() -> None:

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"
"\x1b[0Ksection_start:1234567890:Changes[collapsed=true]\r\x1b"
"[0KChanges detected\nfile2\n\x1b[0Ksection_end:1234567890:Changes\r\x1b[0K\n"
)
with patch("builtins.print") as mock_print:
with patch("time.time_ns", return_value=1234567890):
Expand Down
11 changes: 8 additions & 3 deletions trestlebot/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def create_pull_request(
class GitLabCIResultsReporter(ResultsReporter):
"""Report bot results to the console in GitLabCI"""

escape_sequence = "\x1b[0K"
carriage_return = "\r"

def report_results(self, results: BotResults) -> None:
"""
Report the results of the Trestle Bot in GitLab CI
Expand Down Expand Up @@ -136,12 +139,14 @@ def _create_group(
section_title: str, section_description: str, content: str
) -> str:
"""Create a group for the GitLab CI output"""
start_char = GitLabCIResultsReporter.escape_sequence
end_char = f"{GitLabCIResultsReporter.carriage_return}{GitLabCIResultsReporter.escape_sequence}"
group_str = ""
group_str += f"section_start:{time.time_ns()}`:{section_title}[collapsed=true]"
group_str += f"\r\e[0K{section_description}" # noqa: W605
group_str += f"{start_char}section_start:{time.time_ns()}:{section_title}[collapsed=true]"
group_str += f"{end_char}{section_description}"
group_str += f"\n{content}\n"
group_str += (
f"section_end:{time.time_ns()}:${section_title}\r\e[0K\n" # noqa: W605
f"{start_char}section_end:{time.time_ns()}:{section_title}{end_char}\n"
)
return group_str

Expand Down

0 comments on commit 34f6345

Please sign in to comment.