-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathout_command_test.go
More file actions
368 lines (321 loc) · 10.5 KB
/
out_command_test.go
File metadata and controls
368 lines (321 loc) · 10.5 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package resource_test
import (
"io"
"os"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
gitlab "gitlab.com/gitlab-org/api/client-go"
resource "github.com/orange-cloudfoundry/gitlab-release-resource"
"github.com/orange-cloudfoundry/gitlab-release-resource/fakes"
)
func file(path, contents string) {
Ω(os.WriteFile(path, []byte(contents), 0644)).Should(Succeed())
}
var _ = Describe("Out Command", func() {
var (
command *resource.OutCommand
gitlabClient *fakes.FakeGitLab
sourcesDir string
request resource.OutRequest
)
BeforeEach(func() {
var err error
gitlabClient = &fakes.FakeGitLab{}
command = resource.NewOutCommand(gitlabClient, io.Discard)
sourcesDir, err = os.MkdirTemp("", "gitlab-release")
Ω(err).ShouldNot(HaveOccurred())
gitlabClient.CreateReleaseStub = func(name string, tag string, body *string) (*gitlab.Release, error) {
createdRel := gitlab.Release{}
createdRel.Name = name
if body != nil {
createdRel.Description = *body
}
createdRel.TagName = tag
createdRel.Commit.ID = "a2f4a3"
return &createdRel, nil
}
gitlabClient.CreateTagStub = func(name string, ref string) (*gitlab.Tag, error) {
return &gitlab.Tag{
Commit: &gitlab.Commit{
ID: ref,
ShortID: ref,
},
}, nil
}
gitlabClient.UpdateReleaseStub = func(name string, tag string, body *string) (*gitlab.Release, error) {
return gitlabClient.CreateReleaseStub(name, tag, body)
}
gitlabClient.UploadProjectFileStub = func(file string) (*gitlab.ProjectMarkdownUploadedFile, error) {
return &gitlab.ProjectMarkdownUploadedFile{
URL: "/base/" + filepath.Base(file),
}, nil
}
gitlabClient.GetReleaseLinksStub = func(tag string) ([]*gitlab.ReleaseLink, error) {
return []*gitlab.ReleaseLink{}, nil
}
gitlabClient.CreateReleaseLinkStub = func(tag string, name string, url string) (*gitlab.ReleaseLink, error) {
return &gitlab.ReleaseLink{
URL: url,
Name: name,
}, nil
}
globMatching := filepath.Join(sourcesDir, "great-file.tgz")
globNotMatching := filepath.Join(sourcesDir, "bad-file.txt")
file(globMatching, "matching")
file(globNotMatching, "not matching")
})
AfterEach(func() {
Ω(os.RemoveAll(sourcesDir)).Should(Succeed())
})
Context("when the release has already been created", func() {
assetsLinks1 := []*gitlab.ReleaseLink{
{
ID: 456789,
Name: "unicorns.txt",
},
{
ID: 3450798,
Name: "rainbows.txt",
},
}
assetsLinks2 := []*gitlab.ReleaseLink{
{
ID: 23,
Name: "rainbow.txt",
},
}
existingReleases := []*gitlab.Release{
{
TagName: "v0.3.12",
Name: "v0.3.12-name",
Description: "basic body1",
},
{
TagName: "tag2",
Name: "name2",
Description: "basic body2",
},
}
// damn anonymous structs...
existingReleases[0].Assets.Links = assetsLinks1
existingReleases[1].Assets.Links = assetsLinks2
BeforeEach(func() {
gitlabClient.ListReleasesStub = func() ([]*gitlab.Release, error) {
return existingReleases, nil
}
gitlabClient.GetReleaseStub = func(tag string) (*gitlab.Release, error) {
for _, r := range existingReleases {
if r.TagName == tag {
return r, nil
}
}
return nil, resource.ErrNotFound
}
gitlabClient.GetReleaseLinksStub = func(tag string) ([]*gitlab.ReleaseLink, error) {
for _, r := range existingReleases {
if tag == r.TagName {
return r.Assets.Links, nil
}
}
return nil, resource.ErrNotFound
}
namePath := filepath.Join(sourcesDir, "name")
bodyPath := filepath.Join(sourcesDir, "body")
tagPath := filepath.Join(sourcesDir, "tag")
file(tagPath, "v0.3.12")
file(namePath, "v0.3.12-newname")
file(bodyPath, "this is a great release")
request = resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
BodyPath: "body",
TagPath: "tag",
},
}
})
It("deletes the existing assets", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.GetReleaseLinksCallCount()).Should(Equal(1))
Ω(gitlabClient.GetReleaseLinksArgsForCall(0)).Should(Equal(existingReleases[0].TagName))
Ω(gitlabClient.DeleteReleaseLinkCallCount()).Should(Equal(2))
arg1, arg2 := gitlabClient.DeleteReleaseLinkArgsForCall(0)
Ω(arg1).Should(Equal(existingReleases[0].TagName))
Ω(arg2.ID).Should(Equal(assetsLinks1[0].ID))
})
Context("when a body is not supplied", func() {
BeforeEach(func() {
request.Params.BodyPath = ""
})
It("does not blow away the body", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.UpdateReleaseCallCount()).Should(Equal(1))
name, tag, body := gitlabClient.UpdateReleaseArgsForCall(0)
Ω(name).Should(Equal("v0.3.12-newname"))
Ω(tag).Should(Equal("v0.3.12"))
Ω(body).Should(BeNil())
})
})
Context("when a commitish is not supplied", func() {
It("updates the existing release", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.UpdateReleaseCallCount()).Should(Equal(1))
name, tag, body := gitlabClient.UpdateReleaseArgsForCall(0)
Ω(tag).Should(Equal("v0.3.12"))
Ω(name).Should(Equal("v0.3.12-newname"))
Ω(*body).Should(Equal("this is a great release"))
})
})
Context("when a commitish is supplied", func() {
BeforeEach(func() {
commitishPath := filepath.Join(sourcesDir, "commitish")
file(commitishPath, "1z22f1")
request.Params.CommitishPath = "commitish"
})
It("does not updates the existing release", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.CreateTagCallCount()).Should(Equal(0))
})
})
})
Context("when the release has not already been created", func() {
BeforeEach(func() {
gitlabClient.GetReleaseStub = func(tag string) (*gitlab.Release, error) {
return nil, resource.ErrNotFound
}
namePath := filepath.Join(sourcesDir, "name")
tagPath := filepath.Join(sourcesDir, "tag")
bodyPath := filepath.Join(sourcesDir, "body")
file(namePath, "v0.3.13")
file(tagPath, "v0.3.13")
file(bodyPath, "*markdown*")
request = resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
TagPath: "tag",
BodyPath: "body",
},
}
})
Context("when the underlying tag has not already been created", func() {
BeforeEach(func() {
gitlabClient.GetTagStub = func(name string) (*gitlab.Tag, error) {
return nil, resource.ErrNotFound
}
})
Context("with a commitish", func() {
BeforeEach(func() {
commitishPath := filepath.Join(sourcesDir, "commitish")
file(commitishPath, "a2f4a3")
request.Params.CommitishPath = "commitish"
})
It("creates a release on gitlab with the tag on the commitish", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.CreateTagCallCount()).Should(Equal(1))
tagName, ref := gitlabClient.CreateTagArgsForCall(0)
Ω(tagName).Should(Equal(tagName))
Ω(ref).Should(Equal("a2f4a3"))
Ω(gitlabClient.CreateReleaseCallCount()).Should(Equal(1))
name, tag, body := gitlabClient.CreateReleaseArgsForCall(0)
Ω(name).Should(Equal("v0.3.13"))
Ω(tag).Should(Equal("v0.3.13"))
Ω(*body).Should(Equal("*markdown*"))
})
It("has some sweet metadata", func() {
outResponse, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(outResponse.Metadata).Should(ConsistOf(
resource.MetadataPair{Name: "tag", Value: "v0.3.13"},
resource.MetadataPair{Name: "name", Value: "v0.3.13"},
resource.MetadataPair{Name: "body", Value: "*markdown*", Markdown: true},
resource.MetadataPair{Name: "commit_sha", Value: "a2f4a3"},
))
})
})
Context("without a commitish", func() {
It("fails to create the release and the tag", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).Should(HaveOccurred())
Ω(gitlabClient.CreateTagCallCount()).Should(Equal(0))
Ω(gitlabClient.CreateReleaseCallCount()).Should(Equal(0))
})
})
})
Context("when the underlying tag has already been created", func() {
BeforeEach(func() {
gitlabClient.GetTagStub = func(name string) (*gitlab.Tag, error) {
return &gitlab.Tag{
Name: "v0.3.13",
}, nil
}
})
It("creates a release on gitlab with existing tag", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.CreateTagCallCount()).Should(Equal(0))
name, tag, body := gitlabClient.CreateReleaseArgsForCall(0)
Ω(name).Should(Equal("v0.3.13"))
Ω(tag).Should(Equal("v0.3.13"))
Ω(*body).Should(Equal("*markdown*"))
})
Context("when the tag_prefix is set", func() {
BeforeEach(func() {
namePath := filepath.Join(sourcesDir, "name")
tagPath := filepath.Join(sourcesDir, "tag")
file(namePath, "v0.3.13")
file(tagPath, "0.3.13")
request = resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
TagPath: "tag",
TagPrefix: "v",
},
}
})
It("appends the TagPrefix onto the TagName", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.CreateReleaseCallCount()).Should(Equal(1))
name, tag, _ := gitlabClient.CreateReleaseArgsForCall(0)
Ω(name).Should(Equal("v0.3.13"))
Ω(tag).Should(Equal("v0.3.13"))
})
})
})
Context("with globs", func() {
BeforeEach(func() {
request = resource.OutRequest{
Params: resource.OutParams{
NamePath: "name",
BodyPath: "body",
TagPath: "tag",
Globs: []string{
"*.tgz",
},
},
}
})
It("uploads matching file globs", func() {
_, err := command.Run(sourcesDir, request)
Ω(err).ShouldNot(HaveOccurred())
Ω(gitlabClient.UploadProjectFileCallCount()).Should(Equal(1))
file := gitlabClient.UploadProjectFileArgsForCall(0)
Ω(file).Should(Equal(filepath.Join(sourcesDir, "great-file.tgz")))
})
It("returns an error if a glob is provided that does not match any files", func() {
request.Params.Globs = []string{
"*.tgz",
"*.gif",
}
_, err := command.Run(sourcesDir, request)
Ω(err).Should(HaveOccurred())
Ω(err).Should(MatchError("could not find file that matches glob '*.gif'"))
})
})
})
})