2023-03-07 22:19:09 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
iofs "io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-03-07 22:29:29 +00:00
|
|
|
"sort"
|
2023-03-07 22:19:09 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/franela/goblin"
|
|
|
|
"github.com/mholt/archiver/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestArchive_Stream(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
fs, rfs := NewFs()
|
|
|
|
|
|
|
|
g.Describe("Archive", func() {
|
|
|
|
g.AfterEach(func() {
|
|
|
|
// Reset the filesystem after each run.
|
2024-03-13 03:44:55 +00:00
|
|
|
_ = fs.TruncateRootDirectory()
|
2023-03-07 22:19:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("creates archive with intended files", func() {
|
|
|
|
g.Assert(fs.CreateDirectory("test", "/")).IsNil()
|
|
|
|
g.Assert(fs.CreateDirectory("test2", "/")).IsNil()
|
|
|
|
|
2024-03-13 03:44:55 +00:00
|
|
|
r := strings.NewReader("hello, world!\n")
|
|
|
|
err := fs.Write("test/file.txt", r, r.Size(), 0o644)
|
2023-03-07 22:19:09 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
2024-03-13 03:44:55 +00:00
|
|
|
r = strings.NewReader("hello, world!\n")
|
|
|
|
err = fs.Write("test2/file.txt", r, r.Size(), 0o644)
|
2023-03-07 22:19:09 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
2024-03-13 03:44:55 +00:00
|
|
|
r = strings.NewReader("hello, world!\n")
|
|
|
|
err = fs.Write("test_file.txt", r, r.Size(), 0o644)
|
2023-03-07 22:19:09 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
2024-03-13 03:44:55 +00:00
|
|
|
r = strings.NewReader("hello, world!\n")
|
|
|
|
err = fs.Write("test_file.txt.old", r, r.Size(), 0o644)
|
2023-03-07 22:19:09 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
|
|
|
a := &Archive{
|
2024-03-13 03:44:55 +00:00
|
|
|
Filesystem: fs,
|
2023-03-07 22:19:09 +00:00
|
|
|
Files: []string{
|
2024-03-13 03:44:55 +00:00
|
|
|
"test",
|
|
|
|
"test_file.txt",
|
2023-03-07 22:19:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:29:29 +00:00
|
|
|
// Create the archive.
|
2023-03-07 22:19:09 +00:00
|
|
|
archivePath := filepath.Join(rfs.root, "archive.tar.gz")
|
|
|
|
g.Assert(a.Create(context.Background(), archivePath)).IsNil()
|
|
|
|
|
|
|
|
// Ensure the archive exists.
|
|
|
|
_, err = os.Stat(archivePath)
|
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
|
|
|
// Open the archive.
|
2023-05-10 18:59:22 +00:00
|
|
|
genericFs, err := archiver.FileSystem(context.Background(), archivePath)
|
2023-03-07 22:19:09 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
|
|
|
// Assert that we are opening an archive.
|
|
|
|
afs, ok := genericFs.(archiver.ArchiveFS)
|
|
|
|
g.Assert(ok).IsTrue()
|
|
|
|
|
|
|
|
// Get the names of the files recursively from the archive.
|
|
|
|
files, err := getFiles(afs, ".")
|
|
|
|
g.Assert(err).IsNil()
|
|
|
|
|
|
|
|
// Ensure the files in the archive match what we are expecting.
|
2023-03-07 22:29:29 +00:00
|
|
|
expected := []string{
|
2023-03-07 22:19:09 +00:00
|
|
|
"test_file.txt",
|
|
|
|
"test/file.txt",
|
2023-03-07 22:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the slices to ensure the comparison never fails if the
|
|
|
|
// contents are sorted differently.
|
|
|
|
sort.Strings(expected)
|
|
|
|
sort.Strings(files)
|
|
|
|
|
|
|
|
g.Assert(files).Equal(expected)
|
2023-03-07 22:19:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFiles(f iofs.ReadDirFS, name string) ([]string, error) {
|
|
|
|
var v []string
|
|
|
|
|
|
|
|
entries, err := f.ReadDir(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range entries {
|
|
|
|
entryName := e.Name()
|
|
|
|
if name != "." {
|
|
|
|
entryName = filepath.Join(name, entryName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.IsDir() {
|
|
|
|
files, err := getFiles(f, entryName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if files == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-03-13 03:44:55 +00:00
|
|
|
|
2023-10-02 23:24:17 +00:00
|
|
|
v = append(v, files...)
|
2023-03-07 22:19:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v = append(v, entryName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|