@@ -499,71 +499,48 @@ func TestCopyFile(t *testing.T) {
499
499
}
500
500
501
501
func TestCopyFileSymlink (t * testing.T ) {
502
- dir , err := ioutil .TempDir ("" , "dep" )
503
- if err != nil {
504
- t .Fatal (err )
505
- }
506
- defer os .RemoveAll (dir )
507
-
508
- srcPath := filepath .Join (dir , "src" )
509
- symlinkPath := filepath .Join (dir , "symlink" )
510
- dstPath := filepath .Join (dir , "dst" )
511
-
512
- srcf , err := os .Create (srcPath )
513
- if err != nil {
514
- t .Fatal (err )
515
- }
516
- srcf .Close ()
517
-
518
- if err = os .Symlink (srcPath , symlinkPath ); err != nil {
519
- t .Fatalf ("could not create symlink: %s" , err )
520
- }
521
-
522
- if err = copyFile (symlinkPath , dstPath ); err != nil {
523
- t .Fatalf ("failed to copy symlink: %s" , err )
524
- }
525
-
526
- resolvedPath , err := os .Readlink (dstPath )
527
- if err != nil {
528
- t .Fatalf ("could not resolve symlink: %s" , err )
529
- }
530
-
531
- if resolvedPath != srcPath {
532
- t .Fatalf ("resolved path is incorrect. expected %s, got %s" , srcPath , resolvedPath )
533
- }
534
- }
502
+ h := test .NewHelper (t )
503
+ defer h .Cleanup ()
504
+ h .TempDir ("." )
535
505
536
- func TestCopyFileSymlinkToDirectory ( t * testing. T ) {
537
- dir , err := ioutil . TempDir ( "" , "dep" )
538
- if err != nil {
539
- t . Fatal ( err )
506
+ testcases := map [ string ] string {
507
+ filepath . Join ( "./testdata/symlinks/file-symlink" ): filepath . Join ( h . Path ( "." ) , "dst-file" ),
508
+ filepath . Join ( "./testdata/symlinks/dir-symlink" ): filepath . Join ( h . Path ( "." ), "dst-dir" ),
509
+ filepath . Join ( "./testdata/symlinks/invalid-symlink" ): filepath . Join ( h . Path ( "." ), "invalid-symlink" ),
540
510
}
541
- defer os .RemoveAll (dir )
542
511
543
- srcPath := filepath .Join (dir , "src" )
544
- symlinkPath := filepath .Join (dir , "symlink" )
545
- dstPath := filepath .Join (dir , "dst" )
512
+ for symlink , dst := range testcases {
513
+ var err error
514
+ if err = copyFile (symlink , dst ); err != nil {
515
+ t .Fatalf ("failed to copy symlink: %s" , err )
516
+ }
546
517
547
- err = os .MkdirAll (srcPath , 0777 )
548
- if err != nil {
549
- t .Fatal (err )
550
- }
518
+ var want , got string
551
519
552
- if err = os .Symlink (srcPath , symlinkPath ); err != nil {
553
- t .Fatalf ("could not create symlink: %v" , err )
554
- }
520
+ if runtime .GOOS == "windows" {
521
+ // Creating symlinks on Windows require an additional permission
522
+ // regular users aren't granted usually. So we copy the file
523
+ // content as a fall back instead of creating a real symlink.
524
+ srcb , err := ioutil .ReadFile (symlink )
525
+ h .Must (err )
526
+ dstb , err := ioutil .ReadFile (dst )
527
+ h .Must (err )
555
528
556
- if err = copyFile (symlinkPath , dstPath ); err != nil {
557
- t .Fatalf ("failed to copy symlink: %s" , err )
558
- }
529
+ want = string (srcb )
530
+ got = string (dstb )
531
+ } else {
532
+ want , err = os .Readlink (symlink )
533
+ h .Must (err )
559
534
560
- resolvedPath , err := os .Readlink (dstPath )
561
- if err != nil {
562
- t .Fatalf ("could not resolve symlink: %s" , err )
563
- }
535
+ got , err = os .Readlink (dst )
536
+ if err != nil {
537
+ t .Fatalf ("could not resolve symlink: %s" , err )
538
+ }
539
+ }
564
540
565
- if resolvedPath != srcPath {
566
- t .Fatalf ("resolved path is incorrect. expected %s, got %s" , srcPath , resolvedPath )
541
+ if want != got {
542
+ t .Fatalf ("resolved path is incorrect. expected %s, got %s" , want , got )
543
+ }
567
544
}
568
545
}
569
546
@@ -857,10 +834,7 @@ func TestIsSymlink(t *testing.T) {
857
834
})
858
835
defer cleanup ()
859
836
860
- tests := map [string ]struct {
861
- expected bool
862
- err bool
863
- }{
837
+ tests := map [string ]struct { expected , err bool }{
864
838
dirPath : {false , false },
865
839
filePath : {false , false },
866
840
dirSymlink : {true , false },
@@ -869,6 +843,13 @@ func TestIsSymlink(t *testing.T) {
869
843
inaccessibleSymlink : {false , true },
870
844
}
871
845
846
+ if runtime .GOOS == "windows" {
847
+ // XXX: settings permissions works differently in Windows. Skipping
848
+ // these cases until a compatible implementation is provided.
849
+ delete (tests , inaccessibleFile )
850
+ delete (tests , inaccessibleSymlink )
851
+ }
852
+
872
853
for path , want := range tests {
873
854
got , err := IsSymlink (path )
874
855
if err != nil {
0 commit comments