[BUG] Console tests fail when COLUMNS or LINES is exported in the shell
- [x] I've checked docs and closed issues for possible solutions.
- [x] I can't find my issue in the FAQ.
Describe the bug
Running the test suite in a shell that exports COLUMNS (as CI systems and some terminal multiplexers do) makes 6 tests in tests/test_console.py fail, because the environment takes precedence over the mocked terminal size:
COLUMNS=200 LINES=50 python -m pytest tests/test_console.py -p no:randomly
FAILED tests/test_console.py::test_size_can_fall_back_to_std_descriptors[False-ValueError-ValueError-ValueError-ValueError-expected_size5]
assert (200, 50) == (80, 25)
6 failed, 94 passed
Without the variables exported the same file passes 100/100. I checked this in a controlled comparison (32 runs with the variables exported vs 32 without, with hash seed, timezone and locale also varied as controls): 0/32 pass exported, 32/32 pass clean, and none of the control factors move the result.
The library behavior is intentional (Console.__init__ reads COLUMNS and LINES from self._environ, rich/console.py around line 685). The tests just never isolate themselves from the inherited environment, while other test files already do: tests/test_tree.py and tests/test_logging.py construct their consoles with _environ={} for exactly this reason.
Proposed fix
Pass _environ={} to the Console instances in the affected tests/test_console.py tests (or scrub COLUMNS/LINES with an autouse monkeypatch fixture for that file). Happy to send a PR if that direction works for you.
Platform
Reproduced on macOS (arm64), Python 3.13, pytest 9.1.1, at master 9d8f9a3; the mechanism only depends on the inherited environment, so it should not be platform-specific.
Investigated with the help of Claude (Anthropic's Claude Code).
Test comment
Reproduced exactly as reported: COLUMNS=200 LINES=50 python -m pytest tests/test_console.py -p no:randomly → 6 failed, 94 passed, all six parametrizations of test_size_can_fall_back_to_std_descriptors failing on assert (200, 50) == (80, 25).
Where it happens: Console.size measures the standard file descriptors first, then lets self._environ's COLUMNS/LINES override whatever was measured (the columns = self._environ.get("COLUMNS") block after the descriptor loop in rich/console.py). The test builds Console(legacy_windows=False) with no _environ, so exported variables win over the mocked terminal size.
Fix — branch ready to use: https://github.com/Textualize/rich/compare/main...Shanmukh-l33t:fix-4222-test-env-isolation
tests/test_console.py:Console(legacy_windows=False, _environ={})— the same patterntests/test_tree.pyandtests/test_logging.pyalready use to keep consoles hermetic.tests/test_ansi.py::test_decode_example: same root cause, one test the file-scoped repro above doesn't reach — its expected output wraps at 80 columns, so it fails once the console becomes 200 wide. It now passes_environ={}as well.
Evidence (variables exported unless noted):
tests/test_console.py: 6 failed / 94 passed → 100 passed;tests/test_ansi.py: 24 passed.- Full suite with the variables exported: 15 failed → 8 failed, and those 8 are exactly the tests that already fail on the untouched tree without the variables in my environment (
test_card_render,test_inline_code_in_table_cells, sixtest_syntaxcases — pygments rendering differences here), so the set of environment-caused failures is now empty. - Full suite without the variables: 948 passed / 25 skipped / 8 failed — byte-identical failure set before and after the change, so nothing regressed for shells that don't export them.
black --checkclean on both files;mypy -p richreports only the pre-existingconsole.py:1536error (file untouched here).
You offered to send a PR if that direction works — if you're still planning to, treat this branch as reference-only and yours; if not, it's ready to open as-is. I don't have pull-request creation rights on this repo from this account, so the branch plus compare link is the delivery.
Proposed fix (minimal, test-isolation): in tests/test_console.py, construct Console with _environ={} in the affected tests (mirroring tests/test_tree.py/tests/test_logging.py), so COLUMNS/LINES exported in the shell don’t leak into console size calculations. I’d be happy to open a PR if this direction is approved by a maintainer. If there’s a preferred approach (e.g. fixture/monkeypatch at module level), I’ll follow it.