mirror of https://github.com/databricks/cli.git
Switch to `folders.FindDirWithLeaf` (#1963)
## Changes Remove two duplicate implementations of the same logic, switch everywhere to folders.FindDirWithLeaf. Add Abs() call to FindDirWithLeaf, it cannot really work on relative paths. ## Tests Existing tests.
This commit is contained in:
parent
67f08ba924
commit
4236e7122f
|
@ -2,6 +2,8 @@ package mutator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
|
@ -24,7 +26,9 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
|
||||||
var diags diag.Diagnostics
|
var diags diag.Diagnostics
|
||||||
info, err := git.FetchRepositoryInfo(ctx, b.BundleRoot.Native(), b.WorkspaceClient())
|
info, err := git.FetchRepositoryInfo(ctx, b.BundleRoot.Native(), b.WorkspaceClient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
diags = append(diags, diag.WarningFromErr(err)...)
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
diags = append(diags, diag.WarningFromErr(err)...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.WorktreeRoot == "" {
|
if info.WorktreeRoot == "" {
|
||||||
|
|
|
@ -18,8 +18,8 @@ import (
|
||||||
"github.com/databricks/cli/libs/env"
|
"github.com/databricks/cli/libs/env"
|
||||||
"github.com/databricks/cli/libs/filer"
|
"github.com/databricks/cli/libs/filer"
|
||||||
"github.com/databricks/cli/libs/flags"
|
"github.com/databricks/cli/libs/flags"
|
||||||
|
"github.com/databricks/cli/libs/folders"
|
||||||
"github.com/databricks/cli/libs/template"
|
"github.com/databricks/cli/libs/template"
|
||||||
"github.com/databricks/cli/libs/vfs"
|
|
||||||
"github.com/databricks/databricks-sdk-go"
|
"github.com/databricks/databricks-sdk-go"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -144,15 +144,14 @@ func getBundleRemoteRootPath(w *databricks.WorkspaceClient, t *testing.T, unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func blackBoxRun(t *testing.T, root string, args ...string) (stdout string, stderr string) {
|
func blackBoxRun(t *testing.T, root string, args ...string) (stdout string, stderr string) {
|
||||||
cwd := vfs.MustNew(".")
|
gitRoot, err := folders.FindDirWithLeaf(".", ".git")
|
||||||
gitRoot, err := vfs.FindLeafInTree(cwd, ".git")
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Setenv("BUNDLE_ROOT", root)
|
t.Setenv("BUNDLE_ROOT", root)
|
||||||
|
|
||||||
// Create the command
|
// Create the command
|
||||||
cmd := exec.Command("go", append([]string{"run", "main.go"}, args...)...)
|
cmd := exec.Command("go", append([]string{"run", "main.go"}, args...)...)
|
||||||
cmd.Dir = gitRoot.Native()
|
cmd.Dir = gitRoot
|
||||||
|
|
||||||
// Create buffers to capture output
|
// Create buffers to capture output
|
||||||
var outBuffer, errBuffer bytes.Buffer
|
var outBuffer, errBuffer bytes.Buffer
|
||||||
|
|
|
@ -101,7 +101,7 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
} else {
|
} else {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), test.msg)
|
assert.ErrorContains(t, err, test.msg)
|
||||||
}
|
}
|
||||||
assertEmptyGitInfo(t, info)
|
assertEmptyGitInfo(t, info)
|
||||||
})
|
})
|
||||||
|
@ -151,7 +151,7 @@ func TestAccFetchRepositoryInfoDotGit_FromNonGitRepo(t *testing.T) {
|
||||||
for _, input := range tests {
|
for _, input := range tests {
|
||||||
t.Run(input, func(t *testing.T) {
|
t.Run(input, func(t *testing.T) {
|
||||||
info, err := git.FetchRepositoryInfo(ctx, input, wt.W)
|
info, err := git.FetchRepositoryInfo(ctx, input, wt.W)
|
||||||
assert.NoError(t, err)
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||||
assertEmptyGitInfo(t, info)
|
assertEmptyGitInfo(t, info)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,10 @@ import (
|
||||||
// FindDirWithLeaf returns the first directory that holds `leaf`,
|
// FindDirWithLeaf returns the first directory that holds `leaf`,
|
||||||
// traversing up to the root of the filesystem, starting at `dir`.
|
// traversing up to the root of the filesystem, starting at `dir`.
|
||||||
func FindDirWithLeaf(dir string, leaf string) (string, error) {
|
func FindDirWithLeaf(dir string, leaf string) (string, error) {
|
||||||
|
dir, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
_, err := os.Stat(filepath.Join(dir, leaf))
|
_, err := os.Stat(filepath.Join(dir, leaf))
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,12 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/databricks/cli/libs/dbr"
|
"github.com/databricks/cli/libs/dbr"
|
||||||
|
"github.com/databricks/cli/libs/folders"
|
||||||
"github.com/databricks/cli/libs/log"
|
"github.com/databricks/cli/libs/log"
|
||||||
"github.com/databricks/cli/libs/vfs"
|
"github.com/databricks/cli/libs/vfs"
|
||||||
"github.com/databricks/databricks-sdk-go"
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
@ -105,7 +102,7 @@ func ensureWorkspacePrefix(p string) string {
|
||||||
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
|
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
|
||||||
result := RepositoryInfo{}
|
result := RepositoryInfo{}
|
||||||
|
|
||||||
rootDir, err := findLeafInTree(path, GitDirectoryName)
|
rootDir, err := folders.FindDirWithLeaf(path, GitDirectoryName)
|
||||||
if rootDir == "" {
|
if rootDir == "" {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
@ -134,28 +131,3 @@ func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findLeafInTree(p string, leafName string) (string, error) {
|
|
||||||
var err error
|
|
||||||
for i := 0; i < 10000; i++ {
|
|
||||||
_, err = os.Stat(filepath.Join(p, leafName))
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
// Found [leafName] in p
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrNotExist means we continue traversal up the tree.
|
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
|
||||||
parent := filepath.Dir(p)
|
|
||||||
if parent == p {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
p = parent
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package vfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io/fs"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FindLeafInTree returns the first path that holds `name`,
|
|
||||||
// traversing up to the root of the filesystem, starting at `p`.
|
|
||||||
func FindLeafInTree(p Path, name string) (Path, error) {
|
|
||||||
for p != nil {
|
|
||||||
_, err := fs.Stat(p, name)
|
|
||||||
|
|
||||||
// No error means we found the leaf in p.
|
|
||||||
if err == nil {
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrNotExist means we continue traversal up the tree.
|
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
|
||||||
p = p.Parent()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fs.ErrNotExist
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package vfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFindLeafInTree(t *testing.T) {
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
root := filepath.Join(wd, "..", "..")
|
|
||||||
|
|
||||||
// Find from working directory should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(wd), ".git")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, root, out.Native())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find from project root itself should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(root), ".git")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, root, out.Native())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find for something that doesn't exist should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(root), "this-leaf-doesnt-exist-anywhere")
|
|
||||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
||||||
assert.Equal(t, nil, out)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue