jesseduffield.lazygit/pkg/integration/clients/cli.go
Stefan Haller 8b049be31d Find the lazygit root directory by go.mod instead of .git
Running the tests in an exported source tarball fails with "must run in
lazy project folder or child folder". GetLazyRootDirectory searches the
working directory and its parents for a .git directory, and a tarball
doesn't have one. This has always affected the integration tests; since
34da956f5d a unit test calls the function too, so now even
`go test ./... -short` fails.

Search for the go.mod file that declares lazygit's module instead. It
ships in tarballs, and there is exactly one of it per source tree.

Put the function in our own pkg/utils rather than change lazycore's; the
criterion is specific to lazygit, and I don't feel like making a change
to lazycore.

Return an error rather than call log.Fatal, and report it from the two
callers that run under `go test`. In a test binary, log.Fatal exits
without attributing the failure to any test. That is the failure mode
34da956f5d set out to remove. The remaining callers are development
tools that have nothing useful to do without the root directory; they
keep exiting, now through MustFindLazygitRootDirectory.

Also stop the search at the root of the file system rather than at "/".
On Windows the old loop walks up to "C:\" and then spins there forever.
2026-09-09 09:27:44 +02:00

112 lines
3.2 KiB
Go

package clients
import (
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/jesseduffield/lazygit/pkg/integration/components"
"github.com/jesseduffield/lazygit/pkg/integration/tests"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)
// see pkg/integration/README.md
// The purpose of this program is to run integration tests. It does this by
// building our injector program (in the sibling injector directory) and then for
// each test we're running, invoke the injector program with the test's name as
// an environment variable. Then the injector finds the test and passes it to
// the lazygit startup code.
// If invoked directly, you can specify tests to run by passing their names as positional arguments
func RunCLI(testNames []string, slow bool, sandbox bool, waitForDebugger bool, raceDetector bool) {
inputDelay := tryConvert(os.Getenv("INPUT_DELAY"), 0)
if slow {
inputDelay = SLOW_INPUT_DELAY
}
err := components.RunTests(components.RunTestArgs{
Tests: getTestsToRun(testNames),
Logf: log.Printf,
RunCmd: runCmdInTerminal,
TestWrapper: runAndPrintFatalError,
Sandbox: sandbox,
WaitForDebugger: waitForDebugger,
RaceDetector: raceDetector,
CodeCoverageDir: "",
InputDelay: inputDelay,
MaxAttempts: 1,
})
if err != nil {
log.Print(err.Error())
}
}
func runAndPrintFatalError(test *components.IntegrationTest, f func() error) {
if err := f(); err != nil {
log.Fatal(err.Error())
}
}
func getTestsToRun(testNames []string) []*components.IntegrationTest {
allIntegrationTests := tests.GetTests(utils.MustFindLazygitRootDirectory())
var testsToRun []*components.IntegrationTest
if len(testNames) == 0 {
return allIntegrationTests
}
testNames = lo.Map(testNames, func(name string, _ int) string {
// allowing full test paths to be passed for convenience
return strings.TrimSuffix(
regexp.MustCompile(`.*pkg/integration/tests/`).ReplaceAllString(name, ""),
".go",
)
})
if lo.SomeBy(testNames, func(name string) bool {
return strings.HasSuffix(name, "/shared")
}) {
log.Fatalf("'shared' is a reserved name for tests that are shared between multiple test files. Please rename your test.")
}
outer:
for _, testName := range testNames {
// check if our given test name actually exists
for _, test := range allIntegrationTests {
if test.Name() == testName {
testsToRun = append(testsToRun, test)
continue outer
}
}
log.Fatalf("test %s not found. Perhaps you forgot to add it to `pkg/integration/integration_tests/test_list.go`? This can be done by running `go generate ./...` from the Lazygit root. You'll need to ensure that your test name and the file name match (where the test name is in PascalCase and the file name is in snake_case).", testName)
}
return testsToRun
}
func runCmdInTerminal(cmd *exec.Cmd) (int, error) {
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return -1, err
}
return cmd.Process.Pid, cmd.Wait()
}
func tryConvert(numStr string, defaultVal int) int {
num, err := strconv.Atoi(numStr)
if err != nil {
return defaultVal
}
return num
}