Integrate real filesystem with interface abstraction
- Define pool.FileSystem interface in internal/io/pool/filesystem.go - Move DirEntry interface to internal/io/pool/types/types.go to break the pool <-> mock import cycle - Implement real.FileSystem with atomic writes (temp file + os.Rename) - Update mock.FileSystem to satisfy pool.FileSystem interface - Update worker pool tasks to accept pool.FileSystem interface - Update main.go to use RealFileSystem by default, with -root flag - Update all browser/editor/test references to types.DirEntry
This commit is contained in:
@@ -234,6 +234,7 @@ func TestWorkerPool_ContextPropagation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkerPool_DirPathInResult verifies that the task's DirPath is correctly reflected in the result.
|
||||
func TestWorkerPool_DirPathInResult(t *testing.T) {
|
||||
fs := mock.NewFileSystem()
|
||||
fs.CreateDir("/test/dir1")
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package pool
|
||||
|
||||
import "pad/internal/io/pool/types"
|
||||
|
||||
// FileSystem defines the interface for filesystem operations
|
||||
// used by the worker pool.
|
||||
type FileSystem interface {
|
||||
ReadDir(path string) ([]types.DirEntry, error)
|
||||
DirExists(path string) bool
|
||||
FileExists(path string) bool
|
||||
ReadFile(path string) ([]byte, error)
|
||||
WriteFile(path string, content []byte) error
|
||||
DeleteFile(path string) error
|
||||
CreateDir(path string) error
|
||||
}
|
||||
@@ -9,17 +9,9 @@ import (
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// File represents a file or directory in the mock filesystem.
|
||||
type File struct {
|
||||
Path string
|
||||
Name string
|
||||
Content []byte
|
||||
ModTime time.Time
|
||||
Size int64
|
||||
IsDir bool
|
||||
}
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
// DirEntry mimics fs.DirEntry behavior for the mock.
|
||||
type DirEntry struct {
|
||||
@@ -29,12 +21,22 @@ type DirEntry struct {
|
||||
size int64
|
||||
}
|
||||
|
||||
func (d DirEntry) Name() string { return d.name }
|
||||
func (d DirEntry) IsDir() bool { return d.isDir }
|
||||
func (d DirEntry) Name() string { return d.name }
|
||||
func (d DirEntry) IsDir() bool { return d.isDir }
|
||||
func (d DirEntry) Info() (os.FileInfo, error) {
|
||||
return &mockFileInfo{name: d.name, isDir: d.isDir, modTime: d.modTime, size: d.size}, nil
|
||||
}
|
||||
|
||||
// File represents a file or directory in the mock filesystem.
|
||||
type File struct {
|
||||
Path string
|
||||
Name string
|
||||
Content []byte
|
||||
ModTime time.Time
|
||||
Size int64
|
||||
IsDir bool
|
||||
}
|
||||
|
||||
// mockFileInfo implements os.FileInfo for the mock.
|
||||
type mockFileInfo struct {
|
||||
name string
|
||||
@@ -273,7 +275,7 @@ func (fs *FileSystem) CreateDir(path string) error {
|
||||
}
|
||||
|
||||
// ReadDir returns a list of entries in the given directory.
|
||||
func (fs *FileSystem) ReadDir(path string) ([]DirEntry, error) {
|
||||
func (fs *FileSystem) ReadDir(path string) ([]types.DirEntry, error) {
|
||||
fs.mu.Lock()
|
||||
|
||||
if fs.delay > 0 {
|
||||
@@ -288,7 +290,7 @@ func (fs *FileSystem) ReadDir(path string) ([]DirEntry, error) {
|
||||
return nil, fmt.Errorf("directory not found: %s", path)
|
||||
}
|
||||
|
||||
var entries []DirEntry
|
||||
var entries []types.DirEntry
|
||||
|
||||
for _, f := range fs.files {
|
||||
if f.Path == dirPath {
|
||||
@@ -308,7 +310,7 @@ func (fs *FileSystem) ReadDir(path string) ([]DirEntry, error) {
|
||||
// Sort by name
|
||||
for i := 0; i < len(entries); i++ {
|
||||
for j := i + 1; j < len(entries); j++ {
|
||||
if entries[i].name > entries[j].name {
|
||||
if entries[i].Name() > entries[j].Name() {
|
||||
entries[i], entries[j] = entries[j], entries[i]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package real
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
// RealFileSystem implements the pool.FileSystem interface using the real OS filesystem.
|
||||
type RealFileSystem struct {
|
||||
Root string
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) ReadDir(path string) ([]types.DirEntry, error) {
|
||||
entries, err := os.ReadDir(filepath.Join(fs.Root, path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res []types.DirEntry
|
||||
for _, e := range entries {
|
||||
res = append(res, &realDirEntry{e})
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) DirExists(path string) bool {
|
||||
info, err := os.Stat(filepath.Join(fs.Root, path))
|
||||
return err == nil && info.IsDir()
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) FileExists(path string) bool {
|
||||
info, err := os.Stat(filepath.Join(fs.Root, path))
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) ReadFile(path string) ([]byte, error) {
|
||||
return os.ReadFile(filepath.Join(fs.Root, path))
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) WriteFile(path string, content []byte) error {
|
||||
// Atomic write implementation
|
||||
fullPath := filepath.Join(fs.Root, path)
|
||||
tmpPath := filepath.Join(fs.Root, ".tmp", filepath.Base(path))
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(tmpPath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(tmpPath, content, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Rename(tmpPath, fullPath)
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) DeleteFile(path string) error {
|
||||
return os.Remove(filepath.Join(fs.Root, path))
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) CreateDir(path string) error {
|
||||
return os.MkdirAll(filepath.Join(fs.Root, path), 0755)
|
||||
}
|
||||
|
||||
// realDirEntry wraps os.DirEntry
|
||||
type realDirEntry struct {
|
||||
entry os.DirEntry
|
||||
}
|
||||
|
||||
func (e *realDirEntry) Name() string { return e.entry.Name() }
|
||||
func (e *realDirEntry) IsDir() bool { return e.entry.IsDir() }
|
||||
func (e *realDirEntry) Info() (os.FileInfo, error) { return e.entry.Info() }
|
||||
+20
-22
@@ -9,8 +9,6 @@ import (
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"pad/internal/io/pool/mock"
|
||||
)
|
||||
|
||||
// taskCounter generates unique task IDs.
|
||||
@@ -95,10 +93,10 @@ type Task interface {
|
||||
type ReadDirTask struct {
|
||||
taskID string
|
||||
Dir string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewReadDirTask(dir string, fs *mock.FileSystem) *ReadDirTask {
|
||||
func NewReadDirTask(dir string, fs FileSystem) *ReadDirTask {
|
||||
return &ReadDirTask{
|
||||
taskID: fmt.Sprintf("read_dir_%s_%d", filepath.Base(dir), taskCounter.Add(1)),
|
||||
Dir: dir,
|
||||
@@ -136,10 +134,10 @@ func (t *ReadDirTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
type BuildIndexTask struct {
|
||||
taskID string
|
||||
Dir string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewBuildIndexTask(dir string, fs *mock.FileSystem) *BuildIndexTask {
|
||||
func NewBuildIndexTask(dir string, fs FileSystem) *BuildIndexTask {
|
||||
return &BuildIndexTask{
|
||||
taskID: fmt.Sprintf("build_index_%s_%d", filepath.Base(dir), taskCounter.Add(1)),
|
||||
Dir: dir,
|
||||
@@ -180,10 +178,10 @@ func (t *BuildIndexTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
type LoadIndexTask struct {
|
||||
taskID string
|
||||
Dir string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewLoadIndexTask(dir string, fs *mock.FileSystem) *LoadIndexTask {
|
||||
func NewLoadIndexTask(dir string, fs FileSystem) *LoadIndexTask {
|
||||
return &LoadIndexTask{
|
||||
taskID: fmt.Sprintf("load_index_%s_%d", filepath.Base(dir), taskCounter.Add(1)),
|
||||
Dir: dir,
|
||||
@@ -215,10 +213,10 @@ type LoadPagesTask struct {
|
||||
taskID string
|
||||
Dir string
|
||||
PageIndices []int
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewLoadPagesTask(dir string, pageIndices []int, fs *mock.FileSystem) *LoadPagesTask {
|
||||
func NewLoadPagesTask(dir string, pageIndices []int, fs FileSystem) *LoadPagesTask {
|
||||
return &LoadPagesTask{
|
||||
taskID: fmt.Sprintf("load_pages_%s_%d", filepath.Base(dir), taskCounter.Add(1)),
|
||||
Dir: dir,
|
||||
@@ -249,10 +247,10 @@ func (t *LoadPagesTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
type StatDirTask struct {
|
||||
taskID string
|
||||
Dir string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewStatDirTask(dir string, fs *mock.FileSystem) *StatDirTask {
|
||||
func NewStatDirTask(dir string, fs FileSystem) *StatDirTask {
|
||||
return &StatDirTask{
|
||||
taskID: fmt.Sprintf("stat_dir_%s_%d", filepath.Base(dir), taskCounter.Add(1)),
|
||||
Dir: dir,
|
||||
@@ -284,10 +282,10 @@ func (t *StatDirTask) Timeout() time.Duration { return 30 * time.Second }
|
||||
type ReadFileTask struct {
|
||||
taskID string
|
||||
Path string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
}
|
||||
|
||||
func NewReadFileTask(path string, fs *mock.FileSystem) *ReadFileTask {
|
||||
func NewReadFileTask(path string, fs FileSystem) *ReadFileTask {
|
||||
return &ReadFileTask{
|
||||
taskID: fmt.Sprintf("read_file_%s_%d", filepath.Base(path), taskCounter.Add(1)),
|
||||
Path: path,
|
||||
@@ -325,11 +323,11 @@ func (t *ReadFileTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
type WriteFileTask struct {
|
||||
taskID string
|
||||
Path string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewWriteFileTask(path string, data []byte, fs *mock.FileSystem) *WriteFileTask {
|
||||
func NewWriteFileTask(path string, data []byte, fs FileSystem) *WriteFileTask {
|
||||
return &WriteFileTask{
|
||||
taskID: fmt.Sprintf("write_file_%s_%d", filepath.Base(path), taskCounter.Add(1)),
|
||||
Path: path,
|
||||
@@ -371,11 +369,11 @@ func (t *WriteFileTask) Timeout() time.Duration { return 30 * time.Second }
|
||||
type WriteCacheTask struct {
|
||||
taskID string
|
||||
Path string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewWriteCacheTask(path string, data []byte, fs *mock.FileSystem) *WriteCacheTask {
|
||||
func NewWriteCacheTask(path string, data []byte, fs FileSystem) *WriteCacheTask {
|
||||
return &WriteCacheTask{
|
||||
taskID: fmt.Sprintf("write_cache_%s_%d", filepath.Base(path), taskCounter.Add(1)),
|
||||
Path: path,
|
||||
@@ -415,11 +413,11 @@ func (t *WriteCacheTask) Timeout() time.Duration { return 30 * time.Second }
|
||||
type SaveStateTask struct {
|
||||
taskID string
|
||||
Path string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewSaveStateTask(path string, data []byte, fs *mock.FileSystem) *SaveStateTask {
|
||||
func NewSaveStateTask(path string, data []byte, fs FileSystem) *SaveStateTask {
|
||||
return &SaveStateTask{
|
||||
taskID: fmt.Sprintf("save_state_%s_%d", filepath.Base(path), taskCounter.Add(1)),
|
||||
Path: path,
|
||||
@@ -457,11 +455,11 @@ func (t *SaveStateTask) Timeout() time.Duration { return 30 * time.Second }
|
||||
type SaveUndoTask struct {
|
||||
taskID string
|
||||
Path string
|
||||
FS *mock.FileSystem
|
||||
FS FileSystem
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewSaveUndoTask(path string, data []byte, fs *mock.FileSystem) *SaveUndoTask {
|
||||
func NewSaveUndoTask(path string, data []byte, fs FileSystem) *SaveUndoTask {
|
||||
return &SaveUndoTask{
|
||||
taskID: fmt.Sprintf("save_undo_%s_%d", filepath.Base(path), taskCounter.Add(1)),
|
||||
Path: path,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"pad/internal/io/pool/mock"
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
func TestReadDirTask_Execute_Success(t *testing.T) {
|
||||
@@ -26,9 +27,9 @@ func TestReadDirTask_Execute_Success(t *testing.T) {
|
||||
t.Error("TaskID should not be empty")
|
||||
}
|
||||
|
||||
entries, ok := result.Data.([]mock.DirEntry)
|
||||
entries, ok := result.Data.([]types.DirEntry)
|
||||
if !ok {
|
||||
t.Fatalf("Data is not []mock.DirEntry, got %T", result.Data)
|
||||
t.Fatalf("Data is not []types.DirEntry, got %T", result.Data)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Errorf("Expected 2 entries, got %d", len(entries))
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package types
|
||||
|
||||
import "os"
|
||||
|
||||
// DirEntry interface defines the structure for directory entries.
|
||||
type DirEntry interface {
|
||||
Name() string
|
||||
IsDir() bool
|
||||
Info() (os.FileInfo, error)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"pad/internal/io/pool/mock"
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
// stubTask is a minimal task implementation for testing the worker pool.
|
||||
@@ -471,9 +472,9 @@ func TestWorkerPool_ResultWithRealTasks(t *testing.T) {
|
||||
if !result.IsSuccess() {
|
||||
t.Errorf("ReadDir task failed: %v", result.Error)
|
||||
}
|
||||
entries, ok := result.Data.([]mock.DirEntry)
|
||||
entries, ok := result.Data.([]types.DirEntry)
|
||||
if !ok {
|
||||
t.Fatalf("Result data is not []mock.DirEntry, got %T", result.Data)
|
||||
t.Fatalf("Result data is not []types.DirEntry, got %T", result.Data)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Errorf("Entries count = %d, want 2", len(entries))
|
||||
|
||||
Reference in New Issue
Block a user