github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/putiginore_test.go (about) 1 package upx 2 3 import ( 4 "io/ioutil" 5 "path" 6 "path/filepath" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Ls(up string) ([]string, error) { 15 b, err := Upx("ls", up) 16 if err != nil { 17 return nil, err 18 } 19 20 var ups = make([]string, 0) 21 output := strings.TrimRight(string(b), "\n") 22 for _, line := range strings.Split(output, "\n") { 23 items := strings.Split(line, " ") 24 ups = append(ups, items[len(items)-1]) 25 } 26 return ups, nil 27 } 28 29 func TestPutIgnore(t *testing.T) { 30 SetUp() 31 defer TearDown() 32 33 upRootPath := path.Join(ROOT, "iginore") 34 Upx("mkdir", upRootPath) 35 36 localRootPath, err := ioutil.TempDir("", "test") 37 assert.NoError(t, err) 38 localRootName := filepath.Base(localRootPath) 39 40 CreateFile(path.Join(localRootPath, "FILE1")) 41 CreateFile(path.Join(localRootPath, "FILE2")) 42 CreateFile(path.Join(localRootPath, ".FILE3")) 43 CreateFile(path.Join(localRootPath, ".FILES/FILE")) 44 45 // 上传文件夹 46 // 不包含隐藏的文件,所以只有FILE1和FILE2 47 Upx("put", localRootPath, upRootPath) 48 files, err := Ls(path.Join(upRootPath, localRootName)) 49 50 assert.NoError(t, err) 51 assert.Len(t, files, 2) 52 assert.ElementsMatch( 53 t, 54 files, 55 []string{"FILE1", "FILE2"}, 56 ) 57 58 time.Sleep(time.Second) 59 60 // 上传隐藏的文件夹, 无all,上传失效 61 Upx( 62 "put", 63 path.Join(localRootPath, ".FILES"), 64 path.Join(upRootPath, localRootName, ".FILES"), 65 ) 66 files, err = Ls(path.Join(upRootPath, localRootName)) 67 assert.NoError(t, err) 68 69 assert.Len(t, files, 2) 70 assert.ElementsMatch( 71 t, 72 files, 73 []string{"FILE1", "FILE2"}, 74 ) 75 76 time.Sleep(time.Second) 77 78 // 上传隐藏的文件夹, 有all,上传成功 79 Upx( 80 "put", 81 "-all", 82 path.Join(localRootPath, ".FILES"), 83 path.Join(upRootPath, localRootName, ".FILES"), 84 ) 85 files, err = Ls(path.Join(upRootPath, localRootName)) 86 assert.NoError(t, err) 87 assert.Len(t, files, 3) 88 assert.ElementsMatch( 89 t, 90 files, 91 []string{"FILE1", "FILE2", ".FILES"}, 92 ) 93 94 time.Sleep(time.Second) 95 96 // 上传所有文件 97 Upx("put", "-all", localRootPath, upRootPath) 98 files, err = Ls(path.Join(upRootPath, localRootName)) 99 assert.NoError(t, err) 100 assert.Len(t, files, 4) 101 assert.ElementsMatch( 102 t, 103 files, 104 []string{"FILE1", "FILE2", ".FILE3", ".FILES"}, 105 ) 106 }