@@ -23,6 +23,7 @@ import (
23
23
"github.com/Masterminds/semver/v3"
24
24
"github.com/google/go-github/v61/github"
25
25
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/plugin"
26
+ "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
26
27
"github.com/stretchr/testify/assert"
27
28
"github.com/stretchr/testify/require"
28
29
)
@@ -204,77 +205,96 @@ func Test_parseGithubRepoValues(t *testing.T) {
204
205
expectedOwner = "mongodb"
205
206
expectedName = "atlas-cli-plugin-example"
206
207
)
207
- var expectedVersion , _ = semver .NewVersion ("1.0.0" )
208
+ var v1_0_0 , _ = semver .NewVersion ("1.0.0" )
209
+ //nolint:revive,stylecheck
210
+ var v1_0_0_PRE , _ = semver .NewVersion ("1.0.0-prerelease" )
211
+ //nolint:revive,stylecheck
212
+ var v1_0_0_BETA_AND_META , _ = semver .NewVersion ("1.0.0-beta+very-meta" )
208
213
209
214
tests := []struct {
210
- arg string
211
- expectVersion bool
212
- expectError bool
215
+ arg string
216
+ expectedVersion * semver. Version
217
+ expectError bool
213
218
}{
214
219
{
215
- arg : "mongodb/atlas-cli-plugin-example" ,
216
- expectVersion : false ,
217
- expectError : false ,
220
+ arg : "mongodb/atlas-cli-plugin-example" ,
221
+ expectedVersion : nil ,
222
+ expectError : false ,
223
+ },
224
+ {
225
+
226
+ expectedVersion : v1_0_0 ,
227
+ expectError : false ,
228
+ },
229
+ {
230
+
231
+ expectedVersion : v1_0_0 ,
232
+ expectError : false ,
233
+ },
234
+ {
235
+
236
+ expectedVersion : v1_0_0_PRE ,
237
+ expectError : false ,
218
238
},
219
239
{
220
-
221
- expectVersion : true ,
222
- expectError : false ,
240
+ arg :
"mongodb/[email protected] -beta+very-meta " ,
241
+ expectedVersion : v1_0_0_BETA_AND_META ,
242
+ expectError : false ,
223
243
},
224
244
{
225
- arg : "mongodb/atlas-cli-plugin-example@" ,
226
- expectVersion : false ,
227
- expectError : true ,
245
+ arg : "mongodb/atlas-cli-plugin-example@" ,
246
+ expectedVersion : nil ,
247
+ expectError : true ,
228
248
},
229
249
{
230
- arg : "mongodb/atlas-cli-plugin-example/" ,
231
- expectVersion : false ,
232
- expectError : false ,
250
+ arg : "mongodb/atlas-cli-plugin-example/" ,
251
+ expectedVersion : nil ,
252
+ expectError : false ,
233
253
},
234
254
{
235
- arg : "mongodb/atlas-cli-plugin-example/@v1" ,
236
- expectVersion : true ,
237
- expectError : false ,
255
+ arg : "mongodb/atlas-cli-plugin-example/@v1" ,
256
+ expectedVersion : v1_0_0 ,
257
+ expectError : false ,
238
258
},
239
259
{
240
- arg : "https://github.com/mongodb/atlas-cli-plugin-example" ,
241
- expectVersion : false ,
242
- expectError : false ,
260
+ arg : "https://github.com/mongodb/atlas-cli-plugin-example" ,
261
+ expectedVersion : nil ,
262
+ expectError : false ,
243
263
},
244
264
{
245
- arg :
"https://github.com/mongodb/[email protected] " ,
246
- expectVersion : false ,
247
- expectError : false ,
265
+ arg :
"https://github.com/mongodb/[email protected] " ,
266
+ expectedVersion : v1_0_0 ,
267
+ expectError : false ,
248
268
},
249
269
{
250
- arg : "github.com/mongodb/atlas-cli-plugin-example/" ,
251
- expectVersion : false ,
252
- expectError : false ,
270
+ arg : "github.com/mongodb/atlas-cli-plugin-example/" ,
271
+ expectedVersion : nil ,
272
+ expectError : false ,
253
273
},
254
274
{
255
- arg : "github.com/mongodb/atlas-cli-plugin-example/@v1.0.0" ,
256
- expectVersion : true ,
257
- expectError : false ,
275
+ arg : "github.com/mongodb/atlas-cli-plugin-example/@v1.0.0" ,
276
+ expectedVersion : v1_0_0 ,
277
+ expectError : false ,
258
278
},
259
279
{
260
- arg : "/mongodb/atlas-cli-plugin-example/" ,
261
- expectVersion : false ,
262
- expectError : true ,
280
+ arg : "/mongodb/atlas-cli-plugin-example/" ,
281
+ expectedVersion : nil ,
282
+ expectError : true ,
263
283
},
264
284
{
265
- arg : "mongodb@atlas-cli-plugin-example" ,
266
- expectVersion : false ,
267
- expectError : true ,
285
+ arg : "mongodb@atlas-cli-plugin-example" ,
286
+ expectedVersion : nil ,
287
+ expectError : true ,
268
288
},
269
289
{
270
-
271
- expectVersion : false ,
272
- expectError : true ,
290
+
291
+ expectedVersion : nil ,
292
+ expectError : true ,
273
293
},
274
294
{
275
- arg : "invalidArgString" ,
276
- expectVersion : false ,
277
- expectError : true ,
295
+ arg : "invalidArgString" ,
296
+ expectedVersion : nil ,
297
+ expectError : true ,
278
298
},
279
299
}
280
300
@@ -291,8 +311,12 @@ func Test_parseGithubRepoValues(t *testing.T) {
291
311
if githubRelease .name != expectedName {
292
312
t .Errorf ("expected name: %s, got: %s" , expectedName , githubRelease .owner )
293
313
}
294
- if tt .expectVersion && ! expectedVersion .Equal (githubRelease .version ) {
295
- t .Errorf ("expected version: %s, got: %s" , expectedVersion .String (), githubRelease .version .String ())
314
+ if tt .expectedVersion != nil && ! tt .expectedVersion .Equal (githubRelease .version ) {
315
+ t .Errorf ("expected version: %s, got: %s" , tt .expectedVersion .String (), githubRelease .version .String ())
316
+ }
317
+
318
+ if tt .expectedVersion == nil && githubRelease .version != nil {
319
+ t .Errorf ("expected version to be nil, got: %s" , githubRelease .version .String ())
296
320
}
297
321
}
298
322
})
@@ -352,3 +376,78 @@ func Test_getPluginDirectoryName(t *testing.T) {
352
376
githubAsset := & GithubAsset {owner : "owner" , name : "name" }
353
377
require .Equal (t , "owner@name" , githubAsset .getPluginDirectoryName ())
354
378
}
379
+
380
+ func Test_getLatestStableRelease (t * testing.T ) {
381
+ tests := []struct {
382
+ name string
383
+ releases []* github.RepositoryRelease
384
+ expected * github.RepositoryRelease
385
+ }{
386
+ {
387
+ name : "Single valid value" ,
388
+ releases : []* github.RepositoryRelease {
389
+ {
390
+ TagName : pointer .Get ("v1.0.0" ),
391
+ },
392
+ },
393
+ expected : & github.RepositoryRelease {
394
+ TagName : pointer .Get ("v1.0.0" ),
395
+ },
396
+ },
397
+ {
398
+ name : "Single invalid value" ,
399
+ releases : []* github.RepositoryRelease {
400
+ {
401
+ TagName : pointer .Get ("test" ),
402
+ },
403
+ },
404
+ expected : nil ,
405
+ },
406
+ {
407
+ name : "Single valid pre-release value" ,
408
+ releases : []* github.RepositoryRelease {
409
+ {
410
+ TagName : pointer .Get ("v1.0.0-pre" ),
411
+ },
412
+ },
413
+ expected : nil ,
414
+ },
415
+ {
416
+ name : "Multiple" ,
417
+ releases : []* github.RepositoryRelease {
418
+ {
419
+ TagName : pointer .Get ("v2.0.0-pre" ),
420
+ },
421
+ {
422
+ TagName : pointer .Get ("v2.0.0-beta" ),
423
+ },
424
+ {
425
+ TagName : pointer .Get ("v1.2.1" ),
426
+ },
427
+ {
428
+ TagName : pointer .Get ("v1.2.0" ),
429
+ },
430
+ {
431
+ TagName : pointer .Get ("v1.1.0" ),
432
+ },
433
+ {
434
+ TagName : pointer .Get ("v1.0.1" ),
435
+ },
436
+ {
437
+ TagName : pointer .Get ("v1.0.0" ),
438
+ },
439
+ },
440
+ expected : & github.RepositoryRelease {
441
+ TagName : pointer .Get ("v1.2.1" ),
442
+ },
443
+ },
444
+ }
445
+
446
+ for _ , tt := range tests {
447
+ t .Run (tt .name , func (t * testing.T ) {
448
+ actual := getLatestStableRelease (tt .releases )
449
+
450
+ assert .Equal (t , tt .expected , actual )
451
+ })
452
+ }
453
+ }
0 commit comments