Skip to content

Commit e9962b9

Browse files
Set working directory in lazygit test command (#3215)
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. - **PR Description** - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [x] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [x] Docs (specifically `docs/Config.md`) have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc <!-- Be sure to name your PR with an imperative e.g. 'Add worktrees view' see https://github.com/jesseduffield/lazygit/releases/tag/v0.40.0 for examples -->
2 parents 5c888a0 + a1ce602 commit e9962b9

File tree

11 files changed

+58
-46
lines changed

11 files changed

+58
-46
lines changed

pkg/integration/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Each test has two important steps: the setup step and the run step.
2424

2525
In the setup step, we prepare a repo with shell commands, for example, creating a merge conflict that will need to be resolved upon opening lazygit. This is all done via the `shell` argument.
2626

27+
When the test runs, lazygit will open in the same working directory that the shell ends up in (so if you want to start lazygit somewhere other than the default location, you can use `shell.Chdir()` at the end of the setup step to set that working directory.
28+
2729
### Run step
2830

2931
The run step has two arguments passed in:

pkg/integration/clients/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010

11+
"github.com/jesseduffield/lazycore/pkg/utils"
1112
"github.com/jesseduffield/lazygit/pkg/integration/components"
1213
"github.com/jesseduffield/lazygit/pkg/integration/tests"
1314
"github.com/samber/lo"
@@ -53,7 +54,7 @@ func runAndPrintFatalError(test *components.IntegrationTest, f func() error) {
5354
}
5455

5556
func getTestsToRun(testNames []string) []*components.IntegrationTest {
56-
allIntegrationTests := tests.GetTests()
57+
allIntegrationTests := tests.GetTests(utils.GetLazyRootDirectory())
5758
var testsToRun []*components.IntegrationTest
5859

5960
if len(testNames) == 0 {

pkg/integration/clients/go_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"testing"
1616

1717
"github.com/creack/pty"
18+
"github.com/jesseduffield/lazycore/pkg/utils"
1819
"github.com/jesseduffield/lazygit/pkg/integration/components"
1920
"github.com/jesseduffield/lazygit/pkg/integration/tests"
2021
"github.com/stretchr/testify/assert"
@@ -35,7 +36,7 @@ func TestIntegration(t *testing.T) {
3536
testNumber := 0
3637

3738
err := components.RunTests(components.RunTestArgs{
38-
Tests: tests.GetTests(),
39+
Tests: tests.GetTests(utils.GetLazyRootDirectory()),
3940
Logf: t.Logf,
4041
RunCmd: runCmdHeadless,
4142
TestWrapper: func(test *components.IntegrationTest, f func() error) {

pkg/integration/clients/injector/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func getIntegrationTest() integrationTypes.IntegrationTest {
5858
))
5959
}
6060

61-
allTests := tests.GetTests()
61+
lazygitRootDir := os.Getenv(components.LAZYGIT_ROOT_DIR)
62+
allTests := tests.GetTests(lazygitRootDir)
6263
for _, candidateTest := range allTests {
6364
if candidateTest.Name() == integrationTestName {
6465
return candidateTest

pkg/integration/clients/tui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ type app struct {
233233
}
234234

235235
func newApp(testDir string) *app {
236-
return &app{testDir: testDir, allTests: tests.GetTests()}
236+
return &app{testDir: testDir, allTests: tests.GetTests(utils.GetLazyRootDirectory())}
237237
}
238238

239239
func (self *app) getCurrentTest() *components.IntegrationTest {

pkg/integration/components/runner.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
const (
17+
LAZYGIT_ROOT_DIR = "LAZYGIT_ROOT_DIR"
1718
TEST_NAME_ENV_VAR = "TEST_NAME"
1819
SANDBOX_ENV_VAR = "SANDBOX"
1920
WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"
@@ -98,11 +99,12 @@ func runTest(
9899
return nil
99100
}
100101

101-
if err := prepareTestDir(test, paths, projectRootDir); err != nil {
102+
workingDir, err := prepareTestDir(test, paths, projectRootDir)
103+
if err != nil {
102104
return err
103105
}
104106

105-
cmd, err := getLazygitCommand(test, args, paths, projectRootDir)
107+
cmd, err := getLazygitCommand(test, args, paths, projectRootDir, workingDir)
106108
if err != nil {
107109
return err
108110
}
@@ -124,16 +126,18 @@ func prepareTestDir(
124126
test *IntegrationTest,
125127
paths Paths,
126128
rootDir string,
127-
) error {
129+
) (string, error) {
128130
findOrCreateDir(paths.Root())
129131
deleteAndRecreateEmptyDir(paths.Actual())
130132

131133
err := os.Mkdir(paths.ActualRepo(), 0o777)
132134
if err != nil {
133-
return err
135+
return "", err
134136
}
135137

136-
return createFixture(test, paths, rootDir)
138+
workingDir := createFixture(test, paths, rootDir)
139+
140+
return workingDir, nil
137141
}
138142

139143
func buildLazygit(testArgs RunTestArgs) error {
@@ -154,15 +158,17 @@ func buildLazygit(testArgs RunTestArgs) error {
154158
return osCommand.Cmd.New(args).Run()
155159
}
156160

157-
func createFixture(test *IntegrationTest, paths Paths, rootDir string) error {
161+
// Sets up the fixture for test and returns the working directory to invoke
162+
// lazygit in.
163+
func createFixture(test *IntegrationTest, paths Paths, rootDir string) string {
158164
shell := NewShell(paths.ActualRepo(), func(errorMsg string) { panic(errorMsg) })
159165
shell.Init()
160166

161167
os.Setenv(GIT_CONFIG_GLOBAL_ENV_VAR, globalGitConfigPath(rootDir))
162168

163169
test.SetupRepo(shell)
164170

165-
return nil
171+
return shell.dir
166172
}
167173

168174
func globalGitConfigPath(rootDir string) string {
@@ -179,7 +185,13 @@ func getGitVersion() (*git_commands.GitVersion, error) {
179185
return git_commands.ParseGitVersion(versionStr)
180186
}
181187

182-
func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, rootDir string) (*exec.Cmd, error) {
188+
func getLazygitCommand(
189+
test *IntegrationTest,
190+
args RunTestArgs,
191+
paths Paths,
192+
rootDir string,
193+
workingDir string,
194+
) (*exec.Cmd, error) {
183195
osCommand := oscommands.NewDummyOSCommand()
184196

185197
err := os.RemoveAll(paths.Config())
@@ -194,9 +206,7 @@ func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, roo
194206
}
195207

196208
cmdArgs := []string{tempLazygitPath(), "-debug", "--use-config-dir=" + paths.Config()}
197-
if !test.useCustomPath {
198-
cmdArgs = append(cmdArgs, "--path="+paths.ActualRepo())
199-
}
209+
200210
resolvedExtraArgs := lo.Map(test.ExtraCmdArgs(), func(arg string, _ int) string {
201211
return utils.ResolvePlaceholderString(arg, map[string]string{
202212
"actualPath": paths.Actual(),
@@ -207,6 +217,10 @@ func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, roo
207217

208218
cmdObj := osCommand.Cmd.New(cmdArgs)
209219

220+
cmdObj.SetWd(workingDir)
221+
222+
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", LAZYGIT_ROOT_DIR, rootDir))
223+
210224
if args.CodeCoverageDir != "" {
211225
// We set this explicitly here rather than inherit it from the test runner's
212226
// environment because the test runner has its own coverage directory that

pkg/integration/components/test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ type IntegrationTest struct {
3535
testDriver *TestDriver,
3636
keys config.KeybindingConfig,
3737
)
38-
gitVersion GitVersionRestriction
39-
width int
40-
height int
41-
isDemo bool
42-
useCustomPath bool
38+
gitVersion GitVersionRestriction
39+
width int
40+
height int
41+
isDemo bool
4342
}
4443

4544
var _ integrationTypes.IntegrationTest = &IntegrationTest{}
@@ -55,9 +54,9 @@ type NewIntegrationTestArgs struct {
5554
Run func(t *TestDriver, keys config.KeybindingConfig)
5655
// additional args passed to lazygit
5756
ExtraCmdArgs []string
58-
// for when a test is flakey
5957
ExtraEnvVars map[string]string
60-
Skip bool
58+
// for when a test is flakey
59+
Skip bool
6160
// to run a test only on certain git versions
6261
GitVersion GitVersionRestriction
6362
// width and height when running in headless mode, for testing
@@ -67,10 +66,6 @@ type NewIntegrationTestArgs struct {
6766
Height int
6867
// If true, this is not a test but a demo to be added to our docs
6968
IsDemo bool
70-
// If true, the test won't invoke lazygit with the --path arg.
71-
// Useful for when we're passing --git-dir and --work-tree (because --path is
72-
// incompatible with those args)
73-
UseCustomPath bool
7469
}
7570

7671
type GitVersionRestriction struct {
@@ -130,19 +125,18 @@ func NewIntegrationTest(args NewIntegrationTestArgs) *IntegrationTest {
130125
}
131126

132127
return &IntegrationTest{
133-
name: name,
134-
description: args.Description,
135-
extraCmdArgs: args.ExtraCmdArgs,
136-
extraEnvVars: args.ExtraEnvVars,
137-
skip: args.Skip,
138-
setupRepo: args.SetupRepo,
139-
setupConfig: args.SetupConfig,
140-
run: args.Run,
141-
gitVersion: args.GitVersion,
142-
width: args.Width,
143-
height: args.Height,
144-
isDemo: args.IsDemo,
145-
useCustomPath: args.UseCustomPath,
128+
name: name,
129+
description: args.Description,
130+
extraCmdArgs: args.ExtraCmdArgs,
131+
extraEnvVars: args.ExtraEnvVars,
132+
skip: args.Skip,
133+
setupRepo: args.SetupRepo,
134+
setupConfig: args.SetupConfig,
135+
run: args.Run,
136+
gitVersion: args.GitVersion,
137+
width: args.Width,
138+
height: args.Height,
139+
isDemo: args.IsDemo,
146140
}
147141
}
148142

pkg/integration/tests/filter_by_path/cli_arg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var CliArg = NewIntegrationTest(NewIntegrationTestArgs{
99
Description: "Filter commits by file path, using CLI arg",
10-
ExtraCmdArgs: []string{"-f", "filterFile"},
10+
ExtraCmdArgs: []string{"-f=filterFile"},
1111
Skip: false,
1212
SetupConfig: func(config *config.AppConfig) {
1313
},

pkg/integration/tests/tests.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import (
99
"strings"
1010

1111
"github.com/jesseduffield/generics/set"
12-
"github.com/jesseduffield/lazycore/pkg/utils"
1312
"github.com/jesseduffield/lazygit/pkg/integration/components"
1413
"github.com/samber/lo"
1514
)
1615

17-
func GetTests() []*components.IntegrationTest {
16+
func GetTests(lazygitRootDir string) []*components.IntegrationTest {
1817
// first we ensure that each test in this directory has actually been added to the above list.
1918
testCount := 0
2019

@@ -27,7 +26,7 @@ func GetTests() []*components.IntegrationTest {
2726

2827
missingTestNames := []string{}
2928

30-
if err := filepath.Walk(filepath.Join(utils.GetLazyRootDirectory(), "pkg/integration/tests"), func(path string, info os.FileInfo, err error) error {
29+
if err := filepath.Walk(filepath.Join(lazygitRootDir, "pkg/integration/tests"), func(path string, info os.FileInfo, err error) error {
3130
if !info.IsDir() && strings.HasSuffix(path, ".go") {
3231
// ignoring non-test files
3332
if filepath.Base(path) == "tests.go" || filepath.Base(path) == "test_list.go" || filepath.Base(path) == "test_list_generator.go" {

pkg/integration/tests/worktree/bare_repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var BareRepo = NewIntegrationTest(NewIntegrationTestArgs{
3838

3939
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "repo", "repo", "mybranch"})
4040
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "worktree2", "worktree2", "mybranch"})
41+
42+
shell.Chdir("repo")
4143
},
4244
Run: func(t *TestDriver, keys config.KeybindingConfig) {
4345
t.Views().Branches().

0 commit comments

Comments
 (0)