Properly skip recording of commands prefixed with a space to match bash's history behavior

This commit is contained in:
David Dworken
2022-04-15 00:04:49 -07:00
parent 2fdfbb9d20
commit b09b725f49
5 changed files with 78 additions and 6 deletions

View File

@ -19,6 +19,8 @@ import (
"github.com/ddworken/hishtory/shared"
)
// TODO: Change this to only start the server once for this entire file
func RunInteractiveBashCommands(t *testing.T, script string) string {
out, err := RunInteractiveBashCommandsWithoutStrictMode(t, "set -emo pipefail\n"+script)
if err != nil {
@ -537,6 +539,41 @@ hishtory enable`, i))
}
}
func TestExcludeHiddenCommand(t *testing.T) {
// Set up
defer shared.BackupAndRestore(t)()
defer shared.RunTestServer(t)()
installHishtory(t, "")
RunInteractiveBashCommands(t, `echo hello1
echo hidden
echo hello2
echo hidden`)
RunInteractiveBashCommands(t, " echo hidden")
out := hishtoryQuery(t, "")
if strings.Count(out, "\n") != 6 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
if strings.Count(out, "echo hello") != 2 {
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
}
if strings.Count(out, "echo hello1") != 1 {
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
}
if strings.Count(out, "echo hello2") != 1 {
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
}
if strings.Contains(out, "hidden") {
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
}
out = RunInteractiveBashCommands(t, "hishtory export")
expectedOutput := "set -emo pipefail\necho hello1\necho hello2\nset -emo pipefail\nset -emo pipefail\nhishtory query\nset -emo pipefail\n"
if out != expectedOutput {
t.Fatalf("hishtory export has unexpected output=%#v", out)
}
}
func waitForBackgroundSavesToComplete(t *testing.T) {
for i := 0; i < 20; i++ {
cmd := exec.Command("pidof", "hishtory")