github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/copy_test.go (about) 1 package upx 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path" 7 "path/filepath" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestCopy(t *testing.T) { 15 SetUp() 16 defer TearDown() 17 18 upRootPath := path.Join(ROOT, "copy") 19 Upx("mkdir", upRootPath) 20 21 localRootPath, err := ioutil.TempDir("", "test") 22 assert.NoError(t, err) 23 localRootName := filepath.Base(localRootPath) 24 25 CreateFile(path.Join(localRootPath, "FILE1")) 26 CreateFile(path.Join(localRootPath, "FILE2")) 27 28 // 上传文件 29 _, err = Upx("put", localRootPath, upRootPath) 30 assert.NoError(t, err) 31 32 files, err := Ls(path.Join(upRootPath, localRootName)) 33 assert.NoError(t, err) 34 assert.Len(t, files, 2) 35 assert.ElementsMatch( 36 t, 37 files, 38 []string{"FILE1", "FILE2"}, 39 ) 40 41 time.Sleep(time.Second) 42 43 // 正常复制文件 44 _, err = Upx( 45 "cp", 46 path.Join(upRootPath, localRootName, "FILE1"), 47 path.Join(upRootPath, localRootName, "FILE3"), 48 ) 49 assert.NoError(t, err) 50 51 files, err = Ls(path.Join(upRootPath, localRootName)) 52 assert.NoError(t, err) 53 assert.Len(t, files, 3) 54 assert.ElementsMatch( 55 t, 56 files, 57 []string{"FILE1", "FILE2", "FILE3"}, 58 ) 59 60 time.Sleep(time.Second) 61 62 // 目标文件已存在 63 _, err = Upx( 64 "cp", 65 path.Join(upRootPath, localRootName, "FILE1"), 66 path.Join(upRootPath, localRootName, "FILE2"), 67 ) 68 assert.Error(t, err) 69 assert.Equal( 70 t, 71 err.Error(), 72 fmt.Sprintf( 73 "target path %s already exists use -f to force overwrite\n", 74 path.Join(upRootPath, localRootName, "FILE2"), 75 ), 76 ) 77 78 files, err = Ls(path.Join(upRootPath, localRootName)) 79 assert.NoError(t, err) 80 assert.Len(t, files, 3) 81 assert.ElementsMatch( 82 t, 83 files, 84 []string{"FILE1", "FILE2", "FILE3"}, 85 ) 86 87 time.Sleep(time.Second) 88 89 // 目标文件已存在, 强制覆盖 90 _, err = Upx( 91 "cp", 92 "-f", 93 path.Join(upRootPath, localRootName, "FILE1"), 94 path.Join(upRootPath, localRootName, "FILE2"), 95 ) 96 assert.NoError(t, err) 97 98 files, err = Ls(path.Join(upRootPath, localRootName)) 99 assert.NoError(t, err) 100 assert.Len(t, files, 3) 101 assert.ElementsMatch( 102 t, 103 files, 104 []string{"FILE1", "FILE2", "FILE3"}, 105 ) 106 }