1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/gui_test.go

122 lines
4.0 KiB
Go
Raw Normal View History

2020-10-04 09:41:33 +02:00
package gui
import (
"fmt"
2021-04-05 05:07:25 +02:00
"io"
2020-10-04 09:41:33 +02:00
"io/ioutil"
"os"
"path/filepath"
"testing"
2021-04-05 05:07:25 +02:00
"github.com/creack/pty"
2020-10-04 09:41:33 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-04-05 12:37:18 +02:00
"github.com/jesseduffield/lazygit/pkg/integration"
2020-10-04 09:41:33 +02:00
"github.com/stretchr/testify/assert"
)
2021-04-05 12:37:18 +02:00
// This file is quite similar to integration/main.go. The main difference is that this file is
// run via `go test` whereas the other is run via `test/lazyintegration/main.go` which provides
// a convenient gui wrapper around our integration tests. The `go test` approach is better
// for CI and for running locally in the background to ensure you haven't broken
// anything while making changes. If you want to visually see what's happening when a test is run,
// you'll need to take the other approach
2020-10-04 09:41:33 +02:00
//
2021-04-05 12:37:18 +02:00
// As for this file, to run an integration test, e.g. for test 'commit', go:
// go test pkg/gui/gui_test.go -run /commit
2020-10-04 09:41:33 +02:00
//
2020-10-05 12:00:31 +02:00
// To update a snapshot for an integration test, pass UPDATE_SNAPSHOTS=true
// UPDATE_SNAPSHOTS=true go test pkg/gui/gui_test.go -run /commit
2020-10-04 09:41:33 +02:00
//
2021-04-05 12:37:18 +02:00
// integration tests are run in test/integration/<test_name>/actual and the final test does
2020-10-04 09:41:33 +02:00
// not clean up that directory so you can cd into it to see for yourself what
2021-04-05 12:37:18 +02:00
// happened when a test fails.
2020-10-04 09:41:33 +02:00
//
2020-10-06 00:16:16 +02:00
// 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 may be a decimal.
2020-10-04 09:41:33 +02:00
func Test(t *testing.T) {
2021-04-05 12:37:18 +02:00
rootDir := integration.GetRootDirectory()
err := os.Chdir(rootDir)
assert.NoError(t, err)
testDir := filepath.Join(rootDir, "test", "integration")
osCommand := oscommands.NewDummyOSCommand()
2021-04-05 12:37:18 +02:00
err = osCommand.RunCommand("go build -o %s", integration.TempLazygitPath())
assert.NoError(t, err)
2021-04-05 12:37:18 +02:00
tests, err := integration.LoadTests(testDir)
assert.NoError(t, err)
2020-10-04 09:41:33 +02:00
2021-04-05 12:37:18 +02:00
record := false
2020-10-05 12:00:31 +02:00
updateSnapshots := record || os.Getenv("UPDATE_SNAPSHOTS") != ""
2020-10-04 09:41:33 +02:00
for _, test := range tests {
test := test
2020-10-05 12:00:31 +02:00
t.Run(test.Name, func(t *testing.T) {
2021-04-06 00:18:57 +02:00
speedEnv := os.Getenv("SPEED")
speeds := integration.GetTestSpeeds(test.Speed, updateSnapshots, speedEnv)
2021-04-05 12:37:18 +02:00
testPath := filepath.Join(testDir, test.Name)
actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected")
t.Logf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir)
2020-10-05 12:00:31 +02:00
2021-04-05 13:37:54 +02:00
// 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 %f\n", test.Name, speed)
2020-10-04 09:41:33 +02:00
2021-04-05 12:37:18 +02:00
integration.FindOrCreateDir(testPath)
integration.PrepareIntegrationTestDir(actualDir)
err := integration.CreateFixture(testPath, actualDir)
assert.NoError(t, err)
2020-10-04 09:41:33 +02:00
2021-04-05 04:18:21 +02:00
configDir := filepath.Join(testPath, "used_config")
2021-04-05 12:37:18 +02:00
cmd, err := integration.GetLazygitCommand(testPath, rootDir, record, speed)
assert.NoError(t, err)
2020-10-04 09:41:33 +02:00
2021-04-05 12:37:18 +02:00
cmd.Env = append(
cmd.Env,
"HEADLESS=true",
"TERM=xterm",
)
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 100, Cols: 100})
assert.NoError(t, err)
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
_, _ = io.Copy(ioutil.Discard, f)
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
assert.NoError(t, err)
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
_ = f.Close()
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
if updateSnapshots {
err = oscommands.CopyDir(actualDir, expectedDir)
assert.NoError(t, err)
}
2020-10-06 00:23:09 +02:00
2021-04-05 12:37:18 +02:00
actual, expected, err := integration.GenerateSnapshots(actualDir, expectedDir)
assert.NoError(t, err)
2020-10-04 09:41:33 +02:00
if expected == actual {
t.Logf("%s: success at speed %f\n", test.Name, speed)
break
}
2020-10-04 09:41:33 +02:00
// if the snapshots and we haven't tried all playback speeds different we'll retry at a slower speed
if i == len(speeds)-1 {
2021-04-05 04:18:21 +02:00
// get the log file and print that
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
assert.NoError(t, err)
t.Log(string(bytes))
assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual))
}
}
2020-10-04 09:41:33 +02:00
})
}
}