mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-10 07:36:27 -04:00
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; since34da956f5da 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 mode34da956f5dset 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.
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFindLazygitRootDirectory(t *testing.T) {
|
|
// This test runs in the pkg/utils directory, so we expect the function to
|
|
// search two levels up for the project root.
|
|
expectedRootDir, err := filepath.Abs(filepath.Join("..", ".."))
|
|
assert.NoError(t, err)
|
|
|
|
rootDir, err := FindLazygitRootDirectory()
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expectedRootDir, rootDir)
|
|
}
|
|
|
|
func TestFindLazygitRootDirectoryOutsideProject(t *testing.T) {
|
|
t.Chdir(t.TempDir())
|
|
|
|
_, err := FindLazygitRootDirectory()
|
|
|
|
assert.ErrorContains(t, err, "there is no go.mod for a lazygit module")
|
|
}
|
|
|
|
func TestDeclaresLazygitModule(t *testing.T) {
|
|
scenarios := []struct {
|
|
testName string
|
|
contents string
|
|
expected bool
|
|
}{
|
|
{
|
|
testName: "lazygit's go.mod",
|
|
contents: "module github.com/jesseduffield/lazygit\n\ngo 1.25.0\n",
|
|
expected: true,
|
|
},
|
|
{
|
|
testName: "a fork's go.mod",
|
|
contents: "module gitlab.com/somebody-else/lazygit\n\ngo 1.25.0\n",
|
|
expected: true,
|
|
},
|
|
{
|
|
testName: "a fork's go.mod with a major version suffix",
|
|
contents: "module github.com/somebody-else/lazygit/v2\n\ngo 1.25.0\n",
|
|
expected: true,
|
|
},
|
|
{
|
|
testName: "module declaration preceded by a comment",
|
|
contents: "// a comment\n\nmodule github.com/jesseduffield/lazygit\n",
|
|
expected: true,
|
|
},
|
|
{
|
|
testName: "module declaration followed by a comment",
|
|
contents: "module github.com/jesseduffield/lazygit // comment\n\ngo 1.25.0\n",
|
|
expected: true,
|
|
},
|
|
{
|
|
testName: "another project's go.mod",
|
|
contents: "module github.com/jesseduffield/lazydocker\n\ngo 1.25.0\n",
|
|
expected: false,
|
|
},
|
|
{
|
|
testName: "no module declaration",
|
|
contents: "go 1.25.0\n",
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
t.Run(scenario.testName, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "go.mod")
|
|
assert.NoError(t, os.WriteFile(path, []byte(scenario.contents), 0o644))
|
|
|
|
assert.Equal(t, scenario.expected, declaresLazygitModule(path))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeclaresLazygitModuleWithoutAGoModFile(t *testing.T) {
|
|
assert.False(t, declaresLazygitModule(filepath.Join(t.TempDir(), "go.mod")))
|
|
}
|