Remove unnecessary set -m calls, speed up the local make acttest command, embed ReleaseVersion into the test server to fix the test failures on macos, and update install to be resistant to bashrc and zshrc not existing

This commit is contained in:
David Dworken 2022-04-19 21:05:54 -07:00
parent 0a03ce3407
commit 10ee085d4c
5 changed files with 43 additions and 19 deletions

View File

@ -21,5 +21,4 @@ jobs:
run: | run: |
sudo apt-get update || true sudo apt-get update || true
sudo apt-get install -y zsh || true sudo apt-get install -y zsh || true
touch ~/.zshrc
make test make test

View File

@ -6,7 +6,7 @@ test:
HISHTORY_TEST=1 go test -p 1 ./... HISHTORY_TEST=1 go test -p 1 ./...
acttest: acttest:
act push -j test -e .github/push_event.json act push -j test -e .github/push_event.json --reuse
release: release:
# Bump the version # Bump the version

View File

@ -20,8 +20,6 @@ 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")
@ -228,7 +226,7 @@ func testIntegrationWithNewDevice(t *testing.T, tester shellTester) {
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 := "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" expectedOutputWithoutKey := "hishtory status\nhishtory query\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)
@ -277,8 +275,7 @@ func testBasicUserFlow(t *testing.T, tester shellTester) string {
os.Setenv("FORCED_BANNER", "") os.Setenv("FORCED_BANNER", "")
// Test recording commands // Test recording commands
out, err = tester.RunInteractiveShellRelaxed(t, `set -m out, err = tester.RunInteractiveShellRelaxed(t, `ls /a
ls /a
ls /bar ls /bar
ls /foo ls /foo
echo foo echo foo
@ -827,8 +824,7 @@ go build -o /tmp/client
} }
// Test recording commands // Test recording commands
out, err = tester.RunInteractiveShellRelaxed(t, `set -m out, err = tester.RunInteractiveShellRelaxed(t, `ls /a
ls /a
echo foo`) echo foo`)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -3,6 +3,7 @@ package lib
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -370,15 +371,15 @@ func configureZshrc(homedir, binaryPath string) error {
return fmt.Errorf("failed to write config.zsh file: %v", err) return fmt.Errorf("failed to write config.zsh file: %v", err)
} }
// Check if we need to configure the zshrc // Check if we need to configure the zshrc
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".zshrc")) zshIsConfigured, err := isZshConfigured(homedir)
if err != nil { if err != nil {
return fmt.Errorf("failed to read zshrc: %v", err) return fmt.Errorf("failed to check ~/.zshrc: %v", err)
} }
if strings.Contains(string(bashrc), "# Hishtory Config:") { if zshIsConfigured {
return nil return nil
} }
// Add to zshrc // Add to zshrc
f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_WRONLY, 0o644) f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("failed to append to zshrc: %v", err) return fmt.Errorf("failed to append to zshrc: %v", err)
} }
@ -390,6 +391,18 @@ func configureZshrc(homedir, binaryPath string) error {
return nil return nil
} }
func isZshConfigured(homedir string) (bool, error) {
_, err := os.Stat(path.Join(homedir, ".zshrc"))
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".zshrc"))
if err != nil {
return false, fmt.Errorf("failed to read zshrc: %v", err)
}
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
}
func configureBashrc(homedir, binaryPath string) error { func configureBashrc(homedir, binaryPath string) error {
// Create the file we're going to source in our bashrc. Do this no matter what in case there are updates to it. // Create the file we're going to source in our bashrc. Do this no matter what in case there are updates to it.
bashConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.sh") bashConfigPath := path.Join(homedir, shared.HISHTORY_PATH, "config.sh")
@ -402,15 +415,15 @@ func configureBashrc(homedir, binaryPath string) error {
return fmt.Errorf("failed to write config.sh file: %v", err) return fmt.Errorf("failed to write config.sh file: %v", err)
} }
// Check if we need to configure the bashrc // Check if we need to configure the bashrc
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bashrc")) bashIsConfigured, err := isBashConfigured(homedir)
if err != nil { if err != nil {
return fmt.Errorf("failed to read bashrc: %v", err) return fmt.Errorf("failed to check ~/.bashrc: %v", err)
} }
if strings.Contains(string(bashrc), "# Hishtory Config:") { if bashIsConfigured {
return nil return nil
} }
// Add to bashrc // Add to bashrc
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY, 0o644) f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("failed to append to bashrc: %v", err) return fmt.Errorf("failed to append to bashrc: %v", err)
} }
@ -422,6 +435,18 @@ func configureBashrc(homedir, binaryPath string) error {
return nil return nil
} }
func isBashConfigured(homedir string) (bool, error) {
_, err := os.Stat(path.Join(homedir, ".bashrc"))
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bashrc"))
if err != nil {
return false, fmt.Errorf("failed to read bashrc: %v", err)
}
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
}
func installBinary(homedir string) (string, error) { func installBinary(homedir string) (string, error) {
clientPath, err := exec.LookPath("hishtory") clientPath, err := exec.LookPath("hishtory")
if err != nil { if err != nil {

View File

@ -62,12 +62,16 @@ func buildServer() {
panic("failed to cd into hishtory dir!") panic("failed to cd into hishtory dir!")
} }
} }
cmd := exec.Command("go", "build", "-o", "/tmp/server", "backend/server/server.go") version, err := os.ReadFile("VERSION")
if err != nil {
panic(fmt.Sprintf("failed to read VERSION file: %v", err))
}
cmd := exec.Command("go", "build", "-o", "/tmp/server", "-ldflags", fmt.Sprintf("-X main.ReleaseVersion=v0.%s", version), "backend/server/server.go")
var stdout bytes.Buffer var stdout bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
var stderr bytes.Buffer var stderr bytes.Buffer
cmd.Stderr = &stderr cmd.Stderr = &stderr
err := cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to start to build server: %v, stderr=%#v, stdout=%#v", err, stderr.String(), stdout.String())) panic(fmt.Sprintf("failed to start to build server: %v, stderr=%#v, stdout=%#v", err, stderr.String(), stdout.String()))
} }