-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
55 lines (44 loc) · 1.24 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
package testutils
import (
"os"
"testing"
"github.com/evg4b/fisherman/pkg/guards"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/util"
)
// FsFromMap creates billy.Filesystem in memory from map.
// Where key is a filename and value is file context.
func FsFromMap(t *testing.T, files map[string]string) billy.Filesystem {
t.Helper()
fs := memfs.New()
for path, content := range files {
err := util.WriteFile(fs, path, []byte(content), os.ModePerm)
if err != nil {
t.Error(err)
}
}
return fs
}
// FsFromMap creates billy.Filesystem in memory from slice.
// Each element of slice is filename, content always "test".
func FsFromSlice(t *testing.T, files []string) billy.Filesystem {
t.Helper()
fs := memfs.New()
for _, path := range files {
err := util.WriteFile(fs, path, []byte("test"), os.ModePerm)
if err != nil {
t.Error(err)
}
}
return fs
}
// MakeFiles creates in billy.Filesystem files from map.
// Where key is a filename and value is file context.
func MakeFiles(t *testing.T, fs billy.Basic, files map[string]string) {
t.Helper()
for filename, content := range files {
err := util.WriteFile(fs, filename, []byte(content), os.ModePerm)
guards.NoError(err)
}
}