-
Notifications
You must be signed in to change notification settings - Fork 16
/
asmfmt_test.go
144 lines (125 loc) · 2.88 KB
/
asmfmt_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
144
package asmfmt
import (
"bytes"
"flag"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
var update = flag.Bool("update", false, "update .golden files")
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func runTest(t *testing.T, in, out string) {
f, err := os.Open(in)
if err != nil {
t.Error(err)
return
}
defer f.Close()
got, err := Format(f)
if err != nil {
t.Error(in, "-", err)
return
}
expected, err := ioutil.ReadFile(out)
if err != nil && !*update {
t.Error(out, "-", err)
return
}
// Convert expected file to LF in case someone did it for us.
expected = []byte(strings.Replace(string(expected), "\r\n", "\n", -1))
if !bytes.Equal(got, expected) {
if *update {
if in != out {
if err := ioutil.WriteFile(out, got, 0666); err != nil {
t.Error(err)
}
return
}
// in == out: don't accidentally destroy input
t.Errorf("WARNING: -update did not rewrite input file %s", in)
}
t.Errorf("(gofmt %s) != %s (see %s.asmfmt)", in, out, in)
d, err := diff(expected, got)
if err == nil {
t.Errorf("%s", d)
}
if err := ioutil.WriteFile(in+".asmfmt", got, 0666); err != nil {
t.Error(err)
}
}
}
// TestRewrite processes testdata/*.input files and compares them to the
// corresponding testdata/*.golden files. The gofmt flags used to process
// a file must be provided via a comment of the form
//
// //gofmt flags
//
// in the processed file within the first 20 lines, if any.
func TestRewrite(t *testing.T) {
// determine input files
match, err := filepath.Glob("testdata/*.in")
if err != nil {
t.Fatal(err)
}
for _, in := range match {
out := in // for files where input and output are identical
if strings.HasSuffix(in, ".in") {
out = in[:len(in)-len(".in")] + ".golden"
}
runTest(t, in, out)
if in != out {
// Check idempotence.
runTest(t, out, out)
}
}
}
func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "asmfmt")
if err != nil {
return
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "asmfmt")
if err != nil {
return
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write(b1)
f2.Write(b2)
data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
if len(data) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
}
return
}
// Go files must fail.
func TestGoFile(t *testing.T) {
input := `package main
func main() {
}
`
_, err := Format(bytes.NewBuffer([]byte(input)))
if err == nil {
t.Error("go file not detected")
return
}
}
// Files containg zero byte values must fail.
func TestZeroByteFile(t *testing.T) {
var input = []byte{13, 13, 10, 0, 0, 0, 13}
_, err := Format(bytes.NewBuffer(input))
if err == nil {
t.Fatal("file containing zero (0) byte values not rejected")
return
}
}