2021-04-05 12:37:18 +02:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-04-06 07:20:36 +02:00
|
|
|
"testing"
|
2021-04-05 12:37:18 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Test struct {
|
2021-04-06 01:25:58 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ExtraCmdArgs string `json:"extraCmdArgs"`
|
2021-04-06 10:20:34 +02:00
|
|
|
Skip bool `json:"skip"`
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
// this function is used by both `go test` and from our lazyintegration gui, but
|
|
|
|
// errors need to be handled differently in each (for example go test is always
|
|
|
|
// working with *testing.T) so we pass in any differences as args here.
|
|
|
|
func RunTests(
|
|
|
|
logf func(format string, formatArgs ...interface{}),
|
|
|
|
runCmd func(cmd *exec.Cmd) error,
|
2021-04-06 07:20:36 +02:00
|
|
|
fnWrapper func(test *Test, f func(*testing.T) error),
|
2021-04-06 01:02:01 +02:00
|
|
|
updateSnapshots bool,
|
|
|
|
record bool,
|
|
|
|
speedEnv string,
|
2021-10-22 11:18:40 +02:00
|
|
|
onFail func(t *testing.T, expected string, actual string, prefix string),
|
2021-04-06 10:20:34 +02:00
|
|
|
includeSkipped bool,
|
2021-04-06 01:02:01 +02:00
|
|
|
) error {
|
|
|
|
rootDir := GetRootDirectory()
|
|
|
|
err := os.Chdir(rootDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
testDir := filepath.Join(rootDir, "test", "integration")
|
|
|
|
|
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
2021-12-07 12:59:36 +02:00
|
|
|
err = osCommand.Run(osCommand.NewCmdObj("go build -o " + tempLazygitPath()))
|
2021-04-06 01:02:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tests, err := LoadTests(testDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
|
2021-04-06 10:20:34 +02:00
|
|
|
if test.Skip && !includeSkipped {
|
|
|
|
logf("skipping test: %s", test.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-06 07:20:36 +02:00
|
|
|
fnWrapper(test, func(t *testing.T) error {
|
2021-04-06 01:02:01 +02:00
|
|
|
speeds := getTestSpeeds(test.Speed, updateSnapshots, speedEnv)
|
|
|
|
testPath := filepath.Join(testDir, test.Name)
|
2021-10-22 11:18:40 +02:00
|
|
|
actualRepoDir := filepath.Join(testPath, "actual")
|
|
|
|
expectedRepoDir := filepath.Join(testPath, "expected")
|
|
|
|
actualRemoteDir := filepath.Join(testPath, "actual_remote")
|
|
|
|
expectedRemoteDir := filepath.Join(testPath, "expected_remote")
|
2021-04-06 03:17:32 +02:00
|
|
|
logf("path: %s", testPath)
|
2021-04-06 01:02:01 +02:00
|
|
|
|
|
|
|
// three retries at normal speed for the sake of flakey tests
|
2021-04-08 16:10:51 +02:00
|
|
|
speeds = append(speeds, 1)
|
2021-04-06 01:02:01 +02:00
|
|
|
for i, speed := range speeds {
|
|
|
|
logf("%s: attempting test at speed %f\n", test.Name, speed)
|
|
|
|
|
|
|
|
findOrCreateDir(testPath)
|
2021-10-22 11:18:40 +02:00
|
|
|
prepareIntegrationTestDir(actualRepoDir)
|
|
|
|
removeRemoteDir(actualRemoteDir)
|
|
|
|
err := createFixture(testPath, actualRepoDir)
|
2021-04-06 01:02:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir := filepath.Join(testPath, "used_config")
|
|
|
|
|
2021-04-06 01:25:58 +02:00
|
|
|
cmd, err := getLazygitCommand(testPath, rootDir, record, speed, test.ExtraCmdArgs)
|
2021-04-06 01:02:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = runCmd(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateSnapshots {
|
2021-10-22 11:18:40 +02:00
|
|
|
err = oscommands.CopyDir(actualRepoDir, expectedRepoDir)
|
2021-04-06 01:02:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 03:17:32 +02:00
|
|
|
err = os.Rename(
|
2021-10-22 11:18:40 +02:00
|
|
|
filepath.Join(expectedRepoDir, ".git"),
|
|
|
|
filepath.Join(expectedRepoDir, ".git_keep"),
|
2021-04-06 03:17:32 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-22 11:18:40 +02:00
|
|
|
|
|
|
|
// see if we have a remote dir and if so, copy it over. Otherwise, delete the expected dir because we have no remote folder.
|
|
|
|
if folderExists(actualRemoteDir) {
|
|
|
|
err = oscommands.CopyDir(actualRemoteDir, expectedRemoteDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
removeRemoteDir(expectedRemoteDir)
|
|
|
|
}
|
2021-04-06 01:02:01 +02:00
|
|
|
}
|
|
|
|
|
2021-10-22 11:18:40 +02:00
|
|
|
actualRepo, expectedRepo, err := generateSnapshots(actualRepoDir, expectedRepoDir)
|
2021-04-06 01:02:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:18:40 +02:00
|
|
|
actualRemote := "remote folder does not exist"
|
|
|
|
expectedRemote := "remote folder does not exist"
|
|
|
|
if folderExists(expectedRemoteDir) {
|
|
|
|
actualRemote, expectedRemote, err = generateSnapshotsForRemote(actualRemoteDir, expectedRemoteDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if folderExists(actualRemoteDir) {
|
|
|
|
actualRemote = "remote folder exists"
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedRepo == actualRepo && expectedRemote == actualRemote {
|
2021-04-06 01:02:01 +02:00
|
|
|
logf("%s: success at speed %f\n", test.Name, speed)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the snapshots and we haven't tried all playback speeds different we'll retry at a slower speed
|
|
|
|
if i == len(speeds)-1 {
|
|
|
|
// get the log file and print that
|
|
|
|
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logf("%s", string(bytes))
|
2021-10-22 11:18:40 +02:00
|
|
|
if expectedRepo != actualRepo {
|
|
|
|
onFail(t, expectedRepo, actualRepo, "repo")
|
|
|
|
} else {
|
|
|
|
onFail(t, expectedRemote, actualRemote, "remote")
|
|
|
|
}
|
2021-04-06 01:02:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:18:40 +02:00
|
|
|
func removeRemoteDir(dir string) {
|
|
|
|
err := os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func prepareIntegrationTestDir(actualDir string) {
|
2021-04-05 12:37:18 +02:00
|
|
|
// remove contents of integration test directory
|
|
|
|
dir, err := ioutil.ReadDir(actualDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.Mkdir(actualDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range dir {
|
|
|
|
os.RemoveAll(filepath.Join(actualDir, d.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func createFixture(testPath, actualDir string) error {
|
2021-04-05 12:37:18 +02:00
|
|
|
bashScriptPath := filepath.Join(testPath, "setup.sh")
|
|
|
|
cmd := secureexec.Command("bash", bashScriptPath, actualDir)
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
if _, err := cmd.CombinedOutput(); err != nil {
|
2021-04-05 12:37:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func tempLazygitPath() string {
|
2021-04-05 12:37:18 +02:00
|
|
|
return filepath.Join("/tmp", "lazygit", "test_lazygit")
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func getTestSpeeds(testStartSpeed float64, updateSnapshots bool, speedStr string) []float64 {
|
2021-04-05 12:37:18 +02:00
|
|
|
if updateSnapshots {
|
|
|
|
// have to go at original speed if updating snapshots in case we go to fast and create a junk snapshot
|
2021-04-05 14:18:36 +02:00
|
|
|
return []float64{1.0}
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 00:18:57 +02:00
|
|
|
if speedStr != "" {
|
|
|
|
speed, err := strconv.ParseFloat(speedStr, 64)
|
2021-04-05 12:37:18 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-05 14:18:36 +02:00
|
|
|
return []float64{speed}
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// default is 10, 5, 1
|
2021-04-05 14:18:36 +02:00
|
|
|
startSpeed := 10.0
|
2021-04-05 12:37:18 +02:00
|
|
|
if testStartSpeed != 0 {
|
|
|
|
startSpeed = testStartSpeed
|
|
|
|
}
|
2021-04-05 14:18:36 +02:00
|
|
|
speeds := []float64{startSpeed}
|
2021-04-05 12:37:18 +02:00
|
|
|
if startSpeed > 5 {
|
|
|
|
speeds = append(speeds, 5)
|
|
|
|
}
|
|
|
|
speeds = append(speeds, 1)
|
|
|
|
|
|
|
|
return speeds
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadTests(testDir string) ([]*Test, error) {
|
|
|
|
paths, err := filepath.Glob(filepath.Join(testDir, "/*/test.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := make([]*Test, len(paths))
|
|
|
|
|
|
|
|
for i, path := range paths {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
test := &Test{}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func findOrCreateDir(path string) {
|
2021-04-05 12:37:18 +02:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func generateSnapshot(dir string) (string, error) {
|
2021-04-05 12:37:18 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
|
|
|
|
|
|
|
_, err := os.Stat(filepath.Join(dir, ".git"))
|
|
|
|
if err != nil {
|
|
|
|
return "git directory not found", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot := ""
|
|
|
|
|
2021-04-06 03:17:32 +02:00
|
|
|
cmdStrs := []string{
|
|
|
|
fmt.Sprintf(`git -C %s status`, dir), // file tree
|
|
|
|
fmt.Sprintf(`git -C %s log --pretty=%%B -p -1`, dir), // log
|
|
|
|
fmt.Sprintf(`git -C %s tag -n`, dir), // tags
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 03:17:32 +02:00
|
|
|
for _, cmdStr := range cmdStrs {
|
|
|
|
// ignoring error for now. If there's an error it could be that there are no results
|
2021-12-07 12:59:36 +02:00
|
|
|
output, _ := osCommand.RunWithOutput(osCommand.NewCmdObj(cmdStr))
|
2021-04-05 12:37:18 +02:00
|
|
|
|
2021-04-06 03:17:32 +02:00
|
|
|
snapshot += output + "\n"
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
if f.Name() == ".git" {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot += string(bytes) + "\n"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
func generateSnapshots(actualDir string, expectedDir string) (string, string, error) {
|
|
|
|
actual, err := generateSnapshot(actualDir)
|
2021-04-05 12:37:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// git refuses to track .git folders in subdirectories so we need to rename it
|
|
|
|
// to git_keep after running a test, and then change it back again
|
|
|
|
defer func() {
|
|
|
|
err = os.Rename(
|
|
|
|
filepath.Join(expectedDir, ".git"),
|
|
|
|
filepath.Join(expectedDir, ".git_keep"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ignoring this error because we might not have a .git_keep file here yet.
|
|
|
|
_ = os.Rename(
|
|
|
|
filepath.Join(expectedDir, ".git_keep"),
|
|
|
|
filepath.Join(expectedDir, ".git"),
|
|
|
|
)
|
|
|
|
|
2021-04-06 01:02:01 +02:00
|
|
|
expected, err := generateSnapshot(expectedDir)
|
2021-04-05 12:37:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return actual, expected, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:18:40 +02:00
|
|
|
func generateSnapshotsForRemote(actualDir string, expectedDir string) (string, string, error) {
|
|
|
|
actual, err := generateSnapshot(actualDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := generateSnapshot(expectedDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return actual, expected, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:25:58 +02:00
|
|
|
func getLazygitCommand(testPath string, rootDir string, record bool, speed float64, extraCmdArgs string) (*exec.Cmd, error) {
|
2021-04-05 12:37:18 +02:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
|
|
|
|
|
|
|
replayPath := filepath.Join(testPath, "recording.json")
|
|
|
|
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
|
|
|
|
actualDir := filepath.Join(testPath, "actual")
|
|
|
|
|
|
|
|
exists, err := osCommand.FileExists(filepath.Join(testPath, "config"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
templateConfigDir = filepath.Join(testPath, "config")
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir := filepath.Join(testPath, "used_config")
|
|
|
|
|
|
|
|
err = os.RemoveAll(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = oscommands.CopyDir(templateConfigDir, configDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-06 01:25:58 +02:00
|
|
|
cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), configDir, actualDir, extraCmdArgs)
|
2021-04-05 12:37:18 +02:00
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
cmdObj := osCommand.NewCmdObj(cmdStr)
|
|
|
|
cmdObj.AddEnvVars(fmt.Sprintf("SPEED=%f", speed))
|
2021-04-05 12:37:18 +02:00
|
|
|
|
|
|
|
if record {
|
2021-12-07 12:59:36 +02:00
|
|
|
cmdObj.AddEnvVars(fmt.Sprintf("RECORD_EVENTS_TO=%s", replayPath))
|
2021-04-05 12:37:18 +02:00
|
|
|
} else {
|
2021-12-07 12:59:36 +02:00
|
|
|
cmdObj.AddEnvVars(fmt.Sprintf("REPLAY_EVENTS_FROM=%s", replayPath))
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
return cmdObj.GetCmd(), nil
|
2021-04-05 12:37:18 +02:00
|
|
|
}
|
2021-10-22 11:18:40 +02:00
|
|
|
|
|
|
|
func folderExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return err == nil
|
|
|
|
}
|