diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-10-05 20:55:15 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-10-10 00:23:01 +1100 |
commit | dc953ea680ce2023459b9f4bf72f5ca423fb9c95 (patch) | |
tree | 67224d15415f84d1c3476e1d194e1681572af9e8 /pkg | |
parent | 08f8472db3b2942cbe5bfa977e10c4495ce242e3 (diff) |
fall back to slower speed if test fails
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/gui_test.go | 54 | ||||
-rw-r--r-- | pkg/gui/recording.go | 13 |
2 files changed, 45 insertions, 22 deletions
diff --git a/pkg/gui/gui_test.go b/pkg/gui/gui_test.go index d9d43c7dc..b7c652a99 100644 --- a/pkg/gui/gui_test.go +++ b/pkg/gui/gui_test.go @@ -106,36 +106,48 @@ func Test(t *testing.T) { for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { - testPath := filepath.Join(rootDir, "test", "integration", test.name) - findOrCreateDir(testPath) + speeds := []int{10, 5, 1} + for i, speed := range speeds { + fmt.Printf("%s: trying again at speed %d\n", test.name, speed) - snapshotPath := filepath.Join(testPath, "snapshot.txt") + testPath := filepath.Join(rootDir, "test", "integration", test.name) + findOrCreateDir(testPath) - err := os.Chdir(rootDir) - assert.NoError(t, err) + snapshotPath := filepath.Join(testPath, "snapshot.txt") - prepareIntegrationTestDir() + err := os.Chdir(rootDir) + assert.NoError(t, err) + + prepareIntegrationTestDir() + + err = createFixture(rootDir, test.fixture) + assert.NoError(t, err) - err = createFixture(rootDir, test.fixture) - assert.NoError(t, err) + record := os.Getenv("RECORD_EVENTS") != "" + runLazygit(t, testPath, rootDir, record, speed) - record := os.Getenv("RECORD_EVENTS") != "" - runLazygit(t, testPath, rootDir, record) + updateSnapshot := record || os.Getenv("UPDATE_SNAPSHOT") != "" - updateSnapshot := record || os.Getenv("UPDATE_SNAPSHOT") != "" + actual := generateSnapshot(t) - actual := generateSnapshot(t) + if updateSnapshot { + err := ioutil.WriteFile(snapshotPath, []byte(actual), 0600) + assert.NoError(t, err) + } - if updateSnapshot { - err := ioutil.WriteFile(snapshotPath, []byte(actual), 0600) + expectedBytes, err := ioutil.ReadFile(snapshotPath) assert.NoError(t, err) - } + expected := string(expectedBytes) - expectedBytes, err := ioutil.ReadFile(snapshotPath) - assert.NoError(t, err) - expected := string(expectedBytes) + if expected == actual { + break + } - assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual)) + // if the snapshots and we haven't tried all playback speeds different we'll retry at a slower speed + if i == len(speeds)-1 { + assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual)) + } + } }) } } @@ -169,7 +181,7 @@ func gotoRootDirectory() { } } -func runLazygit(t *testing.T, testPath string, rootDir string, record bool) { +func runLazygit(t *testing.T, testPath string, rootDir string, record bool, speed int) { osCommand := oscommands.NewDummyOSCommand() replayPath := filepath.Join(testPath, "recording.json") @@ -193,6 +205,8 @@ func runLazygit(t *testing.T, testPath string, rootDir string, record bool) { cmdStr = fmt.Sprintf("%s --use-config-dir=%s", cmdStr, configDir) cmd := osCommand.ExecutableFromString(cmdStr) + cmd.Env = append(cmd.Env, fmt.Sprintf("REPLAY_SPEED=%d", speed)) + if record { cmd.Env = append( cmd.Env, diff --git a/pkg/gui/recording.go b/pkg/gui/recording.go index ec44398ae..e47dac0ae 100644 --- a/pkg/gui/recording.go +++ b/pkg/gui/recording.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "os" + "strconv" "time" "github.com/jesseduffield/gocui" @@ -38,14 +39,22 @@ func (gui *Gui) replayRecordedEvents() { // might need to add leeway if this ends up flakey var leeway int64 = 0 // humans are slow so this speeds things up. - var speed int64 = 5 + speed := 1 + envReplaySpeed := os.Getenv("REPLAY_SPEED") + if envReplaySpeed != "" { + var err error + speed, err = strconv.Atoi(envReplaySpeed) + if err != nil { + log.Fatal(err) + } + } for _, event := range events { middle: for { select { case <-ticker.C: - now := gui.timeSinceStart()*speed - leeway + now := gui.timeSinceStart()*int64(speed) - leeway if gui.g != nil && now >= event.Timestamp { gui.g.ReplayedEvents <- *event.Event break middle |