-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
168 lines (146 loc) · 3.67 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
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
package main
import (
"flag"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/jonboulle/clockwork"
)
var update = flag.Bool("update", false, "Update golden test files")
var wantReport = report{
{
name: "a",
bytes: 2,
ratio: float64(2) / 9,
},
{
name: "b",
bytes: 3,
ratio: float64(3) / 9,
},
{
name: "c",
bytes: 4,
ratio: float64(4) / 9,
isDir: true,
subdirs: report{
{
name: "c/d",
bytes: 4,
ratio: 1,
},
},
},
{
name: "emptyDir",
bytes: 0,
ratio: 0,
isDir: true,
subdirs: report{},
},
}
var wantD3Data = d3Data{
Name: "/",
Children: []d3Data{
{Name: "a", Value: 2},
{Name: "b", Value: 3},
{Name: "c", Children: []d3Data{
{Name: "c/d", Value: 4},
}},
{Name: "emptyDir"},
},
}
func TestMain(m *testing.M) {
flag.Parse()
// Git does not store empty directories, so we must create it manually
os.MkdirAll("testdata/base_path/emptyDir", 0750)
os.Exit(m.Run())
}
func TestToD3Data(t *testing.T) {
got := wantReport.toD3Data("/")
if diff := cmp.Diff(wantD3Data, got,
cmp.AllowUnexported(d3Data{}),
); diff != "" {
t.Errorf("diff -want +got:\n%s", diff)
}
}
func TestWalk(t *testing.T) {
got := walk("testdata/base_path", "")
if diff := cmp.Diff(wantReport, got,
cmp.AllowUnexported(reportEntry{}),
cmpopts.SortSlices(func(x, y reportEntry) bool { return x.name < y.name }),
); diff != "" {
t.Errorf("diff -want +got:\n%s", diff)
}
}
func TestHTTP(t *testing.T) {
testCases := []struct {
goldenFile string
artificialDelay time.Duration
waitForApology bool
}{
{goldenFile: "testdata/want.html", artificialDelay: 500 * time.Millisecond, waitForApology: false},
{goldenFile: "testdata/want_slow.html", artificialDelay: 5 * time.Second, waitForApology: true},
}
origClock := clock
defer func() { clock = origClock }()
for _, tc := range testCases {
t.Run(tc.goldenFile, func(t *testing.T) {
fakeClock := clockwork.NewFakeClock()
clock = fakeClock
ts := httptest.NewServer(handler{
basePath: "testdata/base_path",
artificialDelay: tc.artificialDelay,
})
defer ts.Close()
go func() {
t.Logf("Blocking for both clock.After(*apologyTimeout=%s) and clock.Sleep(%s)", *apologyTimeout, tc.artificialDelay)
fakeClock.BlockUntil(2)
if tc.waitForApology {
t.Logf("Advancing clock by apologyTimeout (%s)", *apologyTimeout)
fakeClock.Advance(*apologyTimeout)
t.Logf("clock.After(*apologyTimeout=%s) should trigger, clock.Sleep(%s) should still sleep", *apologyTimeout, tc.artificialDelay)
t.Logf("Blocking for apology to be done writing (clock.Sleep(%s) still sleeping)", tc.artificialDelay)
fakeClock.BlockUntil(2)
t.Logf("Apology now done writing, advancing to release clock.Sleep(%s)", tc.artificialDelay)
fakeClock.Advance(tc.artificialDelay)
} else {
t.Logf("Advancing clock by artificalDelay (%s)", tc.artificialDelay)
fakeClock.Advance(tc.artificialDelay)
}
}()
res, err := http.Get(ts.URL + "?d3=1")
if err != nil {
t.Fatal(err)
}
got, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if *update {
ioutil.WriteFile(tc.goldenFile, got, 0644)
}
want, err := ioutil.ReadFile(tc.goldenFile)
if err != nil {
t.Fatalf("Couldn't read golden file: %v", err)
}
if string(want) != string(got) {
t.Errorf("%s doesn't match; run with -update and use git diff", tc.goldenFile)
}
})
}
}
func Example_output() {
wantReport.output(os.Stdout)
// Output:
// a 22.2% 2 B
// b 33.3% 3 B
// c 44.4% 4 B
// emptyDir 0.0% 0 B
}