1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/integration/components/runner.go

236 lines
5.4 KiB
Go
Raw Normal View History

2022-08-14 03:24:07 +02:00
package components
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/jesseduffield/lazycore/pkg/utils"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2022-08-14 03:24:07 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
2022-08-14 06:33:44 +02:00
const (
2023-02-12 01:21:13 +02:00
TEST_NAME_ENV_VAR = "TEST_NAME"
SANDBOX_ENV_VAR = "SANDBOX"
GIT_CONFIG_GLOBAL_ENV_VAR = "GIT_CONFIG_GLOBAL"
2022-08-14 06:33:44 +02:00
)
2022-08-14 03:24:07 +02:00
2023-02-26 02:49:15 +02:00
// This function lets you run tests either from within `go test` or from a regular binary.
// The reason for having two separate ways of testing is that `go test` isn't great at
// showing what's actually happening during the test, but it's still good at running
// tests in telling you about their results.
2022-08-14 03:24:07 +02:00
func RunTests(
tests []*IntegrationTest,
logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error,
testWrapper func(test *IntegrationTest, f func() error),
sandbox bool,
2022-08-14 06:33:44 +02:00
keyPressDelay int,
2022-09-17 07:31:46 +02:00
maxAttempts int,
2022-08-14 03:24:07 +02:00
) error {
projectRootDir := utils.GetLazyRootDirectory()
2022-08-14 03:24:07 +02:00
err := os.Chdir(projectRootDir)
if err != nil {
return err
}
2023-02-26 02:49:15 +02:00
testDir := filepath.Join(projectRootDir, "test", "results")
2022-08-14 03:24:07 +02:00
if err := buildLazygit(); err != nil {
return err
}
gitVersion, err := getGitVersion()
if err != nil {
return err
}
2022-08-14 03:24:07 +02:00
for _, test := range tests {
test := test
testWrapper(test, func() error { //nolint: thelper
paths := NewPaths(
filepath.Join(testDir, test.Name()),
)
2022-09-17 07:31:46 +02:00
for i := 0; i < maxAttempts; i++ {
err := runTest(test, paths, projectRootDir, logf, runCmd, sandbox, keyPressDelay, gitVersion)
2022-09-17 07:31:46 +02:00
if err != nil {
if i == maxAttempts-1 {
return err
}
logf("retrying test %s", test.Name())
} else {
break
}
}
return nil
2022-08-14 03:24:07 +02:00
})
}
return nil
}
func runTest(
test *IntegrationTest,
paths Paths,
projectRootDir string,
logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error,
sandbox bool,
2022-08-14 06:33:44 +02:00
keyPressDelay int,
gitVersion *git_commands.GitVersion,
2022-08-14 03:24:07 +02:00
) error {
if test.Skip() {
logf("Skipping test %s", test.Name())
return nil
}
if !test.ShouldRunForGitVersion(gitVersion) {
logf("Skipping test %s for git version %d.%d.%d", test.Name(), gitVersion.Major, gitVersion.Minor, gitVersion.Patch)
return nil
}
2022-08-14 03:24:07 +02:00
logf("path: %s", paths.Root())
2023-02-12 01:21:13 +02:00
if err := prepareTestDir(test, paths, projectRootDir); err != nil {
2022-08-14 03:24:07 +02:00
return err
}
cmd, err := getLazygitCommand(test, paths, projectRootDir, sandbox, keyPressDelay)
2022-08-14 03:24:07 +02:00
if err != nil {
return err
}
err = runCmd(cmd)
if err != nil {
return err
}
return nil
2022-08-14 03:24:07 +02:00
}
func prepareTestDir(
test *IntegrationTest,
paths Paths,
2023-02-12 01:21:13 +02:00
rootDir string,
2022-08-14 03:24:07 +02:00
) error {
findOrCreateDir(paths.Root())
deleteAndRecreateEmptyDir(paths.Actual())
err := os.Mkdir(paths.ActualRepo(), 0o777)
if err != nil {
return err
}
2023-02-12 01:21:13 +02:00
return createFixture(test, paths, rootDir)
2022-08-14 03:24:07 +02:00
}
func buildLazygit() error {
// // TODO: remove this line!
// // skipping this because I'm not making changes to the app code atm.
// return nil
2022-08-14 03:24:07 +02:00
osCommand := oscommands.NewDummyOSCommand()
return osCommand.Cmd.New(fmt.Sprintf(
2022-08-14 06:33:44 +02:00
"go build -o %s pkg/integration/clients/injector/main.go", tempLazygitPath(),
2022-08-14 03:24:07 +02:00
)).Run()
}
2023-02-12 01:21:13 +02:00
func createFixture(test *IntegrationTest, paths Paths, rootDir string) error {
2022-12-27 12:47:37 +02:00
shell := NewShell(paths.ActualRepo(), func(errorMsg string) { panic(errorMsg) })
shell.RunCommand("git init -b master")
2023-02-12 01:21:13 +02:00
os.Setenv(GIT_CONFIG_GLOBAL_ENV_VAR, globalGitConfigPath(rootDir))
2022-08-14 03:24:07 +02:00
test.SetupRepo(shell)
return nil
}
2023-02-12 01:21:13 +02:00
func globalGitConfigPath(rootDir string) string {
return filepath.Join(rootDir, "test", "global_git_config")
}
func getGitVersion() (*git_commands.GitVersion, error) {
osCommand := oscommands.NewDummyOSCommand()
cmdObj := osCommand.Cmd.New("git --version")
versionStr, err := cmdObj.RunWithOutput()
if err != nil {
return nil, err
}
return git_commands.ParseGitVersion(versionStr)
}
func getLazygitCommand(test *IntegrationTest, paths Paths, rootDir string, sandbox bool, keyPressDelay int) (*exec.Cmd, error) {
2022-08-14 03:24:07 +02:00
osCommand := oscommands.NewDummyOSCommand()
err := os.RemoveAll(paths.Config())
if err != nil {
return nil, err
}
2023-02-12 01:21:13 +02:00
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
2022-08-14 03:24:07 +02:00
err = oscommands.CopyDir(templateConfigDir, paths.Config())
if err != nil {
return nil, err
}
cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), paths.Config(), paths.ActualRepo(), test.ExtraCmdArgs())
cmdObj := osCommand.Cmd.New(cmdStr)
2022-08-14 06:33:44 +02:00
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", TEST_NAME_ENV_VAR, test.Name()))
if sandbox {
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", SANDBOX_ENV_VAR, "true"))
2022-08-14 03:24:07 +02:00
}
2022-08-14 06:33:44 +02:00
if keyPressDelay > 0 {
cmdObj.AddEnvVars(fmt.Sprintf("KEY_PRESS_DELAY=%d", keyPressDelay))
2022-08-14 03:24:07 +02:00
}
2022-08-14 06:33:44 +02:00
2023-02-12 01:21:13 +02:00
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", GIT_CONFIG_GLOBAL_ENV_VAR, globalGitConfigPath(rootDir)))
2022-08-14 06:33:44 +02:00
return cmdObj.GetCmd(), nil
2022-08-14 03:24:07 +02:00
}
func tempLazygitPath() string {
return filepath.Join("/tmp", "lazygit", "test_lazygit")
}
func findOrCreateDir(path string) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(path, 0o777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
}
func deleteAndRecreateEmptyDir(path string) {
// remove contents of integration test directory
dir, err := ioutil.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(path, 0o777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
for _, d := range dir {
os.RemoveAll(filepath.Join(path, d.Name()))
}
}