-
Notifications
You must be signed in to change notification settings - Fork 127
Properly read Git metadata when running inside workspace #1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
31db923
Properly read git metadata when inside Workspace
denik edac960
Update bundle/bundle.go
denik 4fa99d0
update comment
denik 0ba2314
add test for FetchRepositoryInfo
denik b4251d7
make methods private
denik e49f2f3
rename fixResponsePath to ensureWorkspacePrefix
denik 97c8042
reorder to improve readability
denik 80d9b6a
Expand comment on WorktreeRoot
denik 4250251
use t.Cleanup instead of defer
denik 017836f
prefix integration tests with TestAcc
denik 242b53a
remove incorrect info from the comment
denik d204a93
use path.Join to add /Workspace prefix
denik 8fe958f
Do not fail on git operations in bundle and sync, log warnings instea…
denik 4d38475
refactor test code a bit - extract helper assert groups
denik 8a31be7
Add GuessedWorktreeRoot field; this simplifies usage on the caller si…
denik e7a49a2
clarify comment
denik c4c7a87
expand FileSet tests with cases root != worktreeRoot
denik a623cfd
Switch to string from vfs.Path in the signature
denik d0b088a
s/.git/[leafName]
denik 5131488
clean up comment
denik 1c3f5f2
clean up GuessedWorktreeRoot field; let the caller do the guessing
denik 2d3ee18
don't use result in case of an error
denik a689396
s/GitRepositoryInfo/RepositoryInfo
denik a064d91
use Join instead of +
denik 43da63b
lowercase variable
denik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/internal/acc" | ||
| "github.com/databricks/cli/libs/dbr" | ||
| "github.com/databricks/cli/libs/git" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const examplesRepoUrl = "https://github.com/databricks/bundle-examples" | ||
| const examplesRepoProvider = "gitHub" | ||
|
|
||
| func assertFullGitInfo(t *testing.T, expectedRoot string, info git.RepositoryInfo) { | ||
| assert.Equal(t, "main", info.CurrentBranch) | ||
| assert.NotEmpty(t, info.LatestCommit) | ||
| assert.Equal(t, examplesRepoUrl, info.OriginURL) | ||
| assert.Equal(t, expectedRoot, info.WorktreeRoot) | ||
| } | ||
|
|
||
| func assertEmptyGitInfo(t *testing.T, info git.RepositoryInfo) { | ||
| assertSparseGitInfo(t, "", info) | ||
| } | ||
|
|
||
| func assertSparseGitInfo(t *testing.T, expectedRoot string, info git.RepositoryInfo) { | ||
| assert.Equal(t, "", info.CurrentBranch) | ||
| assert.Equal(t, "", info.LatestCommit) | ||
| assert.Equal(t, "", info.OriginURL) | ||
| assert.Equal(t, expectedRoot, info.WorktreeRoot) | ||
| } | ||
|
|
||
| func TestAccFetchRepositoryInfoAPI_FromRepo(t *testing.T) { | ||
| ctx, wt := acc.WorkspaceTest(t) | ||
| me, err := wt.W.CurrentUser.Me(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| targetPath := acc.RandomName(path.Join("/Workspace/Users", me.UserName, "/testing-clone-bundle-examples-")) | ||
| stdout, stderr := RequireSuccessfulRun(t, "repos", "create", examplesRepoUrl, examplesRepoProvider, "--path", targetPath) | ||
| t.Cleanup(func() { | ||
| RequireSuccessfulRun(t, "repos", "delete", targetPath) | ||
| }) | ||
|
|
||
| assert.Empty(t, stderr.String()) | ||
| assert.NotEmpty(t, stdout.String()) | ||
| ctx = dbr.MockRuntime(ctx, true) | ||
|
|
||
| for _, inputPath := range []string{ | ||
| path.Join(targetPath, "knowledge_base/dashboard_nyc_taxi"), | ||
| targetPath, | ||
| } { | ||
| t.Run(inputPath, func(t *testing.T) { | ||
| info, err := git.FetchRepositoryInfo(ctx, inputPath, wt.W) | ||
| assert.NoError(t, err) | ||
| assertFullGitInfo(t, targetPath, info) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) { | ||
| ctx, wt := acc.WorkspaceTest(t) | ||
| me, err := wt.W.CurrentUser.Me(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| rootPath := acc.RandomName(path.Join("/Workspace/Users", me.UserName, "testing-nonrepo-")) | ||
| _, stderr := RequireSuccessfulRun(t, "workspace", "mkdirs", path.Join(rootPath, "a/b/c")) | ||
| t.Cleanup(func() { | ||
| RequireSuccessfulRun(t, "workspace", "delete", "--recursive", rootPath) | ||
| }) | ||
|
|
||
| assert.Empty(t, stderr.String()) | ||
| ctx = dbr.MockRuntime(ctx, true) | ||
|
|
||
| tests := []struct { | ||
| input string | ||
| msg string | ||
denik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }{ | ||
| { | ||
| input: path.Join(rootPath, "a/b/c"), | ||
| msg: "", | ||
| }, | ||
| { | ||
| input: rootPath, | ||
| msg: "", | ||
| }, | ||
| { | ||
| input: path.Join(rootPath, "/non-existent"), | ||
| msg: "doesn't exist", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.input+" <==> "+test.msg, func(t *testing.T) { | ||
| info, err := git.FetchRepositoryInfo(ctx, test.input, wt.W) | ||
| if test.msg == "" { | ||
| assert.NoError(t, err) | ||
| } else { | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), test.msg) | ||
| } | ||
| assertEmptyGitInfo(t, info) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAccFetchRepositoryInfoDotGit_FromGitRepo(t *testing.T) { | ||
| ctx, wt := acc.WorkspaceTest(t) | ||
|
|
||
| repo := cloneRepoLocally(t, examplesRepoUrl) | ||
|
|
||
| for _, inputPath := range []string{ | ||
| filepath.Join(repo, "knowledge_base/dashboard_nyc_taxi"), | ||
| repo, | ||
| } { | ||
| t.Run(inputPath, func(t *testing.T) { | ||
| info, err := git.FetchRepositoryInfo(ctx, inputPath, wt.W) | ||
| assert.NoError(t, err) | ||
| assertFullGitInfo(t, repo, info) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| func TestAccFetchRepositoryInfoDotGit_FromNonGitRepo(t *testing.T) { | ||
| ctx, wt := acc.WorkspaceTest(t) | ||
|
|
||
| tempDir := t.TempDir() | ||
| root := filepath.Join(tempDir, "repo") | ||
| require.NoError(t, os.MkdirAll(filepath.Join(root, "a/b/c"), 0700)) | ||
|
|
||
| tests := []string{ | ||
| filepath.Join(root, "a/b/c"), | ||
| root, | ||
| filepath.Join(root, "/non-existent"), | ||
| } | ||
denik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for _, input := range tests { | ||
| t.Run(input, func(t *testing.T) { | ||
| info, err := git.FetchRepositoryInfo(ctx, input, wt.W) | ||
| assert.NoError(t, err) | ||
| assertEmptyGitInfo(t, info) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAccFetchRepositoryInfoDotGit_FromBrokenGitRepo(t *testing.T) { | ||
| ctx, wt := acc.WorkspaceTest(t) | ||
|
|
||
| tempDir := t.TempDir() | ||
| root := filepath.Join(tempDir, "repo") | ||
| path := filepath.Join(root, "a/b/c") | ||
| require.NoError(t, os.MkdirAll(path, 0700)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(root, ".git"), []byte(""), 0000)) | ||
|
|
||
| info, err := git.FetchRepositoryInfo(ctx, path, wt.W) | ||
| assert.NoError(t, err) | ||
| assertSparseGitInfo(t, root, info) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.