Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure code block has copy button #625

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions services/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,28 @@
return f"\n{result}\n"


def properly_backtick(content: str) -> str:
max_backtick_count = 0
curr_backtick_count = 0
prev_char = None
for char in content:
if char == "`":
curr_backtick_count += 1
else:
curr_backtick_count = 0

if curr_backtick_count > max_backtick_count:
max_backtick_count = curr_backtick_count

backticks = "`" * (max_backtick_count + 1)
return f"{backticks}\n{content}\n{backticks}"


def wrap_in_code(content: str) -> str:
return f"<pre>\n{content}\n</pre>"
if "```" in content:
return properly_backtick(content)
else:
return f"\n```\n{content}\n```\n"


def display_duration(f: float) -> str:
Expand All @@ -177,10 +197,11 @@
else:
failure_message = "No failure message available"

failure_message = wrap_in_code(failure_message)
if fail.build_url:
return f"<pre>{failure_message}</pre>\n[View]({fail.build_url}) the CI Build"
return f"{failure_message}\n[View]({fail.build_url}) the CI Build"
else:
return f"<pre>{failure_message}</pre>"
return failure_message

Check warning on line 204 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L204

Added line #L204 was not covered by tests


def generate_view_test_analytics_line(commit: Commit) -> str:
Expand All @@ -192,7 +213,7 @@
def messagify_failure(
failure: TestResultsNotificationFailure,
) -> str:
test_name = wrap_in_code(failure.testname)
test_name = wrap_in_code(failure.testname.replace("\x1f", " "))
formatted_duration = display_duration(failure.duration_seconds)
stack_trace_summary = f"Stack Traces | {formatted_duration}s run time"
stack_trace = wrap_in_details(
Expand All @@ -206,7 +227,7 @@
flaky_failure: TestResultsNotificationFailure,
flake_info: FlakeInfo,
) -> str:
test_name = wrap_in_code(flaky_failure.testname)
test_name = wrap_in_code(flaky_failure.testname.replace("\x1f", " "))
formatted_duration = display_duration(flaky_failure.duration_seconds)
flake_rate = flake_info.failed / flake_info.count * 100
flake_rate_section = f"**Flake rate in main:** {flake_rate}% (Passed {flake_info.count - flake_info.failed} times, Failed {flake_info.failed} times)"
Expand Down
31 changes: 24 additions & 7 deletions services/tests/test_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ def test_generate_failure_info():

assert (
res
== "<pre>hello world</pre>\n[View](https://example.com/build_url) the CI Build"
== """
```
hello world
```

[View](https://example.com/build_url) the CI Build"""
)


Expand Down Expand Up @@ -113,12 +118,18 @@ def test_build_message():
| 3 | 1 | 2 | 3 |
<details><summary>View the top 1 failed tests by shortest run time</summary>

> <pre>
>
> ```
> testname
> </pre>
> ```
>
> <details><summary>Stack Traces | 1s run time</summary>
>
> > <pre>hello world</pre>
> >
> > ```
> > hello world
> > ```
> >
> > [View](https://example.com/build_url) the CI Build
>
> </details>
Expand Down Expand Up @@ -157,13 +168,19 @@ def test_build_message_with_flake():
| 3 | 1 | 2 | 3 |
<details><summary>View the full list of 1 :snowflake: flaky tests</summary>

> <pre>
>
> ```
> testname
> </pre>
> ```
>
> **Flake rate in main:** 33.33333333333333% (Passed 2 times, Failed 1 times)
> <details><summary>Stack Traces | 1s run time</summary>
>
> > <pre>hello world</pre>
> >
> > ```
> > hello world
> > ```
> >
> > [View](https://example.com/build_url) the CI Build
>
> </details>
Expand Down
6 changes: 0 additions & 6 deletions tasks/test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@
test_results_finisher_task_name = "app.tasks.test_results.TestResultsFinisherTask"

ESCAPE_FAILURE_MESSAGE_DEFN = [
Replacement(['"'], "&quot;", EscapeEnum.REPLACE),
Replacement(["'"], "&apos;", EscapeEnum.REPLACE),
Replacement(["<"], "&lt;", EscapeEnum.REPLACE),
Replacement([">"], "&gt;", EscapeEnum.REPLACE),
Replacement(["?"], "&amp;", EscapeEnum.REPLACE),
Replacement(["\r"], "", EscapeEnum.REPLACE),
Replacement(["\n"], "<br>", EscapeEnum.REPLACE),
]
QUEUE_NOTIFY_KEY = "queue_notify"

Expand Down
Loading
Loading