114 lines
2.7 KiB
Go
114 lines
2.7 KiB
Go
package archiver
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"github.com/go-git/go-billy/v5/memfs"
|
|
"github.com/stretchr/testify/assert"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func TestArchiveFS(t *testing.T) {
|
|
err := os.MkdirAll(testArchiveBaseDir, os.ModePerm)
|
|
assert.NoError(t, err)
|
|
|
|
memoryFS := memfs.New()
|
|
|
|
archiveF, err := memoryFS.OpenFile("archive.tar.gz", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
|
|
assert.NoError(t, err)
|
|
defer archiveF.Close()
|
|
|
|
gzipWriter := gzip.NewWriter(archiveF)
|
|
defer gzipWriter.Close()
|
|
|
|
tarWriter := tar.NewWriter(gzipWriter)
|
|
defer tarWriter.Close()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
tarHeader := &tar.Header{
|
|
Name: "test" + strconv.Itoa(i) + ".txt",
|
|
Size: int64(len([]byte("Hello, World! #" + strconv.Itoa(i)))),
|
|
Mode: 0600,
|
|
}
|
|
err = tarWriter.WriteHeader(tarHeader)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = tarWriter.Write([]byte("Hello, World! #" + strconv.Itoa(i)))
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
err = tarWriter.Close()
|
|
assert.NoError(t, err)
|
|
err = gzipWriter.Close()
|
|
assert.NoError(t, err)
|
|
err = archiveF.Close()
|
|
assert.NoError(t, err)
|
|
|
|
t.Log("== OpenArchive ==")
|
|
archive, err := OpenFS(WrapBillyFS(memoryFS), TarGzip, "archive.tar.gz")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 10, archive.FileCount())
|
|
|
|
t.Log("== ExtractArchiveFile ==")
|
|
archiveFile, err := archive.GetFile("test0.txt")
|
|
assert.NoError(t, err)
|
|
|
|
err = archiveFile.Extract(ExtractFileOptions{
|
|
Folder: testArchiveBaseDir + "/extracted/fs",
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(testArchiveBaseDir + "/extracted/fs")
|
|
assert.NoError(t, err)
|
|
|
|
t.Log("== ExtractArchive ==")
|
|
err = archive.Extract(ExtractOptions{
|
|
Folder: testArchiveBaseDir + "/extracted/fs",
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(testArchiveBaseDir + "/extracted/fs")
|
|
assert.NoError(t, err)
|
|
|
|
t.Log("== ExtractArchiveFilter ==")
|
|
err = archive.Extract(ExtractOptions{
|
|
Folder: testArchiveBaseDir + "/extracted/fs",
|
|
Filter: archiveRegex,
|
|
})
|
|
|
|
assert.FileExists(t, testArchiveBaseDir+"/extracted/fs/test1.txt")
|
|
assert.FileExists(t, testArchiveBaseDir+"/extracted/fs/test5.txt")
|
|
|
|
err = os.RemoveAll(testArchiveBaseDir + "/extracted/fs")
|
|
assert.NoError(t, err)
|
|
|
|
err = archive.Close()
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestExtractArchiveBillyFS(t *testing.T) {
|
|
err := os.MkdirAll(testArchiveBaseDir, os.ModePerm)
|
|
assert.NoError(t, err)
|
|
testGenerateTar(t)
|
|
|
|
archive, err := Open(Tar, testArchiveBaseDir+"/test.tar")
|
|
assert.NoError(t, err)
|
|
|
|
memoryFS := memfs.New()
|
|
|
|
err = archive.ExtractBillyFS(memoryFS, ExtractOptions{
|
|
Folder: memoryFS.Root(),
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
files, err := memoryFS.ReadDir(memoryFS.Root())
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 10, len(files))
|
|
|
|
for _, file := range files {
|
|
assert.NotEqual(t, int64(0), file.Size())
|
|
}
|
|
}
|