Implement test list files

This commit is contained in:
2024-11-14 22:11:40 +01:00
parent e4510298ed
commit 408746187e
5 changed files with 98 additions and 9 deletions

View File

@@ -0,0 +1,60 @@
// Package storage_test provides tests for the storage package.
package storage_test
import (
"novamd/internal/storage"
"novamd/internal/testutils"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListFilesRecursively(t *testing.T) {
tests := []testutils.TestCase{
{
Name: "empty workspace returns empty list",
Setup: func(t *testing.T, fixtures any) {
fs := fixtures.(*MapFS)
require.NoError(t, fs.MkdirAll("/test/root/1/1", 0755))
},
Fixtures: NewMapFS(),
Validate: func(t *testing.T, result any, err error) {
require.NoError(t, err)
files := result.([]storage.FileNode)
assert.Empty(t, files)
},
},
{
Name: "lists files and directories correctly",
Setup: func(t *testing.T, fixtures any) {
fs := fixtures.(*MapFS)
err := fs.WriteFile("/test/root/1/1/file1.md", []byte("content1"), 0644)
require.NoError(t, err, "Failed to write file1.md")
err = fs.WriteFile("/test/root/1/1/dir/file2.md", []byte("content2"), 0644)
require.NoError(t, err, "Failed to write file2.md")
},
Fixtures: NewMapFS(),
Validate: func(t *testing.T, result any, err error) {
require.NoError(t, err)
files := result.([]storage.FileNode)
require.Len(t, files, 2)
assert.Equal(t, "dir", files[0].Name)
assert.Equal(t, "file1.md", files[1].Name)
assert.Len(t, files[0].Children, 1)
assert.Equal(t, "file2.md", files[0].Children[0].Name)
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
fs := tc.Fixtures.(*MapFS)
srv := storage.NewServiceWithFS("/test/root", fs)
tc.Setup(t, tc.Fixtures)
files, err := srv.ListFilesRecursively(1, 1)
tc.Validate(t, files, err)
})
}
}

View File

@@ -6,19 +6,19 @@ import (
"testing/fstest"
)
// mapFS adapts testing.MapFS to implement our fileSystem interface
type mapFS struct {
// MapFS adapts testing.MapFS to implement our fileSystem interface
type MapFS struct {
fstest.MapFS
}
func NewMapFS() *mapFS {
return &mapFS{
func NewMapFS() *MapFS {
return &MapFS{
MapFS: make(fstest.MapFS),
}
}
// Only implement the methods that MapFS doesn't already provide
func (m *mapFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
func (m *MapFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
m.MapFS[path] = &fstest.MapFile{
Data: data,
Mode: perm,
@@ -26,21 +26,21 @@ func (m *mapFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
return nil
}
func (m *mapFS) Remove(path string) error {
func (m *MapFS) Remove(path string) error {
delete(m.MapFS, path)
return nil
}
func (m *mapFS) MkdirAll(_ string, _ fs.FileMode) error {
func (m *MapFS) MkdirAll(_ string, _ fs.FileMode) error {
// For MapFS, we don't actually need to create directories
return nil
}
func (m *mapFS) RemoveAll(path string) error {
func (m *MapFS) RemoveAll(path string) error {
delete(m.MapFS, path)
return nil
}
func (m *mapFS) IsNotExist(err error) bool {
func (m *MapFS) IsNotExist(err error) bool {
return os.IsNotExist(err)
}

View File

@@ -0,0 +1,12 @@
package storage_test
import (
"novamd/internal/storage"
"testing"
)
func SetupTestService(t *testing.T) (*storage.Service, *MapFS) {
fs := &MapFS{}
srv := storage.NewServiceWithFS("/test/root", fs)
return srv, fs
}

View File

@@ -0,0 +1,13 @@
package testutils
import (
"testing"
)
// TestCase defines a generic test case structure that can be used across packages
type TestCase struct {
Name string
Setup func(t *testing.T, fixtures any)
Fixtures any
Validate func(t *testing.T, result any, err error)
}