@@ -499,71 +499,51 @@ 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
- }
535
-
536
- func TestCopyFileSymlinkToDirectory (t * testing.T ) {
537
- dir , err := ioutil .TempDir ("" , "dep" )
538
- if err != nil {
539
- t .Fatal (err )
540
- }
541
- defer os .RemoveAll (dir )
542
-
543
- srcPath := filepath .Join (dir , "src" )
544
- symlinkPath := filepath .Join (dir , "symlink" )
545
- dstPath := filepath .Join (dir , "dst" )
546
-
547
- err = os .MkdirAll (srcPath , 0777 )
548
- if err != nil {
549
- t .Fatal (err )
550
- }
502
+ h := test .NewHelper (t )
503
+ defer h .Cleanup ()
504
+ h .TempDir ("." )
551
505
552
- if err = os .Symlink (srcPath , symlinkPath ); err != nil {
553
- t .Fatalf ("could not create symlink: %v" , err )
506
+ testcases := map [string ]string {
507
+ filepath .Join ("./testdata/symlinks/file-symlink" ): filepath .Join (h .Path ("." ), "dst-file" ),
508
+ filepath .Join ("./testdata/symlinks/windows-file-symlink" ): filepath .Join (h .Path ("." ), "windows-dst-file" ),
509
+ filepath .Join ("./testdata/symlinks/dir-symlink" ): filepath .Join (h .Path ("." ), "dst-dir" ),
510
+ filepath .Join ("./testdata/symlinks/invalid-symlink" ): filepath .Join (h .Path ("." ), "invalid-symlink" ),
554
511
}
555
512
556
- if err = copyFile (symlinkPath , dstPath ); err != nil {
557
- t .Fatalf ("failed to copy symlink: %s" , err )
558
- }
513
+ for symlink , dst := range testcases {
514
+ t .Run (symlink , func (t * testing.T ) {
515
+ var err error
516
+ if err = copyFile (symlink , dst ); err != nil {
517
+ t .Fatalf ("failed to copy symlink: %s" , err )
518
+ }
559
519
560
- resolvedPath , err := os .Readlink (dstPath )
561
- if err != nil {
562
- t .Fatalf ("could not resolve symlink: %s" , err )
563
- }
520
+ var want , got string
521
+
522
+ if runtime .GOOS == "windows" {
523
+ // Creating symlinks on Windows require an additional permission
524
+ // regular users aren't granted usually. So we copy the file
525
+ // content as a fall back instead of creating a real symlink.
526
+ srcb , err := ioutil .ReadFile (symlink )
527
+ h .Must (err )
528
+ dstb , err := ioutil .ReadFile (dst )
529
+ h .Must (err )
530
+
531
+ want = string (srcb )
532
+ got = string (dstb )
533
+ } else {
534
+ want , err = os .Readlink (symlink )
535
+ h .Must (err )
536
+
537
+ got , err = os .Readlink (dst )
538
+ if err != nil {
539
+ t .Fatalf ("could not resolve symlink: %s" , err )
540
+ }
541
+ }
564
542
565
- if resolvedPath != srcPath {
566
- t .Fatalf ("resolved path is incorrect. expected %s, got %s" , srcPath , resolvedPath )
543
+ if want != got {
544
+ t .Fatalf ("resolved path is incorrect. expected %s, got %s" , want , got )
545
+ }
546
+ })
567
547
}
568
548
}
569
549
@@ -814,13 +794,6 @@ func TestIsNonEmptyDir(t *testing.T) {
814
794
}
815
795
816
796
func TestIsSymlink (t * testing.T ) {
817
- if runtime .GOOS == "windows" {
818
- // XXX: creating symlinks is not supported in Go on
819
- // Microsoft Windows. Skipping this this until a solution
820
- // for creating symlinks is is provided.
821
- t .Skip ("skipping on windows" )
822
- }
823
-
824
797
dir , err := ioutil .TempDir ("" , "dep" )
825
798
if err != nil {
826
799
t .Fatal (err )
@@ -841,6 +814,7 @@ func TestIsSymlink(t *testing.T) {
841
814
842
815
dirSymlink := filepath .Join (dir , "dirSymlink" )
843
816
fileSymlink := filepath .Join (dir , "fileSymlink" )
817
+
844
818
if err = os .Symlink (dirPath , dirSymlink ); err != nil {
845
819
t .Fatal (err )
846
820
}
@@ -866,10 +840,7 @@ func TestIsSymlink(t *testing.T) {
866
840
})
867
841
defer cleanup ()
868
842
869
- tests := map [string ]struct {
870
- expected bool
871
- err bool
872
- }{
843
+ tests := map [string ]struct { expected , err bool }{
873
844
dirPath : {false , false },
874
845
filePath : {false , false },
875
846
dirSymlink : {true , false },
@@ -878,6 +849,13 @@ func TestIsSymlink(t *testing.T) {
878
849
inaccessibleSymlink : {false , true },
879
850
}
880
851
852
+ if runtime .GOOS == "windows" {
853
+ // XXX: setting permissions works differently in Windows. Skipping
854
+ // these cases until a compatible implementation is provided.
855
+ delete (tests , inaccessibleFile )
856
+ delete (tests , inaccessibleSymlink )
857
+ }
858
+
881
859
for path , want := range tests {
882
860
got , err := IsSymlink (path )
883
861
if err != nil {
0 commit comments