mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-01 19:38:34 +02:00
189 lines
4 KiB
Go
189 lines
4 KiB
Go
/*
|
|
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
package archive_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"slices"
|
|
"testing"
|
|
|
|
"go.uploadedlobster.com/scotty/pkg/archive"
|
|
)
|
|
|
|
func ExampleOpenArchive() {
|
|
a, err := archive.OpenArchive("testdata/archive.zip")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer a.Close()
|
|
|
|
files, err := a.Glob("a/*.txt")
|
|
for _, fi := range files {
|
|
fmt.Println(fi.Name)
|
|
f, err := fi.File.Open()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer f.Close()
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(string(data))
|
|
}
|
|
|
|
// Output: a/1.txt
|
|
// a1
|
|
}
|
|
|
|
var testArchives = []string{
|
|
"testdata/archive",
|
|
"testdata/archive.zip",
|
|
}
|
|
|
|
func TestGlob(t *testing.T) {
|
|
for _, path := range testArchives {
|
|
a, err := archive.OpenArchive(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
|
|
files, err := a.Glob("[ab]/1.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(files) != 2 {
|
|
t.Errorf("Expected 2 files, got %d", len(files))
|
|
}
|
|
|
|
expectedName := "b/1.txt"
|
|
var fileInfo *archive.FileInfo = nil
|
|
for _, file := range files {
|
|
if file.Name == expectedName {
|
|
fileInfo = &file
|
|
}
|
|
}
|
|
|
|
if fileInfo == nil {
|
|
t.Fatalf("Expected file %q to be found", expectedName)
|
|
}
|
|
|
|
if fileInfo.File == nil {
|
|
t.Fatalf("Expected FileInfo to hold an openable File")
|
|
}
|
|
|
|
f, err := fileInfo.File.Open()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedData := "b1\n"
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(data) != expectedData {
|
|
fmt.Printf("%s: Expected file content to be %q, got %q",
|
|
path, expectedData, string(data))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGlobAll(t *testing.T) {
|
|
for _, path := range testArchives {
|
|
a, err := archive.OpenArchive(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
|
|
files, err := a.Glob("*/*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
filenames := make([]string, 0, len(files))
|
|
for _, f := range files {
|
|
fmt.Printf("%v: %v\n", path, f.Name)
|
|
filenames = append(filenames, f.Name)
|
|
}
|
|
|
|
slices.Sort(filenames)
|
|
|
|
expectedFilenames := []string{
|
|
"a/1.txt",
|
|
"b/1.txt",
|
|
"b/2.txt",
|
|
}
|
|
if !slices.Equal(filenames, expectedFilenames) {
|
|
t.Errorf("%s: Expected filenames to be %q, got %q",
|
|
path, expectedFilenames, filenames)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpen(t *testing.T) {
|
|
for _, path := range testArchives {
|
|
a, err := archive.OpenArchive(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
|
|
f, err := a.Open("b/2.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedData := "b2\n"
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(data) != expectedData {
|
|
fmt.Printf("%s: Expected file content to be %q, got %q",
|
|
path, expectedData, string(data))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenError(t *testing.T) {
|
|
for _, path := range testArchives {
|
|
a, err := archive.OpenArchive(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
|
|
_, err = a.Open("b/3.txt")
|
|
if err == nil {
|
|
t.Errorf("%s: Expected the Open command to fail", path)
|
|
}
|
|
}
|
|
}
|