github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/move_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 TestMove(t *testing.T) {
    15  	SetUp()
    16  	defer TearDown()
    17  
    18  	upRootPath := path.Join(ROOT, "move")
    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  	Upx("put", localRootPath, upRootPath)
    30  	files, err := Ls(path.Join(upRootPath, localRootName))
    31  
    32  	assert.NoError(t, err)
    33  	assert.Len(t, files, 2)
    34  	assert.ElementsMatch(
    35  		t,
    36  		files,
    37  		[]string{"FILE1", "FILE2"},
    38  	)
    39  
    40  	time.Sleep(time.Second)
    41  
    42  	// 正常移动文件
    43  	_, err = Upx(
    44  		"mv",
    45  		path.Join(upRootPath, localRootName, "FILE1"),
    46  		path.Join(upRootPath, localRootName, "FILE3"),
    47  	)
    48  	assert.NoError(t, err)
    49  
    50  	files, err = Ls(path.Join(upRootPath, localRootName))
    51  	assert.NoError(t, err)
    52  	assert.Len(t, files, 2)
    53  	assert.ElementsMatch(
    54  		t,
    55  		files,
    56  		[]string{"FILE2", "FILE3"},
    57  	)
    58  
    59  	time.Sleep(time.Second)
    60  
    61  	// 目标文件已存在
    62  	_, err = Upx(
    63  		"mv",
    64  		path.Join(upRootPath, localRootName, "FILE2"),
    65  		path.Join(upRootPath, localRootName, "FILE3"),
    66  	)
    67  	assert.Equal(
    68  		t,
    69  		err.Error(),
    70  		fmt.Sprintf(
    71  			"target path %s already exists use -f to force overwrite\n",
    72  			path.Join(upRootPath, localRootName, "FILE3"),
    73  		),
    74  	)
    75  
    76  	files, err = Ls(path.Join(upRootPath, localRootName))
    77  	assert.NoError(t, err)
    78  	assert.Len(t, files, 2)
    79  	assert.ElementsMatch(
    80  		t,
    81  		files,
    82  		[]string{"FILE2", "FILE3"},
    83  	)
    84  
    85  	time.Sleep(time.Second)
    86  
    87  	// 目标文件已存在, 强制覆盖
    88  	_, err = Upx(
    89  		"mv",
    90  		"-f",
    91  		path.Join(upRootPath, localRootName, "FILE2"),
    92  		path.Join(upRootPath, localRootName, "FILE3"),
    93  	)
    94  	assert.NoError(t, err)
    95  
    96  	files, err = Ls(path.Join(upRootPath, localRootName))
    97  	assert.NoError(t, err)
    98  	assert.Len(t, files, 1)
    99  	assert.ElementsMatch(
   100  		t,
   101  		files,
   102  		[]string{"FILE3"},
   103  	)
   104  }