diff --git a/internal/filesystem/filesystem_test.go b/internal/filesystem/filesystem_test.go index 17b815a9..ba215727 100644 --- a/internal/filesystem/filesystem_test.go +++ b/internal/filesystem/filesystem_test.go @@ -2,11 +2,11 @@ package filesystem_test import ( "context" - "io" "io/fs" "os" - "path/filepath" + "strconv" "testing" + "testing/fstest" "github.com/djdv/go-filesystem-utils/internal/filesystem" ) @@ -56,27 +56,13 @@ func TestFilesystem(t *testing.T) { func openFileFS(t *testing.T) { t.Parallel() const fileName = "file" - var ( - tempDir = t.TempDir() - testFilePath = filepath.Join(tempDir, fileName) - ) - f, err := os.Create(testFilePath) - if err != nil { - t.Fatal(err) + testFS := fstest.MapFS{ + fileName: new(fstest.MapFile), } - if err := f.Close(); err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - if err := os.Remove(testFilePath); err != nil { - t.Error(err) - } - }) // Wrapper around standard [fs.File.Open] should succeed // with read-only flags. - stdFS := os.DirFS(tempDir) - stdFSFile, err := filesystem.OpenFile(stdFS, fileName, os.O_RDONLY, 0) + stdFSFile, err := filesystem.OpenFile(testFS, fileName, os.O_RDONLY, 0) if err != nil { t.Fatal(err) } @@ -86,7 +72,7 @@ func openFileFS(t *testing.T) { // Wrapper around standard [fs.File.Open] should /not/ succeed // with other flags. - stdFSFileBad, err := filesystem.OpenFile(stdFS, fileName, os.O_RDWR, 0) + 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 { @@ -97,7 +83,7 @@ func openFileFS(t *testing.T) { } // Extension mock should allow additional flags and arguments. - extendedFS := &openFileFSMock{FS: os.DirFS(tempDir)} + extendedFS := &openFileFSMock{FS: testFS} extendedFSFile, err := filesystem.OpenFile(extendedFS, fileName, os.O_RDWR|os.O_CREATE, 0o777) if err != nil { t.Fatal(err) @@ -109,65 +95,48 @@ func openFileFS(t *testing.T) { func streamDir(t *testing.T) { t.Parallel() - const testEntCount = 64 + const ( + fsRoot = "." + testEntCount = 64 + ) var ( - tempDir = t.TempDir() - dirCleanup = populateDir(t, tempDir, testEntCount) + 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, + ) + } + } ) - t.Cleanup(dirCleanup) - - // Truth values from standard [os]. - osEntries, err := os.ReadDir(tempDir) - if err != nil { - t.Fatal(err) + for i := 0; i < testEntCount; i++ { + testFS[strconv.Itoa(i)] = testFile } // Values returned utilizing standard [fs.ReadDirFile]. - dirFile, err := os.Open(tempDir) + stdFile, err := testFS.Open(fsRoot) if err != nil { t.Fatal(err) } - fsEntries := streamEntries(t, dirFile) - if err := dirFile.Close(); err != nil { + 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) } - compareLen(t, fsEntries, osEntries) + check(stdEntries) // Values returned utilizing our extension. var ( - streamDir = &streamDirMock{entries: fsEntries} - streamEntries = streamEntries(t, streamDir) + extendedReadDirFile = &streamDirMock{entries: stdEntries} + extensionEntries = streamEntries(t, extendedReadDirFile) ) - compareLen(t, streamEntries, osEntries) -} - -func compareLen[T1, T2 any](t *testing.T, got []T1, want []T2) { - t.Helper() - if got, want := len(got), len(want); got != want { - t.Errorf("length mismatch"+ - "\n\tgot: %d"+ - "\n\twant: %d", - got, want, - ) - } -} - -func populateDir(t *testing.T, dir string, entCount int) func() { - closers := make([]io.Closer, 0, entCount) - for i := 0; i < entCount; i++ { - file, err := os.CreateTemp(dir, "") - if err != nil { - t.Error(err) - } - closers = append(closers, file) - } - return func() { - for _, closer := range closers { - if err := closer.Close(); err != nil { - t.Error(err) - } - } - } + check(extensionEntries) } func streamEntries(t *testing.T, dir fs.ReadDirFile) []filesystem.StreamDirEntry {