forked from openshift/origin
/
fs.go
136 lines (111 loc) · 2.99 KB
/
fs.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
package test
import (
"bytes"
"io"
"os"
"sync"
)
// FakeFileSystem provides a fake filesystem structure for testing
type FakeFileSystem struct {
ChmodFile []string
ChmodMode os.FileMode
ChmodError map[string]error
RenameFrom string
RenameTo string
RenameError error
MkdirAllDir []string
MkdirAllError error
MkdirDir string
MkdirError error
ExistsFile []string
ExistsResult map[string]bool
CopySource string
CopyDest string
CopyError error
RemoveDirName string
RemoveDirError error
WorkingDirCalled bool
WorkingDirResult string
WorkingDirError error
OpenFile string
OpenFileResult *FakeReadCloser
OpenContent string
OpenError error
OpenCloseError error
WriteFileName string
WriteFileError error
WriteFileContent string
mutex sync.Mutex
}
// FakeReadCloser provider a fake ReadCloser
type FakeReadCloser struct {
*bytes.Buffer
CloseCalled bool
CloseError error
}
// Close closes the fake ReadCloser
func (f *FakeReadCloser) Close() error {
f.CloseCalled = true
return f.CloseError
}
// Chmod manipulates permissions on the fake filesystem
func (f *FakeFileSystem) Chmod(file string, mode os.FileMode) error {
f.mutex.Lock()
defer f.mutex.Unlock()
f.ChmodFile = append(f.ChmodFile, file)
f.ChmodMode = mode
return f.ChmodError[file]
}
// Rename renames files on the fake filesystem
func (f *FakeFileSystem) Rename(from, to string) error {
f.RenameFrom = from
f.RenameTo = to
return f.RenameError
}
// MkdirAll creates a new directories on the fake filesystem
func (f *FakeFileSystem) MkdirAll(dirname string) error {
f.MkdirAllDir = append(f.MkdirAllDir, dirname)
return f.MkdirAllError
}
// Mkdir creates a new directory on the fake filesystem
func (f *FakeFileSystem) Mkdir(dirname string) error {
f.MkdirDir = dirname
return f.MkdirError
}
// Exists checks if the file exists in fake filesystem
func (f *FakeFileSystem) Exists(file string) bool {
f.ExistsFile = append(f.ExistsFile, file)
return f.ExistsResult[file]
}
// Copy copies files on the fake filesystem
func (f *FakeFileSystem) Copy(sourcePath, targetPath string) error {
f.CopySource = sourcePath
f.CopyDest = targetPath
return f.CopyError
}
// RemoveDirectory removes a directory in the fake filesystem
func (f *FakeFileSystem) RemoveDirectory(dir string) error {
f.RemoveDirName = dir
return f.RemoveDirError
}
// CreateWorkingDirectory creates a fake working directory
func (f *FakeFileSystem) CreateWorkingDirectory() (string, error) {
f.WorkingDirCalled = true
return f.WorkingDirResult, f.WorkingDirError
}
// Open opens a file
func (f *FakeFileSystem) Open(file string) (io.ReadCloser, error) {
f.OpenFile = file
buf := bytes.NewBufferString(f.OpenContent)
f.OpenFileResult = &FakeReadCloser{
Buffer: buf,
CloseError: f.OpenCloseError,
}
return f.OpenFileResult, f.OpenError
}
// Write writes a file
func (f *FakeFileSystem) WriteFile(file string, data []byte) error {
f.WriteFileName = file
f.WriteFileContent = string(data)
return f.WriteFileError
}