-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathgit_repo_branch_test.go
More file actions
73 lines (67 loc) · 1.8 KB
/
git_repo_branch_test.go
File metadata and controls
73 lines (67 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package github
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetGithubRepoBranch(t *testing.T) {
tests := []struct {
name string
owner string
repo string
mockStatus int
mockBody any // Using any allows us to pass structs or raw strings
wantBranch string
wantErr bool
}{
{
name: "valid repo",
owner: "octocat",
repo: "Hello-World",
mockStatus: http.StatusOK,
mockBody: map[string]string{"default_branch": "main"},
wantBranch: "main",
wantErr: false,
},
{
name: "valid repo with master branch",
owner: "octocat",
repo: "Hello-Mesh",
mockStatus: http.StatusOK,
mockBody: map[string]string{"default_branch": "master"},
wantBranch: "master",
wantErr: false,
},
{
name: "repo not found",
owner: "octocat",
repo: "NonExistentRepo",
mockStatus: http.StatusNotFound,
mockBody: map[string]string{"message": "Not Found"},
wantBranch: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := fmt.Sprintf("/repos/%s/%s", tt.owner, tt.repo)
if r.URL.Path != expectedPath {
t.Errorf("expected path %s, got %s", expectedPath, r.URL.Path)
}
w.WriteHeader(tt.mockStatus)
json.NewEncoder(w).Encode(tt.mockBody)
}))
defer server.Close()
branch, err := GetDefaultBranchFromGitHub(server.URL, tt.owner, tt.repo, server.Client())
if (err != nil) != tt.wantErr {
t.Fatalf("GetDefaultBranchFromGitHub() unexpected error state: %v", err)
}
if branch != tt.wantBranch {
t.Errorf("got branch %q, want %q", branch, tt.wantBranch)
}
})
}
}