From d7da6dde0eac7f67350af7292873f976559899f3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 5 Apr 2021 22:18:36 +1000 Subject: [PATCH] allow decimal replay speeds for integration tests --- integration/main.go | 4 +- pkg/gui/gui_test.go | 6 +- pkg/gui/recording.go | 8 +-- pkg/integration/integration.go | 22 +++--- test/lazyintegration/main.go | 71 ++------------------ vendor/github.com/jesseduffield/gocui/gui.go | 14 ++-- 6 files changed, 33 insertions(+), 92 deletions(-) diff --git a/integration/main.go b/integration/main.go index a35adc11d..ace81b24d 100644 --- a/integration/main.go +++ b/integration/main.go @@ -66,7 +66,7 @@ func test() error { log.Printf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir) for i, speed := range speeds { - log.Printf("%s: attempting test at speed %d\n", test.Name, speed) + log.Printf("%s: attempting test at speed %f\n", test.Name, speed) integration.FindOrCreateDir(testPath) integration.PrepareIntegrationTestDir(actualDir) @@ -101,7 +101,7 @@ func test() error { } if expected == actual { - fmt.Printf("%s: success at speed %d\n", test.Name, speed) + fmt.Printf("%s: success at speed %f\n", test.Name, speed) break } diff --git a/pkg/gui/gui_test.go b/pkg/gui/gui_test.go index 887b48de8..c6f945ae9 100644 --- a/pkg/gui/gui_test.go +++ b/pkg/gui/gui_test.go @@ -34,7 +34,7 @@ import ( // To override speed, pass e.g. `SPEED=1` as an env var. Otherwise we start each test // at a high speed and then drop down to lower speeds upon each failure until finally // trying at the original playback speed (speed 1). A speed of 2 represents twice the -// original playback speed. Speed must be an integer. +// original playback speed. Speed may be a decimal. func Test(t *testing.T) { rootDir := integration.GetRootDirectory() @@ -66,7 +66,7 @@ func Test(t *testing.T) { // three retries at normal speed for the sake of flakey tests speeds = append(speeds, 1, 1, 1) for i, speed := range speeds { - t.Logf("%s: attempting test at speed %d\n", test.Name, speed) + t.Logf("%s: attempting test at speed %f\n", test.Name, speed) integration.FindOrCreateDir(testPath) integration.PrepareIntegrationTestDir(actualDir) @@ -102,7 +102,7 @@ func Test(t *testing.T) { assert.NoError(t, err) if expected == actual { - t.Logf("%s: success at speed %d\n", test.Name, speed) + t.Logf("%s: success at speed %f\n", test.Name, speed) break } diff --git a/pkg/gui/recording.go b/pkg/gui/recording.go index 51235b96b..0a7f723df 100644 --- a/pkg/gui/recording.go +++ b/pkg/gui/recording.go @@ -26,13 +26,13 @@ func headless() bool { return os.Getenv("HEADLESS") != "" } -func getRecordingSpeed() int { +func getRecordingSpeed() float64 { // humans are slow so this speeds things up. - speed := 1 - envReplaySpeed := os.Getenv("REPLAY_SPEED") + speed := 1.0 + envReplaySpeed := os.Getenv("SPEED") if envReplaySpeed != "" { var err error - speed, err = strconv.Atoi(envReplaySpeed) + speed, err = strconv.ParseFloat(envReplaySpeed, 64) if err != nil { log.Fatal(err) } diff --git a/pkg/integration/integration.go b/pkg/integration/integration.go index 80b2acf27..e7e21bbd7 100644 --- a/pkg/integration/integration.go +++ b/pkg/integration/integration.go @@ -15,9 +15,9 @@ import ( ) type Test struct { - Name string `json:"name"` - Speed int `json:"speed"` - Description string `json:"description"` + Name string `json:"name"` + Speed float64 `json:"speed"` + Description string `json:"description"` } func PrepareIntegrationTestDir(actualDir string) { @@ -79,27 +79,27 @@ func TempLazygitPath() string { return filepath.Join("/tmp", "lazygit", "test_lazygit") } -func GetTestSpeeds(testStartSpeed int, updateSnapshots bool) []int { +func GetTestSpeeds(testStartSpeed float64, updateSnapshots bool) []float64 { if updateSnapshots { // have to go at original speed if updating snapshots in case we go to fast and create a junk snapshot - return []int{1} + return []float64{1.0} } speedEnv := os.Getenv("SPEED") if speedEnv != "" { - speed, err := strconv.Atoi(speedEnv) + speed, err := strconv.ParseFloat(speedEnv, 64) if err != nil { panic(err) } - return []int{speed} + return []float64{speed} } // default is 10, 5, 1 - startSpeed := 10 + startSpeed := 10.0 if testStartSpeed != 0 { startSpeed = testStartSpeed } - speeds := []int{startSpeed} + speeds := []float64{startSpeed} if startSpeed > 5 { speeds = append(speeds, 5) } @@ -238,7 +238,7 @@ func GenerateSnapshots(actualDir string, expectedDir string) (string, string, er return actual, expected, nil } -func GetLazygitCommand(testPath string, rootDir string, record bool, speed int) (*exec.Cmd, error) { +func GetLazygitCommand(testPath string, rootDir string, record bool, speed float64) (*exec.Cmd, error) { osCommand := oscommands.NewDummyOSCommand() replayPath := filepath.Join(testPath, "recording.json") @@ -268,7 +268,7 @@ func GetLazygitCommand(testPath string, rootDir string, record bool, speed int) cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s", TempLazygitPath(), configDir, actualDir) cmd := osCommand.ExecutableFromString(cmdStr) - cmd.Env = append(cmd.Env, fmt.Sprintf("REPLAY_SPEED=%d", speed)) + cmd.Env = append(cmd.Env, fmt.Sprintf("SPEED=%f", speed)) if record { cmd.Env = append( diff --git a/test/lazyintegration/main.go b/test/lazyintegration/main.go index a2d2f0abf..5fdc6ca82 100644 --- a/test/lazyintegration/main.go +++ b/test/lazyintegration/main.go @@ -1,29 +1,27 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" "log" "os" "os/exec" "path/filepath" - "strings" "github.com/fatih/color" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/integration" "github.com/jesseduffield/lazygit/pkg/secureexec" ) type App struct { - tests []*IntegrationTest + tests []*integration.Test itemIdx int testDir string editing bool g *gocui.Gui } -func (app *App) getCurrentTest() *IntegrationTest { +func (app *App) getCurrentTest() *integration.Test { if len(app.tests) > 0 { return app.tests[app.itemIdx] } @@ -48,7 +46,7 @@ func (app *App) refreshTests() { } func (app *App) loadTests() { - tests, err := loadTests(app.testDir) + tests, err := integration.LoadTests(app.testDir) if err != nil { log.Panicln(err) } @@ -60,7 +58,7 @@ func (app *App) loadTests() { } func main() { - rootDir := getRootDirectory() + rootDir := integration.GetRootDirectory() testDir := filepath.Join(rootDir, "test", "integration") app := &App{testDir: testDir} @@ -365,7 +363,7 @@ func (app *App) layout(g *gocui.Gui) error { } descriptionView.Clear() - fmt.Fprintf(descriptionView, "Speed: %d. %s", currentTest.Speed, currentTest.Description) + fmt.Fprintf(descriptionView, "Speed: %f. %s", currentTest.Speed, currentTest.Description) if err := g.SetKeybinding("list", nil, gocui.KeyArrowDown, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { if app.itemIdx < len(app.tests)-1 { @@ -383,63 +381,6 @@ func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } -func getRootDirectory() string { - path, err := os.Getwd() - if err != nil { - panic(err) - } - - for { - _, err := os.Stat(filepath.Join(path, ".git")) - - if err == nil { - return path - } - - if !os.IsNotExist(err) { - panic(err) - } - - path = filepath.Dir(path) - - if path == "/" { - panic("must run in lazygit folder or child folder") - } - } -} - -type IntegrationTest struct { - Name string `json:"name"` - Speed int `json:"speed"` - Description string `json:"description"` -} - -func loadTests(testDir string) ([]*IntegrationTest, error) { - paths, err := filepath.Glob(filepath.Join(testDir, "/*/test.json")) - if err != nil { - return nil, err - } - tests := make([]*IntegrationTest, len(paths)) - - for i, path := range paths { - data, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - test := &IntegrationTest{} - - err = json.Unmarshal(data, test) - if err != nil { - return nil, err - } - test.Name = strings.TrimPrefix(filepath.Dir(path), testDir+"/") - - tests[i] = test - } - - return tests, nil -} - func coloredString(str string, colorAttributes ...color.Attribute) string { colour := color.New(colorAttributes...) return coloredStringDirect(str, colour) diff --git a/vendor/github.com/jesseduffield/gocui/gui.go b/vendor/github.com/jesseduffield/gocui/gui.go index 52e2926b2..ed83f34c1 100644 --- a/vendor/github.com/jesseduffield/gocui/gui.go +++ b/vendor/github.com/jesseduffield/gocui/gui.go @@ -95,7 +95,7 @@ type replayedEvents struct { } type RecordingConfig struct { - Speed int + Speed float64 Leeway int } @@ -1218,11 +1218,11 @@ func (g *Gui) replayRecording() { if i > 0 { prevEventTimestamp = g.Recording.KeyEvents[i-1].Timestamp } - timeToWait := (event.Timestamp - prevEventTimestamp) / int64(g.RecordingConfig.Speed) + timeToWait := float64(event.Timestamp-prevEventTimestamp) / g.RecordingConfig.Speed if i == 0 { - timeToWait += int64(g.RecordingConfig.Leeway) + timeToWait += float64(g.RecordingConfig.Leeway) } - var timeWaited int64 = 0 + var timeWaited float64 = 0 middle: for { select { @@ -1251,11 +1251,11 @@ func (g *Gui) replayRecording() { if i > 0 { prevEventTimestamp = g.Recording.ResizeEvents[i-1].Timestamp } - timeToWait := (event.Timestamp - prevEventTimestamp) / int64(g.RecordingConfig.Speed) + timeToWait := float64(event.Timestamp-prevEventTimestamp) / g.RecordingConfig.Speed if i == 0 { - timeToWait += int64(g.RecordingConfig.Leeway) + timeToWait += float64(g.RecordingConfig.Leeway) } - var timeWaited int64 = 0 + var timeWaited float64 = 0 middle2: for { select {