github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/cp/cp_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cp 6 7 import ( 8 "errors" 9 "fmt" 10 "io/fs" 11 "os" 12 "path/filepath" 13 "strings" 14 "testing" 15 16 "golang.org/x/sys/unix" 17 ) 18 19 var ( 20 testdata = []byte("This is a test string") 21 ) 22 23 func TestCopySimple(t *testing.T) { 24 var err error 25 tmpdirDst := t.TempDir() 26 tmpdirSrc := t.TempDir() 27 28 srcfiles := make([]*os.File, 2) 29 dstfiles := make([]*os.File, 2) 30 for iterator := range srcfiles { 31 srcfiles[iterator], err = os.CreateTemp(tmpdirSrc, "file-to-copy"+fmt.Sprintf("%d", iterator)) 32 if err != nil { 33 t.Errorf("failed to create temp file: %q", err) 34 } 35 if _, err = srcfiles[iterator].Write(testdata); err != nil { 36 t.Errorf("failed to write testdata to file") 37 } 38 dstfiles[iterator], err = os.CreateTemp(tmpdirDst, "file-to-copy"+fmt.Sprintf("%d", iterator)) 39 if err != nil { 40 t.Errorf("failed to create temp file: %q", err) 41 } 42 } 43 44 sl := filepath.Join(tmpdirDst, "test-symlink") 45 if err := os.Symlink(srcfiles[1].Name(), sl); err != nil { 46 t.Errorf("creating symlink failed") 47 } 48 49 for _, tt := range []struct { 50 name string 51 srcfile string 52 dstfile string 53 opt Options 54 wantErr error 55 }{ 56 { 57 name: "Success", 58 srcfile: srcfiles[0].Name(), 59 dstfile: dstfiles[0].Name(), 60 opt: Default, 61 }, 62 { 63 name: "SrcDstDirctoriesSuccess", 64 srcfile: tmpdirSrc, 65 dstfile: tmpdirDst, 66 }, 67 { 68 name: "SrcNotExist", 69 srcfile: "file-does-not-exist", 70 dstfile: dstfiles[0].Name(), 71 wantErr: fs.ErrNotExist, 72 }, 73 { 74 name: "DstIsDirectory", 75 srcfile: srcfiles[0].Name(), 76 dstfile: tmpdirDst, 77 wantErr: unix.EISDIR, 78 }, 79 { 80 name: "CopySymlink", 81 srcfile: sl, 82 dstfile: dstfiles[1].Name(), 83 opt: Options{ 84 NoFollowSymlinks: false, 85 }, 86 }, 87 { 88 name: "CopySymlinkFollow", 89 srcfile: sl, 90 dstfile: filepath.Join(tmpdirDst, "followed-symlink"), 91 opt: Options{ 92 NoFollowSymlinks: true, 93 }, 94 }, 95 } { 96 t.Run(tt.name, func(t *testing.T) { 97 err := Copy(tt.srcfile, tt.dstfile) 98 if !errors.Is(err, tt.wantErr) { 99 t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err) 100 } 101 }) 102 //After every test with NoFollowSymlink we have to delete the created symlink 103 if strings.Contains(tt.dstfile, "symlink") { 104 os.Remove(tt.dstfile) 105 } 106 107 t.Run(tt.name, func(t *testing.T) { 108 if err := tt.opt.Copy(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) { 109 t.Errorf("%q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err) 110 } 111 }) 112 //After every test with NoFollowSymlink we have to delete the created symlink 113 if strings.Contains(tt.dstfile, "symlink") { 114 os.Remove(tt.dstfile) 115 } 116 117 t.Run(tt.name, func(t *testing.T) { 118 if err := tt.opt.CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) { 119 t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err) 120 } 121 }) 122 //After every test with NoFollowSymlink we have to delete the created symlink 123 if strings.Contains(tt.dstfile, "symlink") { 124 os.Remove(tt.dstfile) 125 } 126 127 t.Run(tt.name, func(t *testing.T) { 128 if err := CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) { 129 t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err) 130 } 131 }) 132 //After every test with NoFollowSymlink we have to delete the created symlink 133 if strings.Contains(tt.dstfile, "symlink") { 134 os.Remove(tt.dstfile) 135 } 136 } 137 }