From 78df035aad409e026ad9a9474e4a5b640c5f987c Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Mon, 2 Dec 2024 15:00:26 +0100 Subject: [PATCH] add test for FetchRepositoryInfo --- internal/git_fetch_test.go | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 internal/git_fetch_test.go diff --git a/internal/git_fetch_test.go b/internal/git_fetch_test.go new file mode 100644 index 00000000..cf3015ad --- /dev/null +++ b/internal/git_fetch_test.go @@ -0,0 +1,76 @@ +package internal + +import ( + "os/exec" + "path/filepath" + "testing" + + "github.com/databricks/cli/internal/acc" + "github.com/databricks/cli/libs/dbr" + "github.com/databricks/cli/libs/git" + "github.com/databricks/cli/libs/vfs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const examplesRepoUrl = "https://github.com/databricks/bundle-examples" +const examplesRepoProvider = "gitHub" + +func assertGitInfo(t *testing.T, info git.GitRepositoryInfo, expectedRoot string) { + assert.Equal(t, "main", info.CurrentBranch) + assert.NotEmpty(t, info.LatestCommit) + assert.Equal(t, examplesRepoUrl, info.OriginURL) + assert.Equal(t, expectedRoot, info.WorktreeRoot.Native()) +} + +func TestFetchRepositoryInfoAPI(t *testing.T) { + ctx, wt := acc.WorkspaceTest(t) + me, err := wt.W.CurrentUser.Me(ctx) + require.NoError(t, err) + + targetPath := acc.RandomName("/Workspace/Users/" + me.UserName + "/testing-clone-bundle-examples-") + stdout, stderr := RequireSuccessfulRun(t, "repos", "create", examplesRepoUrl, examplesRepoProvider, "--path", targetPath) + defer RequireSuccessfulRun(t, "repos", "delete", targetPath) + + assert.Empty(t, stderr.String()) + assert.NotEmpty(t, stdout.String()) + ctx = dbr.MockRuntime(ctx, true) + + for _, inputPath := range []string{ + targetPath + "/knowledge_base/dashboard_nyc_taxi", + targetPath, + } { + t.Run(inputPath, func(t *testing.T) { + info, err := git.FetchRepositoryInfo(ctx, vfs.MustNew(inputPath), wt.W) + assert.NoError(t, err) + assertGitInfo(t, info, targetPath) + }) + } +} + +func TestFetchRepositoryInfoDotGit(t *testing.T) { + ctx, wt := acc.WorkspaceTest(t) + + repo := cloneRepoLocally(t, examplesRepoUrl) + + for _, inputPath := range []string{ + repo + "/knowledge_base/dashboard_nyc_taxi", + repo, + } { + t.Run(inputPath, func(t *testing.T) { + info, err := git.FetchRepositoryInfo(ctx, vfs.MustNew(inputPath), wt.W) + assert.NoError(t, err) + assertGitInfo(t, info, repo) + }) + } +} + +func cloneRepoLocally(t *testing.T, repoUrl string) string { + tempDir := t.TempDir() + localRoot := filepath.Join(tempDir, "repo") + + cmd := exec.Command("git", "clone", "--depth=1", examplesRepoUrl, localRoot) + err := cmd.Run() + require.NoError(t, err) + return localRoot +}