-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package writefs | ||
|
||
import ( | ||
"io/fs" | ||
"testing/fstest" | ||
) | ||
|
||
type testWriteFS struct { | ||
fstest.MapFS | ||
expectedErr error | ||
} | ||
|
||
var _ WriteFS = testWriteFS{} | ||
|
||
type testRemoveFS struct { | ||
testWriteFS | ||
removed string | ||
} | ||
|
||
var _ RemoveFS = &testRemoveFS{} | ||
|
||
func (fsys *testRemoveFS) Remove(name string) error { | ||
fsys.removed = name | ||
return nil | ||
} | ||
|
||
type testMkDirFS struct { | ||
testWriteFS | ||
created string | ||
} | ||
|
||
var _ MkDirFS = &testMkDirFS{} | ||
|
||
func (fsys *testMkDirFS) MkDir(name string, perm fs.FileMode) error { | ||
fsys.created = name | ||
return nil | ||
} | ||
|
||
type testFileWriter struct { | ||
} | ||
|
||
var _ FileWriter = testFileWriter{} | ||
|
||
func (w testFileWriter) Close() error { | ||
return nil | ||
} | ||
func (w testFileWriter) Write(buf []byte) (int, error) { | ||
return len(buf), nil | ||
} | ||
func (w testFileWriter) Read(buf []byte) (int, error) { | ||
return len(buf), nil | ||
} | ||
func (w testFileWriter) Stat() (fs.FileInfo, error) { | ||
return nil, nil | ||
} | ||
|
||
// OpenFile ... | ||
func (fsys testWriteFS) OpenFile(name string, flag int, perm fs.FileMode) (FileWriter, error) { | ||
if !fs.ValidPath(name) { | ||
return nil, &fs.PathError{} | ||
} | ||
return testFileWriter{}, fsys.expectedErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package writefs | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMkDirFS(t *testing.T) { | ||
roFS := fstest.MapFS{} | ||
writefs := testWriteFS{roFS, nil} | ||
mkdirfs := &testMkDirFS{writefs, ""} | ||
|
||
t.Run("MkDir calls Open with O_CREATE for non RemoveFS but WriteFs instances", func(t *testing.T) { | ||
writefs.expectedErr = errors.New("adir2/afile2") | ||
err := MkDir(writefs, "adir2/afile2", fs.FileMode(0)) | ||
assert.Error(t, err) | ||
writefs.expectedErr = nil | ||
assert.Equal(t, "adir2/afile2", err.Error()) | ||
}) | ||
|
||
t.Run("MkDir calls fsys.MkDir for RemoveFS instances", func(t *testing.T) { | ||
mkdirfs.created = "" | ||
err := MkDir(mkdirfs, "adir2/afile2", fs.FileMode(0)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "adir2/afile2", mkdirfs.created) | ||
mkdirfs.created = "" | ||
}) | ||
|
||
t.Run("MkDir return error for read only fs.FS", func(t *testing.T) { | ||
err := MkDir(roFS, "adir2", fs.FileMode(0)) | ||
assert.Error(t, err) | ||
assert.True(t, errors.Is(err, fs.ErrInvalid)) | ||
assert.Equal(t, "invalid argument: fsys does not support creation of directories", err.Error()) | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package writefs | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveFS(t *testing.T) { | ||
roFS := fstest.MapFS{} | ||
writefs := testWriteFS{roFS, nil} | ||
removefs := &testRemoveFS{writefs, ""} | ||
|
||
t.Run("Remove calls Open with O_TRUNC for non RemoveFS but WriteFs instances", func(t *testing.T) { | ||
writefs.expectedErr = errors.New("adir2/afile2") | ||
err := Remove(writefs, "adir2/afile2") | ||
assert.Error(t, err) | ||
writefs.expectedErr = nil | ||
assert.Equal(t, "adir2/afile2", err.Error()) | ||
}) | ||
|
||
t.Run("Remove calls fsys.Remove for RemoveFS instances", func(t *testing.T) { | ||
removefs.removed = "" | ||
err := Remove(removefs, "adir2/afile2") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "adir2/afile2", removefs.removed) | ||
removefs.removed = "" | ||
}) | ||
|
||
t.Run("Remove return error for read onyl fs.FS", func(t *testing.T) { | ||
err := Remove(roFS, "adir2") | ||
assert.Error(t, err) | ||
assert.True(t, errors.Is(err, fs.ErrInvalid)) | ||
assert.Equal(t, "invalid argument: fsys does not support removal of files", err.Error()) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters