-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
82 lines (63 loc) · 1.69 KB
/
main_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
package main
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func TestBenchmarkCompareToBranch(t *testing.T) {
t.Skip("TODO(bep) Fix me.")
os.Args = []string{"-bench=Sleep", "-base=testing", "-package=./testing"}
out := captureOutput(main)
// Assert that the correct steps have been run.
assertContainsAll(t, out,
"old bytes",
`Benchmark and compare branch "testing" and "master"`)
}
func TestBenchmarkCurrent(t *testing.T) {
t.Skip("TODO(bep) Fix me.")
os.Args = []string{"-bench=Sleep", "-package=./testing"}
out := captureOutput(main)
// Assert that the correct steps have been run.
assertContainsAll(t, out,
"ns/op",
`Benchmark branch "master"`)
assertNotContainsAll(t, out,
"old bytes")
}
func TestBenchmarkCompareToStashed(t *testing.T) {
t.Skip("TODO(bep) Fix me.")
os.Args = []string{"-bench=Sleep", "-package=./testing"}
const dummy = "gobenchdummyfile.txt"
ioutil.WriteFile(dummy, []byte("hello"), 0o777)
defer os.Remove(dummy)
out := captureOutput(main)
// Assert that the correct steps have been run.
assertContainsAll(t, out,
"old bytes",
`Stash changes`)
}
func assertContainsAll(t *testing.T, content string, values ...string) {
for _, value := range values {
if !strings.Contains(content, value) {
t.Fatalf("%q not found in:\n\n%s", value, content)
}
}
}
func assertNotContainsAll(t *testing.T, content string, values ...string) {
for _, value := range values {
if strings.Contains(content, value) {
t.Fatalf("%q found in:\n\n%s", value, content)
}
}
}
func captureOutput(f func()) string {
oldOut := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = oldOut
return string(out)
}