]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/mock" | |
5 | "io/fs" | |
6 | "os" | |
7 | ) | |
8 | ||
9 | var FS FileStore | |
10 | ||
11 | type FileStore interface { | |
12 | Mkdir(name string, perm os.FileMode) error | |
13 | Stat(name string) (os.FileInfo, error) | |
14 | Open(name string) (*os.File, error) | |
15 | Symlink(oldname, newname string) error | |
16 | Remove(name string) error | |
17 | Create(name string) (*os.File, error) | |
18 | WriteFile(name string, data []byte, perm fs.FileMode) error | |
19 | // TODO: implement these | |
20 | // Rename(oldpath string, newpath string) error | |
21 | // RemoveAll(path string) error | |
22 | } | |
23 | ||
24 | type OSFileStore struct{} | |
25 | ||
26 | func (fs *OSFileStore) Mkdir(name string, perm os.FileMode) error { | |
27 | return os.Mkdir(name, perm) | |
28 | } | |
29 | ||
30 | func (fs *OSFileStore) Stat(name string) (os.FileInfo, error) { | |
31 | return os.Stat(name) | |
32 | } | |
33 | ||
34 | func (fs *OSFileStore) Open(name string) (*os.File, error) { | |
35 | return os.Open(name) | |
36 | } | |
37 | ||
38 | func (fs *OSFileStore) Symlink(oldname, newname string) error { | |
39 | return os.Symlink(oldname, newname) | |
40 | } | |
41 | ||
42 | func (fs *OSFileStore) Remove(name string) error { | |
43 | return os.Remove(name) | |
44 | } | |
45 | ||
46 | func (fs *OSFileStore) Create(name string) (*os.File, error) { | |
47 | return os.Create(name) | |
48 | } | |
49 | ||
50 | func (fs *OSFileStore) WriteFile(name string, data []byte, perm fs.FileMode) error { | |
51 | return os.WriteFile(name, data, perm) | |
52 | } | |
53 | ||
54 | type MockFileStore struct { | |
55 | mock.Mock | |
56 | } | |
57 | ||
58 | func (mfs *MockFileStore) Mkdir(name string, perm os.FileMode) error { | |
59 | args := mfs.Called(name, perm) | |
60 | return args.Error(0) | |
61 | } | |
62 | ||
63 | func (mfs *MockFileStore) Stat(name string) (os.FileInfo, error) { | |
64 | args := mfs.Called(name) | |
65 | if args.Get(0) == nil { | |
66 | return nil, args.Error(1) | |
67 | ||
68 | } | |
69 | return args.Get(0).(os.FileInfo), args.Error(1) | |
70 | } | |
71 | ||
72 | func (mfs *MockFileStore) Open(name string) (*os.File, error) { | |
73 | args := mfs.Called(name) | |
74 | return args.Get(0).(*os.File), args.Error(1) | |
75 | } | |
76 | ||
77 | func (mfs *MockFileStore) Symlink(oldname, newname string) error { | |
78 | args := mfs.Called(oldname, newname) | |
79 | return args.Error(0) | |
80 | } | |
81 | ||
82 | func (mfs *MockFileStore) Remove(name string) error { | |
83 | args := mfs.Called(name) | |
84 | return args.Error(0) | |
85 | } | |
86 | ||
87 | func (mfs *MockFileStore) Create(name string) (*os.File, error) { | |
88 | args := mfs.Called(name) | |
89 | return args.Get(0).(*os.File), args.Error(1) | |
90 | } | |
91 | ||
92 | func (mfs *MockFileStore) WriteFile(name string, data []byte, perm fs.FileMode) error { | |
93 | args := mfs.Called(name, data, perm) | |
94 | return args.Error(0) | |
95 | } |