Compare commits

...

4 Commits

Author SHA1 Message Date
Denis Bilenko 1e7d06cc3e handle os.ErrNotExist; update tests 2024-12-10 16:20:28 +01:00
Denis Bilenko 1789cca2cb fix 2024-12-10 14:28:09 +01:00
Denis Bilenko 122e6f5a4c Add Abs(); clean up 2024-12-10 14:26:12 +01:00
Denis Bilenko dab674bbdb switch to folders.FindDirWithLeaf 2024-12-10 14:23:46 +01:00
6 changed files with 15 additions and 76 deletions

View File

@ -2,6 +2,8 @@ package mutator
import (
"context"
"errors"
"os"
"path/filepath"
"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
info, err := git.FetchRepositoryInfo(ctx, b.BundleRoot.Native(), b.WorkspaceClient())
if err != nil {
diags = append(diags, diag.WarningFromErr(err)...)
if !errors.Is(err, os.ErrNotExist) {
diags = append(diags, diag.WarningFromErr(err)...)
}
}
if info.WorktreeRoot == "" {

View File

@ -18,7 +18,7 @@ import (
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/git"
"github.com/databricks/cli/libs/folders"
"github.com/databricks/cli/libs/template"
"github.com/databricks/databricks-sdk-go"
"github.com/stretchr/testify/require"
@ -144,7 +144,7 @@ func getBundleRemoteRootPath(w *databricks.WorkspaceClient, t *testing.T, unique
}
func blackBoxRun(t *testing.T, root string, args ...string) (stdout string, stderr string) {
gitRoot, err := git.FindLeafInTree(".", ".git")
gitRoot, err := folders.FindDirWithLeaf(".", ".git")
require.NoError(t, err)
t.Setenv("BUNDLE_ROOT", root)

View File

@ -101,7 +101,7 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
assert.NoError(t, err)
} else {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.msg)
assert.ErrorContains(t, err, test.msg)
}
assertEmptyGitInfo(t, info)
})
@ -151,7 +151,7 @@ func TestAccFetchRepositoryInfoDotGit_FromNonGitRepo(t *testing.T) {
for _, input := range tests {
t.Run(input, func(t *testing.T) {
info, err := git.FetchRepositoryInfo(ctx, input, wt.W)
assert.NoError(t, err)
assert.ErrorIs(t, err, os.ErrNotExist)
assertEmptyGitInfo(t, info)
})
}

View File

@ -9,6 +9,10 @@ import (
// FindDirWithLeaf returns the first directory that holds `leaf`,
// traversing up to the root of the filesystem, starting at `dir`.
func FindDirWithLeaf(dir string, leaf string) (string, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
for {
_, err := os.Stat(filepath.Join(dir, leaf))

View File

@ -2,15 +2,12 @@ package git
import (
"context"
"errors"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/folders"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/vfs"
"github.com/databricks/databricks-sdk-go"
@ -105,7 +102,7 @@ func ensureWorkspacePrefix(p string) string {
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
result := RepositoryInfo{}
rootDir, err := FindLeafInTree(path, GitDirectoryName)
rootDir, err := folders.FindDirWithLeaf(path, GitDirectoryName)
if rootDir == "" {
return result, err
}
@ -134,31 +131,3 @@ func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo
return result, nil
}
func FindLeafInTree(p string, leafName string) (string, error) {
p, err := filepath.Abs(p)
if err != nil {
return "", err
}
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
}

View File

@ -1,38 +0,0 @@
package git
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(wd, ".git")
assert.NoError(t, err)
assert.Equal(t, root, out)
}
// Find from project root itself should work.
{
out, err := FindLeafInTree(root, ".git")
assert.NoError(t, err)
assert.Equal(t, root, out)
}
// Find for something that doesn't exist should work.
{
out, err := FindLeafInTree(root, "this-leaf-doesnt-exist-anywhere")
assert.NoError(t, err)
assert.Equal(t, "", out)
}
}