Skip to content

Commit

Permalink
filesystem: add extension wrappers + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Dec 2, 2022
1 parent 9fc9bae commit 06dd171
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package filesystem

import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"time"
)

Expand Down Expand Up @@ -67,6 +71,11 @@ type (
fs.FileInfo
CreationTime() time.Time
}

dirEntryWrapper struct {
fs.DirEntry
error
}
)

// Go file permission bits.
Expand All @@ -83,3 +92,46 @@ const (
WriteUser
ReadUser
)

func (dw dirEntryWrapper) Error() error { return dw.error }

func OpenFile(fsys fs.FS, name string, flag int, perm fs.FileMode) (fs.File, error) {
if fsys, ok := fsys.(OpenFileFS); ok {
return fsys.OpenFile(name, flag, perm)
}
if flag == os.O_RDONLY {
return fsys.Open(name)
}
return nil, fmt.Errorf(`open "%s": operation not supported`, name)
}

func StreamDir(ctx context.Context, directory fs.ReadDirFile) <-chan StreamDirEntry {
if dirStreamer, ok := directory.(StreamDirFile); ok {
return dirStreamer.StreamDir(ctx)
}
stream := make(chan StreamDirEntry)
go func() {
defer close(stream)
const batchCount = 16 // NOTE: Count chosen arbitrarily.
for {
ents, err := directory.ReadDir(batchCount)
if err != nil {
if !errors.Is(err, io.EOF) {
select {
case stream <- dirEntryWrapper{error: err}:
case <-ctx.Done():
}
}
return
}
for _, ent := range ents {
select {
case stream <- dirEntryWrapper{DirEntry: ent}:
case <-ctx.Done():
return
}
}
}
}()
return stream
}
154 changes: 154 additions & 0 deletions internal/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package filesystem_test

import (
"context"
"io/fs"
"os"
"strconv"
"testing"
"testing/fstest"

"github.com/djdv/go-filesystem-utils/internal/filesystem"
)

type (
openFileFSMock struct{ fs.FS }
streamDirMock struct {
fs.ReadDirFile
entries []filesystem.StreamDirEntry
}
)

var (
_ filesystem.OpenFileFS = (*openFileFSMock)(nil)
_ filesystem.StreamDirFile = (*streamDirMock)(nil)
)

func (of *openFileFSMock) OpenFile(name string, _ int, _ fs.FileMode) (fs.File, error) {
// NOTE: Mock discards arguments.
// We're only interested in seeing the test coverage trace.
// The wrapper should follow the [filesystem.OpenFileFS]
// type-assertion branch, when passed our mock.
return of.FS.Open(name)
}

func (sd *streamDirMock) StreamDir(ctx context.Context) <-chan filesystem.StreamDirEntry {
entries := make(chan filesystem.StreamDirEntry)
go func() {
defer close(entries)
for _, entry := range sd.entries {
select {
case entries <- entry:
case <-ctx.Done():
return
}
}
}()
return entries
}

func TestFilesystem(t *testing.T) {
t.Parallel()
t.Run("OpenFileFS", openFileFS)
t.Run("StreamDir", streamDir)
}

func openFileFS(t *testing.T) {
t.Parallel()
const fileName = "file"
testFS := fstest.MapFS{
fileName: new(fstest.MapFile),
}

// Wrapper around standard [fs.File.Open] should succeed
// with read-only flags.
stdFSFile, err := filesystem.OpenFile(testFS, fileName, os.O_RDONLY, 0)
if err != nil {
t.Fatal(err)
}
if err := stdFSFile.Close(); err != nil {
t.Fatal(err)
}

// Wrapper around standard [fs.File.Open] should /not/ succeed
// with other flags.
stdFSFileBad, err := filesystem.OpenFile(testFS, fileName, os.O_RDWR, 0)
if err == nil {
t.Error("expected wrapper to deny access with unexpected flags, but got no error")
if stdFSFileBad != nil {
if err := stdFSFileBad.Close(); err != nil {
t.Errorf("additionally, close failed for returned file: %s", err)
}
}
}

// Extension mock should allow additional flags and arguments.
extendedFS := &openFileFSMock{FS: testFS}
extendedFSFile, err := filesystem.OpenFile(extendedFS, fileName, os.O_RDWR|os.O_CREATE, 0o777)
if err != nil {
t.Fatal(err)
}
if err := extendedFSFile.Close(); err != nil {
t.Fatal(err)
}
}

func streamDir(t *testing.T) {
t.Parallel()
const (
fsRoot = "."
testEntCount = 64
)
var (
testFS = make(fstest.MapFS, testEntCount)
testFile = new(fstest.MapFile)
check = func(entries []filesystem.StreamDirEntry) {
if got, want := len(entries), len(testFS); got != want {
t.Errorf("length mismatch"+
"\n\tgot: %d"+
"\n\twant: %d",
got, want,
)
}
}
)
for i := 0; i < testEntCount; i++ {
testFS[strconv.Itoa(i)] = testFile
}

// Values returned utilizing standard [fs.ReadDirFile].
stdFile, err := testFS.Open(fsRoot)
if err != nil {
t.Fatal(err)
}
stdReadDirFile, ok := stdFile.(fs.ReadDirFile)
if !ok {
t.Fatalf("%T does no impliment expected fs.ReadDirFile interface", stdFile)
}
stdEntries := streamEntries(t, stdReadDirFile)
if err := stdFile.Close(); err != nil {
t.Error(err)
}
check(stdEntries)

// Values returned utilizing our extension.
var (
extendedReadDirFile = &streamDirMock{entries: stdEntries}
extensionEntries = streamEntries(t, extendedReadDirFile)
)
check(extensionEntries)
}

func streamEntries(t *testing.T, dir fs.ReadDirFile) []filesystem.StreamDirEntry {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
entries := make([]filesystem.StreamDirEntry, 0)
for entry := range filesystem.StreamDir(ctx, dir) {
if err := entry.Error(); err != nil {
t.Error(err)
} else {
entries = append(entries, entry)
}
}
return entries
}

0 comments on commit 06dd171

Please sign in to comment.