mirror of https://github.com/databricks/cli.git
Add tests for worktree specific resolution
This commit is contained in:
parent
c6fc519fc0
commit
0f681146e0
|
@ -3,6 +3,7 @@ package git
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -47,7 +48,7 @@ func readGitDir(root vfs.Path) (string, error) {
|
|||
}
|
||||
|
||||
if gitDir == "" {
|
||||
return "", errors.New("gitdir line not found")
|
||||
return "", fmt.Errorf(`expected %q to contain a line with "gitdir: [...]"`, filepath.Join(root.Native(), GitDirectoryName))
|
||||
}
|
||||
|
||||
return gitDir, nil
|
||||
|
@ -62,10 +63,10 @@ func readGitCommonDir(gitDir vfs.Path) (string, error) {
|
|||
}
|
||||
|
||||
if len(lines) == 0 {
|
||||
return "", errors.New("commondir file not found")
|
||||
return "", errors.New("file is empty")
|
||||
}
|
||||
|
||||
return lines[0], nil
|
||||
return strings.TrimSpace(lines[0]), nil
|
||||
}
|
||||
|
||||
// resolveGitDirs resolves the paths for $GIT_DIR and $GIT_COMMON_DIR.
|
||||
|
@ -107,7 +108,7 @@ func resolveGitDirs(root vfs.Path) (vfs.Path, vfs.Path, error) {
|
|||
// Read value for $GIT_COMMON_DIR.
|
||||
gitCommonDirValue, err := readGitCommonDir(gitDir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, fmt.Errorf(`expected "commondir" file in worktree git folder at %q: %w`, gitDir.Native(), err)
|
||||
}
|
||||
|
||||
// Resolve $GIT_COMMON_DIR.
|
||||
|
|
|
@ -1 +1,108 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/libs/vfs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func setupWorktree(t *testing.T) string {
|
||||
var err error
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Checkout path
|
||||
err = os.MkdirAll(filepath.Join(tmpDir, "my_worktree"), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Main $GIT_COMMON_DIR
|
||||
err = os.MkdirAll(filepath.Join(tmpDir, ".git"), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Worktree $GIT_DIR
|
||||
err = os.MkdirAll(filepath.Join(tmpDir, ".git/worktrees/my_worktree"), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
|
||||
return tmpDir
|
||||
}
|
||||
|
||||
func writeGitDir(t *testing.T, dir, content string) {
|
||||
err := os.WriteFile(filepath.Join(dir, "my_worktree/.git"), []byte(content), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func writeGitCommonDir(t *testing.T, dir, content string) {
|
||||
err := os.WriteFile(filepath.Join(dir, ".git/worktrees/my_worktree/commondir"), []byte(content), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func verifyCorrectDirs(t *testing.T, dir string) {
|
||||
gitDir, gitCommonDir, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree")))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filepath.Join(dir, ".git/worktrees/my_worktree"), gitDir.Native())
|
||||
assert.Equal(t, filepath.Join(dir, ".git"), gitCommonDir.Native())
|
||||
}
|
||||
|
||||
func TestWorktreeResolveGitDir(t *testing.T) {
|
||||
dir := setupWorktree(t)
|
||||
writeGitCommonDir(t, dir, "../..")
|
||||
|
||||
t.Run("relative", func(t *testing.T) {
|
||||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", "../.git/worktrees/my_worktree"))
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("absolute", func(t *testing.T) {
|
||||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", filepath.Join(dir, ".git/worktrees/my_worktree")))
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("additional spaces", func(t *testing.T) {
|
||||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s \n\n\n", "../.git/worktrees/my_worktree"))
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
writeGitDir(t, dir, "")
|
||||
|
||||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree")))
|
||||
assert.ErrorContains(t, err, ` to contain a line with "gitdir: [...]"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWorktreeResolveCommonDir(t *testing.T) {
|
||||
dir := setupWorktree(t)
|
||||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", "../.git/worktrees/my_worktree"))
|
||||
|
||||
t.Run("relative", func(t *testing.T) {
|
||||
writeGitCommonDir(t, dir, "../..")
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("absolute", func(t *testing.T) {
|
||||
writeGitCommonDir(t, dir, filepath.Join(dir, ".git"))
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("additional spaces", func(t *testing.T) {
|
||||
writeGitCommonDir(t, dir, " ../.. \n\n\n")
|
||||
verifyCorrectDirs(t, dir)
|
||||
})
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
writeGitCommonDir(t, dir, "")
|
||||
|
||||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree")))
|
||||
assert.ErrorContains(t, err, `expected "commondir" file in worktree git folder at `)
|
||||
})
|
||||
|
||||
t.Run("missing", func(t *testing.T) {
|
||||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree")))
|
||||
assert.ErrorContains(t, err, `expected "commondir" file in worktree git folder at `)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue