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 1 commit
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

Check warning on line 166 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L161-L166

Added lines #L161 - L166 were not covered by tests
else:
curr_backtick_count = 0

Check warning on line 168 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L168

Added line #L168 was not covered by tests

if curr_backtick_count > max_backtick_count:
max_backtick_count = curr_backtick_count

Check warning on line 171 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L170-L171

Added lines #L170 - L171 were not covered by tests

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

Check warning on line 174 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L173-L174

Added lines #L173 - L174 were not covered by tests


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

Check warning on line 179 in services/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/test_results.py#L179

Added line #L179 was not covered by tests
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
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
14 changes: 7 additions & 7 deletions tasks/tests/unit/test_test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def test_results_setup(mocker, dbsession):
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__username="test-username",
repository__owner__service="github",
repository__name="codecov-demo",
repository__name="test-repo-name",
)
commit.branch = "main"
dbsession.add(commit)
Expand Down Expand Up @@ -176,14 +176,14 @@ def test_results_setup(mocker, dbsession):
TestInstance(
test_id=test_id2,
outcome=str(Outcome.Failure),
failure_message="Shared failure message",
failure_message="Shared \n\n\n\n <pre> ````````\n \r\n\r\n | test | test | test </pre>failure message",
duration_seconds=2.0,
upload_id=uploads[1].id,
),
TestInstance(
test_id=test_id3,
outcome=str(Outcome.Failure),
failure_message="Shared failure message",
failure_message="Shared \n\n\n\n <pre> \n ```````` \n \r\n\r\n | test | test | test </pre>failure message",
duration_seconds=3.0,
upload_id=uploads[2].id,
),
Expand Down Expand Up @@ -351,7 +351,7 @@ def test_upload_finisher_task_call(
assert expected_result == result
mock_repo_provider_comments.post_comment.assert_called_with(
pull.pullid,
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 3 failed tests by shortest run time</summary>\n\n> <pre>\n> test_name1\n> </pre>\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> <pre>\n> Other Class Name\x1ftest_name2\n> </pre>\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n\n> <pre>\n> Class Name\x1ftest_name0\n> </pre>\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > <pre>&lt;pre&gt;Fourth <br><br>&lt;/pre&gt; | test | instance |</pre>\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/joseph-sentry/codecov-demo/tests/main)",
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 3 failed tests by shortest run time</summary>\n\n> ```\n> test_name1\n> ```\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> ```\n> Other Class Name test_name2\n> ```\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n\n> ```\n> Class Name test_name0\n> ```\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > ```\n> > &lt;pre&gt;Fourth \n> > \n> > &lt;/pre&gt; | test | instance |\n> > ```\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/test-username/test-repo-name/tests/main)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these snapshots are pretty impossible to review. can you format them as multiline strings?

)

mock_metrics.incr.assert_has_calls(
Expand Down Expand Up @@ -551,7 +551,7 @@ def test_upload_finisher_task_call_existing_comment(
mock_repo_provider_comments.edit_comment.assert_called_with(
pull.pullid,
1,
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 3 failed tests by shortest run time</summary>\n\n> <pre>\n> test_name1\n> </pre>\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> <pre>\n> Other Class Name\x1ftest_name2\n> </pre>\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n\n> <pre>\n> Class Name\x1ftest_name0\n> </pre>\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > <pre>&lt;pre&gt;Fourth <br><br>&lt;/pre&gt; | test | instance |</pre>\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/joseph-sentry/codecov-demo/tests/main)",
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 3 failed tests by shortest run time</summary>\n\n> ```\n> test_name1\n> ```\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> ```\n> Other Class Name test_name2\n> ```\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n\n> ```\n> Class Name test_name0\n> ```\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > ```\n> > &lt;pre&gt;Fourth \n> > \n> > &lt;/pre&gt; | test | instance |\n> > ```\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/test-username/test-repo-name/tests/main)",
)

assert expected_result == result
Expand Down Expand Up @@ -677,7 +677,7 @@ def test_upload_finisher_task_call_with_flaky(

mock_repo_provider_comments.post_comment.assert_called_with(
pull.pullid,
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 2 failed tests by shortest run time</summary>\n\n> <pre>\n> test_name1\n> </pre>\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> <pre>\n> Class Name\x1ftest_name0\n> </pre>\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > <pre>&lt;pre&gt;Fourth <br><br>&lt;/pre&gt; | test | instance |</pre>\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n<details><summary>View the full list of 1 :snowflake: flaky tests</summary>\n\n> <pre>\n> Other Class Name\x1ftest_name2\n> </pre>\n> **Flake rate in main:** 40.0% (Passed 3 times, Failed 2 times)\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > <pre>Shared failure message</pre>\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/joseph-sentry/codecov-demo/tests/main)",
"### :x: 4 Tests Failed:\n| Tests completed | Failed | Passed | Skipped |\n|---|---|---|---|\n| 4 | 4 | 0 | 0 |\n<details><summary>View the top 2 failed tests by shortest run time</summary>\n\n> ```\n> test_name1\n> ```\n> <details><summary>Stack Traces | 2s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_1) the CI Build\n> \n> </details>\n\n\n> ```\n> Class Name test_name0\n> ```\n> <details><summary>Stack Traces | 4s run time</summary>\n> \n> > ```\n> > &lt;pre&gt;Fourth \n> > \n> > &lt;/pre&gt; | test | instance |\n> > ```\n> > [View](https://example.com/build_url_3) the CI Build\n> \n> </details>\n\n</details>\n<details><summary>View the full list of 1 :snowflake: flaky tests</summary>\n\n> ```\n> Other Class Name test_name2\n> ```\n> **Flake rate in main:** 40.0% (Passed 3 times, Failed 2 times)\n> <details><summary>Stack Traces | 3s run time</summary>\n> \n> > ```\n> > Shared \n> > \n> > \n> > \n> > &lt;pre&gt; \n> > \n> > \n> > | test | test | test &lt;/pre&gt;failure message\n> > ```\n> > [View](https://example.com/build_url_2) the CI Build\n> \n> </details>\n\n</details>\n\nTo view individual test run time comparison to the main branch, go to the [Test Analytics Dashboard](https://app.codecov.io/gh/test-username/test-repo-name/tests/main)",
)

mock_metrics.incr.assert_has_calls(
Expand Down
Loading