Skip to content

Commit

Permalink
Refactor tests to make them non-flakey and pass on actions
Browse files Browse the repository at this point in the history
This required adding the ability to have hishtory run synchronously to avoid reconditions. I also added additional waiting code. Also a whole bunch of new tests and disabled gorm's default logger which also caued flakeyness
  • Loading branch information
ddworken committed Apr 15, 2022
1 parent 24db9d8 commit dc6fb6a
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 63 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
forcetest:
go clean -testcache
HISHTORY_TEST=1 go test -p 1 ./...
test:
HISHTORY_TEST=1 go test -p 1 ./...

Expand Down
10 changes: 9 additions & 1 deletion backend/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func TestUpdateReleaseVersion(t *testing.T) {
t.Fatalf("updateReleaseVersion failed: %v", err)
}

// And check that the new value looks reasonable
// If ReleaseVersion is still unknown, skip because we're getting rate limited
if ReleaseVersion == "UNKNOWN" {
t.Skip()
}
// Otherwise, check that the new value looks reasonable
if !strings.HasPrefix(ReleaseVersion, "v0.") {
t.Fatalf("ReleaseVersion wasn't updated to contain a version: %#v", ReleaseVersion)
}
Expand All @@ -151,6 +155,10 @@ func TestGithubRedirects(t *testing.T) {
t.Fatalf("expected endpoint to return redirect")
}
locationHeader := resp.Header.Get("location")
if strings.Contains(locationHeader, "https://github.com/ddworken/hishtory/releases/download/UNKNOWN") {
// Getting rate limited, skip the test
t.Skip()
}
if !strings.Contains(locationHeader, "https://github.com/ddworken/hishtory/releases/download/v") {
t.Fatalf("expected location header to point to github")
}
Expand Down
287 changes: 230 additions & 57 deletions client/client_test.go

Large diffs are not rendered by default.

33 changes: 30 additions & 3 deletions client/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"

"github.com/fatih/color"
"github.com/google/uuid"
Expand All @@ -35,6 +36,9 @@ import (
//go:embed config.sh
var ConfigShContents string

//go:embed test_config.sh
var TestConfigShContents string

func getCwd() (string, error) {
cwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -219,7 +223,16 @@ func GetConfig() (ClientConfig, error) {
}
data, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH))
if err != nil {
return ClientConfig{}, fmt.Errorf("failed to read config file: %v", err)
files, err := ioutil.ReadDir(path.Join(homedir, shared.HISHTORY_PATH))
if err != nil {
return ClientConfig{}, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
}
filenames := ""
for _, file := range files {
filenames += file.Name()
filenames += ", "
}
return ClientConfig{}, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, err)
}
var config ClientConfig
err = json.Unmarshal(data, &config)
Expand Down Expand Up @@ -312,7 +325,11 @@ func Install() 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.
bashConfigPath := path.Join(filepath.Dir(binaryPath), "config.sh")
err := ioutil.WriteFile(bashConfigPath, []byte(ConfigShContents), 0o644)
configContents := ConfigShContents
if os.Getenv("HISHTORY_TEST") != "" {
configContents = TestConfigShContents
}
err := ioutil.WriteFile(bashConfigPath, []byte(configContents), 0o644)
if err != nil {
return fmt.Errorf("failed to write config.sh file: %v", err)
}
Expand Down Expand Up @@ -428,7 +445,17 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
if err != nil {
return nil, fmt.Errorf("failed to create ~/.hishtory dir: %v", err)
}
db, err := gorm.Open(sqlite.Open(path.Join(homedir, shared.HISHTORY_PATH, shared.DB_PATH)), &gorm.Config{SkipDefaultTransaction: true})
newLogger := logger.New(
// TODO: change this back to warn, but have it go to a file?
log.New(os.Stdout, "\n", log.LstdFlags),
logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Silent,
IgnoreRecordNotFoundError: true,
Colorful: false,
},
)
db, err := gorm.Open(sqlite.Open(path.Join(homedir, shared.HISHTORY_PATH, shared.DB_PATH)), &gorm.Config{SkipDefaultTransaction: true, Logger: newLogger})
if err != nil {
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
}
Expand Down
29 changes: 29 additions & 0 deletions client/lib/test_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This script should be sourced inside of .bashrc to integrate bash with hishtory
# This is the same as config.sh, except it doesn't run the save process in the background. This is crucial to making tests reproducible.

# Implementation of PreCommand and PostCommand based on https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/
function PreCommand() {
if [ -z "$HISHTORY_AT_PROMPT" ]; then
return
fi
unset HISHTORY_AT_PROMPT

# Run before every command
HISHTORY_START_TIME=`date +%s%N`
}
trap "PreCommand" DEBUG

HISHTORY_FIRST_PROMPT=1
function PostCommand() {
EXIT_CODE=$?
HISHTORY_AT_PROMPT=1

if [ -n "$HISHTORY_FIRST_PROMPT" ]; then
unset HISHTORY_FIRST_PROMPT
return
fi

# Run after every prompt
hishtory saveHistoryEntry $EXIT_CODE "`history 1`" $HISHTORY_START_TIME
}
PROMPT_COMMAND="PostCommand"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -197,6 +199,7 @@ golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
5 changes: 4 additions & 1 deletion hishtory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -127,7 +128,9 @@ func displayBannerIfSet() error {

func saveHistoryEntry() {
config, err := lib.GetConfig()
lib.CheckFatalError(err)
if err != nil {
log.Fatalf("hishtory cannot save an entry because the hishtory config file does not exist, try running `hishtory init` (err=%v)", err)
}
if !config.IsEnabled {
return
}
Expand Down
8 changes: 7 additions & 1 deletion shared/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func ResetLocalState(t *testing.T) {

_ = os.Remove(path.Join(homedir, HISHTORY_PATH, DB_PATH))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "hishtory"))
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "config.sh"))
}

func BackupAndRestore(t *testing.T) func() {
Expand All @@ -30,9 +32,13 @@ func BackupAndRestore(t *testing.T) func() {

_ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH), path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory"), path.Join(homedir, HISHTORY_PATH, "hishtory.bak"))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh"), path.Join(homedir, HISHTORY_PATH, "config.sh.bak"))
return func() {
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, DB_PATH))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory.bak"), path.Join(homedir, HISHTORY_PATH, "hishtory"))
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh.bak"), path.Join(homedir, HISHTORY_PATH, "config.sh"))
}
}

Expand Down Expand Up @@ -88,7 +94,7 @@ func RunTestServer(t *testing.T) func() {
}()
return func() {
err := cmd.Process.Kill()
if err != nil {
if err != nil && err.Error() != "os: process already finished" {
t.Fatalf("failed to kill process: %v", err)
}
if strings.Contains(stderr.String()+stdout.String(), "failed to") {
Expand Down

0 comments on commit dc6fb6a

Please sign in to comment.