-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
150 lines (128 loc) · 2.79 KB
/
file.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Package lfilemock provides some mock file objects. This is not meant to be
// exhaustive, it is used to cover the cases needed to test luce. Over time it
// will expand.
package lfilemock
import (
"bytes"
"io/fs"
"os"
"time"
"github.com/adamcolton/luce/lerr"
)
// File mock allows for *os.File to be simulated including various errors.
type File struct {
FileName string
*bytes.Buffer
Dir bool
DirEntries []os.DirEntry
os.FileInfo
FileSize int64
os.FileMode
Err error
}
const (
ErrNewType = lerr.Str("lfilemock.New contents must be string or []byte")
ErrParseType = lerr.Str("lfilemock.Parse values must be string, []byte or map[string]any")
)
// New creates a file using either a string or []byte.
func New(name string, contents any) *File {
f := (&ByteFile{
Name: name,
})
switch c := contents.(type) {
case []byte:
f.Data = c
case string:
f.Data = []byte(c)
default:
panic(ErrNewType)
}
return f.File()
}
// Close returns f.Err.
func (f *File) Close() error {
return f.Err
}
// Read returns f.Err is that is set, otherwise it wraps f.Buffer.Read.
func (f *File) Read(b []byte) (int, error) {
if f.Err != nil {
return 0, f.Err
}
return f.Buffer.Read(b)
}
// ReadDir returns DirEntries and Err.
func (f *File) ReadDir(n int) ([]os.DirEntry, error) {
return f.DirEntries, f.Err
}
// Name returns Filename
func (f *File) Name() string {
return f.FileName
}
// Stat creates an instance of lfilemock.FileInfo derived from the instance of
// File.
func (f *File) Stat() (os.FileInfo, error) {
fi := &FileInfo{
FileName: f.FileName,
FileSize: f.FileSize,
FileMode: f.FileMode,
Mod: time.Time{},
Dir: f.Dir,
}
return fi, f.Err
}
// DirEntry fulfills os.DirEntry.
type DirEntry struct {
EntryName string
Dir bool
fs.FileMode
Err error
fs.FileInfo
}
// Name returns EntryName
func (de *DirEntry) Name() string {
return de.EntryName
}
// IsDir returns Dir
func (de *DirEntry) IsDir() bool {
return de.Dir
}
// Type returns FileMode
func (de *DirEntry) Type() fs.FileMode {
return de.FileMode
}
// Info returns FileInfo and Err
func (de *DirEntry) Info() (fs.FileInfo, error) {
return de.FileInfo, de.Err
}
// FileInfo fulfills os.FileInfo
type FileInfo struct {
FileName string
FileSize int64
os.FileMode
Mod time.Time
Dir bool
}
// Name returns Filename
func (fi *FileInfo) Name() string {
return fi.FileName
}
// Size returns Filesize
func (fi *FileInfo) Size() int64 {
return fi.FileSize
}
// Mode returns FileMode
func (fi *FileInfo) Mode() os.FileMode {
return fi.FileMode
}
// ModTime returns Mod
func (fi *FileInfo) ModTime() time.Time {
return fi.Mod
}
// IsDir returns Dir
func (fi *FileInfo) IsDir() bool {
return fi.Dir
}
// Sys exists to fulfill os.FileInfo but always returns nil.
func (fi *FileInfo) Sys() any {
return nil
}