|
|
@ -20,6 +20,8 @@ import (
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Go through all the set -m that are sprinkled around and clean those up
|
|
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
defer shared.RunTestServer()()
|
|
|
|
defer shared.RunTestServer()()
|
|
|
|
cmd := exec.Command("go", "build", "-o", "/tmp/client")
|
|
|
|
cmd := exec.Command("go", "build", "-o", "/tmp/client")
|
|
|
@ -30,8 +32,17 @@ func TestMain(m *testing.M) {
|
|
|
|
m.Run()
|
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RunInteractiveBashCommands(t *testing.T, script string) string {
|
|
|
|
type shellTester interface {
|
|
|
|
out, err := RunInteractiveBashCommandsWithoutStrictMode(t, "set -emo pipefail\n"+script)
|
|
|
|
RunInteractiveShell(t *testing.T, script string) string
|
|
|
|
|
|
|
|
RunInteractiveShellRelaxed(t *testing.T, script string) (string, error)
|
|
|
|
|
|
|
|
ShellName() string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type bashTester struct {
|
|
|
|
|
|
|
|
shellTester
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (b bashTester) RunInteractiveShell(t *testing.T, script string) string {
|
|
|
|
|
|
|
|
out, err := b.RunInteractiveShellRelaxed(t, "set -emo pipefail\n"+script)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("error when running command at %s:%d: %v", filename, line, err)
|
|
|
|
t.Fatalf("error when running command at %s:%d: %v", filename, line, err)
|
|
|
@ -39,7 +50,7 @@ func RunInteractiveBashCommands(t *testing.T, script string) string {
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RunInteractiveBashCommandsWithoutStrictMode(t *testing.T, script string) (string, error) {
|
|
|
|
func (b bashTester) RunInteractiveShellRelaxed(t *testing.T, script string) (string, error) {
|
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stdout bytes.Buffer
|
|
|
@ -57,29 +68,85 @@ func RunInteractiveBashCommandsWithoutStrictMode(t *testing.T, script string) (s
|
|
|
|
return outStr, nil
|
|
|
|
return outStr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
func (b bashTester) ShellName() string {
|
|
|
|
// Set up
|
|
|
|
return "bash"
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run the test
|
|
|
|
|
|
|
|
testIntegration(t)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
type zshTester struct {
|
|
|
|
|
|
|
|
shellTester
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (z zshTester) RunInteractiveShell(t *testing.T, script string) string {
|
|
|
|
|
|
|
|
// TODO: make this strict
|
|
|
|
|
|
|
|
res, err := z.RunInteractiveShellRelaxed(t, script)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (z zshTester) RunInteractiveShellRelaxed(t *testing.T, script string) (string, error) {
|
|
|
|
|
|
|
|
cmd := exec.Command("zsh", "-is")
|
|
|
|
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", fmt.Errorf("unexpected error when running commands, out=%#v, err=%#v: %v", stdout.String(), stderr.String(), err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
outStr := stdout.String()
|
|
|
|
|
|
|
|
if strings.Contains(outStr, "hishtory fatal error") {
|
|
|
|
|
|
|
|
t.Fatalf("Ran command, but hishtory had a fatal error! out=%#v", outStr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return outStr, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (z zshTester) ShellName() string {
|
|
|
|
|
|
|
|
return "zsh"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var shellTesters []shellTester = []shellTester{bashTester{}, zshTester{}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestParameterized(t *testing.T) {
|
|
|
|
|
|
|
|
for _, tester := range shellTesters {
|
|
|
|
|
|
|
|
t.Run("testRepeatedCommandThenQuery/"+tester.ShellName(), func(t *testing.T) { testRepeatedCommandThenQuery(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testRepeatedCommandAndQuery/"+tester.ShellName(), func(t *testing.T) { testRepeatedCommandAndQuery(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testRepeatedEnableDisable/"+tester.ShellName(), func(t *testing.T) { testRepeatedEnableDisable(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testExcludeHiddenCommand/"+tester.ShellName(), func(t *testing.T) { testExcludeHiddenCommand(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testUpdate/"+tester.ShellName(), func(t *testing.T) { testUpdate(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testAdvancedQuery/"+tester.ShellName(), func(t *testing.T) { testAdvancedQuery(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testIntegration/"+tester.ShellName(), func(t *testing.T) { testIntegration(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testIntegrationWithNewDevice/"+tester.ShellName(), func(t *testing.T) { testIntegrationWithNewDevice(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testHishtoryBackgroundSaving/"+tester.ShellName(), func(t *testing.T) { testHishtoryBackgroundSaving(t, tester) })
|
|
|
|
|
|
|
|
t.Run("testDisplayTable/"+tester.ShellName(), func(t *testing.T) { testDisplayTable(t, tester) })
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testIntegration(t *testing.T, tester shellTester) {
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
|
|
|
|
// Run the test
|
|
|
|
// Run the test
|
|
|
|
userSecret := testIntegration(t)
|
|
|
|
testBasicUserFlow(t, tester)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testIntegrationWithNewDevice(t *testing.T, tester shellTester) {
|
|
|
|
|
|
|
|
// Set up
|
|
|
|
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run the test
|
|
|
|
|
|
|
|
userSecret := testBasicUserFlow(t, tester)
|
|
|
|
|
|
|
|
|
|
|
|
// Clear all local state
|
|
|
|
// Clear all local state
|
|
|
|
shared.ResetLocalState(t)
|
|
|
|
shared.ResetLocalState(t)
|
|
|
|
|
|
|
|
|
|
|
|
// Install it again
|
|
|
|
// Install it again
|
|
|
|
installHishtory(t, userSecret)
|
|
|
|
installHishtory(t, tester, userSecret)
|
|
|
|
|
|
|
|
|
|
|
|
// Querying should show the history from the previous run
|
|
|
|
// Querying should show the history from the previous run
|
|
|
|
out := hishtoryQuery(t, "")
|
|
|
|
out := hishtoryQuery(t, tester, "")
|
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
@ -90,8 +157,8 @@ func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, "echo mynewcommand")
|
|
|
|
tester.RunInteractiveShell(t, "echo mynewcommand")
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "echo mynewcommand") {
|
|
|
|
if !strings.Contains(out, "echo mynewcommand") {
|
|
|
|
t.Fatalf("output is missing `echo mynewcommand`")
|
|
|
|
t.Fatalf("output is missing `echo mynewcommand`")
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -103,23 +170,23 @@ func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
shared.ResetLocalState(t)
|
|
|
|
shared.ResetLocalState(t)
|
|
|
|
|
|
|
|
|
|
|
|
// Install it a 3rd time
|
|
|
|
// Install it a 3rd time
|
|
|
|
installHishtory(t, "adifferentsecret")
|
|
|
|
installHishtory(t, tester, "adifferentsecret")
|
|
|
|
|
|
|
|
|
|
|
|
// Run a command that shouldn't be in the hishtory later on
|
|
|
|
// Run a command that shouldn't be in the hishtory later on
|
|
|
|
RunInteractiveBashCommands(t, `echo notinthehistory`)
|
|
|
|
tester.RunInteractiveShell(t, `echo notinthehistory`)
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "echo notinthehistory") {
|
|
|
|
if !strings.Contains(out, "echo notinthehistory") {
|
|
|
|
t.Fatalf("output is missing `echo notinthehistory`")
|
|
|
|
t.Fatalf("output is missing `echo notinthehistory`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the secret key to the previous secret key
|
|
|
|
// Set the secret key to the previous secret key
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory init `+userSecret)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory init `+userSecret)
|
|
|
|
if !strings.Contains(out, "Setting secret hishtory key to "+userSecret) {
|
|
|
|
if !strings.Contains(out, "Setting secret hishtory key to "+userSecret) {
|
|
|
|
t.Fatalf("Failed to re-init with the user secret: %v", out)
|
|
|
|
t.Fatalf("Failed to re-init with the user secret: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Querying should show the history from the previous run
|
|
|
|
// Querying should show the history from the previous run
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
expected = []string{"echo thisisrecorded", "echo mynewcommand", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
expected = []string{"echo thisisrecorded", "echo mynewcommand", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
@ -134,8 +201,8 @@ func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
t.Fatalf("output contains the unexpected item: notinthehistory")
|
|
|
|
t.Fatalf("output contains the unexpected item: notinthehistory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, "echo mynewercommand")
|
|
|
|
tester.RunInteractiveShell(t, "echo mynewercommand")
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "echo mynewercommand") {
|
|
|
|
if !strings.Contains(out, "echo mynewercommand") {
|
|
|
|
t.Fatalf("output is missing `echo mynewercommand`")
|
|
|
|
t.Fatalf("output is missing `echo mynewercommand`")
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -149,25 +216,25 @@ func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, newEntry)
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, newEntry)
|
|
|
|
|
|
|
|
|
|
|
|
// Now check if that is in there when we do hishtory query
|
|
|
|
// Now check if that is in there when we do hishtory query
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "othercomputer") {
|
|
|
|
if !strings.Contains(out, "othercomputer") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain cmd run on another machine! out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain cmd run on another machine! out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finally, test the export command
|
|
|
|
// Finally, test the export command
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory export`)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail | grep -v '/tmp/client install'`)
|
|
|
|
if strings.Contains(out, "thisisnotrecorded") {
|
|
|
|
if strings.Contains(out, "thisisnotrecorded") {
|
|
|
|
t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expectedOutputWithoutKey := "set -emo pipefail\nhishtory status\nset -emo pipefail\nhishtory query\nset -m\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nset -emo pipefail\nhishtory query\nset -emo pipefail\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nset -emo pipefail\nhishtory query complex\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mynewcommand\nset -emo pipefail\nhishtory query\nhishtory init %s\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mynewercommand\nset -emo pipefail\nhishtory query\nothercomputer\nset -emo pipefail\nhishtory query\nset -emo pipefail\n"
|
|
|
|
expectedOutputWithoutKey := "hishtory status\nhishtory query\nset -m\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nhishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\n"
|
|
|
|
expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret)
|
|
|
|
expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret)
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func installHishtory(t *testing.T, userSecret string) string {
|
|
|
|
func installHishtory(t *testing.T, tester shellTester, userSecret string) string {
|
|
|
|
out := RunInteractiveBashCommands(t, `/tmp/client install `+userSecret)
|
|
|
|
out := tester.RunInteractiveShell(t, `/tmp/client install `+userSecret)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
matches := r.FindStringSubmatch(out)
|
|
|
|
matches := r.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
if len(matches) != 2 {
|
|
|
@ -176,12 +243,12 @@ func installHishtory(t *testing.T, userSecret string) string {
|
|
|
|
return matches[1]
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func testIntegration(t *testing.T) string {
|
|
|
|
func testBasicUserFlow(t *testing.T, tester shellTester) string {
|
|
|
|
// Test install
|
|
|
|
// Test install
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Test the status subcommand
|
|
|
|
// Test the status subcommand
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out := tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -201,14 +268,14 @@ func testIntegration(t *testing.T) string {
|
|
|
|
|
|
|
|
|
|
|
|
// Test the banner
|
|
|
|
// Test the banner
|
|
|
|
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
|
|
|
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
|
|
|
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
|
|
|
t.Fatalf("hishtory query didn't show the banner message! out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query didn't show the banner message! out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Setenv("FORCED_BANNER", "")
|
|
|
|
os.Setenv("FORCED_BANNER", "")
|
|
|
|
|
|
|
|
|
|
|
|
// Test recording commands
|
|
|
|
// Test recording commands
|
|
|
|
out, err = RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
|
|
|
out, err = tester.RunInteractiveShellRelaxed(t, `set -m
|
|
|
|
ls /a
|
|
|
|
ls /a
|
|
|
|
ls /bar
|
|
|
|
ls /bar
|
|
|
|
ls /foo
|
|
|
|
ls /foo
|
|
|
@ -227,7 +294,7 @@ echo thisisrecorded`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test querying for all commands
|
|
|
|
// Test querying for all commands
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
@ -241,7 +308,7 @@ echo thisisrecorded`)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// Test querying for a specific command
|
|
|
|
// Test querying for a specific command
|
|
|
|
out = hishtoryQuery(t, "foo")
|
|
|
|
out = hishtoryQuery(t, tester, "foo")
|
|
|
|
expected = []string{"echo foo", "ls /foo"}
|
|
|
|
expected = []string{"echo foo", "ls /foo"}
|
|
|
|
unexpected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "ls /bar", "ls /a"}
|
|
|
|
unexpected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
for _, item := range expected {
|
|
|
@ -260,10 +327,12 @@ echo thisisrecorded`)
|
|
|
|
|
|
|
|
|
|
|
|
// Add a complex command
|
|
|
|
// Add a complex command
|
|
|
|
complexCommand := "echo hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\""
|
|
|
|
complexCommand := "echo hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\""
|
|
|
|
_, _ = RunInteractiveBashCommandsWithoutStrictMode(t, complexCommand)
|
|
|
|
_, _ = tester.RunInteractiveShellRelaxed(t, complexCommand)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: add a test here that includes a double quote
|
|
|
|
|
|
|
|
|
|
|
|
// Query for it
|
|
|
|
// Query for it
|
|
|
|
out = hishtoryQuery(t, "complex")
|
|
|
|
out = hishtoryQuery(t, tester, "complex")
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -274,15 +343,15 @@ echo thisisrecorded`)
|
|
|
|
return userSecret
|
|
|
|
return userSecret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestAdvancedQuery(t *testing.T) {
|
|
|
|
func testAdvancedQuery(t *testing.T, tester shellTester) {
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
|
|
|
|
// Install hishtory
|
|
|
|
// Install hishtory
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Run some commands we can query for
|
|
|
|
// Run some commands we can query for
|
|
|
|
_, err := RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
|
|
|
_, err := tester.RunInteractiveShellRelaxed(t, `set -m
|
|
|
|
echo nevershouldappear
|
|
|
|
echo nevershouldappear
|
|
|
|
notacommand
|
|
|
|
notacommand
|
|
|
|
cd /tmp/
|
|
|
|
cd /tmp/
|
|
|
@ -293,7 +362,7 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A super basic query just to ensure the basics are working
|
|
|
|
// A super basic query just to ensure the basics are working
|
|
|
|
out := hishtoryQuery(t, `echo`)
|
|
|
|
out := hishtoryQuery(t, tester, `echo`)
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching echo querybydir, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching echo querybydir, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -302,7 +371,7 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on cwd
|
|
|
|
// Query based on cwd
|
|
|
|
out = hishtoryQuery(t, `cwd:/tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `cwd:/tmp`)
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -314,7 +383,7 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on cwd without the slash
|
|
|
|
// Query based on cwd without the slash
|
|
|
|
out = hishtoryQuery(t, `cwd:tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `cwd:tmp`)
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:tmp, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -323,7 +392,7 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on cwd and another term
|
|
|
|
// Query based on cwd and another term
|
|
|
|
out = hishtoryQuery(t, `cwd:/tmp querybydir`)
|
|
|
|
out = hishtoryQuery(t, tester, `cwd:/tmp querybydir`)
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -335,7 +404,7 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on exit_code
|
|
|
|
// Query based on exit_code
|
|
|
|
out = hishtoryQuery(t, `exit_code:127`)
|
|
|
|
out = hishtoryQuery(t, tester, `exit_code:127`)
|
|
|
|
if !strings.Contains(out, "notacommand") {
|
|
|
|
if !strings.Contains(out, "notacommand") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -344,21 +413,21 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on exit_code and something else that matches nothing
|
|
|
|
// Query based on exit_code and something else that matches nothing
|
|
|
|
out = hishtoryQuery(t, `exit_code:127 foo`)
|
|
|
|
out = hishtoryQuery(t, tester, `exit_code:127 foo`)
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on before: and cwd:
|
|
|
|
// Query based on before: and cwd:
|
|
|
|
out = hishtoryQuery(t, `before:2125-07-02 cwd:/tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `before:2125-07-02 cwd:/tmp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `before:2125-07-02 cwd:tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `before:2125-07-02 cwd:tmp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `before:2125-07-02 cwd:mp`)
|
|
|
|
out = hishtoryQuery(t, tester, `before:2125-07-02 cwd:mp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -366,14 +435,14 @@ hishtory disable`)
|
|
|
|
// Query based on after: and cwd:
|
|
|
|
// Query based on after: and cwd:
|
|
|
|
// TODO: This fails on macos for some reason
|
|
|
|
// TODO: This fails on macos for some reason
|
|
|
|
if !(runtime.GOOS == "darwin" && os.Getenv("GITHUB_ACTIONS") != "") {
|
|
|
|
if !(runtime.GOOS == "darwin" && os.Getenv("GITHUB_ACTIONS") != "") {
|
|
|
|
out = hishtoryQuery(t, `after:1980-07-02 cwd:/tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `after:1980-07-02 cwd:/tmp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on after: that returns no results
|
|
|
|
// Query based on after: that returns no results
|
|
|
|
out = hishtoryQuery(t, `after:2120-07-02 cwd:/tmp`)
|
|
|
|
out = hishtoryQuery(t, tester, `after:2120-07-02 cwd:/tmp`)
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -385,7 +454,7 @@ hishtory disable`)
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on the username that exists
|
|
|
|
// Query based on the username that exists
|
|
|
|
out = hishtoryQuery(t, `user:otheruser`)
|
|
|
|
out = hishtoryQuery(t, tester, `user:otheruser`)
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -394,13 +463,13 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on the username that doesn't exist
|
|
|
|
// Query based on the username that doesn't exist
|
|
|
|
out = hishtoryQuery(t, `user:noexist`)
|
|
|
|
out = hishtoryQuery(t, tester, `user:noexist`)
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Query based on the hostname
|
|
|
|
// Query based on the hostname
|
|
|
|
out = hishtoryQuery(t, `hostname:otherhostname`)
|
|
|
|
out = hishtoryQuery(t, tester, `hostname:otherhostname`)
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -409,32 +478,35 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test filtering out a search item
|
|
|
|
// Test filtering out a search item
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `-cmd_with_diff_hostname_and_username`)
|
|
|
|
out = hishtoryQuery(t, tester, `-cmd_with_diff_hostname_and_username`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `-echo`)
|
|
|
|
out = hishtoryQuery(t, tester, `-echo`)
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tester.ShellName() == "bash" {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
if strings.Count(out, "\n") != 5 {
|
|
|
|
if strings.Count(out, "\n") != 5 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test filtering out with an atom
|
|
|
|
// Test filtering out with an atom
|
|
|
|
out = hishtoryQuery(t, `-hostname:otherhostname`)
|
|
|
|
out = hishtoryQuery(t, tester, `-hostname:otherhostname`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `-user:otheruser`)
|
|
|
|
out = hishtoryQuery(t, tester, `-user:otheruser`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, `-exit_code:0`)
|
|
|
|
out = hishtoryQuery(t, tester, `-exit_code:0`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -442,7 +514,7 @@ hishtory disable`)
|
|
|
|
// Test filtering out a search item that also looks like it could be a search for a flag
|
|
|
|
// Test filtering out a search item that also looks like it could be a search for a flag
|
|
|
|
entry = data.MakeFakeHistoryEntry("foo -echo")
|
|
|
|
entry = data.MakeFakeHistoryEntry("foo -echo")
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
|
|
|
out = hishtoryQuery(t, `-echo -install`)
|
|
|
|
out = hishtoryQuery(t, tester, `-echo -install`)
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -451,27 +523,28 @@ hishtory disable`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
func testUpdate(t *testing.T, tester shellTester) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
// TODO: Once updates work for non-linux platforms, remove this
|
|
|
|
// TODO: Once updates work for non-linux platforms, remove this
|
|
|
|
t.Skip()
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Skip() // TODO: currently failing because of the change in API
|
|
|
|
|
|
|
|
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Record a command before the update
|
|
|
|
// Record a command before the update
|
|
|
|
RunInteractiveBashCommands(t, "echo hello")
|
|
|
|
tester.RunInteractiveShell(t, "echo hello")
|
|
|
|
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out := tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update
|
|
|
|
// Update
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory update`)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory update`)
|
|
|
|
isExpected, err := regexp.MatchString(`Verified against tlog entry \d+\nSuccessfully updated hishtory from v0[.]Unknown to v0.\d+\n`, out)
|
|
|
|
isExpected, err := regexp.MatchString(`Verified against tlog entry \d+\nSuccessfully updated hishtory from v0[.]Unknown to v0.\d+\n`, out)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("regex failure: %v", err)
|
|
|
|
t.Fatalf("regex failure: %v", err)
|
|
|
@ -481,13 +554,13 @@ func TestUpdate(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update again and assert that it skipped the update
|
|
|
|
// Update again and assert that it skipped the update
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory update`)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory update`)
|
|
|
|
if strings.Count(out, "\n") != 1 || !strings.Contains(out, "is already installed") {
|
|
|
|
if strings.Count(out, "\n") != 1 || !strings.Contains(out, "is already installed") {
|
|
|
|
t.Fatalf("repeated hishtory update didn't skip the update, out=%#v", out)
|
|
|
|
t.Fatalf("repeated hishtory update didn't skip the update, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then check the status command again to confirm the update worked
|
|
|
|
// Then check the status command again to confirm the update worked
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if !strings.Contains(out, fmt.Sprintf("\nEnabled: true\nSecret Key: %s\nCommit Hash: ", userSecret)) {
|
|
|
|
if !strings.Contains(out, fmt.Sprintf("\nEnabled: true\nSecret Key: %s\nCommit Hash: ", userSecret)) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -496,31 +569,34 @@ func TestUpdate(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check that the history was preserved after the update
|
|
|
|
// Check that the history was preserved after the update
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory export")
|
|
|
|
if tester.ShellName() == "bash" {
|
|
|
|
expectedOutput := "set -emo pipefail\necho hello\nset -emo pipefail\nhishtory status\nset -emo pipefail\nhishtory update\nset -emo pipefail\nhishtory update\nset -emo pipefail\nhishtory status\nset -emo pipefail\n"
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail | grep -v '/tmp/client install'")
|
|
|
|
|
|
|
|
expectedOutput := "echo hello\nhishtory status\n"
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRepeatedCommandThenQuery(t *testing.T) {
|
|
|
|
func testRepeatedCommandThenQuery(t *testing.T, tester shellTester) {
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out := tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
tester.RunInteractiveShell(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check that it shows up correctly
|
|
|
|
// Check that it shows up correctly
|
|
|
|
out = hishtoryQuery(t, `mycommand`)
|
|
|
|
out = hishtoryQuery(t, tester, `mycommand`)
|
|
|
|
if strings.Count(out, "\n") != 26 {
|
|
|
|
if strings.Count(out, "\n") != 26 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -528,32 +604,33 @@ func TestRepeatedCommandThenQuery(t *testing.T) {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, `echo mycommand-30
|
|
|
|
// Run a few more commands
|
|
|
|
|
|
|
|
tester.RunInteractiveShell(t, `echo mycommand-30
|
|
|
|
echo mycommand-31
|
|
|
|
echo mycommand-31
|
|
|
|
echo mycommand-3`)
|
|
|
|
echo mycommand-3`)
|
|
|
|
|
|
|
|
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory export")
|
|
|
|
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail | grep -v '/tmp/client install'")
|
|
|
|
expectedOutput := "set -emo pipefail\nhishtory status\nset -emo pipefail\necho mycommand-0\nset -emo pipefail\necho mycommand-1\nset -emo pipefail\necho mycommand-2\nset -emo pipefail\necho mycommand-3\nset -emo pipefail\necho mycommand-4\nset -emo pipefail\necho mycommand-5\nset -emo pipefail\necho mycommand-6\nset -emo pipefail\necho mycommand-7\nset -emo pipefail\necho mycommand-8\nset -emo pipefail\necho mycommand-9\nset -emo pipefail\necho mycommand-10\nset -emo pipefail\necho mycommand-11\nset -emo pipefail\necho mycommand-12\nset -emo pipefail\necho mycommand-13\nset -emo pipefail\necho mycommand-14\nset -emo pipefail\necho mycommand-15\nset -emo pipefail\necho mycommand-16\nset -emo pipefail\necho mycommand-17\nset -emo pipefail\necho mycommand-18\nset -emo pipefail\necho mycommand-19\nset -emo pipefail\necho mycommand-20\nset -emo pipefail\necho mycommand-21\nset -emo pipefail\necho mycommand-22\nset -emo pipefail\necho mycommand-23\nset -emo pipefail\necho mycommand-24\nset -emo pipefail\nhishtory query mycommand\nset -emo pipefail\necho mycommand-30\necho mycommand-31\necho mycommand-3\nset -emo pipefail\n"
|
|
|
|
expectedOutput := "hishtory status\necho mycommand-0\necho mycommand-1\necho mycommand-2\necho mycommand-3\necho mycommand-4\necho mycommand-5\necho mycommand-6\necho mycommand-7\necho mycommand-8\necho mycommand-9\necho mycommand-10\necho mycommand-11\necho mycommand-12\necho mycommand-13\necho mycommand-14\necho mycommand-15\necho mycommand-16\necho mycommand-17\necho mycommand-18\necho mycommand-19\necho mycommand-20\necho mycommand-21\necho mycommand-22\necho mycommand-23\necho mycommand-24\nhishtory query mycommand\necho mycommand-30\necho mycommand-31\necho mycommand-3\n"
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRepeatedCommandAndQuery(t *testing.T) {
|
|
|
|
func testRepeatedCommandAndQuery(t *testing.T, tester shellTester) {
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out := tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
tester.RunInteractiveShell(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
out = hishtoryQuery(t, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
out = hishtoryQuery(t, tester, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -564,49 +641,53 @@ func TestRepeatedCommandAndQuery(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRepeatedEnableDisable(t *testing.T) {
|
|
|
|
func testRepeatedEnableDisable(t *testing.T, tester shellTester) {
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
installHishtory(t, "")
|
|
|
|
installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf(`echo mycommand-%d
|
|
|
|
tester.RunInteractiveShell(t, fmt.Sprintf(`echo mycommand-%d
|
|
|
|
hishtory disable
|
|
|
|
hishtory disable
|
|
|
|
echo shouldnotshowup
|
|
|
|
echo shouldnotshowup
|
|
|
|
sleep 0.5
|
|
|
|
sleep 0.5
|
|
|
|
hishtory enable`, i))
|
|
|
|
hishtory enable`, i))
|
|
|
|
out := hishtoryQuery(t, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
out := hishtoryQuery(t, tester, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mycommand") != 1 {
|
|
|
|
if strings.Count(out, "echo mycommand") != 1 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of commands=%d, out=%#v", i, strings.Count(out, "echo mycommand"), out)
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of commands=%d, out=%#v", i, strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
if strings.Contains(out, "shouldnotshowup") {
|
|
|
|
if strings.Contains(out, "shouldnotshowup") {
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
out := RunInteractiveBashCommands(t, "hishtory export")
|
|
|
|
out := tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail | grep -v '/tmp/client install'")
|
|
|
|
expectedOutput := "set -emo pipefail\necho mycommand-0\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-0\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-1\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-1\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-2\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-2\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-3\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-3\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-4\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-4\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-5\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-5\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-6\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-6\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-7\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-7\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-8\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-8\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-9\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-9\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-10\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-10\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-11\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-11\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-12\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-12\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-13\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-13\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-14\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-14\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-15\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-15\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-16\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-16\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-17\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-17\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-18\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-18\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-19\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-19\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-20\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-20\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-21\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-21\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-22\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-22\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-23\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-23\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-24\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-24\nset -emo pipefail\nhishtory query\nset -emo pipefail\n"
|
|
|
|
expectedOutput := "echo mycommand-0\nhishtory enable\nhishtory query mycommand-0\nhishtory query\necho mycommand-1\nhishtory enable\nhishtory query mycommand-1\nhishtory query\necho mycommand-2\nhishtory enable\nhishtory query mycommand-2\nhishtory query\necho mycommand-3\nhishtory enable\nhishtory query mycommand-3\nhishtory query\necho mycommand-4\nhishtory enable\nhishtory query mycommand-4\nhishtory query\necho mycommand-5\nhishtory enable\nhishtory query mycommand-5\nhishtory query\necho mycommand-6\nhishtory enable\nhishtory query mycommand-6\nhishtory query\necho mycommand-7\nhishtory enable\nhishtory query mycommand-7\nhishtory query\necho mycommand-8\nhishtory enable\nhishtory query mycommand-8\nhishtory query\necho mycommand-9\nhishtory enable\nhishtory query mycommand-9\nhishtory query\necho mycommand-10\nhishtory enable\nhishtory query mycommand-10\nhishtory query\necho mycommand-11\nhishtory enable\nhishtory query mycommand-11\nhishtory query\necho mycommand-12\nhishtory enable\nhishtory query mycommand-12\nhishtory query\necho mycommand-13\nhishtory enable\nhishtory query mycommand-13\nhishtory query\necho mycommand-14\nhishtory enable\nhishtory query mycommand-14\nhishtory query\necho mycommand-15\nhishtory enable\nhishtory query mycommand-15\nhishtory query\necho mycommand-16\nhishtory enable\nhishtory query mycommand-16\nhishtory query\necho mycommand-17\nhishtory enable\nhishtory query mycommand-17\nhishtory query\necho mycommand-18\nhishtory enable\nhishtory query mycommand-18\nhishtory query\necho mycommand-19\nhishtory enable\nhishtory query mycommand-19\nhishtory query\necho mycommand-20\nhishtory enable\nhishtory query mycommand-20\nhishtory query\necho mycommand-21\nhishtory enable\nhishtory query mycommand-21\nhishtory query\necho mycommand-22\nhishtory enable\nhishtory query mycommand-22\nhishtory query\necho mycommand-23\nhishtory enable\nhishtory query mycommand-23\nhishtory query\necho mycommand-24\nhishtory enable\nhishtory query mycommand-24\nhishtory query\n"
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestExcludeHiddenCommand(t *testing.T) {
|
|
|
|
func testExcludeHiddenCommand(t *testing.T, tester shellTester) {
|
|
|
|
|
|
|
|
if tester.ShellName() == "zsh" {
|
|
|
|
|
|
|
|
t.Skip()
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
}
|
|
|
|
// Set up
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
installHishtory(t, "")
|
|
|
|
installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, `echo hello1
|
|
|
|
tester.RunInteractiveShell(t, `echo hello1
|
|
|
|
echo hidden
|
|
|
|
echo hidden
|
|
|
|
echo hello2
|
|
|
|
echo hello2
|
|
|
|
echo hidden`)
|
|
|
|
echo hidden`)
|
|
|
|
RunInteractiveBashCommands(t, " echo hidden")
|
|
|
|
tester.RunInteractiveShell(t, " echo hidden")
|
|
|
|
out := hishtoryQuery(t, "")
|
|
|
|
out := hishtoryQuery(t, tester, "")
|
|
|
|
if strings.Count(out, "\n") != 5 && strings.Count(out, "\n") != 6 {
|
|
|
|
if strings.Count(out, "\n") != 5 && strings.Count(out, "\n") != 6 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -623,7 +704,7 @@ echo hello2
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory export | grep -v pipefail")
|
|
|
|
out = tester.RunInteractiveShell(t, "hishtory export | grep -v pipefail | grep -v '/tmp/client install'")
|
|
|
|
expectedOutput := "echo hello1\necho hello2\nhishtory query\n"
|
|
|
|
expectedOutput := "echo hello1\necho hello2\nhishtory query\n"
|
|
|
|
if out != expectedOutput {
|
|
|
|
if out != expectedOutput {
|
|
|
|
t.Fatalf("hishtory export has unexpected output=%#v", out)
|
|
|
|
t.Fatalf("hishtory export has unexpected output=%#v", out)
|
|
|
@ -658,8 +739,8 @@ func waitForBackgroundSavesToComplete(t *testing.T) {
|
|
|
|
t.Fatalf("failed to wait until hishtory wasn't running")
|
|
|
|
t.Fatalf("failed to wait until hishtory wasn't running")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func hishtoryQuery(t *testing.T, query string) string {
|
|
|
|
func hishtoryQuery(t *testing.T, tester shellTester, query string) string {
|
|
|
|
return RunInteractiveBashCommands(t, "hishtory query "+query)
|
|
|
|
return tester.RunInteractiveShell(t, "hishtory query "+query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Maybe a dedicated unit test for retrieveAdditionalEntriesFromRemote
|
|
|
|
// TODO: Maybe a dedicated unit test for retrieveAdditionalEntriesFromRemote
|
|
|
@ -676,12 +757,12 @@ func manuallySubmitHistoryEntry(t *testing.T, userSecret string, entry data.Hist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHishtoryBackgroundSaving(t *testing.T) {
|
|
|
|
func testHishtoryBackgroundSaving(t *testing.T, tester shellTester) {
|
|
|
|
// Setup
|
|
|
|
// Setup
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
|
|
|
|
// Test install with an unset HISHTORY_TEST var so that we save in the background (this is likely to be flakey!)
|
|
|
|
// Test install with an unset HISHTORY_TEST var so that we save in the background (this is likely to be flakey!)
|
|
|
|
out := RunInteractiveBashCommands(t, `unset HISHTORY_TEST
|
|
|
|
out := tester.RunInteractiveShell(t, `unset HISHTORY_TEST
|
|
|
|
go build -o /tmp/client
|
|
|
|
go build -o /tmp/client
|
|
|
|
/tmp/client install`)
|
|
|
|
/tmp/client install`)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
@ -705,13 +786,13 @@ go build -o /tmp/client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test the status subcommand
|
|
|
|
// Test the status subcommand
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
out = tester.RunInteractiveShell(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test recording commands
|
|
|
|
// Test recording commands
|
|
|
|
out, err = RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
|
|
|
out, err = tester.RunInteractiveShellRelaxed(t, `set -m
|
|
|
|
ls /a
|
|
|
|
ls /a
|
|
|
|
echo foo`)
|
|
|
|
echo foo`)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -723,7 +804,7 @@ echo foo`)
|
|
|
|
|
|
|
|
|
|
|
|
// Test querying for all commands
|
|
|
|
// Test querying for all commands
|
|
|
|
waitForBackgroundSavesToComplete(t)
|
|
|
|
waitForBackgroundSavesToComplete(t)
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
out = hishtoryQuery(t, tester, "")
|
|
|
|
expected := []string{"echo foo", "ls /a"}
|
|
|
|
expected := []string{"echo foo", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
@ -733,7 +814,7 @@ echo foo`)
|
|
|
|
|
|
|
|
|
|
|
|
// Test querying for a specific command
|
|
|
|
// Test querying for a specific command
|
|
|
|
waitForBackgroundSavesToComplete(t)
|
|
|
|
waitForBackgroundSavesToComplete(t)
|
|
|
|
out = hishtoryQuery(t, "foo")
|
|
|
|
out = hishtoryQuery(t, tester, "foo")
|
|
|
|
if !strings.Contains(out, "echo foo") {
|
|
|
|
if !strings.Contains(out, "echo foo") {
|
|
|
|
t.Fatalf("output doesn't contain the expected item, out=%#v", out)
|
|
|
|
t.Fatalf("output doesn't contain the expected item, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -742,10 +823,10 @@ echo foo`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestDisplayTable(t *testing.T) {
|
|
|
|
func testDisplayTable(t *testing.T, tester shellTester) {
|
|
|
|
// Setup
|
|
|
|
// Setup
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
userSecret := installHishtory(t, tester, "")
|
|
|
|
|
|
|
|
|
|
|
|
// Submit two fake entries
|
|
|
|
// Submit two fake entries
|
|
|
|
tmz, err := time.LoadLocation("America/Los_Angeles")
|
|
|
|
tmz, err := time.LoadLocation("America/Los_Angeles")
|
|
|
@ -764,7 +845,7 @@ func TestDisplayTable(t *testing.T) {
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry2)
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry2)
|
|
|
|
|
|
|
|
|
|
|
|
// Query and check the table
|
|
|
|
// Query and check the table
|
|
|
|
out := RunInteractiveBashCommands(t, "hishtory query table")
|
|
|
|
out := hishtoryQuery(t, tester, "table")
|
|
|
|
expectedOutput := "Hostname CWD Timestamp Runtime Exit Code Command \nlocalhost ~/foo/ Apr 16 2022 01:03:16 -0700 24s 3 table_cmd2 \nlocalhost /tmp/ Apr 16 2022 01:03:06 -0700 4s 2 table_cmd1 \n"
|
|
|
|
expectedOutput := "Hostname CWD Timestamp Runtime Exit Code Command \nlocalhost ~/foo/ Apr 16 2022 01:03:16 -0700 24s 3 table_cmd2 \nlocalhost /tmp/ Apr 16 2022 01:03:06 -0700 4s 2 table_cmd1 \n"
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory query table test mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
t.Fatalf("hishtory query table test mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|