Skip to content

Commit 66c2ab4

Browse files
evankandersonpjbgf
authored andcommitted
Prevent test failures on Windows, address feedback from pjbgf
1 parent d0af75c commit 66c2ab4

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

helper/iofs/iofs.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ type adapterFs struct {
2020
fs billyfs.Filesystem
2121
}
2222

23+
// Type assertion that adapterFS implements the following interfaces:
2324
var _ fs.FS = (*adapterFs)(nil)
2425
var _ fs.ReadDirFS = (*adapterFs)(nil)
2526
var _ fs.StatFS = (*adapterFs)(nil)
2627
var _ fs.ReadFileFS = (*adapterFs)(nil)
2728

28-
// GlobFS would be harder, we don't implement for now.
29+
// TODO: implement fs.GlobFS, which will be a fair bit more code.
2930

30-
// Open implements fs.FS.
31+
// Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error).
3132
func (a *adapterFs) Open(name string) (fs.File, error) {
3233
if name[0] == '/' || name != filepath.Clean(name) {
33-
// fstest.TestFS explicitly checks that these should return error
34-
// MemFS is performs the clean internally, so we need to block that here for testing.
34+
// fstest.TestFS explicitly checks that these should return error.
35+
// MemFS performs the clean internally, so we need to block that here for testing purposes.
3536
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
3637
}
3738
stat, err := a.fs.Stat(name)
@@ -49,7 +50,7 @@ func (a *adapterFs) Open(name string) (fs.File, error) {
4950
return &adapterFile{file: file, info: stat}, err
5051
}
5152

52-
// ReadDir implements fs.ReadDirFS.
53+
// ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error).
5354
func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
5455
items, err := a.fs.ReadDir(name)
5556
if err != nil {
@@ -62,12 +63,12 @@ func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
6263
return entries, nil
6364
}
6465

65-
// Stat implements fs.StatFS.
66+
// Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error).
6667
func (a *adapterFs) Stat(name string) (fs.FileInfo, error) {
6768
return a.fs.Stat(name)
6869
}
6970

70-
// ReadFile implements fs.ReadFileFS.
71+
// ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error).
7172
func (a *adapterFs) ReadFile(name string) ([]byte, error) {
7273
stat, err := a.fs.Stat(name)
7374
if err != nil {
@@ -90,17 +91,17 @@ type adapterFile struct {
9091

9192
var _ fs.File = (*adapterFile)(nil)
9293

93-
// Close implements fs.File.
94+
// Close closes the file, implementing fs.File (and io.Closer).
9495
func (a *adapterFile) Close() error {
9596
return a.file.Close()
9697
}
9798

98-
// Read implements fs.File.
99+
// Read reads bytes from the file, implementing fs.File (and io.Reader).
99100
func (a *adapterFile) Read(b []byte) (int, error) {
100101
return a.file.Read(b)
101102
}
102103

103-
// Stat implements fs.File.
104+
// Stat returns file information, implementing fs.File (returning FileInfo or error).
104105
func (a *adapterFile) Stat() (fs.FileInfo, error) {
105106
return a.info, nil
106107
}
@@ -119,24 +120,21 @@ func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile {
119120
}
120121
}
121122

122-
// Close implements fs.File.
123+
// Close closes the directory, implementing fs.File (and io.Closer).
123124
// Subtle: note that this is shadowing adapterFile.Close.
124125
func (a *adapterDirFile) Close() error {
125126
return nil
126127
}
127128

128-
// ReadDir implements fs.ReadDirFile.
129+
// ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error).
129130
func (a *adapterDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
130131
if len(a.entries) == 0 && n > 0 {
131132
return nil, io.EOF
132133
}
133-
if n <= 0 {
134-
n = len(a.entries)
135-
}
136-
if n > len(a.entries) {
134+
if n <= 0 || n > len(a.entries) {
137135
n = len(a.entries)
138136
}
139137
entries := a.entries[:n]
140138
a.entries = a.entries[n:]
141139
return entries, nil
142-
}
140+
}

helper/iofs/iofs_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,27 @@ type wrappedError interface {
2121
Unwrap() error
2222
}
2323

24-
// TestWithFSTest leverages the packaged Go fstest package, which seems comprehensive
24+
// TestWithFSTest leverages the packaged Go fstest package, which seems comprehensive.
2525
func TestWithFSTest(t *testing.T) {
2626
t.Parallel()
2727
memfs := memfs.New()
2828
iofs := Wrap(memfs)
2929

3030
files := map[string]string{
31-
"foo.txt": "hello, world",
32-
"bar.txt": "goodbye, world",
33-
"dir/baz.txt": "こんにちわ, world",
31+
"foo.txt": "hello, world",
32+
"bar.txt": "goodbye, world",
33+
filepath.Join("dir", "baz.txt"): "こんにちわ, world",
3434
}
3535
created_files := make([]string, 0, len(files))
3636
for filename, contents := range files {
3737
makeFile(memfs, t, filename, contents)
3838
created_files = append(created_files, filename)
3939
}
4040

41+
if runtime.GOOS == "windows" {
42+
t.Skip("fstest.TestFS is not yet windows path aware")
43+
}
44+
4145
err := fstest.TestFS(iofs, created_files...)
4246
if err != nil {
4347
checkFsTestError(t, err, files)

0 commit comments

Comments
 (0)