-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
61 lines (51 loc) · 1.36 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
package testutil
import (
"io/fs"
"golang.org/x/tools/go/analysis/analysistest"
)
// WriteFiles wrapper of analysistest.WriteFiles.
//
// WriteFiles is a helper function that creates a temporary directory
// and populates it with a GOPATH-style project using filemap (which
// maps file names to contents).
//
// On success it returns the name of the directory.
// The directory will be deleted by t.Cleanup.
func WriteFiles(t TestingT, filemap map[string]string) string {
t.Helper()
dir, clean, err := analysistest.WriteFiles(filemap)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
t.Cleanup(clean)
return dir
}
// WriteFiles wrapper of analysistest.WriteFiles.
//
// WriteFiles is a helper function that creates a temporary directory
// and populates it with a GOPATH-style project using fs.FS.
//
// On success it returns the name of the directory.
// The directory will be deleted by t.Cleanup.
func WriteFilesFS(t TestingT, fsys fs.FS) string {
t.Helper()
filemap := make(map[string]string)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
data, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
filemap[path] = string(data)
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return WriteFiles(t, filemap)
}