@@ -6,13 +6,17 @@ package main
6
6
7
7
import (
8
8
"bytes"
9
+ "io/ioutil"
10
+ "log"
11
+ "reflect"
9
12
"testing"
10
13
"text/tabwriter"
11
14
12
15
"strings"
13
16
14
17
"github.com/golang/dep"
15
18
"github.com/golang/dep/internal/gps"
19
+ "github.com/golang/dep/internal/test"
16
20
)
17
21
18
22
func TestStatusFormatVersion (t * testing.T ) {
@@ -287,3 +291,109 @@ func TestBasicStatusGetConsolidatedLatest(t *testing.T) {
287
291
})
288
292
}
289
293
}
294
+
295
+ func TestCollectConstraints (t * testing.T ) {
296
+ ver1 , _ := gps .NewSemverConstraintIC ("v1.0.0" )
297
+ ver08 , _ := gps .NewSemverConstraintIC ("v0.8.0" )
298
+ ver2 , _ := gps .NewSemverConstraintIC ("v2.0.0" )
299
+
300
+ cases := []struct {
301
+ name string
302
+ project dep.Project
303
+ wantConstraints map [string ][]gps.Constraint
304
+ }{
305
+ {
306
+ name : "without any constraints" ,
307
+ project : dep.Project {
308
+ Lock : & dep.Lock {
309
+ P : []gps.LockedProject {
310
+ gps .NewLockedProject (
311
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/sdboyer/deptest" )},
312
+ gps .NewVersion ("v1.0.0" ),
313
+ []string {"." },
314
+ ),
315
+ },
316
+ },
317
+ },
318
+ wantConstraints : map [string ][]gps.Constraint {},
319
+ },
320
+ {
321
+ name : "with multiple constraints" ,
322
+ project : dep.Project {
323
+ Lock : & dep.Lock {
324
+ P : []gps.LockedProject {
325
+ gps .NewLockedProject (
326
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/sdboyer/deptest" )},
327
+ gps .NewVersion ("v1.0.0" ),
328
+ []string {"." },
329
+ ),
330
+ gps .NewLockedProject (
331
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/darkowlzz/deptest-project-1" )},
332
+ gps .NewVersion ("v0.1.0" ),
333
+ []string {"." },
334
+ ),
335
+ gps .NewLockedProject (
336
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/darkowlzz/deptest-project-2" )},
337
+ gps .NewBranch ("master" ).Pair (gps .Revision ("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e" )),
338
+ []string {"." },
339
+ ),
340
+ },
341
+ },
342
+ },
343
+ wantConstraints : map [string ][]gps.Constraint {
344
+ "github.com/sdboyer/deptest" : []gps.Constraint {ver1 , ver08 },
345
+ "github.com/sdboyer/deptestdos" : []gps.Constraint {ver2 },
346
+ "github.com/sdboyer/dep-test" : []gps.Constraint {ver1 },
347
+ },
348
+ },
349
+ {
350
+ name : "skip projects with invalid versions" ,
351
+ project : dep.Project {
352
+ Lock : & dep.Lock {
353
+ P : []gps.LockedProject {
354
+ gps .NewLockedProject (
355
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/darkowlzz/deptest-project-1" )},
356
+ gps .NewVersion ("v0.1.0" ),
357
+ []string {"." },
358
+ ),
359
+ gps .NewLockedProject (
360
+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/darkowlzz/deptest-project-2" )},
361
+ gps .NewVersion ("v1.0.0" ),
362
+ []string {"." },
363
+ ),
364
+ },
365
+ },
366
+ },
367
+ wantConstraints : map [string ][]gps.Constraint {
368
+ "github.com/sdboyer/deptest" : []gps.Constraint {ver1 },
369
+ },
370
+ },
371
+ }
372
+
373
+ h := test .NewHelper (t )
374
+ defer h .Cleanup ()
375
+
376
+ h .TempDir ("src" )
377
+ pwd := h .Path ("." )
378
+ discardLogger := log .New (ioutil .Discard , "" , 0 )
379
+
380
+ ctx := & dep.Ctx {
381
+ GOPATH : pwd ,
382
+ Out : discardLogger ,
383
+ Err : discardLogger ,
384
+ }
385
+
386
+ sm , err := ctx .SourceManager ()
387
+ h .Must (err )
388
+ defer sm .Release ()
389
+
390
+ for _ , c := range cases {
391
+ t .Run (c .name , func (t * testing.T ) {
392
+ gotConstraints := collectConstraints (ctx , & c .project , sm )
393
+
394
+ if ! reflect .DeepEqual (gotConstraints , c .wantConstraints ) {
395
+ t .Fatalf ("Unexpected collected constraints: \n \t (GOT): %v\n \t (WNT): %v" , gotConstraints , c .wantConstraints )
396
+ }
397
+ })
398
+ }
399
+ }
0 commit comments