add tests

This commit is contained in:
royalcat 2023-10-16 19:36:36 +03:00
parent d30ef6cc9c
commit 741bae49a4
6 changed files with 125 additions and 136 deletions

View file

@ -88,11 +88,11 @@ PARTS_LOOP:
} }
if nestOn == -1 { if nestOn == -1 {
return name, nil, "", nil return clean(name), nil, "", nil
} }
fsPath = Clean(strings.Join(parts[:nestOn], Separator)) fsPath = clean(strings.Join(parts[:nestOn], Separator))
nestedFsPath = Clean(strings.Join(parts[nestOn:], Separator)) nestedFsPath = clean(strings.Join(parts[nestOn:], Separator))
// we dont need lock until now // we dont need lock until now
// it must be before fsmap read to exclude race condition: // it must be before fsmap read to exclude race condition:

View file

@ -2,6 +2,9 @@ package vfs
import ( import (
"os" "os"
"testing"
"github.com/stretchr/testify/require"
) )
type Dummy struct { type Dummy struct {
@ -49,144 +52,95 @@ func (d *DummyFs) ReadDir(path string) (map[string]File, error) {
var _ Filesystem = &DummyFs{} var _ Filesystem = &DummyFs{}
// func TestDefaultFactories(t *testing.T) { func TestResolver(t *testing.T) {
// t.Parallel() t.Parallel()
resolver := newResolver(ArchiveFactories)
t.Run("nested fs", func(t *testing.T) {
t.Parallel()
require := require.New(t)
// 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)
// require.Contains(defaultFactories, ".zip") fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("/", func(path string) (File, error) {
// require.Contains(defaultFactories, ".rar") require.Equal("/", path)
// require.Contains(defaultFactories, ".7z") return &Dummy{}, nil
})
require.Nil(err)
require.Nil(nestedFs)
require.Equal("/", fsPath)
require.Equal("", nestedFsPath)
})
// fs, err := defaultFactories[".zip"](&Dummy{}, nil) t.Run("root dirty", func(t *testing.T) {
// require.NoError(err) t.Parallel()
// require.NotNil(fs) require := require.New(t)
// fs, err = defaultFactories[".rar"](&Dummy{}, nil) fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//", func(path string) (File, error) {
// require.NoError(err) require.Equal("/", path)
// require.NotNil(fs) 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)
// fs, err = defaultFactories[".7z"](&Dummy{}, nil) fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//.//f1.rar", func(path string) (File, error) {
// require.NoError(err) require.Equal("/f1.rar", path)
// require.NotNil(fs) 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)
// func TestStorageAddFs(t *testing.T) { fsPath, nestedFs, nestedFsPath, err := resolver.resolvePath("//test1/f1.rar", func(path string) (File, error) {
// t.Parallel() 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)
})
}
// require := require.New(t) func TestArchiveFactories(t *testing.T) {
t.Parallel()
// s := newStorage(dummyFactories) require := require.New(t)
// err := s.AddFS(&DummyFs{}, "/test") require.Contains(ArchiveFactories, ".zip")
// require.NoError(err) require.Contains(ArchiveFactories, ".rar")
require.Contains(ArchiveFactories, ".7z")
// f, err := s.Get("/test/dir/here/file1.txt") fs, err := ArchiveFactories[".zip"](&Dummy{})
// require.NoError(err) require.NoError(err)
// require.NotNil(f) require.NotNil(fs)
// err = s.AddFS(&DummyFs{}, "/test") fs, err = ArchiveFactories[".rar"](&Dummy{})
// require.Error(err) require.NoError(err)
// } require.NotNil(fs)
// func TestStorageWindowsPath(t *testing.T) { fs, err = ArchiveFactories[".7z"](&Dummy{})
// t.Parallel() require.NoError(err)
require.NotNil(fs)
// require := require.New(t) }
// s := newStorage(dummyFactories)
// err := s.Add(&Dummy{}, "\\path\\to\\dummy\\file.txt")
// require.NoError(err)
// file, err := s.Get("\\path\\to\\dummy\\file.txt")
// require.NoError(err)
// require.Equal(&Dummy{}, file)
// file, err = s.Get("/path/to/dummy/file.txt")
// require.NoError(err)
// require.Equal(&Dummy{}, file)
// }
// var dummyFactories = map[string]vfs.FsFactory{
// ".test": func(f vfs.File, factories map[string]vfs.FsFactory) (vfs.Filesystem, error) {
// return &DummyFs{}, nil
// },
// }
// func TestStorage(t *testing.T) {
// t.Parallel()
// require := require.New(t)
// s := newStorage(dummyFactories)
// err := s.Add(&Dummy{}, "/path/to/dummy/file.txt")
// require.NoError(err)
// err = s.Add(&Dummy{}, "/path/to/dummy/file2.txt")
// require.NoError(err)
// contains := s.Has("/path")
// require.True(contains)
// contains = s.Has("/path/to/dummy/")
// require.True(contains)
// file, err := s.Get("/path/to/dummy/file.txt")
// require.NoError(err)
// require.Equal(&Dummy{}, file)
// file, err = s.Get("/path/to/dummy/file3.txt")
// require.Error(err)
// require.Nil(file)
// files, err := s.Children("/path/to/dummy/")
// require.NoError(err)
// require.Len(files, 2)
// require.Contains(files, "file.txt")
// require.Contains(files, "file2.txt")
// err = s.Add(&Dummy{}, "/path/to/dummy/folder/file.txt")
// require.NoError(err)
// files, err = s.Children("/path/to/dummy/")
// require.NoError(err)
// require.Len(files, 3)
// require.Contains(files, "file.txt")
// require.Contains(files, "file2.txt")
// require.Contains(files, "folder")
// err = s.Add(&Dummy{}, "path/file4.txt")
// require.NoError(err)
// require.True(s.Has("/path/file4.txt"))
// files, err = s.Children("/")
// require.NoError(err)
// require.Len(files, 1)
// err = s.Add(&Dummy{}, "/path/special_file.test")
// require.NoError(err)
// file, err = s.Get("/path/special_file.test/dir/here/file1.txt")
// require.NoError(err)
// require.Equal(&Dummy{}, file)
// files, err = s.Children("/path/special_file.test")
// require.NoError(err)
// require.NotNil(files)
// files, err = s.Children("/path/special_file.test/dir/here")
// require.NoError(err)
// require.Len(files, 2)
// err = s.Add(&Dummy{}, "/path/to/__special__path/file3.txt")
// require.NoError(err)
// file, err = s.Get("/path/to/__special__path/file3.txt")
// require.NoError(err)
// require.Equal(&Dummy{}, file)
// s.Clear()
// }

