-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfs.go
More file actions
49 lines (39 loc) · 1.04 KB
/
fs.go
File metadata and controls
49 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package sys
import (
"fmt"
"io/ioutil"
)
// FS is the interface to a file system
type FS interface {
// ReadAll gets the contents of filename, or an error if the file didn't exist or there was an error reading it
ReadFile(filename string) ([]byte, error)
}
// RealFS returns an FS object that interacts with the real local filesystem
func RealFS() FS {
return &realFS{}
}
type realFS struct{}
func (r *realFS) ReadFile(name string) ([]byte, error) {
return ioutil.ReadFile(name)
}
// FakeFileNotFound is the error returned by FakeFS when a requested file isn't found
type FakeFileNotFound struct {
Filename string
}
// Error is the error interface implementation
func (f FakeFileNotFound) Error() string {
return fmt.Sprintf("Fake file %s not found", f.Filename)
}
type FakeFS struct {
Files map[string][]byte
}
func NewFakeFS() *FakeFS {
return &FakeFS{Files: make(map[string][]byte)}
}
func (f *FakeFS) ReadFile(name string) ([]byte, error) {
b, ok := f.Files[name]
if !ok {
return nil, FakeFileNotFound{Filename: name}
}
return b, nil
}