Skip to content

Commit

Permalink
fix: Internal error when redirecting CLI stdout on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed May 8, 2024
1 parent 07f6951 commit bef643e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Changelog
- Clarify error message on unsupported recursive references.
- Report more details on some internal errors instead of "Unknown Schema Error".

**Fixed**

- Internal error on Windows when the CLI output is redirected to a file and code samples contain non CP1252 characters.

.. _v3.27.1:

:version:`3.27.1 <v3.27.0...v3.27.1>` - 2024-04-29
Expand Down
2 changes: 2 additions & 0 deletions src/schemathesis/cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class ExecutionContext:
report: ServiceReportContext | FileReportContext | None = None
probes: list[ProbeRun] | None = None
analysis: Result[AnalysisResult, Exception] | None = None
# Special flag to display a warning about Windows-specific encoding issue
encountered_windows_encoding_issue: bool = False

@deprecated_property(removed_in="4.0", replacement="show_trace")
def show_errors_tracebacks(self) -> bool:
Expand Down
31 changes: 28 additions & 3 deletions src/schemathesis/cli/output/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,23 @@ def display_failures_for_single_test(context: ExecutionContext, result: Serializ
except UnicodeDecodeError:
click.echo("\n <BINARY>")

click.echo(
f"\n{bold('Reproduce with')}: \n\n {code_sample}\n",
)
try:
click.echo(
f"\n{bold('Reproduce with')}: \n\n {code_sample}\n",
)
except UnicodeEncodeError:
# On Windows it may fail when redirecting the output to a file
# because it uses a different encoding than the console encoding and the default
# is cp1252, which doesn't support some Unicode characters.
# In this case, display a stub message and set a flag to display a warning at the end
if platform.system() != "Windows":
raise
click.echo(
f"\n{bold('Reproduce with')}: \n\n"
" CAN NOT DISPLAY THIS CODE SAMPLE DUE TO TERMINAL LIMITATIONS.\n"
" SEE DETAILS AT THE END OF THE OUTPUT\n",
)
context.encountered_windows_encoding_issue = True


def display_application_logs(context: ExecutionContext, event: events.Finished) -> None:
Expand Down Expand Up @@ -466,6 +480,17 @@ def display_statistic(context: ExecutionContext, event: events.Finished) -> None
seed_option = f"`--hypothesis-seed={context.seed}`"
click.secho(f"\n{bold('Note')}: To replicate these test failures, rerun with {bold(seed_option)}")

if context.encountered_windows_encoding_issue:
click.echo()
title = click.style("WARNING:", bold=True, fg="yellow")
name = click.style("PYTHONIOENCODING", bold=True)
value = click.style("utf8", bold=True)
warning = (
"Some code samples could not be displayed due to terminal limitations on Windows.\n"
f"To resolve this set the '{name}' environment variable to '{value}' and rerun your command."
)
click.secho(f"{title} {warning}")

if context.report is not None and not context.is_interrupted:
if isinstance(context.report, FileReportContext):
click.echo()
Expand Down

0 comments on commit bef643e

Please sign in to comment.