]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/mock" | |
5 | "os" | |
6 | ) | |
7 | ||
8 | var FS FileStore | |
9 | ||
10 | type FileStore interface { | |
11 | Mkdir(name string, perm os.FileMode) error | |
12 | Stat(name string) (os.FileInfo, error) | |
13 | Open(name string) (*os.File, error) | |
14 | Symlink(oldname, newname string) error | |
15 | Remove(name string) error | |
16 | Create(name string) (*os.File, 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 | type MockFileStore struct { | |
49 | mock.Mock | |
50 | } | |
51 | ||
52 | func (mfs *MockFileStore) Mkdir(name string, perm os.FileMode) error { | |
53 | args := mfs.Called(name, perm) | |
54 | return args.Error(0) | |
55 | } | |
56 | ||
57 | func (mfs *MockFileStore) Stat(name string) (os.FileInfo, error) { | |
58 | args := mfs.Called(name) | |
59 | if args.Get(0) == nil { | |
60 | return nil, args.Error(1) | |
61 | ||
62 | } | |
63 | return args.Get(0).(os.FileInfo), args.Error(1) | |
64 | } | |
65 | ||
66 | func (mfs *MockFileStore) Open(name string) (*os.File, error) { | |
67 | args := mfs.Called(name) | |
68 | return args.Get(0).(*os.File), args.Error(1) | |
69 | } | |
70 | ||
71 | func (mfs *MockFileStore) Symlink(oldname, newname string) error { | |
72 | args := mfs.Called(oldname, newname) | |
73 | return args.Error(0) | |
74 | } | |
75 | ||
76 | func (mfs *MockFileStore) Remove(name string) error { | |
77 | args := mfs.Called(name) | |
78 | return args.Error(0) | |
79 | } | |
80 | ||
81 | func (mfs *MockFileStore) Create(name string) (*os.File, error) { | |
82 | args := mfs.Called(name) | |
83 | return args.Get(0).(*os.File), args.Error(1) | |
84 | } |