1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-05 13:24:54 +02:00
lazygit/pkg/integration/integration.go

184 lines
4.5 KiB
Go
Raw Normal View History

2021-04-05 20:37:18 +10:00
package integration
import (
2022-01-26 16:46:54 +11:00
"errors"
2021-04-05 20:37:18 +10:00
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
2021-04-05 20:37:18 +10:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2022-08-07 22:09:39 +10:00
"github.com/jesseduffield/lazygit/pkg/integration/integration_tests"
"github.com/jesseduffield/lazygit/pkg/integration/types"
2021-04-05 20:37:18 +10:00
)
2022-08-07 22:09:39 +10:00
// this is the integration runner for the new and improved integration interface
2022-05-07 15:42:36 +10:00
2022-08-07 22:09:39 +10:00
// re-exporting this so that clients only need to import one package
var Tests = integration_tests.Tests
2022-08-07 22:09:39 +10:00
func RunTestsNew(
2021-04-06 09:02:01 +10:00
logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error,
2022-08-07 22:09:39 +10:00
fnWrapper func(test types.Test, f func(*testing.T) error),
mode Mode,
2021-10-22 20:18:40 +11:00
onFail func(t *testing.T, expected string, actual string, prefix string),
includeSkipped bool,
2021-04-06 09:02:01 +10:00
) error {
rootDir := GetRootDirectory()
err := os.Chdir(rootDir)
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
testDir := filepath.Join(rootDir, "test", "integration_new")
2021-04-06 09:02:01 +10:00
osCommand := oscommands.NewDummyOSCommand()
2021-12-29 14:33:38 +11:00
err = osCommand.Cmd.New("go build -o " + tempLazygitPath()).Run()
2021-04-06 09:02:01 +10:00
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
for _, test := range Tests {
2021-04-06 09:02:01 +10:00
test := test
fnWrapper(test, func(t *testing.T) error { //nolint: thelper
2022-08-07 22:09:39 +10:00
if test.Skip() && !includeSkipped {
logf("skipping test: %s", test.Name())
2022-03-26 21:18:10 +11:00
return nil
}
2022-08-07 22:09:39 +10:00
testPath := filepath.Join(testDir, test.Name())
2022-03-27 11:47:07 +11:00
actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected")
actualRepoDir := filepath.Join(actualDir, "repo")
logf("path: %s", testPath)
2021-04-06 09:02:01 +10:00
2022-08-07 22:09:39 +10:00
findOrCreateDir(testPath)
prepareIntegrationTestDir(actualDir)
findOrCreateDir(actualRepoDir)
err := createFixtureNew(test, actualRepoDir, rootDir)
if err != nil {
return err
}
configDir := filepath.Join(testPath, "used_config")
2021-04-06 09:02:01 +10:00
2022-08-07 22:09:39 +10:00
cmd, err := getLazygitCommandNew(test, testPath, rootDir)
if err != nil {
return err
}
err = runCmd(cmd)
if err != nil {
return err
}
if mode == UPDATE_SNAPSHOT {
// create/update snapshot
err = oscommands.CopyDir(actualDir, expectedDir)
2021-04-06 09:02:01 +10:00
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
if err := renameSpecialPaths(expectedDir); err != nil {
return err
}
2021-04-06 09:02:01 +10:00
2022-08-07 22:09:39 +10:00
logf("%s", "updated snapshot")
} else {
if err := validateSameRepos(expectedDir, actualDir); err != nil {
2021-04-06 09:02:01 +10:00
return err
}
2022-08-07 22:09:39 +10:00
// iterate through each repo in the expected dir and comparet to the corresponding repo in the actual dir
expectedFiles, err := ioutil.ReadDir(expectedDir)
2021-04-06 09:02:01 +10:00
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
for _, f := range expectedFiles {
if !f.IsDir() {
return errors.New("unexpected file (as opposed to directory) in integration test 'expected' directory")
2021-04-06 09:02:01 +10:00
}
2022-01-28 21:56:01 +11:00
2022-08-07 22:09:39 +10:00
// get corresponding file name from actual dir
actualRepoPath := filepath.Join(actualDir, f.Name())
expectedRepoPath := filepath.Join(expectedDir, f.Name())
2021-10-22 20:18:40 +11:00
2022-08-07 22:09:39 +10:00
actualRepo, expectedRepo, err := generateSnapshots(actualRepoPath, expectedRepoPath)
2021-10-22 20:18:40 +11:00
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
if expectedRepo != actualRepo {
// get the log file and print it
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
if err != nil {
return err
}
2022-08-07 22:09:39 +10:00
logf("%s", string(bytes))
2022-03-27 11:47:07 +11:00
2022-08-07 22:09:39 +10:00
onFail(t, expectedRepo, actualRepo, f.Name())
2022-03-27 11:47:07 +11:00
}
2021-04-06 09:02:01 +10:00
}
}
2022-08-07 22:09:39 +10:00
logf("test passed: %s", test.Name())
2021-04-06 09:02:01 +10:00
return nil
})
}
return nil
}
2022-08-07 22:09:39 +10:00
func createFixtureNew(test types.Test, actualDir string, rootDir string) error {
if err := os.Chdir(actualDir); err != nil {
2021-04-05 20:37:18 +10:00
panic(err)
}
2022-08-07 22:09:39 +10:00
shell := &ShellImpl{}
shell.RunCommand("git init")
shell.RunCommand(`git config user.email "CI@example.com"`)
shell.RunCommand(`git config user.name "CI"`)
2021-04-05 20:37:18 +10:00
2022-08-07 22:09:39 +10:00
test.SetupRepo(shell)
2021-04-05 20:37:18 +10:00
2022-08-07 22:09:39 +10:00
// changing directory back to rootDir after the setup is done
if err := os.Chdir(rootDir); err != nil {
2022-01-28 21:56:01 +11:00
panic(err)
}
return nil
}
2022-08-07 22:09:39 +10:00
func getLazygitCommandNew(test types.Test, testPath string, rootDir string) (*exec.Cmd, error) {
2021-04-05 20:37:18 +10:00
osCommand := oscommands.NewDummyOSCommand()
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
2022-03-27 11:47:07 +11:00
actualRepoDir := filepath.Join(testPath, "actual", "repo")
2021-04-05 20:37:18 +10:00
configDir := filepath.Join(testPath, "used_config")
2022-08-07 22:09:39 +10:00
err := os.RemoveAll(configDir)
2021-04-05 20:37:18 +10:00
if err != nil {
return nil, err
}
err = oscommands.CopyDir(templateConfigDir, configDir)
if err != nil {
return nil, err
}
2022-08-07 22:09:39 +10:00
cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), configDir, actualRepoDir, test.ExtraCmdArgs())
2021-04-05 20:37:18 +10:00
2021-12-29 14:33:38 +11:00
cmdObj := osCommand.Cmd.New(cmdStr)
2021-04-05 20:37:18 +10:00
2022-08-07 22:09:39 +10:00
cmdObj.AddEnvVars(fmt.Sprintf("LAZYGIT_TEST_NAME=%s", test.Name()))
2021-04-05 20:37:18 +10:00
2021-12-07 21:59:36 +11:00
return cmdObj.GetCmd(), nil
2021-04-05 20:37:18 +10:00
}