Exemplo
Render rich Console output to a string a test can assert on, and pin the escape codes, width, markup and highlighting that decide what the string holds
sha256:3dae0c04df2ebeff413e9a4a6dc0213423def8b204f079fa3e738abce3780035
PUBLISHED
L3_CONTRACT_PASS
MIT-0
Caso
- Objetivo
- Render rich Console output to a string a test can assert on, and pin the escape codes, width, markup and highlighting that decide what the string holds HOW
- Pacotes
- rich 15.0.0
- Ambiente
- python 3.12
- Criado
- 2026-08-14T13:20:38Z
Contrato
- assert a Console writing to a StringIO reports is_terminal False and color_system None, and emits the markup's text with no escape byte in it
- assert force_terminal=True on the same StringIO emits the bold escape sequence, so the codes are a setting rather than a property of the output
- assert TERM and COLORTERM decide which color_system is detected and COLORTERM outranks TERM, while neither moves the bytes for bold, which is an attribute and not a colour
- assert color_system is settled in the constructor where width is read at access time, so one console follows the environment for its width and ignores it for its colours
- assert FORCE_COLOR in the environment turns is_terminal on for a plain file, which is how a CI log fills with escape codes nobody asked for
- assert TTY_COMPATIBLE is read before FORCE_COLOR and 0 defeats it while 1 turns is_terminal on by itself, so that fix stops working in a runner exporting it
- assert TERM=dumb leaves is_terminal True while dropping color_system to None, so the two are separate switches
- assert no_color=True still emits the bold code and only color_system=None suppresses every escape sequence
- assert a Style keeps the codes it rendered first, so the console that renders a colour first fixes its bytes for the process and a later console with a different color_system repeats them
- assert console.capture() returns the rendered text and redirects rather than tees, so the console's own file receives nothing while the capture is open
- assert a capture keeps the escape codes the console would have written, so it is not a way to get plain text
- assert nested captures do not nest: the inner result holds everything printed since the outer block began and the outer result holds only what followed
- assert record=True with export_text clears the recording by default, so a second call returns an empty string
- assert os.get_terminal_size raises with no tty and the console falls back to width 80 by height 25
- assert COLUMNS overrides that fallback and is read at access time, so an existing console changes width when the environment does, while an explicit width= beats COLUMNS
- assert a small Table renders identically at 40, 80 and 120 columns because it measures its content, while expand=True and overflowing text are what follow the console width
- assert markup is interpreted by default and markup=False renders the tags literally, both per-call and per-console
- assert markup deletes user text without an error for list[int], for a bracketed sentence and for an unknown style name, on a console emitting codes as well as one that is not
- assert a stray closing tag raises rich.errors.MarkupError, which is a ConsoleError and not a ValueError, and that escape() or markup=False prevents it
- assert emoji codes are substituted by default and that a Text object is never parsed for markup
- assert console.print uses __rich__ first, then __rich_console__, and that __rich__ wins when both exist and is looked up on the instance
- assert an object with neither hook is rendered with str() and not repr(), while a container is pretty-printed with repr() on its items
- assert the str() branch skips markup and emoji parsing, so printing an object cannot be injected the way printing a string can, while the highlighter is the one rewrite it does not skip
- assert a string returned from __rich__ goes through the highlighter, which splits the markup's style run in two on the number inside it
- assert the highlighter styles a bare number, a number inside a sentence and the shape of a repr unless highlight=False
- assert highlight=False changes nothing on a console that emits no codes, because the highlighter still runs and its spans have nowhere to go
- assert rich exports no __version__, so the version has to come from importlib.metadata
Arquivos
- csx.json
- requirements.txt
- src/__init__.py
- src/report.py
- test/contract.py
Seeder de origem
Recibos de verificação
- python 3.12 · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · python@1 · 2026-08-14 · ed25519:a2ec939a4c60e243
- · CONTAINER_RUN · compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS · python@1 · 2026-08-14 · ed25519:7da3f610facce8bf