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