From cc65aa854d2d676ca38b8825a40c43be98a678aa Mon Sep 17 00:00:00 2001 From: nhatthm Date: Mon, 16 Sep 2024 22:50:11 +0200 Subject: [PATCH] Add `FileIntoCallbacks` --- file_callbacks.go | 84 +++++------ file_callbacks_test.go | 56 ++++---- file_info_callbacks.go | 77 ++++++++++ file_info_callbacks_test.go | 275 ++++++++++++++++++++++++++++++++++++ file_test.go | 4 +- fs_callbacks.go | 86 +++++------ fs_callbacks_test.go | 56 ++++---- 7 files changed, 495 insertions(+), 143 deletions(-) create mode 100644 file_info_callbacks.go create mode 100644 file_info_callbacks_test.go diff --git a/file_callbacks.go b/file_callbacks.go index 5ef74a4..bf8f75a 100644 --- a/file_callbacks.go +++ b/file_callbacks.go @@ -26,123 +26,123 @@ type FileCallbacks struct { } // Close satisfies the afero.File interface. -func (f *FileCallbacks) Close() error { +func (f FileCallbacks) Close() error { return f.CloseFunc() } // Name satisfies the afero.File interface. -func (f *FileCallbacks) Name() string { +func (f FileCallbacks) Name() string { return f.NameFunc() } // Read satisfies the afero.File interface. -func (f *FileCallbacks) Read(p []byte) (int, error) { +func (f FileCallbacks) Read(p []byte) (int, error) { return f.ReadFunc(p) } // ReadAt satisfies the afero.File interface. -func (f *FileCallbacks) ReadAt(p []byte, off int64) (int, error) { +func (f FileCallbacks) ReadAt(p []byte, off int64) (int, error) { return f.ReadAtFunc(p, off) } // Readdir satisfies the afero.File interface. -func (f *FileCallbacks) Readdir(count int) ([]fs.FileInfo, error) { +func (f FileCallbacks) Readdir(count int) ([]fs.FileInfo, error) { return f.ReaddirFunc(count) } // Readdirnames satisfies the afero.File interface. -func (f *FileCallbacks) Readdirnames(n int) ([]string, error) { +func (f FileCallbacks) Readdirnames(n int) ([]string, error) { return f.ReaddirnamesFunc(n) } // Seek satisfies the afero.File interface. -func (f *FileCallbacks) Seek(offset int64, whence int) (int64, error) { +func (f FileCallbacks) Seek(offset int64, whence int) (int64, error) { return f.SeekFunc(offset, whence) } // Stat satisfies the afero.File interface. -func (f *FileCallbacks) Stat() (fs.FileInfo, error) { +func (f FileCallbacks) Stat() (fs.FileInfo, error) { return f.StatFunc() } // Sync satisfies the afero.File interface. -func (f *FileCallbacks) Sync() error { +func (f FileCallbacks) Sync() error { return f.SyncFunc() } // Truncate satisfies the afero.File interface. -func (f *FileCallbacks) Truncate(size int64) error { +func (f FileCallbacks) Truncate(size int64) error { return f.TruncateFunc(size) } // Write satisfies the afero.File interface. -func (f *FileCallbacks) Write(p []byte) (int, error) { +func (f FileCallbacks) Write(p []byte) (int, error) { return f.WriteFunc(p) } // WriteAt satisfies the afero.File interface. -func (f *FileCallbacks) WriteAt(p []byte, off int64) (int, error) { +func (f FileCallbacks) WriteAt(p []byte, off int64) (int, error) { return f.WriteAtFunc(p, off) } // WriteString satisfies the afero.File interface. -func (f *FileCallbacks) WriteString(s string) (int, error) { +func (f FileCallbacks) WriteString(s string) (int, error) { return f.WriteStringFunc(s) } -// OverrideFile overrides the file methods with the given callbacks. -func OverrideFile(file afero.File, callbacks FileCallbacks) *FileCallbacks { //nolint: cyclop,dupl - if callbacks.CloseFunc == nil { - callbacks.CloseFunc = file.Close +// OverrideFile overrides the afero.File methods with the provided callbacks. +func OverrideFile(file afero.File, c FileCallbacks) FileCallbacks { //nolint: cyclop,dupl + if c.CloseFunc == nil { + c.CloseFunc = file.Close } - if callbacks.NameFunc == nil { - callbacks.NameFunc = file.Name + if c.NameFunc == nil { + c.NameFunc = file.Name } - if callbacks.ReadFunc == nil { - callbacks.ReadFunc = file.Read + if c.ReadFunc == nil { + c.ReadFunc = file.Read } - if callbacks.ReadAtFunc == nil { - callbacks.ReadAtFunc = file.ReadAt + if c.ReadAtFunc == nil { + c.ReadAtFunc = file.ReadAt } - if callbacks.ReaddirFunc == nil { - callbacks.ReaddirFunc = file.Readdir + if c.ReaddirFunc == nil { + c.ReaddirFunc = file.Readdir } - if callbacks.ReaddirnamesFunc == nil { - callbacks.ReaddirnamesFunc = file.Readdirnames + if c.ReaddirnamesFunc == nil { + c.ReaddirnamesFunc = file.Readdirnames } - if callbacks.SeekFunc == nil { - callbacks.SeekFunc = file.Seek + if c.SeekFunc == nil { + c.SeekFunc = file.Seek } - if callbacks.StatFunc == nil { - callbacks.StatFunc = file.Stat + if c.StatFunc == nil { + c.StatFunc = file.Stat } - if callbacks.SyncFunc == nil { - callbacks.SyncFunc = file.Sync + if c.SyncFunc == nil { + c.SyncFunc = file.Sync } - if callbacks.TruncateFunc == nil { - callbacks.TruncateFunc = file.Truncate + if c.TruncateFunc == nil { + c.TruncateFunc = file.Truncate } - if callbacks.WriteFunc == nil { - callbacks.WriteFunc = file.Write + if c.WriteFunc == nil { + c.WriteFunc = file.Write } - if callbacks.WriteAtFunc == nil { - callbacks.WriteAtFunc = file.WriteAt + if c.WriteAtFunc == nil { + c.WriteAtFunc = file.WriteAt } - if callbacks.WriteStringFunc == nil { - callbacks.WriteStringFunc = file.WriteString + if c.WriteStringFunc == nil { + c.WriteStringFunc = file.WriteString } - return &callbacks + return c } diff --git a/file_callbacks_test.go b/file_callbacks_test.go index 4777596..834f218 100644 --- a/file_callbacks_test.go +++ b/file_callbacks_test.go @@ -36,7 +36,7 @@ func TestFileCallbacks_Close(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ CloseFunc: func() error { @@ -46,7 +46,7 @@ func TestFileCallbacks_Close(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ CloseFunc: func() error { @@ -63,7 +63,7 @@ func TestFileCallbacks_Close(t *testing.T) { actual := aferomock.OverrideFile(tc.mockFile(t), tc.fileCallbacks).Close() - require.Equal(t, actual, tc.expectedError) + require.Equal(t, tc.expectedError, actual) }) } } @@ -93,7 +93,7 @@ func TestFileCallbacks_Name(t *testing.T) { expectedResult: "name", }, { - scenario: "wrapped - no name", + scenario: "overridden - no name", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ NameFunc: func() string { @@ -102,7 +102,7 @@ func TestFileCallbacks_Name(t *testing.T) { }, }, { - scenario: "wrapped - has name", + scenario: "overridden - has name", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ NameFunc: func() string { @@ -152,7 +152,7 @@ func TestFileCallbacks_Read(t *testing.T) { expectedResult: 5, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReadFunc: func([]byte) (int, error) { @@ -162,7 +162,7 @@ func TestFileCallbacks_Read(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReadFunc: func(b []byte) (int, error) { @@ -213,7 +213,7 @@ func TestFileCallbacks_ReadAt(t *testing.T) { expectedResult: 4, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReadAtFunc: func([]byte, int64) (int, error) { @@ -223,7 +223,7 @@ func TestFileCallbacks_ReadAt(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReadAtFunc: func(b []byte, off int64) (int, error) { @@ -276,7 +276,7 @@ func TestFileCallbacks_Readdir(t *testing.T) { expectedResult: []fs.FileInfo{fi}, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReaddirFunc: func(int) ([]fs.FileInfo, error) { @@ -286,7 +286,7 @@ func TestFileCallbacks_Readdir(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReaddirFunc: func(int) ([]fs.FileInfo, error) { @@ -337,7 +337,7 @@ func TestFileCallbacks_Readdirnames(t *testing.T) { expectedResult: []string{"foobar"}, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReaddirnamesFunc: func(int) ([]string, error) { @@ -347,7 +347,7 @@ func TestFileCallbacks_Readdirnames(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ ReaddirnamesFunc: func(int) ([]string, error) { @@ -398,7 +398,7 @@ func TestFileCallbacks_Seek(t *testing.T) { expectedResult: int64(10), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ SeekFunc: func(int64, int) (int64, error) { @@ -408,7 +408,7 @@ func TestFileCallbacks_Seek(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ SeekFunc: func(int64, int) (int64, error) { @@ -461,7 +461,7 @@ func TestFileCallbacks_Stat(t *testing.T) { expectedResult: fi, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ StatFunc: func() (fs.FileInfo, error) { @@ -471,7 +471,7 @@ func TestFileCallbacks_Stat(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ StatFunc: func() (fs.FileInfo, error) { @@ -520,7 +520,7 @@ func TestFileCallbacks_Sync(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ SyncFunc: func() error { @@ -530,7 +530,7 @@ func TestFileCallbacks_Sync(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ SyncFunc: func() error { @@ -547,7 +547,7 @@ func TestFileCallbacks_Sync(t *testing.T) { actual := aferomock.OverrideFile(tc.mockFile(t), tc.fileCallbacks).Sync() - require.Equal(t, actual, tc.expectedError) + require.Equal(t, tc.expectedError, actual) }) } } @@ -577,7 +577,7 @@ func TestFileCallbacks_Truncate(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ TruncateFunc: func(int64) error { @@ -587,7 +587,7 @@ func TestFileCallbacks_Truncate(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ TruncateFunc: func(int64) error { @@ -636,7 +636,7 @@ func TestFileCallbacks_Write(t *testing.T) { expectedResult: 5, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteFunc: func([]byte) (int, error) { @@ -646,7 +646,7 @@ func TestFileCallbacks_Write(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteFunc: func(b []byte) (int, error) { @@ -697,7 +697,7 @@ func TestFileCallbacks_WriteAt(t *testing.T) { expectedResult: 4, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteAtFunc: func([]byte, int64) (int, error) { @@ -707,7 +707,7 @@ func TestFileCallbacks_WriteAt(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteAtFunc: func(b []byte, off int64) (int, error) { @@ -758,7 +758,7 @@ func TestFileCallbacks_WriteString(t *testing.T) { expectedResult: 5, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteStringFunc: func(string) (int, error) { @@ -768,7 +768,7 @@ func TestFileCallbacks_WriteString(t *testing.T) { expectedError: errors.New("error"), }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFile: aferomock.NopFile, fileCallbacks: aferomock.FileCallbacks{ WriteStringFunc: func(s string) (int, error) { diff --git a/file_info_callbacks.go b/file_info_callbacks.go new file mode 100644 index 0000000..d082e5c --- /dev/null +++ b/file_info_callbacks.go @@ -0,0 +1,77 @@ +package aferomock + +import ( + "io/fs" + "time" +) + +var _ fs.FileInfo = (*FileInfoCallbacks)(nil) + +// FileInfoCallbacks is a callback-based mock for fs.FileInfo. +type FileInfoCallbacks struct { + NameFunc func() string + SizeFunc func() int64 + ModeFunc func() fs.FileMode + ModTimeFunc func() time.Time + IsDirFunc func() bool + SysFunc func() interface{} +} + +// Name satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) Name() string { + return f.NameFunc() +} + +// Size satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) Size() int64 { + return f.SizeFunc() +} + +// Mode satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) Mode() fs.FileMode { + return f.ModeFunc() +} + +// ModTime satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) ModTime() time.Time { + return f.ModTimeFunc() +} + +// IsDir satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) IsDir() bool { + return f.IsDirFunc() +} + +// Sys satisfies the fs.FileInfo interface. +func (f FileInfoCallbacks) Sys() interface{} { + return f.SysFunc() +} + +// OverrideFileInfo overrides the fs.FileInfo methods with the provided callbacks. +func OverrideFileInfo(fi fs.FileInfo, c FileInfoCallbacks) FileInfoCallbacks { + if c.NameFunc == nil { + c.NameFunc = fi.Name + } + + if c.SizeFunc == nil { + c.SizeFunc = fi.Size + } + + if c.ModeFunc == nil { + c.ModeFunc = fi.Mode + } + + if c.ModTimeFunc == nil { + c.ModTimeFunc = fi.ModTime + } + + if c.IsDirFunc == nil { + c.IsDirFunc = fi.IsDir + } + + if c.SysFunc == nil { + c.SysFunc = fi.Sys + } + + return c +} diff --git a/file_info_callbacks_test.go b/file_info_callbacks_test.go new file mode 100644 index 0000000..c97a18c --- /dev/null +++ b/file_info_callbacks_test.go @@ -0,0 +1,275 @@ +package aferomock_test + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "go.nhat.io/aferomock" +) + +func TestFileInfoCallbacks_Name(t *testing.T) { + t.Parallel() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult string + }{ + { + scenario: "upstream - no name", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("Name"). + Return("") + }), + }, + { + scenario: "upstream - has name", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("Name"). + Return("name") + }), + expectedResult: "name", + }, + { + scenario: "overridden - no name", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + NameFunc: func() string { + return "" + }, + }, + }, + { + scenario: "overridden - has name", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + NameFunc: func() string { + return "name" + }, + }, + expectedResult: "name", + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).Name() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} + +func TestFileInfoCallbacks_Size(t *testing.T) { + t.Parallel() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult int64 + }{ + { + scenario: "upstream", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("Size"). + Return(int64(10)) + }), + expectedResult: 10, + }, + { + scenario: "overridden", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + SizeFunc: func() int64 { + return 64 + }, + }, + expectedResult: 64, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).Size() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} + +func TestFileInfoCallbacks_Mode(t *testing.T) { + t.Parallel() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult os.FileMode + }{ + { + scenario: "upstream", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("Mode"). + Return(os.FileMode(10)) + }), + expectedResult: 10, + }, + { + scenario: "overridden", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + ModeFunc: func() os.FileMode { + return 64 + }, + }, + expectedResult: 64, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).Mode() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} + +func TestFileInfoCallbacks_ModTime(t *testing.T) { + t.Parallel() + + ts := time.Now() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult time.Time + }{ + { + scenario: "upstream", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("ModTime"). + Return(ts) + }), + expectedResult: ts, + }, + { + scenario: "overridden", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + ModTimeFunc: func() time.Time { + return ts + }, + }, + expectedResult: ts, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).ModTime() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} + +func TestFileInfoCallbacks_IsDir(t *testing.T) { + t.Parallel() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult bool + }{ + { + scenario: "upstream", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("IsDir"). + Return(true) + }), + expectedResult: true, + }, + { + scenario: "overridden", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + IsDirFunc: func() bool { + return true + }, + }, + expectedResult: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).IsDir() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} + +func TestFileInfoCallbacks_Sys(t *testing.T) { + t.Parallel() + + testCases := []struct { + scenario string + mockFileInfo aferomock.FileInfoMocker + fileInfoCallbacks aferomock.FileInfoCallbacks + expectedResult interface{} + }{ + { + scenario: "upstream", + mockFileInfo: aferomock.MockFileInfo(func(fi *aferomock.FileInfo) { + fi.On("Sys"). + Return(64) + }), + expectedResult: 64, + }, + { + scenario: "overridden", + mockFileInfo: aferomock.NopFileInfo, + fileInfoCallbacks: aferomock.FileInfoCallbacks{ + SysFunc: func() interface{} { + return true + }, + }, + expectedResult: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.scenario, func(t *testing.T) { + t.Parallel() + + actual := aferomock.OverrideFileInfo(tc.mockFileInfo(t), tc.fileInfoCallbacks).Sys() + + assert.Equal(t, tc.expectedResult, actual) + }) + } +} diff --git a/file_test.go b/file_test.go index 7646089..3d587a7 100644 --- a/file_test.go +++ b/file_test.go @@ -54,7 +54,7 @@ func TestFile_Close(t *testing.T) { actual := tc.mockFile(t).Close() - require.Equal(t, actual, tc.expectedError) + require.Equal(t, tc.expectedError, actual) }) } } @@ -712,7 +712,7 @@ func TestFile_Sync(t *testing.T) { actual := tc.mockFile(t).Sync() - require.Equal(t, actual, tc.expectedError) + require.Equal(t, tc.expectedError, actual) }) } } diff --git a/fs_callbacks.go b/fs_callbacks.go index 6f680f3..2548a12 100644 --- a/fs_callbacks.go +++ b/fs_callbacks.go @@ -31,129 +31,129 @@ type FsCallbacks struct { } // Chmod satisfies the afero.Fs interface. -func (fs *FsCallbacks) Chmod(name string, mode fs.FileMode) error { +func (fs FsCallbacks) Chmod(name string, mode fs.FileMode) error { return fs.ChmodFunc(name, mode) } // Chown satisfies the afero.Fs interface. -func (fs *FsCallbacks) Chown(name string, uid int, gid int) error { +func (fs FsCallbacks) Chown(name string, uid int, gid int) error { return fs.ChownFunc(name, uid, gid) } // Chtimes satisfies the afero.Fs interface. -func (fs *FsCallbacks) Chtimes(name string, atime time.Time, mtime time.Time) error { +func (fs FsCallbacks) Chtimes(name string, atime time.Time, mtime time.Time) error { return fs.ChtimesFunc(name, atime, mtime) } // Create satisfies the afero.Fs interface. -func (fs *FsCallbacks) Create(name string) (afero.File, error) { +func (fs FsCallbacks) Create(name string) (afero.File, error) { return fs.CreateFunc(name) } // Mkdir satisfies the afero.Fs interface. -func (fs *FsCallbacks) Mkdir(name string, perm fs.FileMode) error { +func (fs FsCallbacks) Mkdir(name string, perm fs.FileMode) error { return fs.MkdirFunc(name, perm) } // MkdirAll satisfies the afero.Fs interface. -func (fs *FsCallbacks) MkdirAll(path string, perm fs.FileMode) error { +func (fs FsCallbacks) MkdirAll(path string, perm fs.FileMode) error { return fs.MkdirAllFunc(path, perm) } // Name satisfies the afero.Fs interface. -func (fs *FsCallbacks) Name() string { +func (fs FsCallbacks) Name() string { return fs.NameFunc() } // Open satisfies the afero.Fs interface. -func (fs *FsCallbacks) Open(name string) (afero.File, error) { +func (fs FsCallbacks) Open(name string) (afero.File, error) { return fs.OpenFunc(name) } // OpenFile satisfies the afero.Fs interface. -func (fs *FsCallbacks) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) { +func (fs FsCallbacks) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) { return fs.OpenFileFunc(name, flag, perm) } // Remove satisfies the afero.Fs interface. -func (fs *FsCallbacks) Remove(name string) error { +func (fs FsCallbacks) Remove(name string) error { return fs.RemoveFunc(name) } // RemoveAll satisfies the afero.Fs interface. -func (fs *FsCallbacks) RemoveAll(path string) error { +func (fs FsCallbacks) RemoveAll(path string) error { return fs.RemoveAllFunc(path) } // Rename satisfies the afero.Fs interface. -func (fs *FsCallbacks) Rename(oldname string, newname string) error { +func (fs FsCallbacks) Rename(oldname string, newname string) error { return fs.RenameFunc(oldname, newname) } // Stat satisfies the afero.Fs interface. -func (fs *FsCallbacks) Stat(name string) (fs.FileInfo, error) { +func (fs FsCallbacks) Stat(name string) (fs.FileInfo, error) { return fs.StatFunc(name) } // WrapFs wraps a afero.Fs with custom callbacks. // Deprecated: Use OverrideFs instead. -func WrapFs(fs afero.Fs, callbacks WrappedFs) *FsCallbacks { +func WrapFs(fs afero.Fs, callbacks WrappedFs) FsCallbacks { return OverrideFs(fs, callbacks) } -// OverrideFs wraps an afero.Fs with custom callbacks. -func OverrideFs(fs afero.Fs, callbacks FsCallbacks) *FsCallbacks { //nolint: cyclop,dupl - if callbacks.ChmodFunc == nil { - callbacks.ChmodFunc = fs.Chmod +// OverrideFs overrides a afero.Fs with custom callbacks. +func OverrideFs(fs afero.Fs, c FsCallbacks) FsCallbacks { //nolint: cyclop,dupl + if c.ChmodFunc == nil { + c.ChmodFunc = fs.Chmod } - if callbacks.ChownFunc == nil { - callbacks.ChownFunc = fs.Chown + if c.ChownFunc == nil { + c.ChownFunc = fs.Chown } - if callbacks.ChtimesFunc == nil { - callbacks.ChtimesFunc = fs.Chtimes + if c.ChtimesFunc == nil { + c.ChtimesFunc = fs.Chtimes } - if callbacks.CreateFunc == nil { - callbacks.CreateFunc = fs.Create + if c.CreateFunc == nil { + c.CreateFunc = fs.Create } - if callbacks.MkdirFunc == nil { - callbacks.MkdirFunc = fs.Mkdir + if c.MkdirFunc == nil { + c.MkdirFunc = fs.Mkdir } - if callbacks.MkdirAllFunc == nil { - callbacks.MkdirAllFunc = fs.MkdirAll + if c.MkdirAllFunc == nil { + c.MkdirAllFunc = fs.MkdirAll } - if callbacks.NameFunc == nil { - callbacks.NameFunc = fs.Name + if c.NameFunc == nil { + c.NameFunc = fs.Name } - if callbacks.OpenFunc == nil { - callbacks.OpenFunc = fs.Open + if c.OpenFunc == nil { + c.OpenFunc = fs.Open } - if callbacks.OpenFileFunc == nil { - callbacks.OpenFileFunc = fs.OpenFile + if c.OpenFileFunc == nil { + c.OpenFileFunc = fs.OpenFile } - if callbacks.RemoveFunc == nil { - callbacks.RemoveFunc = fs.Remove + if c.RemoveFunc == nil { + c.RemoveFunc = fs.Remove } - if callbacks.RemoveAllFunc == nil { - callbacks.RemoveAllFunc = fs.RemoveAll + if c.RemoveAllFunc == nil { + c.RemoveAllFunc = fs.RemoveAll } - if callbacks.RenameFunc == nil { - callbacks.RenameFunc = fs.Rename + if c.RenameFunc == nil { + c.RenameFunc = fs.Rename } - if callbacks.StatFunc == nil { - callbacks.StatFunc = fs.Stat + if c.StatFunc == nil { + c.StatFunc = fs.Stat } - return &callbacks + return c } diff --git a/fs_callbacks_test.go b/fs_callbacks_test.go index cee5908..05a9782 100644 --- a/fs_callbacks_test.go +++ b/fs_callbacks_test.go @@ -41,7 +41,7 @@ func TestFsCallbacks_Create(t *testing.T) { expectedResult: f, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ CreateFunc: func(string) (afero.File, error) { @@ -51,7 +51,7 @@ func TestFsCallbacks_Create(t *testing.T) { expectedError: "create error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ CreateFunc: func(string) (afero.File, error) { @@ -106,7 +106,7 @@ func TestFsCallbacks_Mkdir(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ MkdirFunc: func(string, os.FileMode) error { @@ -116,7 +116,7 @@ func TestFsCallbacks_Mkdir(t *testing.T) { expectedError: "mkdir error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ MkdirFunc: func(string, os.FileMode) error { @@ -168,7 +168,7 @@ func TestFsCallbacks_MkdirAll(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ MkdirAllFunc: func(string, os.FileMode) error { @@ -178,7 +178,7 @@ func TestFsCallbacks_MkdirAll(t *testing.T) { expectedError: "mkdir all error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ MkdirAllFunc: func(string, os.FileMode) error { @@ -234,7 +234,7 @@ func TestFsCallbacks_Open(t *testing.T) { expectedResult: f, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ OpenFunc: func(string) (afero.File, error) { @@ -244,7 +244,7 @@ func TestFsCallbacks_Open(t *testing.T) { expectedError: "create error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ OpenFunc: func(string) (afero.File, error) { @@ -303,7 +303,7 @@ func TestFsCallbacks_OpenFile(t *testing.T) { expectedResult: f, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ OpenFileFunc: func(string, int, os.FileMode) (afero.File, error) { @@ -313,7 +313,7 @@ func TestFsCallbacks_OpenFile(t *testing.T) { expectedError: "open file error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ OpenFileFunc: func(string, int, os.FileMode) (afero.File, error) { @@ -368,7 +368,7 @@ func TestFsCallbacks_Remove(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RemoveFunc: func(string) error { @@ -378,7 +378,7 @@ func TestFsCallbacks_Remove(t *testing.T) { expectedError: "remove error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RemoveFunc: func(string) error { @@ -430,7 +430,7 @@ func TestFsCallbacks_RemoveAll(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RemoveAllFunc: func(string) error { @@ -440,7 +440,7 @@ func TestFsCallbacks_RemoveAll(t *testing.T) { expectedError: "remove all error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RemoveAllFunc: func(string) error { @@ -492,7 +492,7 @@ func TestFsCallbacks_Rename(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RenameFunc: func(string, string) error { @@ -502,7 +502,7 @@ func TestFsCallbacks_Rename(t *testing.T) { expectedError: "rename error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ RenameFunc: func(string, string) error { @@ -558,7 +558,7 @@ func TestFsCallbacks_Stat(t *testing.T) { expectedResult: fi, }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ StatFunc: func(string) (os.FileInfo, error) { @@ -568,7 +568,7 @@ func TestFsCallbacks_Stat(t *testing.T) { expectedError: "stat error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ StatFunc: func(string) (os.FileInfo, error) { @@ -611,13 +611,13 @@ func TestFsCallbacks_Name(t *testing.T) { expectedResult: "aferomock.Fs", }, { - scenario: "wrapped", + scenario: "overridden", fsCallbacks: aferomock.FsCallbacks{ NameFunc: func() string { - return "wrapped" + return "overridden" }, }, - expectedResult: "wrapped", + expectedResult: "overridden", }, } @@ -659,7 +659,7 @@ func TestFsCallbacks_Chmod(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChmodFunc: func(string, os.FileMode) error { @@ -669,7 +669,7 @@ func TestFsCallbacks_Chmod(t *testing.T) { expectedError: "chmod error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChmodFunc: func(string, os.FileMode) error { @@ -721,7 +721,7 @@ func TestFsCallbacks_Chown(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChownFunc: func(string, int, int) error { @@ -731,7 +731,7 @@ func TestFsCallbacks_Chown(t *testing.T) { expectedError: "chown error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChownFunc: func(string, int, int) error { @@ -785,7 +785,7 @@ func TestFsCallbacks_Chtimes(t *testing.T) { }), }, { - scenario: "wrapped - error", + scenario: "overridden - error", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChtimesFunc: func(string, time.Time, time.Time) error { @@ -795,7 +795,7 @@ func TestFsCallbacks_Chtimes(t *testing.T) { expectedError: "chtimes error", }, { - scenario: "wrapped - success", + scenario: "overridden - success", mockFs: aferomock.NopFs, fsCallbacks: aferomock.FsCallbacks{ ChtimesFunc: func(string, time.Time, time.Time) error { @@ -828,5 +828,5 @@ func TestWrapFs(t *testing.T) { fs := aferomock.WrapFs(aferomock.NopFs(t), aferomock.WrappedFs{}) assert.NotNil(t, fs) - assert.IsType(t, &aferomock.FsCallbacks{}, fs) + assert.IsType(t, aferomock.FsCallbacks{}, fs) }