-
Notifications
You must be signed in to change notification settings - Fork 2
/
testutil.go
95 lines (83 loc) · 2.4 KB
/
testutil.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
package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/rules_go/go/tools/bazel"
)
func MustReadAndPrepareTestFiles(t *testing.T, files []testtools.FileSpec) (tmpDir string, filenames []string, clean func()) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
for i := range files {
if files[i].Content == "" {
files[i].Content = MustReadTestFile(t, cwd, files[i].Path)
}
}
return MustPrepareTestFiles(t, files)
}
func MustPrepareTestFiles(t *testing.T, files []testtools.FileSpec) (tmpDir string, filenames []string, clean func()) {
tmpDir, err := bazel.NewTmpDir("")
if err != nil {
t.Fatal(err)
}
cleanup := func() {
os.RemoveAll(tmpDir)
}
filenames = MustWriteTestFiles(t, tmpDir, files)
return tmpDir, filenames, cleanup
}
func MustWriteTestFiles(t *testing.T, tmpDir string, files []testtools.FileSpec) []string {
var filenames []string
for _, file := range files {
abs := filepath.Join(tmpDir, file.Path)
dir := filepath.Dir(abs)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
t.Fatal(err)
}
if !file.NotExist {
if err := ioutil.WriteFile(abs, []byte(file.Content), os.ModePerm); err != nil {
t.Fatal(err)
}
}
filenames = append(filenames, abs)
}
return filenames
}
func MustReadTestFile(t *testing.T, dir string, filename string) string {
data, err := ioutil.ReadFile(filepath.Join(dir, filename))
if err != nil {
ListFiles(t, dir)
t.Fatal("reading", filename, ":", err)
}
return string(data)
}
// EqualError reports whether errors a and b are considered equal.
// They're equal if both are nil, or both are not nil and a.Error() == b.Error().
func EqualError(a, b error) bool {
return a == nil && b == nil || a != nil && b != nil && a.Error() == b.Error()
}
// ExpectError asserts that the errors are equal. Return value is true
// if the "want" argument is non-nil.
func ExpectError(t *testing.T, want, got error) bool {
if !EqualError(want, got) {
t.Fatal("errors: want:", want, "got:", got)
}
return want != nil
}
// ListFiles is a convenience debugging function to log the files under a given dir.
func ListFiles(t *testing.T, dir string) {
t.Log("Listing files under:", dir)
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
t.Log(path)
return nil
}); err != nil {
t.Fatal(err)
}
}