Add option --color=<always|never|auto>#8135
Conversation
Providing a message prevents pytest from introspecting the assertion [1]. This also fixes E501 (line too long). [1]: https://docs.pytest.org/en/latest/assert.html#asserting-with-the-assert-statement
|
The test for Update: This was resolved by the commits below. The commits add unit tests for |
On Windows, colorama strips ANSI color sequences from the output and converts them into win32 calls. So the test case does not find them in pip's output. We might be able to monkeypatch our way around this, but the added value is doubtful. If the color sequences are emitted as expected on the other platforms, then colorama should take care of the rest.
On Windows, colorama translates ANSI color sequences into calls to SetConsoleTextAttribute[1]. We can check if pip uses colored output on Windows by monkey-patching `colorama.win32.SetConsoleTextAttribute` with pretend's call recorder. Note that this test relies on an internal interface of colorama. While SetConsoleTextAttribute itself is part of the public win32 API, how colorama encapsulates this function is an implementation detail. [1]: https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute
Monkey-patching around the win32 call cannot work because the functional tests run pip as a subprocess. And anyway, this is an implementation detail of colorama. Instead, simply assert that the ANSI sequence is never present on Windows.
These tests check if SetConsoleTextAttribute is called on Windows for various settings of the `color` parameter to ColorizedStreamHandler. This is achieved by monkey-patching colorama.win32. Note that this module is an internal interface, and may change without notice. The tests disable pytest's output capturing. Colorama checks if either STDOUT or STDERR are a console, using GetConsoleScreenBufferInfo. With pytest's output capturing enabled, this function fails with ERROR_INVALID_HANDLE. A log record with an empty message is used for testing, to avoid garbage in the test runner output.
Passing color="always" won't have an effect on Windows if the output is not a terminal. Color output on Windows relies on the win32 console interface, and that does not apply to pipes and the like. Colorama checks if the output is a console using GetConsoleScreenBufferInfo with STDOUT and STDERR handles. If this check fails, it will simply strip color sequences from the output.
|
|
||
| with capsys.disabled(): | ||
| if not win32.winapi_test(): | ||
| pytest.skip("output is not connected to a terminal") |
There was a problem hiding this comment.
Is there a way to not skip a test in the middle of itself? Maybe just return or something?
I may be wrong on this 😄
There was a problem hiding this comment.
IMHO it might be a better idea to move the check out as a decorator, that'd be easier to understand.
There was a problem hiding this comment.
Yeah, there is a skipif(colorma is None)
You can add/replace in this check
There was a problem hiding this comment.
The thing is that the check needs to run with output capturing disabled (capsys.disabled) 😳 I think it’s clearer this way, but let me know if I missing something
|
Hello! I am an automated bot and I have noticed that this pull request is not currently able to be merged. If you are able to either merge the |
|
Hello! I am an automated bot and I have noticed that this pull request is not currently able to be merged. If you are able to either merge the |
|
Closing as this is significantly out of date, and has merge conflicts. Please do feel free to resubmit an updated PR for this! ^>^ Thanks for filing this PR @cjolowicz! :) |
Add a new option
--color=WHEN.WHENcan bealways,auto, ornever:always: Force color outputauto: Enable color if output is a tty (default behavior)never: Do not color output.With this change, the existing option
--no-colorbecomes a shorthand for--color=never.Closes #8132
Having a way to force color output is useful because some environments don't provide a tty, while colors would still improve readability. For example, this is currently the case with GitHub Actions.