|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/fs" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/databricks/cli/internal/testutil" |
| 12 | + "github.com/databricks/databricks-sdk-go" |
| 13 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 14 | + "github.com/databricks/databricks-sdk-go/qa" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFetchRepositoryInfoFromWorkspace(t *testing.T) { |
| 20 | + dir := setupWorktree(t) |
| 21 | + worktree := filepath.Join(dir, "my_worktree") |
| 22 | + writeGitDir(t, dir, "gitdir: ../.git/worktrees/my_worktree") |
| 23 | + writeGitCommonDir(t, dir, "../..") |
| 24 | + want := RepositoryInfo{ |
| 25 | + OriginURL: "https://example.test/repo.git", |
| 26 | + LatestCommit: strings.Repeat("1", 40), |
| 27 | + CurrentBranch: "projects/task", |
| 28 | + WorktreeRoot: worktree, |
| 29 | + } |
| 30 | + gitDirectoryWant := want |
| 31 | + gitDirectoryWant.WorktreeRoot = dir |
| 32 | + noGitRoot := filepath.VolumeName(dir) + string(filepath.Separator) |
| 33 | + testutil.WriteFile(t, filepath.Join(dir, ".git", "refs", "heads", want.CurrentBranch), want.LatestCommit) |
| 34 | + testutil.WriteFile(t, filepath.Join(dir, ".git", "HEAD"), "ref: refs/heads/"+want.CurrentBranch) |
| 35 | + testutil.WriteFile(t, filepath.Join(dir, ".git", "worktrees", "my_worktree", "HEAD"), "ref: refs/heads/"+want.CurrentBranch) |
| 36 | + testutil.WriteFile(t, filepath.Join(dir, ".git", "config"), "[remote \"origin\"]\n\turl = "+want.OriginURL) |
| 37 | + file := testutil.Touch(t, worktree, "subdir", "README.md") |
| 38 | + malformed := t.TempDir() |
| 39 | + testutil.WriteFile(t, filepath.Join(malformed, ".git"), "invalid gitfile") |
| 40 | + |
| 41 | + for _, tc := range []struct { |
| 42 | + name string |
| 43 | + path string |
| 44 | + body string |
| 45 | + status int |
| 46 | + want RepositoryInfo |
| 47 | + wantErr error |
| 48 | + }{ |
| 49 | + {name: "worktree", path: worktree, want: want}, |
| 50 | + {name: "subdirectory", path: filepath.Dir(file), want: want}, |
| 51 | + { |
| 52 | + name: "workspace metadata takes precedence", path: worktree, |
| 53 | + body: `{"git_info":{"branch":"api-main","head_commit_id":"2222222222222222222222222222222222222222","path":"/Users/me/api","url":"https://example.test/api.git"}}`, |
| 54 | + want: RepositoryInfo{ |
| 55 | + OriginURL: "https://example.test/api.git", LatestCommit: strings.Repeat("2", 40), |
| 56 | + CurrentBranch: "api-main", WorktreeRoot: "/Workspace/Users/me/api", |
| 57 | + }, |
| 58 | + }, |
| 59 | + {name: "git directory", path: dir, want: gitDirectoryWant}, |
| 60 | + {name: "no git metadata", path: noGitRoot, wantErr: fs.ErrNotExist}, |
| 61 | + {name: "malformed gitfile", path: malformed, want: RepositoryInfo{WorktreeRoot: malformed}}, |
| 62 | + { |
| 63 | + name: "permission denied", path: worktree, status: http.StatusForbidden, |
| 64 | + body: `{"error_code":"PERMISSION_DENIED","message":"denied"}`, wantErr: apierr.ErrPermissionDenied, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "workspace path missing", path: worktree, status: http.StatusNotFound, |
| 68 | + body: `{"error_code":"RESOURCE_DOES_NOT_EXIST","message":"missing"}`, wantErr: fs.ErrNotExist, |
| 69 | + }, |
| 70 | + } { |
| 71 | + t.Run(tc.name, func(t *testing.T) { |
| 72 | + body := tc.body |
| 73 | + if body == "" { |
| 74 | + body = `{}` |
| 75 | + } |
| 76 | + cfg, server := qa.HTTPFixtures{{ |
| 77 | + Method: "GET", |
| 78 | + Resource: "/api/2.0/workspace/get-status?path=" + url.QueryEscape(tc.path) + "&return_git_info=true", |
| 79 | + Response: body, |
| 80 | + Status: tc.status, |
| 81 | + }}.Config(t) |
| 82 | + t.Cleanup(server.Close) |
| 83 | + w := databricks.Must(databricks.NewWorkspaceClient((*databricks.Config)(cfg))) |
| 84 | + |
| 85 | + got, err := fetchRepositoryInfoFromWorkspace(t.Context(), tc.path, w) |
| 86 | + require.ErrorIs(t, err, tc.wantErr) |
| 87 | + assert.Equal(t, tc.want, got) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments