/
fmt_test.go
93 lines (77 loc) · 1.75 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
// chris 080615
package main
import (
"bytes"
"fmt"
"io"
"path"
"strings"
"testing"
"os/exec"
"github.com/aryann/difflib"
)
// Make sure that generated code isn't altered by running gofmt.
func testFmt(t *testing.T, srcname string, src io.Reader) {
var err error
var fmtin io.WriteCloser
var fmtout io.ReadCloser
var validbuf *bytes.Buffer
var fmtbuf *bytes.Buffer
var validbytes []byte
// process (i.e., "govalid" output) will be buffered here.
validbuf = new(bytes.Buffer)
// gofmt output will be buffered here.
fmtbuf = new(bytes.Buffer)
testProcess(t, "-", validbuf, srcname, src)
validbytes = validbuf.Bytes()
// XXX Isn't there a way to gofmt without having to launch a
// subprocess?
cmd := exec.Command("gofmt")
fmtin, err = cmd.StdinPipe()
if err != nil {
t.Error(err)
}
fmtout, err = cmd.StdoutPipe()
if err != nil {
t.Error(err)
}
if err := cmd.Start(); err != nil {
t.Error(err)
}
sync := make(chan struct{})
go func() {
io.Copy(fmtin, bytes.NewBuffer(validbytes))
fmtin.Close()
sync <- struct{}{}
}()
go func() {
io.Copy(fmtbuf, fmtout)
sync <- struct{}{}
}()
<-sync
<-sync
if err := cmd.Wait(); err != nil {
t.Error(err)
}
if !bytes.Equal(validbytes, fmtbuf.Bytes()) {
t.Log("gofmt output differs")
validlines := strings.Split(string(validbytes), "\n")
fmtlines := strings.Split(string(fmtbuf.Bytes()), "\n")
diffrecords := difflib.Diff(validlines, fmtlines)
t.Log("--- valid output")
t.Log("+++ gofmt output")
for _, dr := range diffrecords {
if dr.Delta == difflib.Common {
continue
}
t.Log(dr)
}
t.Fail()
}
}
func TestFmt(t *testing.T) {
for _, name := range testGoodNames {
srcname := path.Join("test", fmt.Sprintf("%s.v", name))
testFmt(t, srcname, nil)
}
}