tests.test_recording module

Tests for click_extra.capture.recording, which records a terminal’s screens.

class tests.test_recording.StepClock(step=1.0)[source]

Bases: object

A clock advancing a fixed step per reading, so a test can time frames.

tests.test_recording.test_recorder_claims_to_be_a_terminal()[source]

A spinner animates for a recorder without being told to.

The whole question a spinner asks before drawing is whether its stream is a terminal. Answering it is what makes the in-process path need no pseudo-terminal, and therefore work on every platform.

tests.test_recording.test_recorder_frames_carry_what_the_screen_held()[source]

Each frame lasts until the next screen replaced it.

tests.test_recording.test_recorder_drops_a_write_that_changed_nothing()[source]

A write leaving the screen as it was is not a frame of its own.

tests.test_recording.test_recorder_drops_a_cursor_shape_change()[source]

A sequence the screen does not act on adds no frame, whatever its bytes.

ESC [ 2 SP q sets the cursor’s shape and carries an intermediate byte. A grammar stopping at the first letter missed it, so the sequence landed on the screen as text and split one picture into two identical frames.

tests.test_recording.test_recorder_stores_one_sample_per_distinct_screen()[source]

A write changing nothing stores nothing, so memory follows the screens.

A spinner redraws the same frame for as long as its label holds, and a long recording would otherwise keep a copy of the whole screen per redraw.

tests.test_recording.test_recorder_drops_a_screen_holding_only_blanks()[source]

An erasure, and the newline ending it, picture nothing.

tests.test_recording.test_recorder_records_a_spinner_without_a_terminal(monkeypatch)[source]

A spinner handed a recorder animates into it, on any platform.

No pseudo-terminal is involved, which is what a documentation build on Windows depends on.

tests.test_recording.test_recorder_notes_a_redraw_once_its_reads_settle()[source]

Reads arriving within one redraw make one frame, never a half-drawn one.

A pseudo-terminal hands a large redraw back in several reads, a fraction of a millisecond apart. A screen noted after each of them would picture the program halfway through drawing.

tests.test_recording.test_recorder_holds_a_synchronized_update_until_it_closes()[source]

A program asking its terminal to wait gets no frame of what it drew meanwhile.

tests.test_recording.test_recorder_takes_no_frame_of_its_preface_alone()[source]

A screen showing only what stood before the command started is no frame.

The command erasing its own output leaves the prompt it was typed at, which is where the recording started, not a picture of the command.

tests.test_recording.BAR_SCRIPT = "import sys, time\nfor filled in range(4):\n    sys.stderr.write('\\r[' + '#' * filled + ']\\x1b[K')\n    sys.stderr.flush()\n    time.sleep(0.05)\n"

A progress bar drawn to stderr, redrawn in place, as a real one is.

tests.test_recording.test_record_command_recovers_a_foreign_animation()[source]

A command this process does not host still animates, under a terminal.

The case a pipe cannot reach: the program asks whether it is talking to a terminal, and only a pseudo-terminal makes it draw. Its frames go to stderr, where a spinner and a progress bar both write.

tests.test_recording.STUBBORN_SCRIPT = '\nimport signal, sys, time\nsignal.signal(signal.SIGTERM, signal.SIG_IGN)\nsys.stdout.write("ripe")\nsys.stdout.flush()\ntime.sleep(6)\n'

A command that ignores SIGTERM and outlives the recording.

tests.test_recording.test_record_command_kills_a_command_ignoring_sigterm(monkeypatch)[source]

A command ignoring SIGTERM is killed once the grace runs out, so the recording returns instead of waiting for the command to exit on its own.

tests.test_recording.SLEEPER_SCRIPT = '\nimport pathlib, sys, time\nsys.stdout.write("ripe")\nsys.stdout.flush()\npathlib.Path(sys.argv[1]).touch()\ntime.sleep(30)\n'

A command that draws once, touches the file it is given, then waits for a signal to end it.

tests.test_recording.test_record_command_registers_its_command_for_ctrl_c(tmp_path)[source]

A recording registers its command where the Ctrl+C handlers find it.

A worker thread records while the main thread calls terminate_live_processes, which is what a Ctrl+C runs: the command ends, and so does the recording, long before its duration. The interrupt waits for the command to draw, which a slow runner can delay past registration.

tests.test_recording.STUBBORN_PARENT_SCRIPT = '\nimport signal, subprocess, sys, time\nsignal.signal(signal.SIGTERM, signal.SIG_IGN)\nseed = subprocess.Popen((sys.executable, "-c", "import time; time.sleep(30)"))\nsys.stdout.write(f"seed={seed.pid}")\nsys.stdout.flush()\ntime.sleep(30)\n'

A command ignoring SIGTERM that starts a child of its own, which does too.

tests.test_recording.test_record_command_kills_what_its_command_started(monkeypatch)[source]

A command killed at the end of its recording takes its descendants along.

They would otherwise outlive the recording, holding the terminal it drew on.

tests.test_recording.SPLIT_GLYPH_SCRIPT = '\nimport sys, time\nraw = sys.stdout.buffer\nglyph = "╰".encode("UTF-8")\nraw.write(glyph[:1])\nraw.flush()\ntime.sleep(0.2)\nraw.write(glyph[1:] + b" ripe")\nraw.flush()\n'

A box-drawing corner flushed one byte at a time, as a busy pty read splits it.

tests.test_recording.test_record_command_reassembles_a_glyph_split_across_reads()[source]

A multi-byte glyph straddling two pty reads decodes whole.

A pseudo-terminal hands the stream back in kernel-buffer-sized chunks, so a heavy redraw regularly splits a box-drawing glyph or an emoji across two reads. Decoding each chunk on its own mangles both halves into U+FFFD; the recorder must hold the partial sequence until its tail arrives.

tests.test_recording.test_record_command_closes_its_terminal_when_the_command_cannot_start(tmp_path)[source]

A command that never started leaves no end of its terminal open.

The pseudo-terminal is opened before the command is spawned, so a spawn refused by the system is the one exit that has both ends to close.

tests.test_recording.FULL_SCREEN_SCRIPT = 'import curses, time\n\ndef fill(screen):\n    curses.curs_set(0)\n    # Draw on the terminal\'s own background, not on curses\' default black.\n    curses.use_default_colors()\n    for count in (1, 2, 3):\n        screen.erase()\n        screen.addstr(1, 4, f"apples: {count}")\n        screen.refresh()\n        time.sleep(0.15)\n\ncurses.wrapper(fill)\n'

A full-screen program counting apples on the alternate screen, with curses.

tests.test_recording.test_record_command_follows_a_full_screen_program()[source]

Each redraw of a curses program is a frame of the screen it addresses.

The recording names xterm-256color to the program, so curses finds an entry even where TERM is unset. Every frame holds the alternate screen at its full height, and the first screen coming back at exit, blank, is none.

tests.test_recording.test_record_and_render_draws_the_prompt_on_every_frame()[source]

The stated invocation leads each frame, styled as a shell prompt.

tests.test_recording.test_record_and_render_hides_an_empty_prompt()[source]

An empty –prompt draws no invocation at all.

tests.test_recording.test_record_and_render_reports_the_exit_code()[source]

The command’s own verdict travels beside the picture of it.

tests.test_recording.test_record_and_render_refuses_an_empty_recording()[source]

A command that drew nothing leaves nothing to animate.

tests.test_recording.text_columns(svg, text)[source]

Every column a capture starts a run reading text on, in any frame.

Return type:

set[int]

tests.test_recording.test_record_and_render_numbers_every_frame_in_one_gutter(monkeypatch)[source]

A screen reaching ten rows does not push its text a cell to the right.

Each frame counts its own rows, from one. Numbered on its own, a nine-row screen aligns its numbers on one digit and the ten-row screen after it on two, so every row moves between the two frames. The recording is stated rather than timed, so both screens are sure to be in it.

tests.test_recording.test_quantize_lands_jitter_on_one_grid(recorded, expected)[source]

Two runs of one unchanged command time their frames the same.

tests.test_recording.test_quantize_keeps_the_frames_it_times()[source]

Rounding a duration never rewrites the screen it belongs to.

tests.test_recording.test_quantize_rejects_a_grid_going_nowhere()[source]
tests.test_recording.test_start_at_opens_on_the_screen_standing_then(start, expected)[source]

The frame on screen at start stays, with what remains of its time.

tests.test_recording.test_start_at_refuses_a_start_leaving_nothing(start, message)[source]
tests.test_recording.test_end_at_stops_on_the_screen_standing_then(end, expected)[source]

The frame on screen at end keeps what came before it, the rest goes.

tests.test_recording.test_end_at_refuses_an_end_before_anything(end)[source]
tests.test_recording.test_record_and_render_ends_where_asked(monkeypatch)[source]

start and end keep the slice between them, on one clock.

tests.test_recording.test_record_and_render_refuses_an_end_before_the_start(monkeypatch)[source]

A slice that holds nothing is refused before the command runs.

tests.test_recording.test_record_and_render_starts_where_asked(monkeypatch)[source]

start drops the screens shown before it, and keeps the one standing then.

The recording is stated rather than timed, so the cut lands inside the second screen whatever the machine’s load.

tests.test_recording.test_quantized_recordings_of_one_command_render_the_same_bytes()[source]

The point of the exercise: a rerun rewrites the asset byte for byte.

Two recordings of one unchanged command, timed a few milliseconds apart the way two runs on a differently loaded machine are.

tests.test_recording.test_a_dropped_frame_does_not_move_the_recording()[source]

A frame the scheduler lost leaves the animation’s identity alone.

Quantizing cannot recover it, so the fingerprint is written not to care: it covers the frames a cycle holds and the beat, not how many were caught.

tests.test_recording.test_a_changed_command_moves_the_recording()[source]

What the fingerprint is for: real drift is still reported.

tests.test_recording.TYPED_PROMPT = '\x1b[90m$\x1b[0m \x1b[97mbasket\x1b[0m ripen --all'

A styled prompt line, as format_cli_prompt composes one.

tests.test_recording.test_ansi_prefix_keeps_the_styling_in_force(count, expected)[source]

An escape is drawn nowhere, so every one before the cut still applies.

tests.test_recording.test_ansi_prefix_never_cuts_an_escape(count)[source]

A half-written escape lands on the screen as the digits it is made of.

tests.test_recording.test_type_line_makes_one_frame_per_character()[source]

A keystroke is a screen, so a character is a frame.

tests.test_recording.test_type_line_holds_the_finished_line_for_the_submit_beat()[source]

The pause before the return key, and only on the frame that waits it.

tests.test_recording.test_type_line_types_nothing_for_an_empty_line()[source]

A recording carrying no prompt has no opening to play.

tests.test_recording.test_type_line_rejects_a_beat_going_nowhere(kwargs, message)[source]

Seconds run forwards, and a frame lasting none is never shown.

tests.test_recording.test_a_typed_opening_walks_its_cursor_one_cell_at_a_time()[source]

The caret is the cursor, and it follows the text with no code of its own.

What the design rests on: a cursor is drawn as part of the row it stands on, read off that row’s text, so an animation typing a line gets its caret for nothing.

tests.test_recording.test_a_recording_types_its_prompt_when_asked()[source]

The typed opening leads the recorded frames, under the prompt it types.

tests.test_recording.test_a_recording_types_nothing_unless_asked()[source]

By default, a recording opens on the finished prompt.

tests.test_recording.RIPENED_SCRIPT = "import sys, time\nfor filled in range(4):\n    sys.stderr.write('\\r[' + '#' * filled + ']\\x1b[K')\n    sys.stderr.flush()\n    time.sleep(0.05)\nsys.stderr.write('\\r\\x1b[Kripe\\n')\n"

The same bar, ending on a newline the way a finished command does.

tests.test_recording.last_drawn_row(svg)[source]

Index of the lowest row a capture draws a glyph on, counted from zero.

Matched on the styled runs alone: the caption and the credit line are <text> too, and both sit outside the grid this measures.

Return type:

int

tests.test_recording.window_height(svg)[source]

How tall the capture is, in pixels.

Return type:

float

tests.test_recording.recorded(script, **kwargs)[source]

Record one script, however the capture is asked to close.

Return type:

str

tests.test_recording.test_a_closing_prompt_fills_the_row_the_cursor_waited_on()[source]

A command closing on a newline already left the row the shell wants.

Which is what makes the pair free: the window is the same height either way, and the row holding nothing but a cursor holds a prompt instead.

tests.test_recording.test_a_closing_prompt_opens_a_row_under_a_screen_left_mid_line()[source]

A command that never ended its line is given one, as a shell gives it.

The free case above is the common one and not the only one: a progress bar redrawing in place leaves the cursor mid-line, so the shell coming back costs the row it prints its own newline onto.

tests.test_recording.test_a_closing_prompt_stays_off_a_full_screen_program(monkeypatch)[source]

A command stopped on the alternate screen gets no shell prompt drawn.

Stopped by the recording, a full-screen program leaves its own screen up, where the shell coming back would print over the program.

tests.test_recording.TACHYON_CAST = PosixPath('/home/runner/work/click-extra/click-extra/tests/tachyon-live.cast')

Tachyon’s live mode as a terminal received it, see tests/tachyon_cast.py.

tests.test_recording.test_tachyon_live_mode_replays_screen_by_screen(monkeypatch)[source]

Python’s live profiler records into an animation of every screen it drew.

Closes the loop on the screenshot the Python documentation shows as a GIF: the curses interface of python -m profiling.sampling run --live, as ncurses wrote it at an xterm-256color terminal of the same size, is read back through the recorder with the timing each chunk arrived with, and rendered the way the screenshot --record command renders it.

tests.test_recording.test_line_screen_names_warn_until_their_removal(name, released)[source]

The names of the 10.2.0 line screen still resolve, with a warning.

TerminalScreen resolves to the class of its new module.

tests.test_recording.test_recording_module_serves_no_other_missing_name()[source]

A name the module never had stays missing, and sets off no warning.