mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-19 19:37:59 +02:00
Add tests for the User column + swap to using require.NoError(...) in a few places
This commit is contained in:
parent
3a15010e95
commit
596b5b2770
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
"github.com/ddworken/hishtory/shared/testutils"
|
"github.com/ddworken/hishtory/shared/testutils"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func skipSlowTests() bool {
|
func skipSlowTests() bool {
|
||||||
@ -92,9 +94,7 @@ type zshTester struct {
|
|||||||
|
|
||||||
func (z zshTester) RunInteractiveShell(t *testing.T, script string) string {
|
func (z zshTester) RunInteractiveShell(t *testing.T, script string) string {
|
||||||
res, err := z.RunInteractiveShellRelaxed(t, "set -eo pipefail\n"+script)
|
res, err := z.RunInteractiveShellRelaxed(t, "set -eo pipefail\n"+script)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,9 +374,7 @@ echo thisisnotrecorded
|
|||||||
sleep 0.5
|
sleep 0.5
|
||||||
hishtory enable
|
hishtory enable
|
||||||
echo thisisrecorded`)
|
echo thisisrecorded`)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\n" {
|
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\n" {
|
||||||
t.Fatalf("unexpected output from running commands: %#v", out)
|
t.Fatalf("unexpected output from running commands: %#v", out)
|
||||||
}
|
}
|
||||||
@ -472,9 +470,7 @@ notacommand
|
|||||||
cd /tmp/
|
cd /tmp/
|
||||||
echo querybydir
|
echo querybydir
|
||||||
hishtory disable`)
|
hishtory disable`)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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, tester, `echo`)
|
out := hishtoryQuery(t, tester, `echo`)
|
||||||
@ -1017,9 +1013,7 @@ CGO_ENABLED=0 go build -o /tmp/client
|
|||||||
// Test recording commands
|
// Test recording commands
|
||||||
out, err = tester.RunInteractiveShellRelaxed(t, `ls /a
|
out, err = tester.RunInteractiveShellRelaxed(t, `ls /a
|
||||||
echo foo`)
|
echo foo`)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if out != "foo\n" {
|
if out != "foo\n" {
|
||||||
t.Fatalf("unexpected output from running commands: %#v", out)
|
t.Fatalf("unexpected output from running commands: %#v", out)
|
||||||
}
|
}
|
||||||
@ -1257,9 +1251,7 @@ func testInstallViaPythonScriptChild(t *testing.T, tester shellTester) {
|
|||||||
|
|
||||||
// Test the status subcommand
|
// Test the status subcommand
|
||||||
downloadData, err := lib.GetDownloadData()
|
downloadData, err := lib.GetDownloadData()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
out = tester.RunInteractiveShell(t, `hishtory status`)
|
out = tester.RunInteractiveShell(t, `hishtory status`)
|
||||||
expectedOut := fmt.Sprintf("hiSHtory: %s\nEnabled: true\nSecret Key: %s\nCommit Hash: ", downloadData.Version, userSecret)
|
expectedOut := fmt.Sprintf("hiSHtory: %s\nEnabled: true\nSecret Key: %s\nCommit Hash: ", downloadData.Version, userSecret)
|
||||||
if !strings.Contains(out, expectedOut) {
|
if !strings.Contains(out, expectedOut) {
|
||||||
@ -1293,9 +1285,7 @@ hishtory enable
|
|||||||
echo thisisrecorded
|
echo thisisrecorded
|
||||||
echo bar &
|
echo bar &
|
||||||
sleep 1`)
|
sleep 1`)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\nbar\n" {
|
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\nbar\n" {
|
||||||
t.Fatalf("unexpected output from running commands: %#v", out)
|
t.Fatalf("unexpected output from running commands: %#v", out)
|
||||||
}
|
}
|
||||||
@ -1364,19 +1354,13 @@ func TestStripBashTimePrefix(t *testing.T) {
|
|||||||
|
|
||||||
// Add a HISTTIMEFORMAT to the bashrc
|
// Add a HISTTIMEFORMAT to the bashrc
|
||||||
homedir, err := os.UserHomeDir()
|
homedir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
f, err := os.OpenFile(path.Join(homedir, data.GetHishtoryPath(), "config.sh"),
|
f, err := os.OpenFile(path.Join(homedir, data.GetHishtoryPath(), "config.sh"),
|
||||||
os.O_APPEND|os.O_WRONLY, 0644)
|
os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
_, err = f.WriteString("\nexport HISTTIMEFORMAT='%F %T '\n")
|
_, err = f.WriteString("\nexport HISTTIMEFORMAT='%F %T '\n")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record a command
|
// Record a command
|
||||||
tester.RunInteractiveShell(t, `ls -Slah`)
|
tester.RunInteractiveShell(t, `ls -Slah`)
|
||||||
@ -1389,19 +1373,13 @@ func TestStripBashTimePrefix(t *testing.T) {
|
|||||||
|
|
||||||
// Update it to another complex one
|
// Update it to another complex one
|
||||||
homedir, err = os.UserHomeDir()
|
homedir, err = os.UserHomeDir()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
f, err = os.OpenFile(path.Join(homedir, data.GetHishtoryPath(), "config.sh"),
|
f, err = os.OpenFile(path.Join(homedir, data.GetHishtoryPath(), "config.sh"),
|
||||||
os.O_APPEND|os.O_WRONLY, 0644)
|
os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
_, err = f.WriteString("\nexport HISTTIMEFORMAT='[%c] '\n")
|
_, err = f.WriteString("\nexport HISTTIMEFORMAT='[%c] '\n")
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record a command
|
// Record a command
|
||||||
tester.RunInteractiveShell(t, `echo foo`)
|
tester.RunInteractiveShell(t, `echo foo`)
|
||||||
@ -1967,6 +1945,17 @@ func TestTui(t *testing.T) {
|
|||||||
out = strings.Split(strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]), "\n")[0]
|
out = strings.Split(strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]), "\n")[0]
|
||||||
testutils.CompareGoldens(t, out, "TestTui-SelectAndCd")
|
testutils.CompareGoldens(t, out, "TestTui-SelectAndCd")
|
||||||
|
|
||||||
|
// Test the User column
|
||||||
|
tester.RunInteractiveShell(t, `hishtory config-add displayed-columns User`)
|
||||||
|
out = captureTerminalOutput(t, tester, []string{
|
||||||
|
"hishtory SPACE tquery ENTER",
|
||||||
|
})
|
||||||
|
out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1])
|
||||||
|
require.Contains(t, out, " User")
|
||||||
|
u, err := user.Current()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Contains(t, out, " "+u.Username+" ")
|
||||||
|
|
||||||
// Assert there are no leaked connections
|
// Assert there are no leaked connections
|
||||||
assertNoLeakedConnections(t)
|
assertNoLeakedConnections(t)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user