diff options
| author | Peter Geissler <peter@haxx.in> | 2026-08-28 14:41:42 +0200 |
|---|---|---|
| committer | Peter Geissler <peter@haxx.in> | 2026-08-28 14:41:42 +0200 |
| commit | 2fbd36c1cef6ea6c9fcf9c36afb5209089288257 (patch) | |
| tree | c95a97a4277306eddc7daeb67dddac6daa1e943d /tests/test_ctrl_d.py | |
| parent | windows build: re-stamp version.h when the version changes (diff) | |
| download | slosh-2fbd36c1cef6ea6c9fcf9c36afb5209089288257.tar.gz slosh-2fbd36c1cef6ea6c9fcf9c36afb5209089288257.tar.xz slosh-2fbd36c1cef6ea6c9fcf9c36afb5209089288257.zip | |
ctrl_d_exits: Ctrl-D at a prompt, where the tty cannot do it
Windows has no VEOF. The console driver does not know the character, so
^D arrives at cmd.exe and PowerShell as an ordinary keystroke and they
ignore it -- it even echoes, which is how you find out: the line reads
`echo hello^D`. The one key that closes a pane on every other platform
does nothing at all, and closing a pane becomes a word you have to type.
That you can type `exit` instead is the tell: the intent was never
ambiguous, only unspoken. So slosh speaks it. What goes to the pane is
the word, not a signal and not a close from outside -- the shell runs
its own cleanup, returns its own status, and keep_dead decides what is
left behind, all three of which reaching past it would skip. It is also
what the user would have typed, so there is nothing new to explain.
The knob is what makes it honest, because a wrong guess costs a pane.
It fires only with the shell itself in the foreground (a program that
is running may want the EOF for itself -- `cat` reading to end is the
whole job), only on the primary screen (in an editor or a pager ^D is
half a page down, and the alternate screen is the pane stating that
rather than us matching process names), and only on an empty line
(POSIX sends EOF there; what it does not do is discard what you typed).
The line is an estimate. The shell's editor is on the far side of a
pty, so pane_send_key counts what it forwarded since the last Enter.
Editing it does not model -- a recalled history entry, ^U, completion
-- leaves the count too high, which forgets to exit. That is the
direction to be wrong in; the other one throws away a half-typed
command.
Default true on Windows, false on POSIX, where the line discipline
already does this and does it properly: ^D there is also EOF for `cat`
and every REPL, none of which is ours to intercept. A knob rather than
an #ifdef so the guards are the same code everywhere and can be tested
where the test suite runs -- test_ctrl_d.py turns it on and drives all
four decisions, plus the case that does exit.
Telling trapped from forwarded needs a program that reports what it
received: on POSIX a forwarded ^D also ends an idle shell, and looking
for the word `exit` cannot separate them either, because bash prints
`exit` itself on EOF.
Verified on the Windows box as well, where it is the default: ^D at an
idle cmd.exe prompt takes the pane (and with it the session, having no
panes left), `more` in the foreground keeps its own EOF, and a
half-typed line survives with the line still on screen.
Diffstat (limited to 'tests/test_ctrl_d.py')
| -rw-r--r-- | tests/test_ctrl_d.py | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/tests/test_ctrl_d.py b/tests/test_ctrl_d.py new file mode 100644 index 0000000..c95ddab --- /dev/null +++ b/tests/test_ctrl_d.py @@ -0,0 +1,192 @@ +#!/usr/bin/env python3 +"""`ctrl_d_exits`: Ctrl-D at an idle prompt, where the tty cannot do it. + +Windows has no VEOF, so ^D there is a key that does nothing. The knob makes +slosh send `exit` on the user's behalf -- but only where that is unambiguously +what ^D meant, because every other case costs a pane. Those refusals are the +interesting half, and most of what is below. + +Off by default on POSIX, so the tests that want it say so. The guards are the +same code on every platform, which is why it is a knob and not an `#ifdef` +nobody outside Windows could ever run. + +Telling trapped from forwarded needs care here. On POSIX a *forwarded* ^D also +ends an idle shell -- that is the line discipline doing its job -- so "did the +pane go" cannot tell the two apart, and neither can looking for the word +`exit`, which bash prints itself on EOF. PROBE settles it: a program that +reads one line and shows exactly what arrived. `exit` is what slosh types; +an empty line is EOF reaching the program untouched. +""" + +import os +import sys +import tempfile + +from harness import Session, check, report + +SH = ["/bin/sh"] +# Reads one line, then says what it got. The sleep keeps the pane up long +# enough to be read: the answer is on screen, not in an exit status. +PROBE = ["/bin/sh", "-c", 'read x; echo "GOT:[$x]"; sleep 5'] +# The same, from the alternate screen, switching back so the answer is visible. +PROBE_ALT = [ + "/bin/sh", + "-c", + 'printf "\\033[?1049h"; read x; printf "\\033[?1049l"; echo "GOT:[$x]"; sleep 5', +] + + +def conf(text): + f = tempfile.NamedTemporaryFile("w", suffix=".kdl", delete=False) + f.write(text) + f.close() + return f.name + + +ON = conf("ctrl_d_exits true\n") +OFF = conf("ctrl_d_exits false\n") +# keep_dead all: the corpse stays, so its exit status can be read. +CORPSE = conf("ctrl_d_exits true\nkeep_dead all\n") + + +def got(s): + """What the probe program actually received.""" + snap = s.until_text("GOT:") + for line in snap.screen().splitlines(): + if "GOT:[" in line: + return line.split("GOT:[", 1)[1].split("]", 1)[0] + return None + + +def gone(s): + return len(s.panes()) == 0 + + +def test_ctrl_d_at_an_idle_prompt_ends_the_shell(): + with Session(SH, cols=60, rows=10, config=ON) as s: + s.settle(80) + check("the pane is alive to begin with", len(s.panes()) == 1, str(s.panes())) + s.send("\\x04") + snap = s.until(lambda _: gone(s)) + check("^D closed it", gone(s), snap.screen()) + + +def test_what_arrives_is_the_word_exit(): + with Session(PROBE, cols=60, rows=10, config=ON) as s: + s.settle(80) + s.send("\\x04") + check("the pane was sent `exit`", got(s) == "exit", repr(got(s))) + + +def test_the_shell_exits_rather_than_being_reached_past(): + """The shell ends itself: status 0, no signal. + + Closing the pane from outside would leave no status at all, and killing the + process would leave a signal. Both are visible here, which makes this the + test that says *how* it was done rather than only that it was.""" + with Session(SH, cols=60, rows=10, config=CORPSE) as s: + s.settle(80) + s.send("\\x04") + s.until(lambda _: not s.pane(0)["alive"]) + pane = s.pane(0) + check("the pane is dead but kept", not pane["alive"], str(pane)) + check("it exited, and was not signalled", pane["exit_signal"] == 0, str(pane)) + check("with the shell's own status", pane["exit_code"] == 0, str(pane)) + + +def test_off_by_default(): + with Session(PROBE, cols=60, rows=10) as s: + s.settle(80) + s.send("\\x04") + check("the raw ^D is forwarded, and arrives as EOF", got(s) == "", repr(got(s))) + + +def test_the_knob_turns_it_off(): + with Session(PROBE, cols=60, rows=10, config=OFF) as s: + s.settle(80) + s.send("\\x04") + check("off means off", got(s) == "", repr(got(s))) + + +def test_a_half_typed_line_is_not_thrown_away(): + """POSIX sends EOF on a line with something on it and the shell ignores it. + What it does not do is discard the line, and neither do we -- this is the + guard that stops a mistyped ^D costing the command you were composing.""" + with Session(SH, cols=60, rows=10, config=ON) as s: + s.settle(80) + s.send("echo hello") + s.settle(60) + s.send("\\x04") + s.settle(120) + check("the pane survived", len(s.panes()) == 1, str(s.panes())) + screen = s.snapshot().screen() + check("and the line is still there", "echo hello" in screen, screen) + + +def test_backspacing_the_line_away_makes_it_empty_again(): + """The count comes back down, so a typo you corrected does not wedge ^D + off for the rest of the line.""" + with Session(SH, cols=60, rows=10, config=ON) as s: + s.settle(80) + s.send("ab") + s.settle(60) + s.send("\\x7f\\x7f") # two backspaces + s.settle(60) + s.send("\\x04") + snap = s.until(lambda _: gone(s)) + check("an emptied line exits again", gone(s), snap.screen()) + + +def test_enter_clears_the_line(): + """Having run a command must leave the next prompt exitable.""" + with Session(SH, cols=60, rows=10, config=ON) as s: + s.settle(80) + s.send("echo one\\r") + s.until_text("one") + s.send("\\x04") + snap = s.until(lambda _: gone(s)) + check("^D works at the next prompt", gone(s), snap.screen()) + + +def test_a_program_in_the_foreground_keeps_its_own_eof(): + """`cat` wants the EOF itself: reading stdin to the end is the whole job. + Swallowing ^D here would break the thing this is imitating.""" + with Session(SH, cols=60, rows=10, config=ON) as s: + s.settle(80) + s.send("cat\\r") + s.settle(120) + s.send("hi\\r") + s.until_text("hi") + s.send("\\x04") # ends cat, not the shell + s.settle(150) + check( + "the pane is still here: ^D went to cat", + len(s.panes()) == 1, + str(s.panes()), + ) + s.send("echo back\\r") + snap = s.until_text("back") + check( + "and the shell has its prompt again", "back" in snap.screen(), snap.screen() + ) + + +def test_the_alternate_screen_keeps_ctrl_d(): + """In an editor or a pager ^D is half a page down. The alternate screen is + the pane stating so, rather than us guessing from a process name. + + Same program and same knob as test_what_arrives_is_the_word_exit, differing + only by the screen it reads from -- so an empty answer here is the screen + guard and nothing else.""" + with Session(PROBE_ALT, cols=60, rows=10, config=ON) as s: + s.settle(120) + s.send("\\x04") + check("^D was forwarded, not spent", got(s) == "", repr(got(s))) + + +for name, fn in sorted(list(globals().items())): + if name.startswith("test_"): + fn() +for path in (ON, OFF, CORPSE): + os.unlink(path) +sys.exit(report()) |
