93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package zip
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
content = "test-content"
|
|
)
|
|
|
|
func TestZipper(t *testing.T) {
|
|
archiveFolder := "testdata"
|
|
archiveFilename := archiveFolder + "/dummy"
|
|
testFileName := "testZipper.data"
|
|
if os.WriteFile(testFileName, []byte(content), 0644) == nil {
|
|
defer os.Remove(testFileName)
|
|
}
|
|
z := NewZipper(archiveFilename)
|
|
if z.GetWriter() != nil {
|
|
t.Log("Found unexpected writer")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test adding files
|
|
if z.AddZipFiles([]string{testFileName}, "testfolder") != nil {
|
|
t.Log("Unable to add a single file")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test adding more files than allowed
|
|
if z.AddFileToZip(testFileName, "testfolder/more") != nil {
|
|
t.Log("Unexpectedly failed in adding more than one file")
|
|
t.Fail()
|
|
}
|
|
|
|
z.Close()
|
|
|
|
uz := NewUnzipper(archiveFilename + ArchiveExt)
|
|
|
|
if uz.ExtractFiles("") != nil {
|
|
t.Log("Failed to extract files")
|
|
t.Fail()
|
|
} else {
|
|
if data, err := os.ReadFile("testfolder/" + testFileName); err != nil {
|
|
t.Log("Expected extracted file #1 is missing")
|
|
t.Fail()
|
|
} else if string(data) != content {
|
|
t.Logf("Unexpected extracted file #1 content: '%s'", string(data))
|
|
t.Fail()
|
|
}
|
|
if data, err := os.ReadFile("testfolder/more/" + testFileName); err != nil {
|
|
t.Log("Expected extracted file #2 is missing")
|
|
t.Fail()
|
|
} else if string(data) != content {
|
|
t.Logf("Unexpected extracted file #2 content: '%s'", string(data))
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
// Clean up
|
|
removeRecursive("testfolder")
|
|
os.Remove(archiveFilename + ArchiveExt)
|
|
os.Remove(archiveFolder)
|
|
}
|
|
|
|
func removeRecursive(path string) {
|
|
filepath.Walk(path, deleteFiles)
|
|
filepath.WalkDir(path, deleteFolders)
|
|
filepath.WalkDir(path, deleteFolders)
|
|
filepath.WalkDir(path, deleteFolders)
|
|
}
|
|
|
|
func deleteFiles(path string, info fs.FileInfo, errIn error) (err error) {
|
|
if errIn != nil {
|
|
return
|
|
}
|
|
if !info.IsDir() {
|
|
err = os.Remove(path)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func deleteFolders(path string, info fs.DirEntry, errIn error) (err error) {
|
|
if errIn != nil {
|
|
return
|
|
}
|
|
os.Remove(path)
|
|
return
|
|
}
|