aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_ctrl_d.py
blob: c95ddab79c7a78ffc7cd38a6aa39525d5be0e9fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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())