2020-10-04 09:41:33 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-04 13:05:39 +02:00
|
|
|
"io"
|
2020-10-04 09:41:33 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-10-06 00:16:16 +02:00
|
|
|
"strconv"
|
2020-10-04 09:41:33 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-10-04 13:05:39 +02:00
|
|
|
"github.com/creack/pty"
|
2020-10-04 09:41:33 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// To run an integration test, e.g. for test 'commit', go:
|
|
|
|
// go test pkg/gui/gui_test.go -run /commit
|
|
|
|
//
|
|
|
|
// To record keypresses for an integration test, pass RECORD_EVENTS=true like so:
|
|
|
|
// RECORD_EVENTS=true go test pkg/gui/gui_test.go -run /commit
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
// When RECORD_EVENTS is true, updates will be updated automatically
|
|
|
|
//
|
|
|
|
// integration tests are run in test/integration_test and the final test does
|
|
|
|
// not clean up that directory so you can cd into it to see for yourself what
|
|
|
|
// happened when a test failed.
|
|
|
|
//
|
2020-10-06 00:16:16 +02:00
|
|
|
// To run tests in parallel pass `PARALLEL=true` as an env var. Tests are run in parallel
|
|
|
|
// on CI, and are run in a pty so you won't be able to see the stdout of the program
|
|
|
|
//
|
|
|
|
// 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.
|
2020-10-04 09:41:33 +02:00
|
|
|
|
|
|
|
type integrationTest struct {
|
2020-10-05 23:01:25 +02:00
|
|
|
name string
|
|
|
|
fixture string
|
|
|
|
startSpeed int
|
2020-10-04 09:41:33 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 13:05:39 +02:00
|
|
|
func tests() []integrationTest {
|
|
|
|
return []integrationTest{
|
|
|
|
{
|
2020-10-05 23:01:25 +02:00
|
|
|
name: "commit",
|
|
|
|
fixture: "newFile",
|
2020-10-06 00:16:16 +02:00
|
|
|
startSpeed: 20,
|
2020-10-04 13:05:39 +02:00
|
|
|
},
|
|
|
|
{
|
2020-10-06 00:16:16 +02:00
|
|
|
name: "squash",
|
|
|
|
fixture: "manyCommits",
|
|
|
|
startSpeed: 6,
|
2020-10-04 13:05:39 +02:00
|
|
|
},
|
|
|
|
{
|
2020-10-05 23:01:25 +02:00
|
|
|
name: "patchBuilding",
|
|
|
|
fixture: "updatedFile",
|
|
|
|
startSpeed: 3,
|
2020-10-04 13:05:39 +02:00
|
|
|
},
|
2020-10-05 00:08:43 +02:00
|
|
|
{
|
2020-10-05 23:01:25 +02:00
|
|
|
name: "patchBuilding2",
|
|
|
|
fixture: "updatedFile",
|
|
|
|
startSpeed: 3,
|
2020-10-05 00:08:43 +02:00
|
|
|
},
|
2020-10-05 11:09:34 +02:00
|
|
|
{
|
|
|
|
name: "mergeConflicts",
|
|
|
|
fixture: "mergeConflicts",
|
|
|
|
},
|
2020-10-05 11:18:53 +02:00
|
|
|
{
|
|
|
|
name: "searching",
|
|
|
|
fixture: "newFile",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "searchingInStagingPanel",
|
|
|
|
fixture: "newFile2",
|
|
|
|
},
|
2020-10-04 13:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
func generateSnapshot(t *testing.T, actualDir string) string {
|
2020-10-04 09:41:33 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
2020-10-05 23:01:25 +02:00
|
|
|
cmd := fmt.Sprintf(`bash -c "cd %s && git status; cat ./*; git log --pretty=%%B -p"`, actualDir)
|
|
|
|
|
|
|
|
// need to copy from current directory to
|
2020-10-04 09:41:33 +02:00
|
|
|
|
|
|
|
snapshot, err := osCommand.RunCommandWithOutput(cmd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func findOrCreateDir(path string) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:16:16 +02:00
|
|
|
func getTestSpeeds(testStartSpeed int, updateSnapshots bool) []int {
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
speedEnv := os.Getenv("SPEED")
|
|
|
|
if speedEnv != "" {
|
|
|
|
speed, err := strconv.Atoi(speedEnv)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return []int{speed}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is 10, 5, 1
|
|
|
|
startSpeed := 10
|
|
|
|
if testStartSpeed != 0 {
|
|
|
|
startSpeed = testStartSpeed
|
|
|
|
}
|
|
|
|
speeds := []int{startSpeed}
|
|
|
|
if startSpeed > 5 {
|
|
|
|
speeds = append(speeds, 5)
|
|
|
|
}
|
|
|
|
speeds = append(speeds, 1)
|
|
|
|
|
|
|
|
return speeds
|
|
|
|
}
|
|
|
|
|
2020-10-04 09:41:33 +02:00
|
|
|
func Test(t *testing.T) {
|
2020-10-04 13:05:39 +02:00
|
|
|
tests := tests()
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
rootDir := getRootDirectory()
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 12:00:31 +02:00
|
|
|
record := os.Getenv("RECORD_EVENTS") != ""
|
|
|
|
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
|
|
|
|
2020-10-04 09:41:33 +02:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-10-06 00:16:16 +02:00
|
|
|
if runInParallel() {
|
2020-10-05 23:01:25 +02:00
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:16:16 +02:00
|
|
|
speeds := getTestSpeeds(test.startSpeed, updateSnapshots)
|
2020-10-05 12:00:31 +02:00
|
|
|
|
2020-10-05 11:55:15 +02:00
|
|
|
for i, speed := range speeds {
|
2020-10-05 12:00:31 +02:00
|
|
|
t.Logf("%s: attempting test at speed %d\n", test.name, speed)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 11:55:15 +02:00
|
|
|
testPath := filepath.Join(rootDir, "test", "integration", test.name)
|
2020-10-05 23:01:25 +02:00
|
|
|
actualDir := filepath.Join(testPath, "actual")
|
2020-10-06 00:01:40 +02:00
|
|
|
expectedDir := filepath.Join(testPath, "expected")
|
2020-10-05 11:55:15 +02:00
|
|
|
findOrCreateDir(testPath)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
prepareIntegrationTestDir(testPath)
|
2020-10-05 11:55:15 +02:00
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
err := createFixture(rootDir, test.fixture, actualDir)
|
2020-10-05 11:55:15 +02:00
|
|
|
assert.NoError(t, err)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 11:55:15 +02:00
|
|
|
runLazygit(t, testPath, rootDir, record, speed)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
actual := generateSnapshot(t, actualDir)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 12:00:31 +02:00
|
|
|
if updateSnapshots {
|
2020-10-06 00:01:40 +02:00
|
|
|
err = oscommands.CopyDir(actualDir, expectedDir)
|
2020-10-05 11:55:15 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-06 00:01:40 +02:00
|
|
|
expected := generateSnapshot(t, expectedDir)
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 11:55:15 +02:00
|
|
|
if expected == actual {
|
2020-10-05 23:01:25 +02:00
|
|
|
t.Logf("%s: success at speed %d\n", test.name, speed)
|
2020-10-05 11:55:15 +02:00
|
|
|
break
|
|
|
|
}
|
2020-10-04 09:41:33 +02:00
|
|
|
|
2020-10-05 11:55:15 +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 {
|
|
|
|
assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual))
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 09:41:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
func createFixture(rootDir string, name string, actualDir string) error {
|
2020-10-04 12:34:32 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
2020-10-05 23:01:25 +02:00
|
|
|
cmd := exec.Command("bash", filepath.Join(rootDir, "test", "fixtures", fmt.Sprintf("%s.sh", name)), actualDir)
|
2020-10-04 12:34:32 +02:00
|
|
|
|
|
|
|
if err := osCommand.RunExecutable(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
func getRootDirectory() string {
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-10-04 09:41:33 +02:00
|
|
|
for {
|
2020-10-05 23:01:25 +02:00
|
|
|
_, err := os.Stat(filepath.Join(path, ".git"))
|
2020-10-04 09:41:33 +02:00
|
|
|
|
|
|
|
if err == nil {
|
2020-10-05 23:01:25 +02:00
|
|
|
return path
|
2020-10-04 09:41:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
path = filepath.Dir(path)
|
|
|
|
|
|
|
|
if path == "/" {
|
|
|
|
panic("must run in lazygit folder or child folder")
|
2020-10-04 09:41:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:55:15 +02:00
|
|
|
func runLazygit(t *testing.T, testPath string, rootDir string, record bool, speed int) {
|
2020-10-04 09:41:33 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
|
|
|
|
2020-10-04 13:05:39 +02:00
|
|
|
replayPath := filepath.Join(testPath, "recording.json")
|
|
|
|
cmdStr := fmt.Sprintf("go run %s", filepath.Join(rootDir, "main.go"))
|
|
|
|
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
|
2020-10-05 23:01:25 +02:00
|
|
|
actualDir := filepath.Join(testPath, "actual")
|
2020-10-04 13:05:39 +02:00
|
|
|
|
|
|
|
exists, err := osCommand.FileExists(filepath.Join(testPath, "config"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
templateConfigDir = filepath.Join(testPath, "config")
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
configDir := filepath.Join(testPath, "used_config")
|
2020-10-04 13:05:39 +02:00
|
|
|
|
|
|
|
err = os.RemoveAll(configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = oscommands.CopyDir(templateConfigDir, configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-10-05 23:01:25 +02:00
|
|
|
cmdStr = fmt.Sprintf("%s --use-config-dir=%s --path=%s", cmdStr, configDir, actualDir)
|
2020-10-04 13:05:39 +02:00
|
|
|
|
|
|
|
cmd := osCommand.ExecutableFromString(cmdStr)
|
2020-10-05 11:55:15 +02:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("REPLAY_SPEED=%d", speed))
|
|
|
|
|
2020-10-04 09:41:33 +02:00
|
|
|
if record {
|
|
|
|
cmd.Env = append(
|
|
|
|
cmd.Env,
|
|
|
|
fmt.Sprintf("RECORD_EVENTS_TO=%s", replayPath),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
cmd.Env = append(
|
|
|
|
cmd.Env,
|
|
|
|
fmt.Sprintf("REPLAY_EVENTS_FROM=%s", replayPath),
|
|
|
|
)
|
|
|
|
}
|
2020-10-04 13:05:39 +02:00
|
|
|
|
|
|
|
// if we're on CI we'll need to use a PTY. We can work that out by seeing if the 'TERM' env is defined.
|
2020-10-06 00:16:16 +02:00
|
|
|
if runInParallel() {
|
2020-10-04 13:05:39 +02:00
|
|
|
cmd.Env = append(cmd.Env, "TERM=xterm")
|
|
|
|
|
|
|
|
f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 100, Cols: 100})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-10-05 12:00:31 +02:00
|
|
|
_, _ = io.Copy(ioutil.Discard, f)
|
|
|
|
|
2020-10-04 13:05:39 +02:00
|
|
|
assert.NoError(t, err)
|
2020-10-05 12:00:31 +02:00
|
|
|
|
|
|
|
_ = f.Close()
|
2020-10-04 13:05:39 +02:00
|
|
|
} else {
|
|
|
|
err := osCommand.RunExecutable(cmd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-10-04 09:41:33 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 00:16:16 +02:00
|
|
|
func runInParallel() bool {
|
|
|
|
return os.Getenv("PARALLEL") != "" || os.Getenv("TERM") == ""
|
2020-10-05 23:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareIntegrationTestDir(testPath string) {
|
|
|
|
path := filepath.Join(testPath, "actual")
|
2020-10-04 09:41:33 +02:00
|
|
|
|
|
|
|
// remove contents of integration test directory
|
|
|
|
dir, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.Mkdir(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range dir {
|
|
|
|
os.RemoveAll(filepath.Join(path, d.Name()))
|
|
|
|
}
|
|
|
|
}
|