github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/get_test.go (about)

     1  package upx
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"sort"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func GetStartBetweenEndFiles(t *testing.T, src, dst, correct, start, end string) {
    17  	var err error
    18  	src = AbsPath(src)
    19  
    20  	if start != "" && start[0] != '/' {
    21  		start = path.Join(src, start)
    22  	}
    23  	if end != "" && end[0] != '/' {
    24  		end = path.Join(src, end)
    25  	}
    26  	if dst == "" {
    27  		_, err = Upx("get", src, "--start="+start, "--end="+end)
    28  	} else {
    29  		_, err = Upx("get", src, dst, "--start="+start, "--end="+end)
    30  	}
    31  	assert.NoError(t, err)
    32  }
    33  
    34  /*
    35  测试目录					test1:start=123 end=999    test2:start=111 end=666
    36  input:						local:					  local:
    37  
    38  		|-- 111						├── 333					  ├── 111
    39  		|-- 333						├── 666					  ├── 333
    40  		|-- 777						│ ├── 111				  └── 444
    41  		|-- 444						│ ├── 333				      └── 666
    42  		!   `-- 666					│ ├── 666
    43  		`-- 666						│ └── 777
    44  			|-- 111					└── 777
    45  			|-- 333
    46  			|-- 666
    47  	    	`-- 777
    48  */
    49  func TestGetStartBetweenEndFiles(t *testing.T) {
    50  	nowpath, _ := os.Getwd()
    51  	root := strings.Join(strings.Split(ROOT, " "), "-")
    52  	base := root + "/get/"
    53  	pwd, err := ioutil.TempDir("", "test")
    54  	assert.NoError(t, err)
    55  	localBase := filepath.Join(pwd, "get")
    56  
    57  	func() {
    58  		SetUp()
    59  		err := os.MkdirAll(localBase, 0755)
    60  		assert.NoError(t, err)
    61  	}()
    62  	defer TearDown()
    63  
    64  	err = os.Chdir(localBase)
    65  	assert.NoError(t, err)
    66  	Upx("mkdir", base)
    67  	Upx("cd", base)
    68  
    69  	type uploadFiles []struct {
    70  		name    string
    71  		file    string
    72  		dst     string
    73  		correct string
    74  	}
    75  	type uploadDirs []struct {
    76  		dir     string
    77  		dst     string
    78  		correct string
    79  	}
    80  	//构造测试目录
    81  	files := uploadFiles{
    82  		{name: "111", file: filepath.Join(localBase, "111"), dst: "", correct: path.Join(base, "111")},
    83  		{name: "333", file: filepath.Join(localBase, "333"), dst: "", correct: path.Join(base, "333")},
    84  		{name: "333", file: "333", dst: path.Join(base, "333"), correct: path.Join(base, "333")},
    85  		{name: "777", file: "777", dst: base, correct: path.Join(base, "777")},
    86  		{name: "666", file: "666", dst: base + "/444/", correct: path.Join(base, "444", "666")},
    87  	}
    88  	for _, file := range files {
    89  		CreateFile(file.name)
    90  		putFile(t, file.file, file.dst, file.correct)
    91  	}
    92  	log.Println(122)
    93  
    94  	dirs := uploadDirs{
    95  		{dir: localBase, dst: base + "/666/", correct: base + "/666/"},
    96  	}
    97  	for _, dir := range dirs {
    98  		putDir(t, dir.dir, dir.dst, dir.correct)
    99  	}
   100  
   101  	type list struct {
   102  		start   string
   103  		end     string
   104  		testDir string
   105  	}
   106  	type test struct {
   107  		input list
   108  		real  []string
   109  		want  []string
   110  	}
   111  	//构造测试
   112  	tests := []test{
   113  		{input: list{start: "123", end: "999", testDir: filepath.Join(nowpath, "test1")}, real: localFile("test1", base), want: upFile(t, base, "123", "999")},
   114  		{input: list{start: "111", end: "666", testDir: filepath.Join(nowpath, "test2")}, real: localFile("test2", base), want: upFile(t, base, "444", "666")},
   115  	}
   116  	for _, tc := range tests {
   117  		input := tc.input
   118  
   119  		err = os.MkdirAll(input.testDir, os.ModePerm)
   120  		if err != nil {
   121  			log.Println(err)
   122  		}
   123  
   124  		GetStartBetweenEndFiles(t, base, input.testDir, input.testDir, input.start, input.end)
   125  
   126  		sort.Strings(tc.real)
   127  		sort.Strings(tc.want)
   128  		assert.Equal(t, len(tc.real), len(tc.want))
   129  
   130  		for i := 0; i < len(tc.real); i++ {
   131  			log.Println("compare:", tc.real[i], " ", tc.want[i])
   132  			assert.Equal(t, tc.real[i], tc.want[i])
   133  		}
   134  	}
   135  }
   136  
   137  // 递归获取下载到本地的文件
   138  func localFile(local, up string) []string {
   139  	var locals []string
   140  	localLen := len(local)
   141  	fInfos, _ := ioutil.ReadDir(local + "/")
   142  	for _, fInfo := range fInfos {
   143  		fp := filepath.Join(local, fInfo.Name())
   144  		//使用云存储目录作为前缀方便比较
   145  		locals = append(locals, up[:len(up)-1]+fp[localLen:])
   146  		if IsDir(fp) {
   147  			localFile(fp, up)
   148  		}
   149  	}
   150  	return locals
   151  }
   152  
   153  // 递归获取云存储目录文件
   154  func upFile(t *testing.T, up, start, end string) []string {
   155  	b, err := Upx("ls", up)
   156  	assert.NoError(t, err)
   157  
   158  	var ups []string
   159  	output := strings.TrimRight(string(b), "\n")
   160  	for _, line := range strings.Split(output, "\n") {
   161  		items := strings.Split(line, " ")
   162  		fp := path.Join(up, items[len(items)-1])
   163  		ups = append(ups, fp)
   164  		if items[0][0] == 'd' {
   165  			upFile(t, fp, start, end)
   166  		}
   167  	}
   168  
   169  	var upfiles []string
   170  	for _, file := range ups {
   171  		if file >= start && file < end {
   172  			upfiles = append(upfiles, file)
   173  		}
   174  	}
   175  	return upfiles
   176  }
   177  
   178  func IsDir(path string) bool {
   179  	s, err := os.Stat(path)
   180  	if err != nil {
   181  
   182  		return false
   183  	}
   184  
   185  	return s.IsDir()
   186  }
   187  
   188  func AbsPath(upPath string) (ret string) {
   189  	if strings.HasPrefix(upPath, "/") {
   190  		ret = path.Join(upPath)
   191  	} else {
   192  		ret = path.Join("/", upPath)
   193  	}
   194  	if strings.HasSuffix(upPath, "/") && ret != "/" {
   195  		ret += "/"
   196  	}
   197  	return
   198  }