Port history test to pexpect

This commit is contained in:
Fabian Homborg 2020-06-13 19:24:36 +02:00
parent 5478d979a0
commit c6dffa226f
4 changed files with 127 additions and 180 deletions

View File

@ -1,167 +0,0 @@
# vim: set filetype=expect:
#
# This is a very fragile test. Sorry about that. But interactively entering
# commands and verifying they are recorded correctly in the interactive
# history and that history can be manipulated is inherently difficult.
#
# This is meant to verify just a few of the most basic behaviors of the
# interactive history to hopefully keep regressions from happening. It is not
# meant to be a comprehensive test of the history subsystem. Those types of
# tests belong in the src/fish_tests.cpp module.
#
# The history function might pipe output through the user's pager. We don't
# want something like `less` to complicate matters so force the use of `cat`.
set ::env(PAGER) cat
spawn $fish
expect_prompt
# ==========
# Start by ensuring we're not affected by earlier tests. Clear the history.
send "builtin history clear\r"
expect_prompt
# ==========
# The following tests verify the behavior of the history builtin.
# ==========
# ==========
# List our history which should be empty after just clearing it.
send "echo start1; builtin history; echo end1\r"
expect_prompt -re {start1\r\nend1\r\n} {
puts "empty history detected as expected"
} timeout {
puts stderr "empty history not detected as expected"
}
# ==========
# Our history should now contain the previous command and nothing else.
send "echo start2; builtin history; echo end2\r"
expect_prompt -re {start2\r\necho start1; builtin history; echo end1\r\nend2\r\n} {
puts "first history command detected as expected"
} timeout {
puts stderr "first history command not detected as expected"
}
# ==========
# The following tests verify the behavior of the history function.
# ==========
# ==========
# Verify explicit searching for the first two commands in the previous tests
# returns the expected results.
send "history search --reverse 'echo start' | cat\r"
expect_prompt -re {\r\necho start1;.*\r\necho start2;} {
puts "history function explicit search succeeded"
} timeout {
puts stderr "history function explicit search failed"
}
# ==========
# Verify searching is the implicit action.
send "history -p 'echo start'\r"
expect_prompt -re {\r\necho start2.*\r\necho start1} {
puts "history function implicit search succeeded"
} timeout {
puts stderr "history function implicit search failed"
}
# ==========
# Verify searching with a request for timestamps includes the timestamps.
send "history search --show-time='# %F %T%n' --prefix 'echo start'\r"
expect_prompt -re {\r\n# \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; } {
puts "history function implicit search with timestamps succeeded"
} timeout {
puts stderr "history function implicit search with timestamps failed"
}
# ==========
# Verify explicit searching for an exact command returns just that command.
# returns the expected results.
send "echo hello\r"
expect_prompt
send "echo goodbye\r"
expect_prompt
send "echo hello again\r"
expect_prompt
send "echo hello AGAIN\r"
expect_prompt
send "history search --exact 'echo goodbye' | cat\r"
expect_prompt -re {\r\necho goodbye\r\n} {
puts "history function explicit exact search 'echo goodbye' succeeded"
} timeout {
puts stderr "history function explicit exact search 'echo goodbye' failed"
}
send "history search --exact 'echo hello' | cat\r"
expect_prompt -re {\r\necho hello\r\n} {
puts "history function explicit exact search 'echo hello' succeeded"
} timeout {
puts stderr "history function explicit exact search 'echo hello' failed"
}
# This is slightly subtle in that it shouldn't actually match anything between
# the command we sent and the next prompt.
send "history search --exact 'echo hell' | cat\r"
expect_prompt -re {history search --exact 'echo hell' | cat\r\n} {
puts "history function explicit exact search 'echo hell' succeeded"
} timeout {
puts stderr "history function explicit exact search 'echo hell' failed"
}
# Verify that glob searching works.
send "history search --prefix 'echo start*echo end' | cat\r"
expect_prompt -re {echo start1; builtin history; echo end1\r\n} {
puts "history function explicit glob search 'echo start*echo end' succeeded"
} timeout {
puts stderr "history function explicit glob search 'echo start*echo end' failed"
}
# ==========
# Delete a single command we recently ran.
send "history delete -e -C 'echo hello'\r"
expect -re {history delete -e -C 'echo hello'\r\n}
send "echo count hello (history search -e -C 'echo hello' | wc -l | string trim)\r"
expect -re {\r\ncount hello 0\r\n} {
puts "history function explicit exact delete 'echo hello' succeeded"
} timeout {
puts stderr "history function explicit exact delete 'echo hello' failed"
}
# ==========
# Interactively delete one of multiple matched commands. This verifies that we
# delete the first entry matched by the prefix search (the most recent command
# sent above that matches).
send "history delete -p 'echo hello'\r"
expect -re {history delete -p 'echo hello'\r\n}
expect -re {\[1\] echo hello AGAIN\r\n}
expect -re {\[2\] echo hello again\r\n\r\n}
expect -re {Enter nothing to cancel.*\r\nEnter "all" to delete all the matching entries\.\r\n}
expect -re {Delete which entries\? >}
send "1\r"
expect -re {Deleting history entry 1: "echo hello AGAIN"\r\n}
# Verify that the deleted history entry is gone and the other one that matched
# the prefix search above is still there.
send "echo count AGAIN (history search -e -C 'echo hello AGAIN' | wc -l | string trim)\r"
expect -re {\r\ncount AGAIN 0\r\n} {
puts "history function explicit prefix delete 'echo hello AGAIN' succeeded"
} timeout {
puts stderr "history function explicit prefix delete 'echo hello AGAIN' failed"
}
send "echo count again (history search -e -C 'echo hello again' | wc -l | string trim)\r"
expect -re {\r\ncount again 1\r\n} {
puts "history function explicit exact search 'echo hello again' succeeded"
} timeout {
puts stderr "history function explicit exact search 'echo hello again' failed"
}
# Verify that the $history var has the expected content.
send "echo history2=\$history\[2\]\r"
expect -re {\r\nhistory2=echo count AGAIN .*\r\n} {
puts "history\[2\] had the correct data"
} timeout {
puts stderr "history\[2\] had the wrong data"
}

