Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions pkg/util/github/branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package github

import (
"fmt"
"net/http"
"testing"
)

type newBranchTest struct {
baseTest
baseBranch string
newBranch string
wantErr bool
}

type delBranchTest struct {
baseTest
branch string
wantErr bool
}

func TestClient_NewBranch(t *testing.T) {
respBody := `
{
"ref": "refs/heads/b",
"url": "https://api.github.com/repos/o/r/git/refs/heads/b",
"object": {
"type": "commit",
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
}
}`
mux, serverUrl, teardown := setup(t)
defer teardown()

tests := []newBranchTest{
// TODO: Add test cases.
{
baseTest{"base", getClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""},
"b", "", true,
},
{
baseTest{"base set wrong register url for GetRef api in mock server", getClientWithOption(
t, &Option{Owner: "o", Repo: "r"}, serverUrl,
),
"repos", http.MethodGet, false, "", ""},
"b", "", true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) {
t.Logf("test name: %s, hit path: %s", tt.name, r.URL.Path)
fmt.Fprint(w, respBody)
})
if err := tt.client.NewBranch(tt.baseBranch, tt.newBranch); (err != nil) != tt.wantErr {
t.Errorf("Client.NewBranch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestClient_DeleteBranch(t *testing.T) {
mux, serverUrl, teardown := setup(t)
defer teardown()

tests := []delBranchTest{
// TODO: Add test cases.
{
baseTest{"base", getClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""},
"b", true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, ``)
})
err := tt.client.DeleteBranch(tt.branch)
t.Log(err)
if (err != nil) != tt.wantErr {
t.Errorf("Client.DeleteBranch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/util/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ var (
}
)

type baseTest struct {
name string //test name
client *Client
registerUrl string // url resigtered in mock server
wantMethod string // wanted method in mock server
wantReqBody bool // want request body nor not in mock server
reqBody string // content of request body
respBody string // content of response body
}

func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
Expand Down
36 changes: 19 additions & 17 deletions pkg/util/github/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ import (
"gotest.tools/assert/cmp"
)

type releaseTest struct {
baseTest
wantTag string
wantErr bool
}

func TestClient_GetLatestReleaseTagName(t *testing.T) {
mux, serverUrl, teardown := setup(t)
defer teardown()

tests := []struct {
name string
client *Client
registerUrl string
wantMethod string
wantReqBody bool
reqBody string
respBody string
wantTag string
wantErr bool
}{
{"base err != nil", getClientWithOption(
t, &Option{Owner: ""}, serverUrl,
), "/repos2/o/r/releases/latest", http.MethodGet, false, "", "", "", true},
{"base 200", getClientWithOption(
t, &Option{Owner: "", Org: "o", Repo: "r"}, serverUrl,
), "/repos/o/r/releases/latest", http.MethodGet, false, "", `{"id":3,"tag_name":"v1.0.0"}`, "v1.0.0", false},
tests := []releaseTest{
{
baseTest{"base err != nil", getClientWithOption(
t, &Option{Owner: ""}, serverUrl,
),
"/repos2/o/r/releases/latest", http.MethodGet, false, "", ""},
"", true},
{
baseTest{"base 200", getClientWithOption(
t, &Option{Owner: "", Org: "o", Repo: "r"}, serverUrl,
),
"/repos/o/r/releases/latest", http.MethodGet, false, "", `{"id":3,"tag_name":"v1.0.0"}`},
"v1.0.0", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down