@@ -352,22 +352,15 @@ func TestResolveProjectRoot(t *testing.T) {
352
352
tg := test .NewHelper (t )
353
353
defer tg .Cleanup ()
354
354
355
- tg .TempDir ("go" )
356
- tg .TempDir ("go/src" )
357
- tg .TempDir ("go/src/real" )
358
355
tg .TempDir ("go/src/real/path" )
359
356
tg .TempDir ("go/src/sym" )
360
357
361
- tg .TempDir ("gotwo" ) // Another directory used as a GOPATH
362
- tg .TempDir ("gotwo/src" )
363
- tg .TempDir ("gotwo/src/real" )
358
+ // Another directory used as a GOPATH
364
359
tg .TempDir ("gotwo/src/real/path" )
365
360
tg .TempDir ("gotwo/src/sym" )
366
361
367
362
tg .TempDir ("sym" ) // Directory for symlinks
368
363
369
- tg .Setenv ("GOPATH" , tg .Path (filepath .Join ("." , "go" )))
370
-
371
364
ctx := & Ctx {
372
365
GOPATH : tg .Path (filepath .Join ("." , "go" )),
373
366
GOPATHS : []string {
@@ -376,45 +369,65 @@ func TestResolveProjectRoot(t *testing.T) {
376
369
},
377
370
}
378
371
379
- realPath := filepath .Join (ctx .GOPATH , "src" , "real" , "path" )
380
- realPathTwo := filepath .Join (ctx .GOPATHS [1 ], "src" , "real" , "path" )
381
- symlinkedPath := filepath .Join (tg .Path ("." ), "sym" , "symlink" )
382
- symlinkedInGoPath := filepath .Join (ctx .GOPATH , "src/sym/path" )
383
- symlinkedInOtherGoPath := filepath .Join (tg .Path ("." ), "sym" , "symtwo" )
384
- os .Symlink (realPath , symlinkedPath )
385
- os .Symlink (realPath , symlinkedInGoPath )
386
- os .Symlink (realPathTwo , symlinkedInOtherGoPath )
387
-
388
- // Real path should be returned, no symlinks to deal with
389
- p , err := ctx .resolveProjectRoot (realPath )
390
- if err != nil {
391
- t .Fatalf ("Error resolving project root: %s" , err )
392
- }
393
- if p != realPath {
394
- t .Fatalf ("Want path to be %s, got %s" , realPath , p )
372
+ testcases := []struct {
373
+ name string
374
+ path string
375
+ resolvedPath string
376
+ symlink bool
377
+ expectErr bool
378
+ }{
379
+ {
380
+ name : "no-symlinks" ,
381
+ path : filepath .Join (ctx .GOPATH , "src/real/path" ),
382
+ resolvedPath : filepath .Join (ctx .GOPATH , "src/real/path" ),
383
+ },
384
+ {
385
+ name : "symlink-outside-gopath" ,
386
+ path : filepath .Join (tg .Path ("." ), "sym/symlink" ),
387
+ resolvedPath : filepath .Join (ctx .GOPATH , "src/real/path" ),
388
+ symlink : true ,
389
+ },
390
+ {
391
+ name : "symlink-in-another-gopath" ,
392
+ path : filepath .Join (tg .Path ("." ), "sym/symtwo" ),
393
+ resolvedPath : filepath .Join (ctx .GOPATHS [1 ], "src/real/path" ),
394
+ symlink : true ,
395
+ },
396
+ {
397
+ name : "symlink-in-gopath" ,
398
+ path : filepath .Join (ctx .GOPATH , "src/sym/path" ),
399
+ resolvedPath : filepath .Join (ctx .GOPATH , "src/real/path" ),
400
+ symlink : true ,
401
+ expectErr : true ,
402
+ },
395
403
}
396
404
397
- // Real path should be returned, symlink is outside GOPATH
398
- p , err = ctx .resolveProjectRoot (symlinkedPath )
399
- if err != nil {
400
- t .Fatalf ("Error resolving project root: %s" , err )
401
- }
402
- if p != realPath {
403
- t .Fatalf ("Want path to be %s, got %s" , realPath , p )
404
- }
405
+ for _ , tc := range testcases {
406
+ t .Run (tc .name , func (t * testing.T ) {
407
+ if tc .symlink {
408
+ if err := os .Symlink (tc .resolvedPath , tc .path ); err != nil {
409
+ if runtime .GOOS == "windows" {
410
+ t .Skipf ("Not testing Windows symlinks because: %s" , err )
411
+ } else {
412
+ t .Fatal (err )
413
+ }
414
+ }
415
+ }
405
416
406
- // Real path should be returned, symlink is in another GOPATH
407
- p , err = ctx .resolveProjectRoot (symlinkedInOtherGoPath )
408
- if err != nil {
409
- t .Fatalf ("Error resolving project root: %s" , err )
410
- }
411
- if p != realPathTwo {
412
- t .Fatalf ("Want path to be %s, got %s" , realPathTwo , p )
413
- }
417
+ p , err := ctx .resolveProjectRoot (tc .path )
418
+ if err != nil {
419
+ if ! tc .expectErr {
420
+ t .Fatalf ("Error resolving project root: %s" , err )
421
+ }
422
+ return
423
+ }
424
+ if err == nil && tc .expectErr {
425
+ t .Fatal ("Wanted an error" )
426
+ }
414
427
415
- // Symlinked path is inside GOPATH, should return error
416
- _ , err = ctx . resolveProjectRoot ( symlinkedInGoPath )
417
- if err == nil {
418
- t . Fatalf ( "Wanted an error" )
428
+ if p != tc . resolvedPath {
429
+ t . Errorf ( "Want path to be %s, got %s" , tc . resolvedPath , p )
430
+ }
431
+ } )
419
432
}
420
433
}
0 commit comments