1
16
17
21
22 package filesystem
23
24 import (
25 "os"
26 "path/filepath"
27 "time"
28
29 "github.com/spf13/afero"
30 )
31
32
33 type fakeFs struct {
34 a afero.Afero
35 }
36
37 var _ Filesystem = (*fakeFs)(nil)
38
39
40 func NewFakeFs() Filesystem {
41 return &fakeFs{a: afero.Afero{Fs: afero.NewMemMapFs()}}
42 }
43
44
45 func (fs *fakeFs) Stat(name string) (os.FileInfo, error) {
46 return fs.a.Fs.Stat(name)
47 }
48
49
50 func (fs *fakeFs) Create(name string) (File, error) {
51 file, err := fs.a.Fs.Create(name)
52 if err != nil {
53 return nil, err
54 }
55 return &fakeFile{file}, nil
56 }
57
58
59 func (fs *fakeFs) Open(name string) (File, error) {
60 file, err := fs.a.Fs.Open(name)
61 if err != nil {
62 return nil, err
63 }
64 return &fakeFile{file}, nil
65 }
66
67
68 func (fs *fakeFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
69 file, err := fs.a.Fs.OpenFile(name, flag, perm)
70 if err != nil {
71 return nil, err
72 }
73 return &fakeFile{file}, nil
74 }
75
76
77 func (fs *fakeFs) Rename(oldpath, newpath string) error {
78 return fs.a.Fs.Rename(oldpath, newpath)
79 }
80
81
82 func (fs *fakeFs) MkdirAll(path string, perm os.FileMode) error {
83 return fs.a.Fs.MkdirAll(path, perm)
84 }
85
86
87 func (fs *fakeFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
88 return fs.a.Fs.Chtimes(name, atime, mtime)
89 }
90
91
92 func (fs *fakeFs) ReadFile(filename string) ([]byte, error) {
93 return fs.a.ReadFile(filename)
94 }
95
96
97 func (fs *fakeFs) WriteFile(filename string, data []byte, perm os.FileMode) error {
98 return fs.a.WriteFile(filename, data, perm)
99 }
100
101
102 func (fs *fakeFs) TempDir(dir, prefix string) (string, error) {
103 return fs.a.TempDir(dir, prefix)
104 }
105
106
107 func (fs *fakeFs) TempFile(dir, prefix string) (File, error) {
108 file, err := fs.a.TempFile(dir, prefix)
109 if err != nil {
110 return nil, err
111 }
112 return &fakeFile{file}, nil
113 }
114
115
116 func (fs *fakeFs) ReadDir(dirname string) ([]os.FileInfo, error) {
117 return fs.a.ReadDir(dirname)
118 }
119
120
121 func (fs *fakeFs) Walk(root string, walkFn filepath.WalkFunc) error {
122 return fs.a.Walk(root, walkFn)
123 }
124
125
126 func (fs *fakeFs) RemoveAll(path string) error {
127 return fs.a.RemoveAll(path)
128 }
129
130 func (fs *fakeFs) Getwd() (dir string, err error) {
131 return "/", nil
132 }
133
134
135 func (fs *fakeFs) Remove(name string) error {
136 return fs.a.Remove(name)
137 }
138
139
140 func (fs *fakeFs) Chmod(name string, mode os.FileMode) error {
141 return fs.a.Chmod(name, mode)
142 }
143
144
145 type fakeFile struct {
146 file afero.File
147 }
148
149 var _ File = (*fakeFile)(nil)
150
151
152 func (file *fakeFile) Name() string {
153 return file.file.Name()
154 }
155
156
157 func (file *fakeFile) Write(b []byte) (n int, err error) {
158 return file.file.Write(b)
159 }
160
161
162 func (file *fakeFile) WriteString(s string) (n int, err error) {
163 return file.file.WriteString(s)
164 }
165
166
167 func (file *fakeFile) Sync() error {
168 return file.file.Sync()
169 }
170
171
172 func (file *fakeFile) Close() error {
173 return file.file.Close()
174 }
175
176 func (file *fakeFile) Readdir(n int) ([]os.FileInfo, error) {
177 return file.file.Readdir(n)
178 }
179
180 func (file *fakeFile) Read(b []byte) (n int, err error) {
181 return file.file.Read(b)
182 }
183
View as plain text