This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreduce_test.go
148 lines (136 loc) · 3.21 KB
/
reduce_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
145
146
147
148
// Copyright (c) 2017, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"bytes"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
var (
write = flag.Bool("w", false, "write test outputs")
fast = flag.Bool("f", false, "skip work to make tests faster")
)
func TestMain(m *testing.M) {
flag.Parse()
fastTest = *fast
os.Exit(m.Run())
}
func TestReductions(t *testing.T) {
paths, err := filepath.Glob(filepath.Join("testdata", "*"))
if err != nil {
t.Fatal(err)
}
*verbose = true
for _, path := range paths {
name := filepath.Base(path)
t.Run(name, testReduction(name))
}
}
func readFile(t testing.TB, dir, path string) string {
bs, err := ioutil.ReadFile(filepath.Join(dir, path))
if err != nil {
t.Fatal(err)
}
return string(bs)
}
func writeFile(t testing.TB, dir, path, cont string) {
err := ioutil.WriteFile(filepath.Join(dir, path), []byte(cont), 0644)
if err != nil {
t.Fatal(err)
}
}
func testReduction(name string) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
dir := filepath.Join("testdata", name)
orig := []byte(readFile(t, dir, "src.go"))
defer ioutil.WriteFile(filepath.Join(dir, "src.go"), orig, 0644)
want := readFile(t, dir, "src.go.min")
match := strings.TrimRight(readFile(t, dir, "match"), "\n")
impPath := "./testdata/" + name
var buf bytes.Buffer
if err := reduce(impPath, match, &buf, ""); err != nil {
t.Fatal(err)
}
got := readFile(t, dir, "src.go")
if want != got {
if *write {
writeFile(t, dir, "src.go.min", got)
} else {
t.Fatalf("unexpected program output\nwant:\n%sgot:\n%s",
want, got)
}
}
// remove testdata/<dir>/ bit
rawLog := buf.String()
buf.Reset()
for _, line := range strings.Split(rawLog, "\n") {
if line == "" {
break
}
line = strings.TrimPrefix(line, dir+string(filepath.Separator))
buf.WriteString(line)
buf.WriteByte('\n')
}
gotLog := buf.String()
wantLog := readFile(t, dir, "log")
if wantLog != gotLog {
if *write {
writeFile(t, dir, "log", gotLog)
} else {
t.Fatalf("unexpected log output\nwant:\n%sgot:\n%s",
wantLog, gotLog)
}
}
}
}
func BenchmarkReduce(b *testing.B) {
dir, err := ioutil.TempDir("", "goreduce")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(dir)
if err := os.Chdir(dir); err != nil {
b.Fatal(err)
}
orig := []byte(`package crasher
import "sync"
func Crasher() {
var a []int
_ = sync.Once{}
println(a[0])
}`)
for i := 0; i < b.N; i++ {
if err := ioutil.WriteFile("src.go", orig, 0644); err != nil {
b.Fatal(err)
}
err := reduce(".", "index out of range", ioutil.Discard, "")
if err != nil {
b.Fatal(err)
}
}
}
func TestReduceErrs(t *testing.T) {
t.Parallel()
fastTest = false
defer func() { fastTest = *fast }()
tests := [...]struct {
dir, match string
errCont string
}{
{"missing-dir", "[", "missing closing ]"},
{"missing-dir", ".", "no such file"},
{"testdata/remove-stmt", "no-match", "does not match"},
}
for _, tc := range tests {
err := reduce(tc.dir, tc.match, ioutil.Discard, "")
if err == nil || !strings.Contains(err.Error(), tc.errCont) {
t.Fatalf("wanted error conatining %q, got: %v",
tc.errCont, err)
}
}
}