forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_test.go
233 lines (194 loc) · 5.37 KB
/
github_test.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"bytes"
"context"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/google/go-github/v39/github"
)
const (
// EnvRepoOwner is an environment var for the repository owner on which the github release test is performed
EnvRepoOwner = "TEST_REPO_OWNER"
// EnvRepoName is an environment var for the name of repository on which the github release test is performed
EnvRepoName = "TEST_REPO_NAME"
)
var (
TestOwner = "ghtools"
TestRepo = "github-api-test"
)
func TestMain(m *testing.M) {
if owner := os.Getenv(EnvRepoOwner); owner != "" {
TestOwner = owner
}
if repo := os.Getenv(EnvRepoName); repo != "" {
TestRepo = repo
}
code := m.Run()
os.Exit(code)
}
func testGithubClient(t *testing.T) GitHub {
token := os.Getenv(EnvGitHubToken)
if token == "" {
if os.Getenv("CI") != "" {
t.Skipf("The %s environment value is not configured. skip it.", EnvGitHubToken)
} else {
t.Fatalf("The %s environment value is not configured. To skip it, set CI=true", EnvGitHubToken)
}
}
client, err := NewGitHubClient(TestOwner, TestRepo, token, defaultBaseURL)
if err != nil {
t.Fatal("NewGitHubClient failed:", err)
}
return client
}
func TestGitHubClient(t *testing.T) {
t.Parallel()
c := testGithubClient(t)
testTag := "github-client"
cases := []struct {
Request *github.RepositoryRelease
}{
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
Prerelease: github.Bool(false),
},
},
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
Prerelease: github.Bool(true),
},
},
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(true),
Prerelease: github.Bool(false),
},
},
}
for i, tc := range cases {
// Prevent a lot of requests at same time to GitHub API
time.Sleep(1 * time.Second)
created, err := c.CreateRelease(context.TODO(), tc.Request)
if err != nil {
t.Fatalf("#%d CreateRelease failed: %s", i, err)
}
// Draft release doesn't create tag. So it's not found.
if !*created.Draft {
got, err := c.GetRelease(context.TODO(), *created.TagName)
if err != nil {
t.Fatalf("#%d GetRelease failed: %s", i, err)
}
if *got.ID != *created.ID {
t.Fatalf("got ID = %d, want %d", *got.ID, *created.ID)
}
}
if err := c.DeleteRelease(context.TODO(), *created.ID); err != nil {
t.Fatalf("#%d DeleteRelease failed: %s", i, err)
}
if *created.Draft {
continue
}
if err := c.DeleteTag(context.TODO(), *tc.Request.TagName); err != nil {
t.Fatalf("#%d DeleteTag failed: %s", i, err)
}
}
}
func TestGitHubClient_Upload(t *testing.T) {
client := testGithubClient(t)
testTag := "github-client-upload-asset"
req := &github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(true),
}
release, err := client.CreateRelease(context.TODO(), req)
if err != nil {
t.Fatal("CreateRelease failed:", err)
}
defer func() {
if err := client.DeleteRelease(context.TODO(), *release.ID); err != nil {
t.Fatalf("DeleteRelease failed: %s", err)
}
}()
filename := filepath.Join("./testdata", "darwin_386")
asset, err := client.UploadAsset(context.TODO(), *release.ID, filename)
if err != nil {
t.Fatal("UploadAsset failed:", err)
}
githubClient, ok := client.(*GitHubClient)
if !ok {
t.Fatal("Failed to asset to GithubClient")
}
rc, url, err := githubClient.Repositories.DownloadReleaseAsset(
context.TODO(), githubClient.Owner, githubClient.Repo, *asset.ID, http.DefaultClient)
if err != nil {
t.Fatal("DownloadReleaseAsset failed:", err)
}
var buf bytes.Buffer
if len(url) != 0 {
res, err := http.Get(url)
if err != nil {
t.Fatal("http.Get failed:", err)
}
if _, err := io.Copy(&buf, res.Body); err != nil {
t.Fatal("Copy failed:", err)
}
res.Body.Close()
} else {
if _, err := io.Copy(&buf, rc); err != nil {
t.Fatal("Copy failed:", err)
}
rc.Close()
}
if got, want := buf.String(), "darwin_386\n"; got != want {
t.Fatalf("file body is %q, want %q", got, want)
}
}
func TestGitHubClient_ListAssets(t *testing.T) {
client := testGithubClient(t)
testTag := "github-list-assets"
req := &github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(true),
}
release, err := client.CreateRelease(context.TODO(), req)
if err != nil {
t.Fatal("CreateRelease failed:", err)
}
defer func() {
if err := client.DeleteRelease(context.TODO(), *release.ID); err != nil {
t.Fatalf("DeleteRelease failed: %s", err)
}
}()
for _, filename := range []string{"darwin_386", "darwin_amd64"} {
filename := filepath.Join("./testdata", filename)
if _, err := client.UploadAsset(context.TODO(), *release.ID, filename); err != nil {
t.Fatal("UploadAsset failed:", err)
}
}
assets, err := client.ListAssets(context.TODO(), *release.ID)
if err != nil {
t.Fatal("ListAssets failed:", err)
}
if got, want := len(assets), 2; got != want {
t.Fatalf("ListAssets number = %d, want %d", got, want)
}
if err := client.DeleteAsset(context.TODO(), *assets[0].ID); err != nil {
t.Fatal("DeleteAsset failed:", err)
}
assets, err = client.ListAssets(context.TODO(), *release.ID)
if err != nil {
t.Fatal("ListAssets failed:", err)
}
if got, want := len(assets), 1; got != want {
t.Fatalf("ListAssets number = %d, want %d", got, want)
}
}