github.com/viant/toolbox@v0.34.5/storage/copy_test.go (about)

     1  package storage_test
     2  
     3  import (
     4  	"archive/zip"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/viant/toolbox"
     7  	"github.com/viant/toolbox/storage"
     8  	_ "github.com/viant/toolbox/storage/scp"
     9  	"os"
    10  	"path"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestCopy(t *testing.T) {
    16  	service := storage.NewService()
    17  	assert.NotNil(t, service)
    18  
    19  	parent := toolbox.CallerDirectory(3)
    20  	baseUrl := "file://" + parent + "/test"
    21  
    22  	toolbox.CreateDirIfNotExist(path.Join(parent, "test/target"))
    23  
    24  	{
    25  		sourceURL := path.Join(baseUrl, "source/")
    26  		targetURL := path.Join(baseUrl, "target/")
    27  		err := storage.Copy(service, sourceURL, service, targetURL, nil, nil)
    28  		assert.Nil(t, err)
    29  
    30  		expectedFiles := []string{
    31  			path.Join(parent, "test/target/file1.txt"),
    32  			path.Join(parent, "test/target/file2.txt"),
    33  			path.Join(parent, "test/target/dir/file.json"),
    34  			path.Join(parent, "test/target/dir2/subdir/file1.txt"),
    35  		}
    36  		for _, file := range expectedFiles {
    37  			assert.True(t, toolbox.FileExists(file))
    38  			//os.Remove(file)
    39  		}
    40  	}
    41  }
    42  
    43  func TestArchive(t *testing.T) {
    44  	memService := storage.NewMemoryService()
    45  	memService.Upload("mem://test/copy/archive/file1.txt", strings.NewReader("abc"))
    46  	memService.Upload("mem://test/copy/archive/file2.txt", strings.NewReader("xyz"))
    47  	memService.Upload("mem://test/copy/archive/config/test.prop", strings.NewReader("123"))
    48  	toolbox.RemoveFileIfExist("/tmp/testCopy.zip")
    49  	var writer, err = os.OpenFile("/tmp/testCopy.zip", os.O_CREATE|os.O_WRONLY, 06444)
    50  	if assert.Nil(t, err) {
    51  		defer writer.Close()
    52  		archive := zip.NewWriter(writer)
    53  		err = storage.Archive(memService, "mem://test/copy/archive/", archive)
    54  		assert.Nil(t, err)
    55  		archive.Flush()
    56  		archive.Close()
    57  	}
    58  }