-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfmt_test.go
143 lines (123 loc) · 3.12 KB
/
fmt_test.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
137
138
139
140
141
142
143
package main
import (
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestGofmt tests that all files are formatted.
func TestGofmt(t *testing.T) {
root, fileMap := copyTree(t)
gofmt := exec.Command("gofmt", "-w", ".")
gofmt.Dir = root
gofmt.Stdout, gofmt.Stderr = os.Stdout, os.Stderr
if err := gofmt.Run(); err != nil {
t.Fatalf("gofmt failed: %v", err)
}
// Diff the trees.
if diffFiles(t, fileMap) {
t.Errorf("Files are not gofmt clean. Please run gofmt.")
}
}
// TestGenerated tests that all generated files are up-to-date.
func TestGenerated(t *testing.T) {
root, fileMap := copyTree(t)
// Build bitstringer
build := exec.Command("go", "build")
build.Dir = filepath.Join(root, "cmd/bitstringer")
build.Stdout, build.Stderr = os.Stdout, os.Stderr
if err := build.Run(); err != nil {
t.Fatalf("go build failed: %v", err)
}
gen := exec.Command("go", "generate", "./...")
gen.Dir = root
gen.Stdout, gen.Stderr = os.Stdout, os.Stderr
if err := gen.Run(); err != nil {
t.Fatalf("go generate failed: %v", err)
}
// Diff the trees.
if diffFiles(t, fileMap) {
t.Errorf("Please run go generate.")
}
}
func copyTree(t *testing.T) (string, map[string]string) {
src, err := os.Getwd()
if err != nil {
t.Fatalf("getting working directory: %v", err)
}
dst := t.TempDir()
// Ensure src ends with "/"
src = fmt.Sprintf("%s%c", filepath.Clean(src), filepath.Separator)
fileMap := make(map[string]string)
err = filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if !strings.HasPrefix(path, src) {
panic(fmt.Sprintf("WalkDir path %q does not start with source path %q", path, src))
}
rel := path[len(src):]
if d.Name() == ".git" {
return filepath.SkipDir
}
if d.IsDir() {
if rel == "" {
// This is the root of the tree, so
// the destination already exists.
return nil
}
return os.Mkdir(filepath.Join(dst, rel), 0777)
}
// Only copy .go and related files.
if n := d.Name(); !(filepath.Ext(n) == ".go" || n == "go.mod" || n == "go.sum") {
return nil
}
// Copy file.
fileMap[path] = filepath.Join(dst, rel)
return copyFile(path, filepath.Join(dst, rel))
})
if err != nil {
t.Fatalf("error copying source tree: %v", err)
}
t.Logf("copied source tree to %s", dst)
return dst, fileMap
}
func copyFile(src, dst string) error {
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func diffFiles(t *testing.T, fileMap map[string]string) bool {
diffs := 0
for orig, new := range fileMap {
// --strip-trailing-cr is important on Windows where
// git and gofmt may disagree on newline conventions.
diff := exec.Command("diff", "-u", "--strip-trailing-cr", orig, new)
diff.Stdout = os.Stdout
diff.Stderr = os.Stderr
if err := diff.Run(); err != nil {
switch err := err.(type) {
case *exec.ExitError:
if err.ExitCode() == 1 {
diffs++
continue
}
}
t.Errorf("diff failed: %v", err)
}
}
return diffs != 0
}