mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-21 12:16:54 +02:00
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 -->
This commit is contained in:
commit
e9962b98a7
@ -24,6 +24,8 @@ Each test has two important steps: the setup step and the run step.
|
|||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
### Run step
|
### Run step
|
||||||
|
|
||||||
The run step has two arguments passed in:
|
The run step has two arguments passed in:
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jesseduffield/lazycore/pkg/utils"
|
||||||
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
@ -53,7 +54,7 @@ func runAndPrintFatalError(test *components.IntegrationTest, f func() error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getTestsToRun(testNames []string) []*components.IntegrationTest {
|
func getTestsToRun(testNames []string) []*components.IntegrationTest {
|
||||||
allIntegrationTests := tests.GetTests()
|
allIntegrationTests := tests.GetTests(utils.GetLazyRootDirectory())
|
||||||
var testsToRun []*components.IntegrationTest
|
var testsToRun []*components.IntegrationTest
|
||||||
|
|
||||||
if len(testNames) == 0 {
|
if len(testNames) == 0 {
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
|
"github.com/jesseduffield/lazycore/pkg/utils"
|
||||||
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -35,7 +36,7 @@ func TestIntegration(t *testing.T) {
|
|||||||
testNumber := 0
|
testNumber := 0
|
||||||
|
|
||||||
err := components.RunTests(components.RunTestArgs{
|
err := components.RunTests(components.RunTestArgs{
|
||||||
Tests: tests.GetTests(),
|
Tests: tests.GetTests(utils.GetLazyRootDirectory()),
|
||||||
Logf: t.Logf,
|
Logf: t.Logf,
|
||||||
RunCmd: runCmdHeadless,
|
RunCmd: runCmdHeadless,
|
||||||
TestWrapper: func(test *components.IntegrationTest, f func() error) {
|
TestWrapper: func(test *components.IntegrationTest, f func() error) {
|
||||||
|
@ -58,7 +58,8 @@ func getIntegrationTest() integrationTypes.IntegrationTest {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
allTests := tests.GetTests()
|
lazygitRootDir := os.Getenv(components.LAZYGIT_ROOT_DIR)
|
||||||
|
allTests := tests.GetTests(lazygitRootDir)
|
||||||
for _, candidateTest := range allTests {
|
for _, candidateTest := range allTests {
|
||||||
if candidateTest.Name() == integrationTestName {
|
if candidateTest.Name() == integrationTestName {
|
||||||
return candidateTest
|
return candidateTest
|
||||||
|
@ -233,7 +233,7 @@ type app struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newApp(testDir string) *app {
|
func newApp(testDir string) *app {
|
||||||
return &app{testDir: testDir, allTests: tests.GetTests()}
|
return &app{testDir: testDir, allTests: tests.GetTests(utils.GetLazyRootDirectory())}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *app) getCurrentTest() *components.IntegrationTest {
|
func (self *app) getCurrentTest() *components.IntegrationTest {
|
||||||
|
@ -14,6 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
LAZYGIT_ROOT_DIR = "LAZYGIT_ROOT_DIR"
|
||||||
TEST_NAME_ENV_VAR = "TEST_NAME"
|
TEST_NAME_ENV_VAR = "TEST_NAME"
|
||||||
SANDBOX_ENV_VAR = "SANDBOX"
|
SANDBOX_ENV_VAR = "SANDBOX"
|
||||||
WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"
|
WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"
|
||||||
@ -98,11 +99,12 @@ func runTest(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := prepareTestDir(test, paths, projectRootDir); err != nil {
|
workingDir, err := prepareTestDir(test, paths, projectRootDir)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd, err := getLazygitCommand(test, args, paths, projectRootDir)
|
cmd, err := getLazygitCommand(test, args, paths, projectRootDir, workingDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -124,16 +126,18 @@ func prepareTestDir(
|
|||||||
test *IntegrationTest,
|
test *IntegrationTest,
|
||||||
paths Paths,
|
paths Paths,
|
||||||
rootDir string,
|
rootDir string,
|
||||||
) error {
|
) (string, error) {
|
||||||
findOrCreateDir(paths.Root())
|
findOrCreateDir(paths.Root())
|
||||||
deleteAndRecreateEmptyDir(paths.Actual())
|
deleteAndRecreateEmptyDir(paths.Actual())
|
||||||
|
|
||||||
err := os.Mkdir(paths.ActualRepo(), 0o777)
|
err := os.Mkdir(paths.ActualRepo(), 0o777)
|
||||||
if err != nil {
|
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 {
|
func buildLazygit(testArgs RunTestArgs) error {
|
||||||
@ -154,7 +158,9 @@ func buildLazygit(testArgs RunTestArgs) error {
|
|||||||
return osCommand.Cmd.New(args).Run()
|
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 := NewShell(paths.ActualRepo(), func(errorMsg string) { panic(errorMsg) })
|
||||||
shell.Init()
|
shell.Init()
|
||||||
|
|
||||||
@ -162,7 +168,7 @@ func createFixture(test *IntegrationTest, paths Paths, rootDir string) error {
|
|||||||
|
|
||||||
test.SetupRepo(shell)
|
test.SetupRepo(shell)
|
||||||
|
|
||||||
return nil
|
return shell.dir
|
||||||
}
|
}
|
||||||
|
|
||||||
func globalGitConfigPath(rootDir string) string {
|
func globalGitConfigPath(rootDir string) string {
|
||||||
@ -179,7 +185,13 @@ func getGitVersion() (*git_commands.GitVersion, error) {
|
|||||||
return git_commands.ParseGitVersion(versionStr)
|
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()
|
osCommand := oscommands.NewDummyOSCommand()
|
||||||
|
|
||||||
err := os.RemoveAll(paths.Config())
|
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()}
|
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 {
|
resolvedExtraArgs := lo.Map(test.ExtraCmdArgs(), func(arg string, _ int) string {
|
||||||
return utils.ResolvePlaceholderString(arg, map[string]string{
|
return utils.ResolvePlaceholderString(arg, map[string]string{
|
||||||
"actualPath": paths.Actual(),
|
"actualPath": paths.Actual(),
|
||||||
@ -207,6 +217,10 @@ func getLazygitCommand(test *IntegrationTest, args RunTestArgs, paths Paths, roo
|
|||||||
|
|
||||||
cmdObj := osCommand.Cmd.New(cmdArgs)
|
cmdObj := osCommand.Cmd.New(cmdArgs)
|
||||||
|
|
||||||
|
cmdObj.SetWd(workingDir)
|
||||||
|
|
||||||
|
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", LAZYGIT_ROOT_DIR, rootDir))
|
||||||
|
|
||||||
if args.CodeCoverageDir != "" {
|
if args.CodeCoverageDir != "" {
|
||||||
// We set this explicitly here rather than inherit it from the test runner's
|
// 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
|
// environment because the test runner has its own coverage directory that
|
||||||
|
@ -39,7 +39,6 @@ type IntegrationTest struct {
|
|||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
isDemo bool
|
isDemo bool
|
||||||
useCustomPath bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ integrationTypes.IntegrationTest = &IntegrationTest{}
|
var _ integrationTypes.IntegrationTest = &IntegrationTest{}
|
||||||
@ -55,8 +54,8 @@ type NewIntegrationTestArgs struct {
|
|||||||
Run func(t *TestDriver, keys config.KeybindingConfig)
|
Run func(t *TestDriver, keys config.KeybindingConfig)
|
||||||
// additional args passed to lazygit
|
// additional args passed to lazygit
|
||||||
ExtraCmdArgs []string
|
ExtraCmdArgs []string
|
||||||
// for when a test is flakey
|
|
||||||
ExtraEnvVars map[string]string
|
ExtraEnvVars map[string]string
|
||||||
|
// for when a test is flakey
|
||||||
Skip bool
|
Skip bool
|
||||||
// to run a test only on certain git versions
|
// to run a test only on certain git versions
|
||||||
GitVersion GitVersionRestriction
|
GitVersion GitVersionRestriction
|
||||||
@ -67,10 +66,6 @@ type NewIntegrationTestArgs struct {
|
|||||||
Height int
|
Height int
|
||||||
// If true, this is not a test but a demo to be added to our docs
|
// If true, this is not a test but a demo to be added to our docs
|
||||||
IsDemo bool
|
IsDemo bool
|
||||||
// If true, the test won't invoke lazygit with the --path arg.
|
|
||||||
// Useful for when we're passing --git-dir and --work-tree (because --path is
|
|
||||||
// incompatible with those args)
|
|
||||||
UseCustomPath bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GitVersionRestriction struct {
|
type GitVersionRestriction struct {
|
||||||
@ -142,7 +137,6 @@ func NewIntegrationTest(args NewIntegrationTestArgs) *IntegrationTest {
|
|||||||
width: args.Width,
|
width: args.Width,
|
||||||
height: args.Height,
|
height: args.Height,
|
||||||
isDemo: args.IsDemo,
|
isDemo: args.IsDemo,
|
||||||
useCustomPath: args.UseCustomPath,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
var CliArg = NewIntegrationTest(NewIntegrationTestArgs{
|
var CliArg = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
Description: "Filter commits by file path, using CLI arg",
|
Description: "Filter commits by file path, using CLI arg",
|
||||||
ExtraCmdArgs: []string{"-f", "filterFile"},
|
ExtraCmdArgs: []string{"-f=filterFile"},
|
||||||
Skip: false,
|
Skip: false,
|
||||||
SetupConfig: func(config *config.AppConfig) {
|
SetupConfig: func(config *config.AppConfig) {
|
||||||
},
|
},
|
||||||
|
@ -9,12 +9,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jesseduffield/generics/set"
|
"github.com/jesseduffield/generics/set"
|
||||||
"github.com/jesseduffield/lazycore/pkg/utils"
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTests() []*components.IntegrationTest {
|
func GetTests(lazygitRootDir string) []*components.IntegrationTest {
|
||||||
// first we ensure that each test in this directory has actually been added to the above list.
|
// first we ensure that each test in this directory has actually been added to the above list.
|
||||||
testCount := 0
|
testCount := 0
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ func GetTests() []*components.IntegrationTest {
|
|||||||
|
|
||||||
missingTestNames := []string{}
|
missingTestNames := []string{}
|
||||||
|
|
||||||
if err := filepath.Walk(filepath.Join(utils.GetLazyRootDirectory(), "pkg/integration/tests"), func(path string, info os.FileInfo, err error) error {
|
if err := filepath.Walk(filepath.Join(lazygitRootDir, "pkg/integration/tests"), func(path string, info os.FileInfo, err error) error {
|
||||||
if !info.IsDir() && strings.HasSuffix(path, ".go") {
|
if !info.IsDir() && strings.HasSuffix(path, ".go") {
|
||||||
// ignoring non-test files
|
// ignoring non-test files
|
||||||
if filepath.Base(path) == "tests.go" || filepath.Base(path) == "test_list.go" || filepath.Base(path) == "test_list_generator.go" {
|
if filepath.Base(path) == "tests.go" || filepath.Base(path) == "test_list.go" || filepath.Base(path) == "test_list_generator.go" {
|
||||||
|
@ -38,6 +38,8 @@ var BareRepo = NewIntegrationTest(NewIntegrationTestArgs{
|
|||||||
|
|
||||||
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "repo", "repo", "mybranch"})
|
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "repo", "repo", "mybranch"})
|
||||||
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "worktree2", "worktree2", "mybranch"})
|
shell.RunCommand([]string{"git", "--git-dir", ".bare", "worktree", "add", "-b", "worktree2", "worktree2", "mybranch"})
|
||||||
|
|
||||||
|
shell.Chdir("repo")
|
||||||
},
|
},
|
||||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||||
t.Views().Branches().
|
t.Views().Branches().
|
||||||
|
@ -12,8 +12,6 @@ var DotfileBareRepo = NewIntegrationTest(NewIntegrationTestArgs{
|
|||||||
Description: "Open lazygit in the worktree of a dotfile bare repo and add a file and commit",
|
Description: "Open lazygit in the worktree of a dotfile bare repo and add a file and commit",
|
||||||
ExtraCmdArgs: []string{"--git-dir={{.actualPath}}/.bare", "--work-tree={{.actualPath}}/repo"},
|
ExtraCmdArgs: []string{"--git-dir={{.actualPath}}/.bare", "--work-tree={{.actualPath}}/repo"},
|
||||||
Skip: false,
|
Skip: false,
|
||||||
// passing this because we're explicitly passing --git-dir and --work-tree args
|
|
||||||
UseCustomPath: true,
|
|
||||||
SetupConfig: func(config *config.AppConfig) {},
|
SetupConfig: func(config *config.AppConfig) {},
|
||||||
SetupRepo: func(shell *Shell) {
|
SetupRepo: func(shell *Shell) {
|
||||||
// we're going to have a directory structure like this:
|
// we're going to have a directory structure like this:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user