From 9b78b20ed7efa9d4995a7f1ed1c8e21c584fea88 Mon Sep 17 00:00:00 2001 From: "xian-jie.shen" <327411586@qq.com> Date: Sat, 23 Jul 2022 12:11:39 +0800 Subject: [PATCH] feat: pkg/util/github/branch enhancement Signed-off-by: xian-jie.shen <327411586@qq.com> --- pkg/util/github/branch_test.go | 92 +++++++++++++++++++++++++++++++++ pkg/util/github/github_test.go | 10 ++++ pkg/util/github/release_test.go | 36 +++++++------ 3 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 pkg/util/github/branch_test.go diff --git a/pkg/util/github/branch_test.go b/pkg/util/github/branch_test.go new file mode 100644 index 000000000..cc5f0a46c --- /dev/null +++ b/pkg/util/github/branch_test.go @@ -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) + } + }) + } +} diff --git a/pkg/util/github/github_test.go b/pkg/util/github/github_test.go index ed79628de..d9e211b3b 100644 --- a/pkg/util/github/github_test.go +++ b/pkg/util/github/github_test.go @@ -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) diff --git a/pkg/util/github/release_test.go b/pkg/util/github/release_test.go index c1b5a7d5f..31694f0d3 100644 --- a/pkg/util/github/release_test.go +++ b/pkg/util/github/release_test.go @@ -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) {