fish-shell/tests/pexpects/read.py

161 lines
3.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from pexpect_helper import SpawnedProc
sp = SpawnedProc()
send, sendline, sleep, expect_prompt, expect_re, expect_str = (
sp.send,
sp.sendline,
sp.sleep,
sp.expect_prompt,
sp.expect_re,
sp.expect_str,
)
def expect_read_prompt():
expect_re(r"\r\n?read> .*$")
def expect_marker(text):
expect_prompt("\r\n.*@MARKER:" + str(text) + "@\\r\\n")
def print_var_contents(varname, expected):
sendline("echo $" + varname)
expect_prompt(expected)
expect_prompt()
# read
sendline("read")
expect_read_prompt()
sendline("text")
expect_prompt()
sendline("read foo")
expect_read_prompt()
sendline("text")
expect_prompt()
print_var_contents("foo", "text")
sendline("read foo")
expect_read_prompt()
sendline("again\r_marker 1")
expect_prompt()
expect_marker(1)
print_var_contents("foo", "again")
sendline("read foo")
expect_read_prompt()
sendline("bar\r_marker 2")
expect_prompt()
expect_marker(2)
print_var_contents("foo", "bar")
# read -c (see #8633)
sendline(r"read -c init_text somevar && echo $somevar")
Disable terminal protocols before cancellable operations The [disambiguate flag](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#disambiguate) means that: > In particular, ctrl+c will no longer generate the SIGINT signal, > but instead be delivered as a CSI u escape code. so cancellation only works while we turn off disambiguation. Today we turn it off while running external commands that want to claim the TTY. Also we do it (only as a workaround for this issue) while expanding wildcards or while running builtin wait. However there are other cases where we don't have a workaround, like in trivial infinite loops or when opening a fifo. Before we run "while true; end", we put the terminal back in ICANON mode. This means it's line-buffered, so we won't be able to detect if the user pressed ctrl-c. Commit 8164855b7 (Disable terminal protocols throughout evaluation, 2024-04-02) had the right solution: simply disable terminal protocols whenever we do computations that might take a long time. eval_node() covers most of that; there are a few others. As pointed out in #10494, the logic was fairly unsophisticated then: it toggled terminal protocols many times. The fix in 29f2da8d1 (Toggle terminal protocols lazily, 2024-05-16) went to the extreme other end of only toggling protocols when absolutely necessary. Back out part of that commit by toggling in eval_node() again, fixing cancellation. Fortunately, we can keep most of the benefits of the lazy approach from 29f2da8d1: we toggle only 2 times instead of 8 times for an empty prompt. There are only two places left where we call signal_check_cancel() without necessarily disabling the disambiguate flag 1. open_cloexec() we assume that the files we open outside eval_node() are never blocking fifos. 2. fire_delayed(). Judging by commit history, this check is not relevant for interactive sessions; we'll soon end up calling eval_node() anyway. In future, we can leave bracketed paste, modifyOtherKeys and application keypad mode turned on again, until we actually run an external command. We really only want to turn off the disambiguate flag. Since this is approach is overly complex, I plan to go with either of these two alternatives in future: - extend the kitty keyboard protocol to optionally support VINTR, VSTOP and friends. Then we can drop most of these changes. - poll stdin for ctrl-c. This promises a great simplification, because it implies that terminal ownership (term_steal/term_donate) will be perfectly synced with us enabling kitty keyboard protocol. This is because polling requires us to turn off ICANON. I started working on this change; I'm convinced it must work, but it's not finished yet. Note that this will also want to add stdin polling to builtin wait. Closes #10864
2024-11-24 00:43:19 +08:00
expect_re(r"\r\n?.*read> init_text")
sendline("someval")
expect_prompt("someval\r\n")
sendline(r"read --command='some other text' somevar && echo $somevar")
Disable terminal protocols before cancellable operations The [disambiguate flag](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#disambiguate) means that: > In particular, ctrl+c will no longer generate the SIGINT signal, > but instead be delivered as a CSI u escape code. so cancellation only works while we turn off disambiguation. Today we turn it off while running external commands that want to claim the TTY. Also we do it (only as a workaround for this issue) while expanding wildcards or while running builtin wait. However there are other cases where we don't have a workaround, like in trivial infinite loops or when opening a fifo. Before we run "while true; end", we put the terminal back in ICANON mode. This means it's line-buffered, so we won't be able to detect if the user pressed ctrl-c. Commit 8164855b7 (Disable terminal protocols throughout evaluation, 2024-04-02) had the right solution: simply disable terminal protocols whenever we do computations that might take a long time. eval_node() covers most of that; there are a few others. As pointed out in #10494, the logic was fairly unsophisticated then: it toggled terminal protocols many times. The fix in 29f2da8d1 (Toggle terminal protocols lazily, 2024-05-16) went to the extreme other end of only toggling protocols when absolutely necessary. Back out part of that commit by toggling in eval_node() again, fixing cancellation. Fortunately, we can keep most of the benefits of the lazy approach from 29f2da8d1: we toggle only 2 times instead of 8 times for an empty prompt. There are only two places left where we call signal_check_cancel() without necessarily disabling the disambiguate flag 1. open_cloexec() we assume that the files we open outside eval_node() are never blocking fifos. 2. fire_delayed(). Judging by commit history, this check is not relevant for interactive sessions; we'll soon end up calling eval_node() anyway. In future, we can leave bracketed paste, modifyOtherKeys and application keypad mode turned on again, until we actually run an external command. We really only want to turn off the disambiguate flag. Since this is approach is overly complex, I plan to go with either of these two alternatives in future: - extend the kitty keyboard protocol to optionally support VINTR, VSTOP and friends. Then we can drop most of these changes. - poll stdin for ctrl-c. This promises a great simplification, because it implies that terminal ownership (term_steal/term_donate) will be perfectly synced with us enabling kitty keyboard protocol. This is because polling requires us to turn off ICANON. I started working on this change; I'm convinced it must work, but it's not finished yet. Note that this will also want to add stdin polling to builtin wait. Closes #10864
2024-11-24 00:43:19 +08:00
expect_re(r"\r\n?.*read> some other text")
sendline("another value")
expect_prompt("another value\r\n")
# read -s
sendline("read -s foo")
expect_read_prompt()
sendline("read_s\r_marker 3")
expect_prompt()
expect_marker(3)
print_var_contents("foo", "read_s")
# read -n
sendline("read -n 3 foo")
expect_read_prompt()
sendline("123_marker 3")
expect_prompt()
expect_marker(3)
print_var_contents("foo", "123")
sendline("read -n 3 foo")
expect_read_prompt()
sendline("456_marker 4")
expect_prompt()
expect_marker(4)
print_var_contents("foo", "456")
sendline("read -n 12 foo bar")
expect_read_prompt()
sendline("hello world!_marker 5")
expect_prompt()
expect_marker(5)
print_var_contents("foo", "hello")
print_var_contents("bar", "world!")
sendline("bind ` 'commandline -i test'`")
expect_prompt()
sendline("read -n 4 foo")
expect_read_prompt()
sendline("te`_marker 6")
expect_prompt()
expect_marker(6)
print_var_contents("foo", "tete")
sendline("read -n 4 foo")
expect_read_prompt()
sendline("12`_marker 7")
expect_prompt()
expect_marker(7)
print_var_contents("foo", "12te")
# Verify we don't hang on `read | cat`. See #4540.
sendline("read | cat")
expect_read_prompt()
sendline("bar\r_marker 4540")
expect_prompt()
expect_marker(4540)
# ==========
# The fix for issue #2007 initially introduced a problem when using a function
# to read from /dev/stdin when that is associated with the tty. These tests
# are to ensure we don't introduce a regression.
send("r2l\n")
expect_read_prompt()
send("abc\n")
expect_read_prompt()
send("def\n")
expect_str("abc then def\r\n")
expect_prompt()
# Some systems don't have /dev/stdin - effectively skip the test there.
# I'd love to warn about it, but I don't know how.
send("test -r /dev/stdin; and r2l </dev/stdin; or r2l\n")
expect_read_prompt()
send("ghi\n")
expect_read_prompt()
send("jkl\n")
expect_str("ghi then jkl\r\n")
expect_prompt()
# Long line so we don't have to count prompts
sendline(
"""set -g fish_prompt_fired 0; function dontfire --on-event fish_prompt; set -g fish_prompt_fired (math $fish_prompt_fired + 1); end; function dofire --on-event fish_read; set -g fish_read_fired 1; end"""
)
expect_prompt()
sendline("read foo")
expect_read_prompt()
sendline("text")
expect_prompt()
# Once for right after setting the listener, another for after the read.
print_var_contents("fish_prompt_fired", "2")
print_var_contents("fish_read_fired", "1")