-
Notifications
You must be signed in to change notification settings - Fork 3
/
lem.go
331 lines (291 loc) · 8.58 KB
/
lem.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright 2022
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lem
import (
"bytes"
"flag"
"fmt"
"go/build"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"github.com/akutz/lem/internal"
)
// Context provides a means to configure the test execution.
type Context struct {
// Benchmarks is an optional map of functions to benchmark.
//
// Keys in this map should correspond go the <ID> from "lem.<ID>" comments.
//
// Please note this is required to assert allocations and/or bytes.
Benchmarks map[string]func(*testing.B)
// BuildContext is the support context for building the specified
// packages and discovering their source files.
//
// Please see https://pkg.go.dev/go/build#Context for more information.
BuildContext *build.Context
// BuildOutput may be used in place of building any of the specified
// packages.
// If this field is specified then there will be no calls to "go build"
// or "go test."
BuildOutput string
// CompilerFlags is a list of flags to pass to the compiler.
//
// Please note the "-m" flag will always be used, whether it is included
// in this list or not.
CompilerFlags []string
// ImportedPackages is a list of imported packages to include in the
// testing.
//
// Please note if this field has a non-zero number of elements then
// the Packages field is ignored.
ImportedPackages []build.Package
// Packages is a list of packages to include in the testing.
//
// Please note this field is ignored if the ImportedPackages field has a
// non-zero number of elements.
Packages []string
}
// Copy returns a copy of this context.
func (src Context) Copy() Context {
return Context{
Benchmarks: copyNillableBenchmarksMap(src.Benchmarks),
BuildContext: copyNillableGoBuildContext(src.BuildContext),
BuildOutput: src.BuildOutput,
CompilerFlags: copyNillableStringSlice(src.CompilerFlags),
ImportedPackages: copyNillableImportedPackageSlice(src.ImportedPackages),
Packages: copyNillableStringSlice(src.Packages),
}
}
func (src Context) toInternal() internal.Context {
return internal.Context{
Benchmarks: copyNillableBenchmarksMap(src.Benchmarks),
BuildOutput: src.BuildOutput,
CompilerFlags: copyNillableStringSlice(src.CompilerFlags),
}
}
// copyGoBuildContext returns a copy of the provided, Go build context.
func copyGoBuildContext(src build.Context) build.Context {
return build.Context{
GOARCH: src.GOARCH,
GOOS: src.GOOS,
GOROOT: src.GOROOT,
GOPATH: src.GOROOT,
Dir: src.Dir,
CgoEnabled: src.CgoEnabled,
UseAllFiles: src.UseAllFiles,
Compiler: src.Compiler,
BuildTags: copyNillableStringSlice(src.BuildTags),
ToolTags: copyNillableStringSlice(src.ToolTags),
ReleaseTags: copyNillableStringSlice(src.ReleaseTags),
InstallSuffix: src.InstallSuffix,
JoinPath: src.JoinPath,
SplitPathList: src.SplitPathList,
IsAbsPath: src.IsAbsPath,
IsDir: src.IsDir,
HasSubdir: src.HasSubdir,
ReadDir: src.ReadDir,
OpenFile: src.OpenFile,
}
}
// NewBuildContext returns a copy of Go's default build context.
func NewBuildContext() build.Context {
return copyGoBuildContext(build.Default)
}
func copyNillableGoBuildContext(src *build.Context) *build.Context {
if src == nil {
return nil
}
dst := copyGoBuildContext(*src)
return &dst
}
func copyNillableBenchmarksMap(
src map[string]func(*testing.B)) map[string]func(*testing.B) {
if src == nil {
return nil
}
dst := map[string]func(*testing.B){}
for k, v := range src {
dst[k] = v
}
return dst
}
func copyNillableStringSlice(src []string) []string {
if src == nil {
return nil
}
if len(src) == 0 {
return []string{}
}
dst := make([]string, len(src))
for i := range src {
dst[i] = src[i]
}
return dst
}
func copyNillableImportedPackageSlice(
src []build.Package) []build.Package {
if src == nil {
return nil
}
if len(src) == 0 {
return []build.Package{}
}
dst := make([]build.Package, len(src))
for i := range src {
dst[i] = src[i]
}
return dst
}
func theirDirectory() (string, error) {
_, callersFilePath, _, ok := runtime.Caller(2)
if !ok {
return "", fmt.Errorf("failed to obtain caller's directory")
}
return filepath.Dir(callersFilePath), nil
}
// Sets the value of the -test.benchtime flag and returns the original
// value if one was present, otherwise an empty string is returned.
//
// Please note this function is a no-op if the flag is not already
// defined.
func SetBenchtime(s string) string {
f := flag.Lookup("test.benchtime")
if f == nil {
return ""
}
og := f.Value.String()
f.Value.Set(s)
return og
}
// Sets the value of the -test.benchmem flag and returns the original
// value if one was present, otherwise an empty string is returned.
//
// Please note this function is a no-op if the flag is not already
// defined.
func SetBenchmem(s string) string {
f := flag.Lookup("test.benchmem")
if f == nil {
return ""
}
og := f.Value.String()
f.Value.Set(s)
return og
}
// Tags returns a slice of the value of the tags flag.
func Tags() []string {
f := flag.Lookup("tags")
if f == nil {
return nil
}
var tags []string
for _, t := range strings.Split(f.Value.String(), ",") {
t := strings.TrimSpace(t)
if t != "" {
tags = append(tags, t)
}
}
return tags
}
// Run validates the leak, escape, and move assertions for the caller's
// package and test package (if different).
func Run(t *testing.T) {
dir, err := theirDirectory()
if err != nil {
t.Fatal(err)
}
run(t, dir, Context{})
}
// RunWithBenchmarks validates the leak, escape, move assertions, and
// heap allocation assertions for the caller's package and test package
// (if different).
func RunWithBenchmarks(t *testing.T, benchmarks map[string]func(*testing.B)) {
dir, err := theirDirectory()
if err != nil {
t.Fatal(err)
}
run(t, dir, Context{Benchmarks: benchmarks})
}
// RunWithContext validates the leak, escape, and move assertions for the
// packages specified in the provided options. Heap allocation assertions
// may also occur if the provided context includes the benchmarks map.
func RunWithContext(t *testing.T, ctx Context) {
dir, err := theirDirectory()
if err != nil {
t.Fatal(err)
}
run(t, dir, ctx)
}
func run(t *testing.T, srcDir string, ctx Context) {
ctx = ctx.Copy()
// Create a new build context if one does not exist.
if ctx.BuildContext == nil {
buildContext := NewBuildContext()
ctx.BuildContext = &buildContext
ctx.BuildContext.BuildTags = Tags()
}
// If no package was specified then default to the package relative to
// the provided source directory.
if len(ctx.Packages) == 0 {
ctx.Packages = []string{"."}
}
// If ctx.ImportedPackages is empty then create it from the
// packages specified in ctx.Packages.
if len(ctx.ImportedPackages) == 0 {
ctx.ImportedPackages = make([]build.Package, len(ctx.Packages))
for i, pkg := range ctx.Packages {
ipkg, err := ctx.BuildContext.Import(
pkg,
srcDir,
build.IgnoreVendor)
if err != nil {
t.Fatalf("failed to import pkg %s: %v", pkg, err)
}
ctx.ImportedPackages[i] = *ipkg
}
}
var (
allSrcFiles []string
buildOutput bytes.Buffer
)
for _, pkg := range ctx.ImportedPackages {
// Build the package if build output has not already been supplied.
if ctx.BuildOutput == "" {
if err := internal.Build(
&buildOutput,
pkg,
ctx.toInternal()); err != nil {
t.Fatalf("failed to build pkg %s: %v", pkg.ImportPath, err)
}
}
// Get the package's sources and sort them so they maintain
// lexographical order between all different types of sources.
pkgSrcs := append([]string{}, pkg.GoFiles...)
pkgSrcs = append(pkgSrcs, pkg.TestGoFiles...)
pkgSrcs = append(pkgSrcs, pkg.XTestGoFiles...)
sort.Strings(pkgSrcs)
// Append the package sources to the overall number of sources.
allSrcFiles = append(allSrcFiles, pkgSrcs...)
}
if ctx.BuildOutput == "" {
ctx.BuildOutput = buildOutput.String()
}
testCases, err := internal.GetTestCases(allSrcFiles...)
if err != nil {
t.Fatalf("failed to get test cases: %v", err)
}
// Build a test case tree and run the tests.
internal.NewTree(testCases...).Run(t, ctx.toInternal())
}