-
Notifications
You must be signed in to change notification settings - Fork 6
/
embeddedFileSystemMock.go
54 lines (46 loc) · 1.3 KB
/
embeddedFileSystemMock.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
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package embedded
import (
"io/fs"
"os"
"path/filepath"
"strings"
)
type MockDirEntry struct {
os.DirEntry
DirName string
}
func (mockDirEntry MockDirEntry) Name() string {
return mockDirEntry.DirName
}
type MockReadOnlyFileSystem struct {
files map[string]string
}
func NewMockReadOnlyFileSystem() *MockReadOnlyFileSystem {
fs := MockReadOnlyFileSystem{
files: make(map[string]string, 0),
}
return &fs
}
// WriteFile - This function is not on the ReadOnlyFileSystem interface, but does allow unit tests
// to add data files to the mock file system, so the code under test can read it back.
func (fs *MockReadOnlyFileSystem) WriteFile(filePath string, content string) {
fs.files[filePath] = content
}
func (fs *MockReadOnlyFileSystem) ReadFile(filePath string) ([]byte, error) {
content := fs.files[filePath]
return []byte(content), nil
}
func (fs *MockReadOnlyFileSystem) ReadDir(directoryPath string) ([]fs.DirEntry, error) {
dirEntries := make([]os.DirEntry, 0)
for key := range fs.files {
if strings.HasPrefix(key, directoryPath) && key != directoryPath {
dirEntries = append(dirEntries, MockDirEntry{DirName: filepath.Base(key)})
}
}
return dirEntries, nil
}