fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
1f8cdf85b
master
1#!/usr/bin/env python3 2# This is a very fragile test. Sorry about that. But interactively entering 3# commands and verifying they are recorded correctly in the interactive 4# history and that history can be manipulated is inherently difficult. 5# 6# This is meant to verify just a few of the most basic behaviors of the 7# interactive history to hopefully keep regressions from happening. It is not 8# meant to be a comprehensive test of the history subsystem. Those types of 9# tests belong in the src/fish_tests.cpp module. 10# 11 12# The history function might pipe output through the user's pager. We don't 13# want something like `less` to complicate matters so force the use of `cat`. 14from pexpect_helper import SpawnedProc 15import os 16 17env = os .environ .copy () 18env ["PAGER" ]= "cat" 19 20sp = SpawnedProc (env = env ) 21 22send ,sendline ,sleep ,expect_prompt ,expect_re ,expect_str = ( 23sp .send , 24sp .sendline , 25sp .sleep , 26sp .expect_prompt , 27sp .expect_re , 28sp .expect_str , 29) 30expect_prompt () 31 32# ========== 33# Start by ensuring we're not affected by earlier tests. Clear the history. 34sendline ("builtin history clear" ) 35expect_prompt () 36 37# ========== 38# The following tests verify the behavior of the history builtin. 39# ========== 40 41# ========== 42# List our history which should be empty after just clearing it. 43sendline ("echo start1; builtin history; echo end1" ) 44expect_prompt ("start1\r\nend1\r\n" ) 45 46# Our history should now contain the previous command and nothing else. 47sendline ("echo start2; builtin history; echo end2" ) 48expect_prompt ("start2\r\necho start1; builtin history; echo end1\r\nend2\r\n" ) 49 50# ========== 51# The following tests verify the behavior of the history function. 52# ========== 53 54# ========== 55# Verify explicit searching for the first two commands in the previous tests 56# returns the expected results. 57sendline ("history search --reverse 'echo start' | cat" ) 58expect_prompt ("echo start1;.*\r\necho start2;" ) 59 60# ========== 61# Verify searching is the implicit action. 62sendline ("history -p 'echo start'" ) 63expect_prompt ("echo start2.*\r\necho start1" ) 64 65# ========== 66# Verify searching with a request for timestamps includes the timestamps. 67sendline ("history search --show-time='# %F %T%n' --prefix 'echo start'" ) 68expect_prompt ( 69"# \\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d\r\necho start2; .*\r\n# \\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d\r\necho start1;" 70) 71 72# ========== 73# Verify explicit searching for an exact command returns just that command. 74# returns the expected results. 75sendline ("echo hello" ) 76expect_prompt () 77sendline ("echo goodbye" ) 78expect_prompt () 79sendline ("echo hello again" ) 80expect_prompt () 81sendline ("echo hello AGAIN" ) 82expect_prompt () 83 84sendline ("history search --exact 'echo goodbye' | cat" ) 85expect_prompt ("echo goodbye\r\n" ) 86 87sendline ("history search --exact 'echo hello' | cat" ) 88expect_prompt ("echo hello\r\n" ) 89 90# This is slightly subtle in that it shouldn't actually match anything between 91# the command we sent and the next prompt. 92sendline ("history search --exact 'echo hell' | cat" ) 93expect_prompt ("history search --exact 'echo hell' | cat\r\n" ) 94 95# Verify that glob searching works. 96sendline ("history search --prefix 'echo start*echo end' | cat" ) 97expect_prompt ("echo start1; builtin history; echo end1\r\n" ) 98 99# ========== 100# Delete a single command we recently ran. 101sendline ("history delete -e -C 'echo hello'" ) 102expect_prompt () 103sendline ("echo count hello (history search -e -C 'echo hello' | count)" ) 104expect_prompt ("count hello 0\r\n" ) 105 106# ========== 107# Interactively delete one of multiple matched commands. This verifies that we 108# delete the first entry matched by the prefix search (the most recent command 109# sent above that matches). 110sendline ("history delete -p 'echo hello'" ) 111expect_re ("history delete -p 'echo hello'\r\n" ) 112expect_re (r"\[1\] echo hello AGAIN\r\n" ) 113expect_re (r"\[2\] echo hello again\r\n\r\n" ) 114expect_re ( 115"Enter nothing to cancel the delete, or\r\nEnter one or more of the entry IDs or ranges like '5..12', separated by a space.\r\nFor example '7 10..15 35 788..812'.\r\nEnter 'all' to delete all the matching entries.\r\n" 116) 117expect_re ("Delete which entries\? " ) 118sendline ("1" ) 119expect_prompt ('Deleting history entry 1: "echo hello AGAIN"\r\n' ) 120 121# Verify that the deleted history entry is gone and the other one that matched 122# the prefix search above is still there. 123sendline ("echo count AGAIN (history search -e -C 'echo hello AGAIN' | count)" ) 124expect_prompt ("count AGAIN 0\r\n" ) 125 126sendline ("echo count again (history search -e -C 'echo hello again' | count)" ) 127expect_prompt ("count again 1\r\n" ) 128 129# Verify that the $history var has the expected content. 130sendline ("echo history2=$history[2]" ) 131expect_prompt ("history2=echo count AGAIN .*\r\n" ) 132 133# Verify that history search is case-insensitive by default 134sendline ("echo term" ) 135expect_prompt ("term" ) 136sendline ("echo TERM" ) 137expect_prompt ("TERM" ) 138sendline ("echo banana" ) 139expect_prompt ("banana" ) 140send ("ter\x1b[A" )# up-arrow 141expect_re ("echo TERM" ) 142sendline ("" ) 143expect_prompt ("TERM" ) 144 145# Check that leading space makes an ephemeral item 146sendline (" echo ephemeral" ) 147expect_prompt ("ephemeral" ) 148send ("\x1b[A" )# up-arrow 149expect_re (" echo ephemeral" ) 150sendline ("" ) 151expect_prompt ("ephemeral" ) 152sendline (" " ) 153expect_prompt () 154send ("\x1b[A" ) 155expect_re ("echo TERM" )# not ephemeral! 156 157# Verify that ctrl-l preserves an active up-arrow history search 158sendline ("echo __ctrl_l_history_a" ) 159expect_prompt ("__ctrl_l_history_a" ) 160sendline ("echo __ctrl_l_history_b" ) 161expect_prompt ("__ctrl_l_history_b" ) 162send ("__ctrl_l_history" ) 163send ("\x1b[A" ) 164expect_re ("echo __ctrl_l_history_b" ) 165send ("\f" ) 166send ("\x1b[A" ) 167expect_re ("echo __ctrl_l_history_a" ) 168sendline ("" ) 169expect_prompt ("__ctrl_l_history_a" ) 170 171# Verify that clear-session works as expected 172# Note: This test depends on that history merge resets the session from history clear-sessions point of view. 173sendline ("builtin history clear" ) 174expect_prompt () 175 176# create before history 177sendline ("echo before1" ) 178expect_prompt () 179sendline ("echo before2" ) 180expect_prompt () 181# "reset" session with history merge 182sendline ("history merge" ) 183expect_prompt () 184# create after history 185sendline ("echo after" ) 186expect_prompt () 187# clear session 188sendline ("history clear-session" ) 189expect_prompt () 190sendline ("history search --exact 'echo after' | cat" ) 191expect_prompt ("\r\n" ) 192 193# Check history filtering 194# We store anything that starts with "echo ephemeral". 195sendline ( 196"function fish_should_add_to_history; string match -q 'echo ephemeral*' -- $argv; and return 2; return 0; end" 197) 198expect_prompt ("" ) 199# Check that matching the line works 200# (fish_should_add_to_history is itself stored in history so we match "ephemeral!" to avoid it) 201sendline ("echo ephemeral! line" ) 202expect_prompt ("ephemeral! line" ) 203sendline ("echo nonephemeral! line" ) 204expect_prompt ("nonephemeral! line" ) 205sendline ("true" ) 206expect_prompt () 207sendline ("echo a; history search '*ephemeral!*' | cat; echo b" ) 208expect_prompt (r"a\r\n.*echo nonephemeral! line\r\nb\r\n" ) 209 210# If fish_should_add_to_history exists, it will completely take over, 211# so even lines with spaces are stored 212sendline (" echo spaced" ) 213expect_prompt ("spaced" ) 214sendline ("true" ) 215expect_prompt () 216sendline ("echo a; history search '*spaced*' | cat; echo b" ) 217expect_prompt ("a\r\n.* echo spaced\r\nb\r\n" )