-
Notifications
You must be signed in to change notification settings - Fork 26
/
integration_test.go
188 lines (155 loc) · 4.26 KB
/
integration_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"text/template"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
)
func TestCOBIntegration(t *testing.T) {
tempDir, err := os.MkdirTemp("", "cob-integration-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
// Build cob in the temporary directory
cmd := exec.Command("go", "build", "-o", tempDir, ".")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("failed to build cob: %v\nOutput: %s", err, output)
}
// Initialize git repository
err = initGitRepo(tempDir)
if err != nil {
t.Fatalf("failed to initialize git repository: %v", err)
}
// Create two mock benchmark files, the second one slower than the first
err = createMockBenchmarks(tempDir)
if err != nil {
t.Fatalf("failed to create mock benchmarks: %v", err)
}
// Run cob in the temporary directory
cmd = exec.Command(filepath.Join(tempDir, "cob"))
cmd.Dir = tempDir
output, err = cmd.CombinedOutput()
// Expect an error because the second benchmark is slower than the first
if err == nil {
t.Fatalf("expected an error, but got none")
}
assert.Contains(t, string(output), "Comparison")
assert.Contains(t, string(output), "BenchmarkFoo")
assert.Contains(t, string(output), "This commit makes benchmarks worse")
}
func initGitRepo(dir string) error {
r, err := git.PlainInit(dir, false)
if err != nil {
return fmt.Errorf("failed to initialize git repository: %w", err)
}
w, err := r.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree: %w", err)
}
err = os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module test\n"), 0644)
if err != nil {
return fmt.Errorf("failed to create go.mod: %w", err)
}
_, err = w.Add(".")
if err != nil {
return fmt.Errorf("failed to add go.mod: %w", err)
}
_, err = w.Commit("Initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Test User",
Email: "[email protected]",
},
})
if err != nil {
return fmt.Errorf("failed to commit: %w", err)
}
return nil
}
func createMockBenchmarks(dir string) error {
r, err := git.PlainOpen(dir)
if err != nil {
return fmt.Errorf("failed to open git repository: %w", err)
}
w, err := r.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree: %w", err)
}
tmpl := `
package benchmark
import "testing"
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
foo({{.Iterations}})
}
}
func foo(n int) int {
sum := 0
for i := 0; i < n; i++ {
sum += i
}
return sum
}
`
// Create initial (good) benchmark
if err := createBenchmarkFile(dir, "bench_test.go", tmpl, 100); err != nil {
return fmt.Errorf("failed to create good benchmark: %w", err)
}
_, err = w.Add("bench_test.go")
if err != nil {
return fmt.Errorf("failed to add bench_test.go: %w", err)
}
_, err = w.Commit("Add initial good benchmark", &git.CommitOptions{
Author: &object.Signature{
Name: "Test User",
Email: "[email protected]",
},
})
if err != nil {
return fmt.Errorf("failed to commit initial benchmark: %w", err)
}
// Create worse benchmark (replacing the good one)
if err := createBenchmarkFile(dir, "bench_test.go", tmpl, 1000); err != nil {
return fmt.Errorf("failed to create worse benchmark: %w", err)
}
_, err = w.Add("bench_test.go")
if err != nil {
return fmt.Errorf("failed to add updated bench_test.go: %w", err)
}
_, err = w.Commit("Update with worse benchmark", &git.CommitOptions{
Author: &object.Signature{
Name: "Test User",
Email: "[email protected]",
},
})
if err != nil {
return fmt.Errorf("failed to commit worse benchmark: %w", err)
}
return nil
}
func createBenchmarkFile(dir, filename, tmpl string, iterations int) error {
t, err := template.New("bench").Parse(tmpl)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
f, err := os.Create(filepath.Join(dir, filename))
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()
data := struct {
Iterations int
}{
Iterations: iterations,
}
if err := t.Execute(f, data); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
return nil
}