mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Set working directory in lazygit test command
We need to fetch our list of tests both outside of our test binary and within. We need to get the list from within so that we can run the code that drives the test and runs assertions. To get the list of tests we need to know where the root of the lazygit repo is, given that the tests live in files under that root. So far, we've used this GetLazyRootDirectory() function for that, but it assumes that we're not in a test directory (it just looks for the first .git dir it can find). Because we didn't want to properly fix this before, we've been setting the working directory of the test command to the lazygit root, and using the --path CLI arg to override it when the test itself ran. This was a terrible hack. Now, we're passing the lazygit root directory as an env var to the integration test, so that we can set the working directory to the actual path of the test repo; removing the need to use the --path arg.
This commit is contained in:
@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
LAZYGIT_ROOT_DIR = "LAZYGIT_ROOT_DIR"
|
||||
TEST_NAME_ENV_VAR = "TEST_NAME"
|
||||
SANDBOX_ENV_VAR = "SANDBOX"
|
||||
WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"
|
||||
@ -98,11 +99,12 @@ func runTest(
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := prepareTestDir(test, paths, projectRootDir); err != nil {
|
||||
workingDir, err := prepareTestDir(test, paths, projectRootDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd, err := getLazygitCommand(test, args, paths, projectRootDir)
|
||||
cmd, err := getLazygitCommand(test, args, paths, projectRootDir, workingDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -124,16 +126,18 @@ func prepareTestDir(
|
||||
test *IntegrationTest,
|
||||
paths Paths,
|
||||
rootDir string,
|
||||
) error {
|
||||
) (string, error) {
|
||||
findOrCreateDir(paths.Root())
|
||||
deleteAndRecreateEmptyDir(paths.Actual())
|
||||
|
||||
err := os.Mkdir(paths.ActualRepo(), 0o777)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return createFixture(test, paths, rootDir)
|
||||
workingDir := createFixture(test, paths, rootDir)
|
||||
|
||||
return workingDir, nil
|
||||
}
|
||||
|
||||
func buildLazygit(testArgs RunTestArgs) error {
|
||||
@ -154,7 +158,9 @@ func buildLazygit(testArgs RunTestArgs) error {
|
||||
return osCommand.Cmd.New(args).Run()
|
||||
}
|
||||
|
||||
func createFixture(test *IntegrationTest, paths Paths, rootDir string) error {
|
||||
// Sets up the fixture for test and returns the working directory to invoke
|
||||
// lazygit in.
|
||||
func createFixture(test *IntegrationTest, paths Paths, rootDir string) string {
|
||||
shell := NewShell(paths.ActualRepo(), func(errorMsg string) { panic(errorMsg) })
|
||||
shell.Init()
|
||||
|
||||
@ -162,7 +168,7 @@ func createFixture(test *IntegrationTest, paths Paths, rootDir string) error {
|
||||
|
||||
test.SetupRepo(shell)
|
||||
|
||||
return nil
|
||||
return shell.dir
|
||||
}
|
||||
|
||||
func globalGitConfigPath(rootDir string) string {
|
||||
@ -179,7 +185,13 @@ func getGitVersion() (*git_commands.GitVersion, error) {
|
||||
return git_commands.ParseGitVersion(versionStr)
|
||||
}
|
||||
|
||||
func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, rootDir string) (*exec.Cmd, error) {
|
||||
func getLazygitCommand(
|
||||
test *IntegrationTest,
|
||||
args RunTestArgs,
|
||||
paths Paths,
|
||||
rootDir string,
|
||||
workingDir string,
|
||||
) (*exec.Cmd, error) {
|
||||
osCommand := oscommands.NewDummyOSCommand()
|
||||
|
||||
err := os.RemoveAll(paths.Config())
|
||||
@ -194,9 +206,7 @@ func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, roo
|
||||
}
|
||||
|
||||
cmdArgs := []string{tempLazygitPath(), "-debug", "--use-config-dir=" + paths.Config()}
|
||||
if !test.useCustomPath {
|
||||
cmdArgs = append(cmdArgs, "--path="+paths.ActualRepo())
|
||||
}
|
||||
|
||||
resolvedExtraArgs := lo.Map(test.ExtraCmdArgs(), func(arg string, _ int) string {
|
||||
return utils.ResolvePlaceholderString(arg, map[string]string{
|
||||
"actualPath": paths.Actual(),
|
||||
@ -207,6 +217,10 @@ func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, roo
|
||||
|
||||
cmdObj := osCommand.Cmd.New(cmdArgs)
|
||||
|
||||
cmdObj.SetWd(workingDir)
|
||||
|
||||
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", LAZYGIT_ROOT_DIR, rootDir))
|
||||
|
||||
if args.CodeCoverageDir != "" {
|
||||
// We set this explicitly here rather than inherit it from the test runner's
|
||||
// environment because the test runner has its own coverage directory that
|
||||
|
Reference in New Issue
Block a user