refactor integration tests

This commit is contained in:
Jesse Duffield 2022-03-27 11:47:07 +11:00
parent 2b3d457aa4
commit 20ec6d98ad
3991 changed files with 1205 additions and 968 deletions

1
.gitignore vendored
View file

@ -33,7 +33,6 @@ lazygit.exe
test/git_server/data test/git_server/data
test/integration/*/actual/ test/integration/*/actual/
test/integration/*/actual_remote/
test/integration/*/used_config/ test/integration/*/used_config/
# these sample hooks waste too much space # these sample hooks waste too much space
test/integration/*/expected/**/hooks/ test/integration/*/expected/**/hooks/

View file

@ -13,6 +13,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/secureexec"
) )
@ -97,11 +98,9 @@ func RunTests(
speeds := getTestSpeeds(test.Speed, mode, speedEnv) speeds := getTestSpeeds(test.Speed, mode, speedEnv)
testPath := filepath.Join(testDir, test.Name) testPath := filepath.Join(testDir, test.Name)
actualRepoDir := filepath.Join(testPath, "actual") actualDir := filepath.Join(testPath, "actual")
expectedRepoDir := filepath.Join(testPath, "expected") expectedDir := filepath.Join(testPath, "expected")
actualRemoteDir := filepath.Join(testPath, "actual_remote") actualRepoDir := filepath.Join(actualDir, "repo")
expectedRemoteDir := filepath.Join(testPath, "expected_remote")
otherRepoDir := filepath.Join(testPath, "other_repo")
logf("path: %s", testPath) logf("path: %s", testPath)
for i, speed := range speeds { for i, speed := range speeds {
@ -110,9 +109,8 @@ func RunTests(
} }
findOrCreateDir(testPath) findOrCreateDir(testPath)
prepareIntegrationTestDir(actualRepoDir) prepareIntegrationTestDir(actualDir)
removeDir(otherRepoDir) findOrCreateDir(actualRepoDir)
removeDir(actualRemoteDir)
err := createFixture(testPath, actualRepoDir) err := createFixture(testPath, actualRepoDir)
if err != nil { if err != nil {
return err return err
@ -130,72 +128,66 @@ func RunTests(
return err return err
} }
// submodule tests currently make use of a repo called 'other_repo' but we don't want that
// to stick around. Long-term we should have an 'actual' folder which itself contains
// repos, and there we can put the 'repo' repo which is the main one, alongside
// any others that we use as part of the test (including remotes). Then we'll do snapshots for
// each of them.
removeDir(otherRepoDir)
if mode == UPDATE_SNAPSHOT || mode == RECORD { if mode == UPDATE_SNAPSHOT || mode == RECORD {
// create/update snapshot // create/update snapshot
err = oscommands.CopyDir(actualRepoDir, expectedRepoDir) err = oscommands.CopyDir(actualDir, expectedDir)
if err != nil { if err != nil {
return err return err
} }
if err := renameGitDirs(expectedRepoDir); err != nil { if err := renameGitDirs(expectedDir); err != nil {
return err return err
} }
// 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 {
removeDir(expectedRemoteDir)
}
logf("%s", "updated snapshot") logf("%s", "updated snapshot")
} else { } else {
// compare result to snapshot if err := validateSameRepos(expectedDir, actualDir); err != nil {
actualRepo, expectedRepo, err := generateSnapshots(actualRepoDir, expectedRepoDir) return err
}
// iterate through each repo in the expected dir and comparet to the corresponding repo in the actual dir
expectedFiles, err := ioutil.ReadDir(expectedDir)
if err != nil { if err != nil {
return err return err
} }
actualRemote := "remote folder does not exist" success := true
expectedRemote := "remote folder does not exist" for _, f := range expectedFiles {
if folderExists(expectedRemoteDir) { if !f.IsDir() {
actualRemote, expectedRemote, err = generateSnapshotsForRemote(actualRemoteDir, expectedRemoteDir) return errors.New("unexpected file (as opposed to directory) in integration test 'expected' directory")
}
// get corresponding file name from actual dir
actualRepoPath := filepath.Join(actualDir, f.Name())
expectedRepoPath := filepath.Join(expectedDir, f.Name())
actualRepo, expectedRepo, err := generateSnapshots(actualRepoPath, expectedRepoPath)
if err != nil { if err != nil {
return err return err
} }
} else if folderExists(actualRemoteDir) {
actualRemote = "remote folder exists" if expectedRepo != actualRepo {
success = false
// if the snapshot doesn't match and we haven't tried all playback speeds different we'll retry at a slower speed
if i < len(speeds)-1 {
break
}
// get the log file and print it
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
if err != nil {
return err
}
logf("%s", string(bytes))
onFail(t, expectedRepo, actualRepo, f.Name())
}
} }
if expectedRepo == actualRepo && expectedRemote == actualRemote { if success {
logf("%s: success at speed %f\n", test.Name, speed) logf("%s: success at speed %f\n", test.Name, speed)
break break
} }
// if the snapshot doesn't match 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))
if expectedRepo != actualRepo {
onFail(t, expectedRepo, actualRepo, "repo")
} else {
onFail(t, expectedRemote, actualRemote, "remote")
}
}
} }
} }
@ -206,11 +198,31 @@ func RunTests(
return nil return nil
} }
func removeDir(dir string) { // validates that the actual and expected dirs have the same repo names (doesn't actually check the contents of the repos)
err := os.RemoveAll(dir) func validateSameRepos(expectedDir string, actualDir string) error {
// iterate through each repo in the expected dir and comparet to the corresponding repo in the actual dir
expectedFiles, err := ioutil.ReadDir(expectedDir)
if err != nil { if err != nil {
panic(err) return err
} }
var actualFiles []os.FileInfo
actualFiles, err = ioutil.ReadDir(actualDir)
if err != nil {
return err
}
expectedFileNames := slices.Map(expectedFiles, getFileName)
actualFileNames := slices.Map(actualFiles, getFileName)
if !slices.Equal(expectedFileNames, actualFileNames) {
return fmt.Errorf("expected and actual repo dirs do not match: expected: %s, actual: %s", expectedFileNames, actualFileNames)
}
return nil
}
func getFileName(f os.FileInfo) string {
return f.Name()
} }
func prepareIntegrationTestDir(actualDir string) { func prepareIntegrationTestDir(actualDir string) {
@ -357,7 +369,9 @@ func generateSnapshot(dir string) (string, error) {
snapshot := "" snapshot := ""
cmdStrs := []string{ cmdStrs := []string{
`remote show -n origin`, // remote branches `remote show -n origin`, // remote branches
// TOOD: find a way to bring this back without breaking tests
// `ls-remote origin`,
`status`, // file tree `status`, // file tree
`log --pretty=%B -p -1`, // log `log --pretty=%B -p -1`, // log
`tag -n`, // tags `tag -n`, // tags
@ -493,26 +507,12 @@ func restoreGitDirs(dir string) error {
return nil return nil
} }
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
}
func getLazygitCommand(testPath string, rootDir string, mode Mode, speed float64, extraCmdArgs string) (*exec.Cmd, error) { func getLazygitCommand(testPath string, rootDir string, mode Mode, speed float64, extraCmdArgs string) (*exec.Cmd, error) {
osCommand := oscommands.NewDummyOSCommand() osCommand := oscommands.NewDummyOSCommand()
replayPath := filepath.Join(testPath, "recording.json") replayPath := filepath.Join(testPath, "recording.json")
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config") templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
actualDir := filepath.Join(testPath, "actual") actualRepoDir := filepath.Join(testPath, "actual", "repo")
exists, err := osCommand.FileExists(filepath.Join(testPath, "config")) exists, err := osCommand.FileExists(filepath.Join(testPath, "config"))
if err != nil { if err != nil {
@ -534,7 +534,7 @@ func getLazygitCommand(testPath string, rootDir string, mode Mode, speed float64
return nil, err return nil, err
} }
cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), configDir, actualDir, extraCmdArgs) cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), configDir, actualRepoDir, extraCmdArgs)
cmdObj := osCommand.Cmd.New(cmdStr) cmdObj := osCommand.Cmd.New(cmdStr)
cmdObj.AddEnvVars(fmt.Sprintf("SPEED=%f", speed)) cmdObj.AddEnvVars(fmt.Sprintf("SPEED=%f", speed))
@ -548,8 +548,3 @@ func getLazygitCommand(testPath string, rootDir string, mode Mode, speed float64
return cmdObj.GetCmd(), nil return cmdObj.GetCmd(), nil
} }
func folderExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

View file

@ -14,6 +14,8 @@ echo -n "Username for 'github': "
read username read username
echo -n "Password for 'github': " echo -n "Password for 'github': "
# this will print the password to the log view but real git won't do that.
# We could use read -s but that's not POSIX compliant.
read password read password
if [ "$username" = "username" -a "$password" = "password" ]; then if [ "$username" = "username" -a "$password" = "password" ]; then

Some files were not shown because too many files have changed in this diff Show more