2018-08-19 06:48:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-05-12 09:04:32 +02:00
|
|
|
"github.com/go-errors/errors"
|
2023-07-29 09:02:04 +02:00
|
|
|
"github.com/spf13/afero"
|
2018-08-27 22:40:15 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-19 06:48:39 +02:00
|
|
|
)
|
|
|
|
|
2023-07-29 09:02:04 +02:00
|
|
|
func TestFindWorktreeRoot(t *testing.T) {
|
2018-09-02 17:15:27 +02:00
|
|
|
type scenario struct {
|
2023-07-29 09:02:04 +02:00
|
|
|
testName string
|
|
|
|
currentPath string
|
|
|
|
before func(fs afero.Fs)
|
|
|
|
expectedPath string
|
|
|
|
expectedErr string
|
2018-09-02 17:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
2018-08-29 22:55:57 +02:00
|
|
|
{
|
2023-07-29 09:02:04 +02:00
|
|
|
testName: "at root of worktree",
|
|
|
|
currentPath: "/path/to/repo",
|
|
|
|
before: func(fs afero.Fs) {
|
|
|
|
_ = fs.MkdirAll("/path/to/repo/.git", 0o755)
|
2018-09-02 17:15:27 +02:00
|
|
|
},
|
2023-07-29 09:02:04 +02:00
|
|
|
expectedPath: "/path/to/repo",
|
|
|
|
expectedErr: "",
|
2018-09-02 17:15:27 +02:00
|
|
|
},
|
|
|
|
{
|
2023-07-29 09:02:04 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
{
|
2023-07-29 09:02:04 +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
|
|
|
},
|
|
|
|
{
|
2023-07-29 09:02:04 +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) {
|
2023-07-29 09:02:04 +02:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
s.before(fs)
|
2019-05-12 09:04:32 +02:00
|
|
|
|
2023-07-29 09:02:04 +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)
|
2023-07-29 09:02:04 +02:00
|
|
|
assert.Equal(t, s.expectedPath, root)
|
|
|
|
}
|
2019-05-12 09:04:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|