github.com/slukits/gounit@v0.8.3/pkg/tfs/fx_fs_test.go (about) 1 // Copyright (c) 2022 Stephan Lukits. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package tfs 6 7 import ( 8 "io" 9 "io/fs" 10 "os" 11 fp "path/filepath" 12 ) 13 14 type FSfx struct { 15 *FS 16 mock *fsTMocker 17 } 18 19 func NewFX(t Tester) *FSfx { 20 return &FSfx{FS: t.FS()} 21 } 22 23 // Mock returns a file system tools mocker to mockup file system 24 // functions used by Dir and TmpDir instance which can fail. Note all 25 // created Dir and TmpDir instances from this FS instance will use the 26 // mocked filesystem tools including the ones which were created before 27 // the mocking; use [FSTMocker.Reset] to go back to the defaults if 28 // necessary. 29 func (fx *FSfx) Mock() *fsTMocker { 30 if fx.mock == nil { 31 fx.tools = defaultFSTools.copy() 32 fx.mock = &fsTMocker{fs: fx.FS} 33 } 34 return fx.mock 35 } 36 37 // A fsTMocker allows to set potentially failing file system operations 38 // which are used by Dir and TmpDir. 39 type fsTMocker struct{ fs *FS } 40 41 // Stat defaults to and has the semantics of os.Stat 42 func (m *fsTMocker) Stat(f func(string) (fs.FileInfo, error)) { 43 m.fs.tools.Stat = f 44 } 45 46 // Getwd defaults to and has the semantics of os.Chdir 47 func (m *fsTMocker) Getwd(f func() (string, error)) { 48 m.fs.tools.Getwd = f 49 } 50 51 // Chdir defaults to and has the semantics of os.Chdir 52 func (m *fsTMocker) Chdir(f func(string) error) { 53 m.fs.tools.Chdir = f 54 } 55 56 // Mkdir defaults to and has the semantics of os.Mkdir 57 func (m *fsTMocker) Mkdir(f func(string, fs.FileMode) error) { 58 m.fs.tools.Mkdir = f 59 } 60 61 // MkdirAll defaults to and has the semantics of os.MkdirAll 62 func (m *fsTMocker) MkdirAll(f func(string, fs.FileMode) error) { 63 m.fs.tools.MkdirAll = f 64 } 65 66 // Remove defaults to and has the semantics of os.Remove 67 func (m *fsTMocker) Remove(f func(string) error) { 68 m.fs.tools.Remove = f 69 } 70 71 // RemoveAll defaults to and has the semantics of os.RemoveAll 72 func (m *fsTMocker) RemoveAll(f func(string) error) { 73 m.fs.tools.RemoveAll = f 74 } 75 76 // Symlink defaults to and has the semantics of os.Symlink 77 func (m *fsTMocker) Symlink(f func(string, string) error) { 78 m.fs.tools.Symlink = f 79 } 80 81 // Open defaults to and has the semantics of os.Open. 82 func (m *fsTMocker) Open(f func(string) (*os.File, error)) { 83 m.fs.tools.Open = f 84 } 85 86 // Create defaults to and has the semantics of os.Create. 87 func (m *fsTMocker) Create(f func(string) (*os.File, error)) { 88 m.fs.tools.Create = f 89 } 90 91 // ReadDir defaults to and has the semantics of os.ReadDir. 92 func (m *fsTMocker) ReadDir(f func(string) ([]fs.DirEntry, error)) { 93 m.fs.tools.ReadDir = f 94 } 95 96 // ReadFile defaults to and has the semantics of os.ReadFile. 97 func (m *fsTMocker) ReadFile(f func(string) ([]byte, error)) { 98 m.fs.tools.ReadFile = f 99 } 100 101 // WriteFile defaults to and has the semantics of os.WriteFile. 102 func (m *fsTMocker) WriteFile(f func(string, []byte, fs.FileMode) error) { 103 m.fs.tools.WriteFile = f 104 } 105 106 // Chmod defaults to and has the semantics of os.Chmod. 107 func (m *fsTMocker) Chmod(f func(string, fs.FileMode) error) { 108 m.fs.tools.Chmod = f 109 } 110 111 // Copy defaults to and has the semantics of io.Copy 112 func (m *fsTMocker) Copy(f func(io.Writer, io.Reader) (int64, error)) { 113 m.fs.tools.Copy = f 114 } 115 116 // Walk defaults to and has the semantics of filepath.Walk. 117 func (m *fsTMocker) Walk(f func(string, fp.WalkFunc) error) { 118 m.fs.tools.Walk = f 119 } 120 121 // Caller default to and has the semantics of runtime.Caller 122 func (m *fsTMocker) Caller(f func(int) (uintptr, string, int, bool)) { 123 m.fs.tools.Caller = f 124 } 125 126 // Reset resets mocked functions to their default. 127 func (m *fsTMocker) Reset() { m.fs.tools = defaultFSTools.copy() }