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

@@ -11,6 +11,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/mattn/go-sqlite3 v1.14.23 github.com/mattn/go-sqlite3 v1.14.23
github.com/stretchr/testify v1.9.0
github.com/unrolled/secure v1.17.0 github.com/unrolled/secure v1.17.0
golang.org/x/crypto v0.21.0 golang.org/x/crypto v0.21.0
) )
@@ -22,6 +23,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
@@ -33,6 +35,7 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect
@@ -42,4 +45,5 @@ require (
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

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