forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghr_test.go
207 lines (176 loc) · 4.83 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
package main
import (
"context"
"io/ioutil"
"testing"
"time"
"github.com/google/go-github/v39/github"
)
func TestGHR_CreateRelease(t *testing.T) {
t.Parallel()
githubClient := testGithubClient(t)
GHR := &GHR{
GitHub: githubClient,
outStream: ioutil.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_CreateReleaseWithExistingRelease(t *testing.T) {
t.Parallel()
githubClient := testGithubClient(t)
GHR := &GHR{
GitHub: githubClient,
outStream: ioutil.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: ioutil.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)
}
}