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

How to sanitize-out ANSI escapes from captured output #6586

Closed
ssbarnea opened this issue Jan 27, 2020 · 3 comments
Closed

How to sanitize-out ANSI escapes from captured output #6586

ssbarnea opened this issue Jan 27, 2020 · 3 comments

Comments

@ssbarnea
Copy link
Member

ssbarnea commented Jan 27, 2020

In order to improve output asserts and display of potential errors I need a generic way to sanitize captured output.

Please, keep in mind that the purpose of this is not to disable the ANSI support in the executed command but to assure that when asserts fail, they can be read by humans.
screenshot

  • Is there something I can do to enable implicit sanitization?
  • How can I improve the assert error report to make it easier to read? Combining newlines like this makes it very difficult to read.

I should add that I know how to strip ANSI part, the question if there is a way to make this implicit as part of assert. Maybe a custom assert that does this?

@The-Compiler
Copy link
Member

You're running str() on a bytes object, which gives you a string like that. Instead, you might want to properly decode it via something like e.value.stdout.decode('utf-8') (or if that's the subprocess module, pass the encoding argument to subprocess

@ssbarnea
Copy link
Member Author

I am more worried about how to improve pytest display of multiline string assert failures in pytest. The problem is still valid even without ANSI escapes.

@The-Compiler
Copy link
Member

That's not pytest - your string has the literal characters \, n in it:

>>> str(b'foo\nbar')
"b'foo\\nbar'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants