1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-13 13:59:06 +02:00

allow decimal replay speeds for integration tests

This commit is contained in:
Jesse Duffield 2021-04-05 22:18:36 +10:00
parent e000620cdf
commit d7da6dde0e
6 changed files with 33 additions and 92 deletions

View File

@ -66,7 +66,7 @@ func test() error {
log.Printf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir) log.Printf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir)
for i, speed := range speeds { 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.FindOrCreateDir(testPath)
integration.PrepareIntegrationTestDir(actualDir) integration.PrepareIntegrationTestDir(actualDir)
@ -101,7 +101,7 @@ func test() error {
} }
if expected == actual { 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 break
} }

View File

@ -34,7 +34,7 @@ import (
// To override speed, pass e.g. `SPEED=1` as an env var. Otherwise we start each test // 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 // 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 // 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) { func Test(t *testing.T) {
rootDir := integration.GetRootDirectory() rootDir := integration.GetRootDirectory()
@ -66,7 +66,7 @@ func Test(t *testing.T) {
// three retries at normal speed for the sake of flakey tests // three retries at normal speed for the sake of flakey tests
speeds = append(speeds, 1, 1, 1) speeds = append(speeds, 1, 1, 1)
for i, speed := range speeds { 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.FindOrCreateDir(testPath)
integration.PrepareIntegrationTestDir(actualDir) integration.PrepareIntegrationTestDir(actualDir)
@ -102,7 +102,7 @@ func Test(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
if expected == actual { 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 break
} }

View File

@ -26,13 +26,13 @@ func headless() bool {
return os.Getenv("HEADLESS") != "" return os.Getenv("HEADLESS") != ""
} }
func getRecordingSpeed() int { func getRecordingSpeed() float64 {
// humans are slow so this speeds things up. // humans are slow so this speeds things up.
speed := 1 speed := 1.0
envReplaySpeed := os.Getenv("REPLAY_SPEED") envReplaySpeed := os.Getenv("SPEED")
if envReplaySpeed != "" { if envReplaySpeed != "" {
var err error var err error
speed, err = strconv.Atoi(envReplaySpeed) speed, err = strconv.ParseFloat(envReplaySpeed, 64)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -15,9 +15,9 @@ import (
) )
type Test struct { type Test struct {
Name string `json:"name"` Name string `json:"name"`
Speed int `json:"speed"` Speed float64 `json:"speed"`
Description string `json:"description"` Description string `json:"description"`
} }
func PrepareIntegrationTestDir(actualDir string) { func PrepareIntegrationTestDir(actualDir string) {
@ -79,27 +79,27 @@ func TempLazygitPath() string {
return filepath.Join("/tmp", "lazygit", "test_lazygit") return filepath.Join("/tmp", "lazygit", "test_lazygit")
} }
func GetTestSpeeds(testStartSpeed int, updateSnapshots bool) []int { func GetTestSpeeds(testStartSpeed float64, updateSnapshots bool) []float64 {
if updateSnapshots { if updateSnapshots {
// have to go at original speed if updating snapshots in case we go to fast and create a junk snapshot // 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") speedEnv := os.Getenv("SPEED")
if speedEnv != "" { if speedEnv != "" {
speed, err := strconv.Atoi(speedEnv) speed, err := strconv.ParseFloat(speedEnv, 64)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return []int{speed} return []float64{speed}
} }
// default is 10, 5, 1 // default is 10, 5, 1
startSpeed := 10 startSpeed := 10.0
if testStartSpeed != 0 { if testStartSpeed != 0 {
startSpeed = testStartSpeed startSpeed = testStartSpeed
} }
speeds := []int{startSpeed} speeds := []float64{startSpeed}
if startSpeed > 5 { if startSpeed > 5 {
speeds = append(speeds, 5) speeds = append(speeds, 5)
} }
@ -238,7 +238,7 @@ func GenerateSnapshots(actualDir string, expectedDir string) (string, string, er
return actual, expected, nil 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() osCommand := oscommands.NewDummyOSCommand()
replayPath := filepath.Join(testPath, "recording.json") 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) cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s", TempLazygitPath(), configDir, actualDir)
cmd := osCommand.ExecutableFromString(cmdStr) 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 { if record {
cmd.Env = append( cmd.Env = append(

View File

@ -1,29 +1,27 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/integration"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/secureexec"
) )
type App struct { type App struct {
tests []*IntegrationTest tests []*integration.Test
itemIdx int itemIdx int
testDir string testDir string
editing bool editing bool
g *gocui.Gui g *gocui.Gui
} }
func (app *App) getCurrentTest() *IntegrationTest { func (app *App) getCurrentTest() *integration.Test {
if len(app.tests) > 0 { if len(app.tests) > 0 {
return app.tests[app.itemIdx] return app.tests[app.itemIdx]
} }
@ -48,7 +46,7 @@ func (app *App) refreshTests() {
} }
func (app *App) loadTests() { func (app *App) loadTests() {
tests, err := loadTests(app.testDir) tests, err := integration.LoadTests(app.testDir)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
@ -60,7 +58,7 @@ func (app *App) loadTests() {
} }
func main() { func main() {
rootDir := getRootDirectory() rootDir := integration.GetRootDirectory()
testDir := filepath.Join(rootDir, "test", "integration") testDir := filepath.Join(rootDir, "test", "integration")
app := &App{testDir: testDir} app := &App{testDir: testDir}
@ -365,7 +363,7 @@ func (app *App) layout(g *gocui.Gui) error {
} }
descriptionView.Clear() 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 err := g.SetKeybinding("list", nil, gocui.KeyArrowDown, gocui.ModNone, func(*gocui.Gui, *gocui.View) error {
if app.itemIdx < len(app.tests)-1 { if app.itemIdx < len(app.tests)-1 {
@ -383,63 +381,6 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit 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 { func coloredString(str string, colorAttributes ...color.Attribute) string {
colour := color.New(colorAttributes...) colour := color.New(colorAttributes...)
return coloredStringDirect(str, colour) return coloredStringDirect(str, colour)

View File

@ -95,7 +95,7 @@ type replayedEvents struct {
} }
type RecordingConfig struct { type RecordingConfig struct {
Speed int Speed float64
Leeway int Leeway int
} }
@ -1218,11 +1218,11 @@ func (g *Gui) replayRecording() {
if i > 0 { if i > 0 {
prevEventTimestamp = g.Recording.KeyEvents[i-1].Timestamp 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 { if i == 0 {
timeToWait += int64(g.RecordingConfig.Leeway) timeToWait += float64(g.RecordingConfig.Leeway)
} }
var timeWaited int64 = 0 var timeWaited float64 = 0
middle: middle:
for { for {
select { select {
@ -1251,11 +1251,11 @@ func (g *Gui) replayRecording() {
if i > 0 { if i > 0 {
prevEventTimestamp = g.Recording.ResizeEvents[i-1].Timestamp 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 { if i == 0 {
timeToWait += int64(g.RecordingConfig.Leeway) timeToWait += float64(g.RecordingConfig.Leeway)
} }
var timeWaited int64 = 0 var timeWaited float64 = 0
middle2: middle2:
for { for {
select { select {