View file

@ -33,7 +33,7 @@ func (fs *TorrentFs) files() map[string]*torrentFile {
files := make(map[string]*torrentFile) files := make(map[string]*torrentFile)
<-fs.t.GotInfo() <-fs.t.GotInfo()
for _, file := range fs.t.Files() { for _, file := range fs.t.Files() {
p := Clean(file.Path()) p := clean(file.Path())
files[p] = &torrentFile{ files[p] = &torrentFile{
readerFunc: file.NewReader, readerFunc: file.NewReader,
len: file.Length(), len: file.Length(),

View file

@ -85,6 +85,8 @@ func TestMain(m *testing.M) {
// } // }
func TestReadAtTorrent(t *testing.T) { func TestReadAtTorrent(t *testing.T) {
t.Parallel()
require := require.New(t) require := require.New(t)
to, err := Cli.AddMagnet(testMagnet) to, err := Cli.AddMagnet(testMagnet)

View file

@ -9,7 +9,7 @@ import (
var ErrNotExist = fs.ErrNotExist var ErrNotExist = fs.ErrNotExist
func getFile[F File](m map[string]F, name string) (File, error) { func getFile[F File](m map[string]F, name string) (File, error) {
name = Clean(name) name = clean(name)
if name == Separator { if name == Separator {
return &Dir{}, nil return &Dir{}, nil
} }
@ -29,7 +29,7 @@ func getFile[F File](m map[string]F, name string) (File, error) {
} }
func listFilesInDir[F File](m map[string]F, name string) (map[string]File, error) { func listFilesInDir[F File](m map[string]F, name string) (map[string]File, error) {
name = Clean(name) name = clean(name)
out := map[string]File{} out := map[string]File{}
for p, f := range m { for p, f := range m {
@ -50,6 +50,6 @@ func trimRelPath(p, t string) string {
return strings.Trim(strings.TrimPrefix(p, t), "/") return strings.Trim(strings.TrimPrefix(p, t), "/")
} }
func Clean(p string) string { func clean(p string) string {
return path.Clean(Separator + strings.ReplaceAll(p, "\\", "/")) return path.Clean(Separator + strings.ReplaceAll(p, "\\", "/"))
} }

View file

@ -0,0 +1,33 @@
package vfs
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestFiles(t *testing.T) {
t.Parallel()
require := require.New(t)
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)
}
{
out, err := listFilesInDir(files, "/test")
require.Nil(err)
require.Contains(out, "file.txt")
require.Equal(&Dummy{}, out["file.txt"])
}
}