View File

@ -1,13 +0,0 @@
empty history detected as expected
first history command detected as expected
history function explicit search succeeded
history function implicit search succeeded
history function implicit search with timestamps succeeded
history function explicit exact search 'echo goodbye' succeeded
history function explicit exact search 'echo hello' succeeded
history function explicit exact search 'echo hell' succeeded
history function explicit glob search 'echo start*echo end' succeeded
history function explicit exact delete 'echo hello' succeeded
history function explicit prefix delete 'echo hello AGAIN' succeeded
history function explicit exact search 'echo hello again' succeeded
history[2] had the correct data

127
tests/pexpects/history.py Normal file
View File

@ -0,0 +1,127 @@
#!/usr/bin/env python3
# This is a very fragile test. Sorry about that. But interactively entering
# commands and verifying they are recorded correctly in the interactive
# history and that history can be manipulated is inherently difficult.
#
# This is meant to verify just a few of the most basic behaviors of the
# interactive history to hopefully keep regressions from happening. It is not
# meant to be a comprehensive test of the history subsystem. Those types of
# tests belong in the src/fish_tests.cpp module.
#
# The history function might pipe output through the user's pager. We don't
# want something like `less` to complicate matters so force the use of `cat`.
from pexpect_helper import SpawnedProc
import os
os.environ["PAGER"] = "cat"
os.environ["TERM"] = "xterm"
sp = SpawnedProc(env=os.environ.copy())
send, sendline, sleep, expect_prompt, expect_re, expect_str = (
sp.send,
sp.sendline,
sp.sleep,
sp.expect_prompt,
sp.expect_re,
sp.expect_str,
)
expect_prompt()
# ==========
# Start by ensuring we're not affected by earlier tests. Clear the history.
sendline("builtin history clear")
expect_prompt()
# ==========
# The following tests verify the behavior of the history builtin.
# ==========
# ==========
# List our history which should be empty after just clearing it.
sendline("echo start1; builtin history; echo end1")
expect_prompt("start1\r\nend1\r\n")
# Our history should now contain the previous command and nothing else.
sendline("echo start2; builtin history; echo end2")
expect_prompt("start2\r\necho start1; builtin history; echo end1\r\nend2\r\n")
# ==========
# The following tests verify the behavior of the history function.
# ==========
# ==========
# Verify explicit searching for the first two commands in the previous tests
# returns the expected results.
sendline("history search --reverse 'echo start' | cat")
expect_prompt("echo start1;.*\r\necho start2;")
# ==========
# Verify searching is the implicit action.
sendline("history -p 'echo start'")
expect_prompt("echo start2.*\r\necho start1")
# ==========
# Verify searching with a request for timestamps includes the timestamps.
sendline("history search --show-time='# %F %T%n' --prefix 'echo start'")
expect_prompt("# \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;")
# ==========
# Verify explicit searching for an exact command returns just that command.
# returns the expected results.
sendline("echo hello")
expect_prompt()
sendline("echo goodbye")
expect_prompt()
sendline("echo hello again")
expect_prompt()
sendline("echo hello AGAIN")
expect_prompt()
sendline("history search --exact 'echo goodbye' | cat")
expect_prompt("echo goodbye\r\n")
sendline("history search --exact 'echo hello' | cat")
expect_prompt("echo hello\r\n")
# This is slightly subtle in that it shouldn't actually match anything between
# the command we sent and the next prompt.
sendline("history search --exact 'echo hell' | cat")
expect_prompt("history search --exact 'echo hell' | cat\r\n")
# Verify that glob searching works.
sendline("history search --prefix 'echo start*echo end' | cat")
expect_prompt("echo start1; builtin history; echo end1\r\n")
# ==========
# Delete a single command we recently ran.
sendline("history delete -e -C 'echo hello'")
expect_str("history delete -e -C 'echo hello'\r\n")
sendline("echo count hello (history search -e -C 'echo hello' | wc -l | string trim)")
expect_re("count hello 0\r\n")
# ==========
# Interactively delete one of multiple matched commands. This verifies that we
# delete the first entry matched by the prefix search (the most recent command
# sent above that matches).
sendline("history delete -p 'echo hello'")
expect_re("history delete -p 'echo hello'\r\n")
expect_re("\[1\] echo hello AGAIN\r\n")
expect_re("\[2\] echo hello again\r\n\r\n")
expect_re('Enter nothing to cancel.*\r\nEnter "all" to delete all the matching entries\.\r\n')
expect_re("Delete which entries\? >")
sendline("1")
expect_re('Deleting history entry 1: "echo hello AGAIN"\r\n')
# Verify that the deleted history entry is gone and the other one that matched
# the prefix search above is still there.
sendline("echo count AGAIN (history search -e -C 'echo hello AGAIN' | wc -l | string trim)")
expect_re("count AGAIN 0\r\n")
sendline("echo count again (history search -e -C 'echo hello again' | wc -l | string trim)")
expect_re("count again 1\r\n")
# Verify that the $history var has the expected content.
sendline("echo history2=$history\[2\]")
expect_re("history2=echo count AGAIN .*\r\n")