2023-10-16 09:18:40 +00:00
|
|
|
package vfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-10-18 09:52:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-16 09:18:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Dummy struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dummy) Size() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dummy) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dummy) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dummy) Read(p []byte) (n int, err error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dummy) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ File = &Dummy{}
|
|
|
|
|
|
|
|
type DummyFs struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DummyFs) Open(filename string) (File, error) {
|
|
|
|
return &Dummy{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DummyFs) ReadDir(path string) (map[string]File, error) {
|
|
|
|
if path == "/dir/here" {
|
|
|
|
return map[string]File{
|
|
|
|
"file1.txt": &Dummy{},
|
|
|
|
"file2.txt": &Dummy{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Filesystem = &DummyFs{}
|
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
func TestResolver(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
resolver := newResolver(ArchiveFactories)
|
|
|
|
t.Run("nested fs", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("/f1.rar/f2.rar", func(path string) (File, error) {
|
|
|
|
require.Equal("/f1.rar", path)
|
|
|
|
return &Dummy{}, nil
|
|
|
|
})
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal("/f1.rar", fsPath)
|
|
|
|
require.Equal("/f2.rar", nestedFsPath)
|
|
|
|
require.IsType(&archive{}, nestedFs)
|
|
|
|
})
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("/", func(path string) (File, error) {
|
|
|
|
require.Equal("/", path)
|
|
|
|
return &Dummy{}, nil
|
|
|
|
})
|
|
|
|
require.Nil(err)
|
|
|
|
require.Nil(nestedFs)
|
|
|
|
require.Equal("/", fsPath)
|
|
|
|
require.Equal("", nestedFsPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("root dirty", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//", func(path string) (File, error) {
|
|
|
|
require.Equal("/", path)
|
|
|
|
return &Dummy{}, nil
|
|
|
|
})
|
|
|
|
require.Nil(err)
|
|
|
|
require.Nil(nestedFs)
|
|
|
|
require.Equal("/", fsPath)
|
|
|
|
require.Equal("", nestedFsPath)
|
|
|
|
})
|
|
|
|
t.Run("fs dirty", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//f1.rar", func(path string) (File, error) {
|
|
|
|
require.Equal("/f1.rar", path)
|
|
|
|
return &Dummy{}, nil
|
|
|
|
})
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal("/f1.rar", fsPath)
|
|
|
|
require.Equal("/", nestedFsPath)
|
|
|
|
require.IsType(&archive{}, nestedFs)
|
|
|
|
})
|
|
|
|
t.Run("inside folder", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//test1/f1.rar", func(path string) (File, error) {
|
|
|
|
require.Equal("/test1/f1.rar", path)
|
|
|
|
return &Dummy{}, nil
|
|
|
|
})
|
|
|
|
require.Nil(err)
|
|
|
|
require.IsType(&archive{}, nestedFs)
|
|
|
|
require.Equal("/test1/f1.rar", fsPath)
|
|
|
|
require.Equal("/", nestedFsPath)
|
|
|
|
})
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
func TestArchiveFactories(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
require := require.New(t)
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
require.Contains(ArchiveFactories, ".zip")
|
|
|
|
require.Contains(ArchiveFactories, ".rar")
|
|
|
|
require.Contains(ArchiveFactories, ".7z")
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
fs, err := ArchiveFactories[".zip"](&Dummy{})
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(fs)
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
fs, err = ArchiveFactories[".rar"](&Dummy{})
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(fs)
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
fs, err = ArchiveFactories[".7z"](&Dummy{})
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(fs)
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
func TestFiles(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
files := map[string]*Dummy{
|
|
|
|
"/test/file.txt": &Dummy{},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
file, err := getFile(files, "/test")
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(&Dir{}, file)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
file, err := getFile(files, "/test/file.txt")
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(&Dummy{}, file)
|
|
|
|
}
|
2023-10-16 09:18:40 +00:00
|
|
|
|
2023-10-18 09:52:48 +00:00
|
|
|
{
|
|
|
|
out, err := listFilesInDir(files, "/test")
|
|
|
|
require.Nil(err)
|
|
|
|
require.Contains(out, "file.txt")
|
|
|
|
require.Equal(&Dummy{}, out["file.txt"])
|
|
|
|
}
|
|
|
|
}
|