1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-10 11:10:18 +02:00
lazygit/pkg/commands/git_test.go

75 lines
1.7 KiB
Go
Raw Normal View History

package commands
import (
"testing"
2019-05-12 09:04:32 +02:00
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
func TestFindWorktreeRoot(t *testing.T) {
type scenario struct {
testName string
currentPath string
before func(fs afero.Fs)
expectedPath string
expectedErr string
}
scenarios := []scenario{
2018-08-29 22:55:57 +02:00
{
testName: "at root of worktree",
currentPath: "/path/to/repo",
before: func(fs afero.Fs) {
_ = fs.MkdirAll("/path/to/repo/.git", 0o755)
},
expectedPath: "/path/to/repo",
expectedErr: "",
},
{
testName: "inside worktree",
currentPath: "/path/to/repo/subdir",
before: func(fs afero.Fs) {
_ = fs.MkdirAll("/path/to/repo/.git", 0o755)
_ = fs.MkdirAll("/path/to/repo/subdir", 0o755)
},
expectedPath: "/path/to/repo",
expectedErr: "",
2018-08-29 22:55:57 +02:00
},
{
testName: "not in a git repo",
currentPath: "/path/to/dir",
before: func(fs afero.Fs) {},
expectedPath: "",
expectedErr: "Must open lazygit in a git repository",
2018-09-02 17:18:33 +02:00
},
{
testName: "In linked worktree",
currentPath: "/path/to/worktree",
before: func(fs afero.Fs) {
_ = fs.MkdirAll("/path/to/worktree", 0o755)
_ = afero.WriteFile(fs, "/path/to/worktree/.git", []byte("blah"), 0o755)
},
expectedPath: "/path/to/worktree",
expectedErr: "",
2018-09-02 17:18:33 +02:00
},
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
2018-09-05 22:16:26 +02:00
t.Run(s.testName, func(t *testing.T) {
fs := afero.NewMemMapFs()
s.before(fs)
2019-05-12 09:04:32 +02:00
root, err := findWorktreeRoot(fs, s.currentPath)
if s.expectedErr != "" {
assert.EqualError(t, errors.New(s.expectedErr), err.Error())
} else {
2019-05-12 09:04:32 +02:00
assert.NoError(t, err)
assert.Equal(t, s.expectedPath, root)
}
2019-05-12 09:04:32 +02:00
})
}
}