forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghr_test.go
243 lines (203 loc) · 5.65 KB
/
ghr_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
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"io"
"testing"
"time"
"github.com/google/go-github/v55/github"
)
func TestGHR_CreateRelease(t *testing.T) {
t.Parallel()
githubClient := testGithubClient(t)
ghr := &GHR{
GitHub: githubClient,
outStream: io.Discard,
}
testTag := "create-release"
req := &github.RepositoryRelease{
TagName: &testTag,
Draft: github.Bool(false),
Body: github.String("This is test release"),
}
recreate := false
release, err := ghr.CreateRelease(context.TODO(), req, recreate)
if err != nil {
t.Fatal("CreateRelease failed:", err)
}
defer ghr.DeleteRelease(context.TODO(), *release.ID, testTag)
}
func TestGHR_GetLatestRelease(t *testing.T) {
t.Parallel()
githubClient := testGithubClient(t)
ghr := &GHR{
GitHub: githubClient,
outStream: io.Discard,
}
testTag := "v1.2.3"
existingReq := &github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
MakeLatest: github.String("true"),
}
newRelease, err := githubClient.CreateRelease(context.TODO(), existingReq)
if err != nil {
t.Fatalf("CreateRelease failed: %s", err)
}
// Create an existing release before
existing, err := ghr.GetLatestRelease(context.TODO())
if err != nil {
t.Fatalf("GetLatestRelease failed: %s", err)
}
if newRelease.Name != existing.Name {
t.Fatalf("GetLatestRelease got release %s instead of release %s", *existing.Name, *newRelease.Name)
}
defer ghr.DeleteRelease(context.TODO(), *existing.ID, testTag)
}
func TestGHR_CreateReleaseWithExistingRelease(t *testing.T) {
t.Parallel()
githubClient := testGithubClient(t)
ghr := &GHR{
GitHub: githubClient,
outStream: io.Discard,
}
testTag := "create-with-existing"
existingReq := &github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
}
cases := []struct {
request *github.RepositoryRelease
recreate bool
newRelease bool
}{
// 0: When same tag as existing release is used
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
},
false,
false,
},
// 1: When draft release is requested
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(true),
},
false,
true,
},
// 2: When recreate is requtested
{
&github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(false),
},
true,
true,
},
// 3: When different tag is requtested
{
&github.RepositoryRelease{
TagName: github.String("v2.0.0"),
Draft: github.Bool(false),
},
false,
true,
},
}
for i, tc := range cases {
// Prevent a lot of requests at same time to GitHub API
time.Sleep(1 * time.Second)
// Create an existing release before
existing, err := githubClient.CreateRelease(context.TODO(), existingReq)
if err != nil {
t.Fatalf("#%d CreateRelease failed: %s", i, err)
}
// Create a release for THIS TEST
created, err := ghr.CreateRelease(context.TODO(), tc.request, tc.recreate)
if err != nil {
t.Fatalf("#%d GHR.CreateRelease failed: %s", i, err)
}
// Clean up existing release
if !tc.recreate {
err = ghr.DeleteRelease(context.TODO(), *existing.ID, *existingReq.TagName)
if err != nil {
t.Fatalf("#%d GHR.DeleteRelease (existing) failed: %s", i, err)
}
}
if !tc.newRelease {
if *created.ID != *existing.ID {
t.Fatalf("#%d created ID %d, want %d (same as existing release ID)",
i, *created.ID, *existing.ID)
}
continue
}
// Clean up newly created release before. When draft request,
// tag is not created. So it need to be deleted separately by Github client.
if *tc.request.Draft {
// Clean up newly created release before checking
if err := githubClient.DeleteRelease(context.TODO(), *created.ID); err != nil {
t.Fatalf("#%d GitHub.DeleteRelease (created) failed: %s", i, err)
}
} else {
err := ghr.DeleteRelease(context.TODO(), *created.ID, *tc.request.TagName)
if err != nil {
t.Fatalf("#%d GHR.DeleteRelease (created) failed: %s", i, err)
}
}
if *created.ID == *existing.ID {
t.Fatalf("#%d expect created ID %d to be different from existing ID %d",
i, *created.ID, *existing.ID)
}
}
}
func TestGHR_UploadAssets(t *testing.T) {
githubClient := testGithubClient(t)
ghr := &GHR{
GitHub: githubClient,
outStream: io.Discard,
}
testTag := "ghr-upload-assets"
req := &github.RepositoryRelease{
TagName: github.String(testTag),
Draft: github.Bool(true),
}
// Create an existing release before
release, err := githubClient.CreateRelease(context.TODO(), req)
if err != nil {
t.Fatalf("CreateRelease failed: %s", err)
}
defer func() {
if err := githubClient.DeleteRelease(context.TODO(), *release.ID); err != nil {
t.Fatal("DeleteRelease failed:", err)
}
}()
localTestAssets, err := LocalAssets(TestDir)
if err != nil {
t.Fatal("LocalAssets failed:", err)
}
if err := ghr.UploadAssets(context.TODO(), *release.ID, localTestAssets, 4); err != nil {
t.Fatal("GHR.UploadAssets failed:", err)
}
assets, err := githubClient.ListAssets(context.TODO(), *release.ID)
if err != nil {
t.Fatal("ListAssets failed:", err)
}
if got, want := len(assets), 4; got != want {
t.Fatalf("upload assets number = %d, want %d", got, want)
}
// Delete all assets
parallel := 4
if err := ghr.DeleteAssets(context.TODO(), *release.ID, localTestAssets, parallel); err != nil {
t.Fatal("GHR.DeleteAssets failed:", err)
}
assets, err = githubClient.ListAssets(context.TODO(), *release.ID)
if err != nil {
t.Fatal("ListAssets failed:", err)
}
if got, want := len(assets), 0; got != want {
t.Fatalf("upload assets number = %d, want %d", got, want)
}
}