-
Notifications
You must be signed in to change notification settings - Fork 13
test: pin Rich colour and width so CLI-output tests stop failing outside CI #1332
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
Open
saltas888
wants to merge
4
commits into
develop
Choose a base branch
from
pha/INBOX-166
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
870ace1
test: pin Rich colour and width so CLI-output tests stop failing locally
saltas888 5c3756b
test: pin the rendering-environment invariant with regression tests
saltas888 caada89
test: cover the empty FORCE_COLOR form in the render-env regression test
saltas888 8d901e8
test: address review — schema_console fixture, meta package, shorter …
saltas888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Pinned Rich's colour and width for the test suite from `pytest_configure`, so CLI-output assertions render identically regardless of the developer's `FORCE_COLOR`/`COLUMNS` environment. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
|
saltas888 marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Guards for the Rich rendering environment pinned in ``tests/conftest.py``. | ||
|
|
||
| CLI-output assertions elsewhere in this suite compare against text whose colour and width Rich | ||
| decides. ``pytest_configure`` pins that decision; these tests pin the pinning, so a future edit | ||
| that loosens it fails here with an explanation rather than as a scatter of puzzling | ||
| output-comparison failures across the ctl tests. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from io import StringIO | ||
|
|
||
| import pytest | ||
| from rich.console import Console | ||
|
|
||
| from tests.conftest import RENDER_ENV, RENDER_ENV_TO_CLEAN | ||
|
|
||
|
|
||
| def test_render_env_is_pinned() -> None: | ||
| """The hook in conftest ran, and ran before this module was imported.""" | ||
| for name in RENDER_ENV_TO_CLEAN: | ||
| assert name not in os.environ, f"{name} must be unset: Rich reads any value as 'this is a terminal'" | ||
| for name, value in RENDER_ENV.items(): | ||
| assert os.environ.get(name) == value | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("term", ["dumb", "unknown", "xterm-256color", "screen", ""]) | ||
| def test_console_width_survives_any_term(term: str, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """``TERM`` must not change the rendered width, whatever the developer's shell exports. | ||
|
|
||
| Rich clamps the width to 80 and ignores ``COLUMNS`` on a *dumb* terminal, but | ||
| ``is_dumb_terminal`` is ``is_terminal and TERM in ("dumb", "unknown")`` -- so the clamp needs | ||
| Rich to also believe it is on a terminal. Captured test output never is, and the conftest hook | ||
| removes the one variable (``FORCE_COLOR``) that would make Rich claim otherwise. That is why | ||
| the hook does not pin ``TERM``, and why pinning it to a non-dumb value would be a no-op. | ||
| """ | ||
| monkeypatch.setenv("TERM", term) | ||
|
|
||
| console = Console(file=StringIO()) | ||
|
|
||
| assert console.is_terminal is False | ||
| assert console.is_dumb_terminal is False | ||
| assert console.width == int(RENDER_ENV["COLUMNS"]) | ||
| assert console.no_color is True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("force_color", ["1", "3", ""]) | ||
| def test_force_color_is_what_would_clamp_the_width(force_color: str, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Pin the failure mode the hook exists to prevent. | ||
|
|
||
| With ``FORCE_COLOR`` set, Rich treats the captured output as a terminal; combined with | ||
| ``TERM=dumb`` that drops it to width 80, which truncates the wide tables the CLI-output | ||
| fixtures record. This is the state a developer's shell puts the suite in, and the reason | ||
| ``FORCE_COLOR`` is removed rather than merely overridden. | ||
|
|
||
| The empty string is the case that makes *removal* the only correct fix: Rich tests | ||
| ``FORCE_COLOR is not None``, so ``export FORCE_COLOR=`` forces a terminal just as ``1`` does, | ||
| and overriding the variable with a falsy value would not defuse it. | ||
| """ | ||
| monkeypatch.setenv("FORCE_COLOR", force_color) | ||
| monkeypatch.setenv("TERM", "dumb") | ||
|
|
||
| console = Console(file=StringIO()) | ||
|
|
||
| assert console.is_dumb_terminal is True | ||
| assert console.width == 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.