-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathtest.go
561 lines (496 loc) · 15.9 KB
/
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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Package test contains the code to parse and execute Gno tests and filetests.
package test
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/gnolang/gno/gnovm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
storetypes "github.com/gnolang/gno/tm2/pkg/store/types"
"go.uber.org/multierr"
)
const (
// DefaultHeight is the default height used in the [Context].
DefaultHeight = 123
// DefaultTimestamp is the Timestamp value used by default in [Context].
DefaultTimestamp = 1234567890
// DefaultCaller is the result of gno.DerivePkgAddr("user1.gno"),
// used as the default caller in [Context].
DefaultCaller crypto.Bech32Address = "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
)
// Context returns a TestExecContext. Usable for test purpose only.
// The returned context has a mock banker, params and event logger. It will give
// the pkgAddr the coins in `send` by default, and only that.
// The Height and Timestamp parameters are set to the [DefaultHeight] and
// [DefaultTimestamp].
func Context(pkgPath string, send std.Coins) *teststd.TestExecContext {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
banker := &teststd.TestBanker{
CoinTable: map[crypto.Bech32Address]std.Coins{
pkgAddr.Bech32(): send,
},
}
ctx := stdlibs.ExecContext{
ChainID: "dev",
ChainDomain: "tests.gno.land",
Height: DefaultHeight,
Timestamp: DefaultTimestamp,
OriginCaller: DefaultCaller,
OriginPkgAddr: pkgAddr.Bech32(),
OriginSend: send,
OriginSendSpent: new(std.Coins),
Banker: banker,
Params: newTestParams(),
EventLogger: sdk.NewEventLogger(),
}
return &teststd.TestExecContext{
ExecContext: ctx,
RealmFrames: make(map[*gno.Frame]teststd.RealmOverride),
}
}
// Machine is a minimal machine, set up with just the Store, Output and Context.
func Machine(testStore gno.Store, output io.Writer, pkgPath string, debug bool) *gno.Machine {
return gno.NewMachineWithOptions(gno.MachineOptions{
Store: testStore,
Output: output,
Context: Context(pkgPath, nil),
Debug: debug,
})
}
// OutputWithError returns an io.Writer that can be used as a [gno.Machine.Output],
// where the test standard libraries will write to errWriter when using
// os.Stderr.
func OutputWithError(output, errWriter io.Writer) io.Writer {
return &outputWithError{output, errWriter}
}
type outputWithError struct {
w io.Writer
errW io.Writer
}
func (o outputWithError) Write(p []byte) (int, error) { return o.w.Write(p) }
func (o outputWithError) StderrWrite(p []byte) (int, error) { return o.errW.Write(p) }
// ----------------------------------------
// testParams
type testParams struct{}
func newTestParams() *testParams {
return &testParams{}
}
func (tp *testParams) SetBool(key string, val bool) { /* noop */ }
func (tp *testParams) SetBytes(key string, val []byte) { /* noop */ }
func (tp *testParams) SetInt64(key string, val int64) { /* noop */ }
func (tp *testParams) SetUint64(key string, val uint64) { /* noop */ }
func (tp *testParams) SetString(key string, val string) { /* noop */ }
func (tp *testParams) SetStrings(key string, val []string) { /* noop */ }
// ----------------------------------------
// main test function
// TestOptions is a list of options that must be passed to [Test].
type TestOptions struct {
// BaseStore / TestStore to use for the tests.
BaseStore storetypes.CommitStore
TestStore gno.Store
// Gno root dir.
RootDir string
// Used for printing program output, during verbose logging.
Output io.Writer
// Used for os.Stderr, and for printing errors.
Error io.Writer
// Debug enables the interactive debugger on gno tests.
Debug bool
// Not set by NewTestOptions:
// Flag to filter tests to run.
RunFlag string
// Whether to update filetest directives.
Sync bool
// Uses Error to print when starting a test, and prints test output directly,
// unbuffered.
Verbose bool
// Uses Error to print runtime metrics for tests.
Metrics bool
// Uses Error to print the events emitted.
Events bool
// Whether to disable test result caching
NoCache bool
filetestBuffer bytes.Buffer
outWriter proxyWriter
}
// WriterForStore is the writer that should be passed to [Store], so that
// [Test] is then able to swap it when needed.
func (opts *TestOptions) WriterForStore() io.Writer {
return &opts.outWriter
}
// NewTestOptions sets up TestOptions, filling out all "required" parameters.
func NewTestOptions(rootDir string, stdout, stderr io.Writer) *TestOptions {
opts := &TestOptions{
RootDir: rootDir,
Output: stdout,
Error: stderr,
}
opts.BaseStore, opts.TestStore = Store(rootDir, opts.WriterForStore())
return opts
}
// proxyWriter is a simple wrapper around a io.Writer, it exists so that the
// underlying writer can be swapped with another when necessary.
type proxyWriter struct {
w io.Writer
errW io.Writer
}
func (p *proxyWriter) Write(b []byte) (int, error) {
return p.w.Write(b)
}
// StderrWrite implements the interface specified in tests/stdlibs/os/os.go,
// which if found in Machine.Output allows to write to stderr from Gno.
func (p *proxyWriter) StderrWrite(b []byte) (int, error) {
return p.errW.Write(b)
}
// tee temporarily appends the writer w to an underlying MultiWriter, which
// should then be reverted using revert().
func (p *proxyWriter) tee(w io.Writer) (revert func()) {
rev := tee(&p.w, w)
revErr := tee(&p.errW, w)
return func() {
rev()
revErr()
}
}
func tee(ptr *io.Writer, dst io.Writer) (revert func()) {
save := *ptr
if save == io.Discard {
*ptr = dst
} else {
*ptr = io.MultiWriter(save, dst)
}
return func() {
*ptr = save
}
}
// Test runs tests on the specified memPkg.
// fsDir is the directory on filesystem of package; it's used in case opts.Sync
// is enabled, and points to the directory where the files are contained if they
// are to be updated.
// opts is a required set of options, which is often shared among different
// tests; you can use [NewTestOptions] for a common base configuration.
func Test(memPkg *gnovm.MemPackage, fsDir string, opts *TestOptions) error {
opts.outWriter.w = opts.Output
opts.outWriter.errW = opts.Error
var errs error
// Eagerly load imports.
if err := LoadImports(opts.TestStore, memPkg); err != nil {
return err
}
// Stands for "test", "integration test", and "filetest".
// "integration test" are the test files with `package xxx_test` (they are
// not necessarily integration tests, it's just for our internal reference.)
tset, itset, itfiles, ftfiles := parseMemPackageTests(memPkg)
// Testing with *_test.gno
if len(tset.Files)+len(itset.Files) > 0 {
// Create a common cw/gs for both the `pkg` tests as well as the `pkg_test`
// tests. This allows us to "export" symbols from the pkg tests and
// import them from the `pkg_test` tests.
cw := opts.BaseStore.CacheWrap()
gs := opts.TestStore.BeginTransaction(cw, cw, nil)
// Run test files in pkg.
if len(tset.Files) > 0 {
err := opts.runTestFiles(memPkg, tset, gs)
if err != nil {
errs = multierr.Append(errs, err)
}
}
// Test xxx_test pkg.
if len(itset.Files) > 0 {
itPkg := &gnovm.MemPackage{
Name: memPkg.Name + "_test",
Path: memPkg.Path + "_test",
Files: itfiles,
}
err := opts.runTestFiles(itPkg, itset, gs)
if err != nil {
errs = multierr.Append(errs, err)
}
}
}
// Testing with *_filetest.gno.
if len(ftfiles) > 0 {
filter := splitRegexp(opts.RunFlag)
for _, testFile := range ftfiles {
testFileName := testFile.Name
testFilePath := filepath.Join(fsDir, testFileName)
testName := "file/" + testFileName
if !shouldRun(filter, testName) {
continue
}
startedAt := time.Now()
if opts.Verbose {
fmt.Fprintf(opts.Error, "=== RUN %s\n", testName)
}
// Try to load from cache first
cache, err := loadTestCache(testFilePath, []byte(testFile.Body))
if err != nil {
fmt.Fprintf(opts.Error, "Warning: failed to load cache: %v\n", err)
}
var changed string
if !opts.NoCache && cache != nil && !opts.Sync {
// Use cached result
duration := cache.Duration
if opts.Verbose {
fmt.Fprintf(opts.Error, "=== CACHED %s (%s)\n", testName, fmtDuration(duration))
fmt.Fprint(opts.Error, cache.Output)
}
continue
}
// Run the test and cache the result
changed, err = opts.runFiletest(testFileName, []byte(testFile.Body))
duration := time.Since(startedAt)
if err == nil && !opts.NoCache && !opts.Sync {
// Cache successful test results
if cacheErr := saveTestCache(testFilePath, []byte(testFile.Body), opts.filetestBuffer.String(), duration); cacheErr != nil {
fmt.Fprintf(opts.Error, "Warning: failed to save cache: %v\n", cacheErr)
}
}
if changed != "" {
// Note: changed always == "" if opts.Sync == false.
err = os.WriteFile(testFilePath, []byte(changed), 0o644)
if err != nil {
panic(fmt.Errorf("could not fix golden file: %w", err))
}
}
dstr := fmtDuration(duration)
if err != nil {
fmt.Fprintf(opts.Error, "--- FAIL: %s (%s)\n", testName, dstr)
fmt.Fprintln(opts.Error, err.Error())
errs = multierr.Append(errs, fmt.Errorf("%s failed", testName))
} else if opts.Verbose {
fmt.Fprintf(opts.Error, "--- PASS: %s (%s)\n", testName, dstr)
}
}
}
return errs
}
func (opts *TestOptions) runTestFiles(
memPkg *gnovm.MemPackage,
files *gno.FileSet,
gs gno.TransactionStore,
) (errs error) {
var m *gno.Machine
defer func() {
if r := recover(); r != nil {
if st := m.ExceptionsStacktrace(); st != "" {
errs = multierr.Append(errors.New(st), errs)
}
errs = multierr.Append(
fmt.Errorf("panic: %v\ngo stacktrace:\n%v\ngno machine: %v\ngno stacktrace:\n%v",
r, string(debug.Stack()), m.String(), m.Stacktrace()),
errs,
)
}
}()
tests := loadTestFuncs(memPkg.Name, files)
var alloc *gno.Allocator
if opts.Metrics {
alloc = gno.NewAllocator(math.MaxInt64)
}
// reset store ops, if any - we only need them for some filetests.
opts.TestStore.SetLogStoreOps(false)
// Check if we already have the package - it may have been eagerly loaded.
m = Machine(gs, opts.WriterForStore(), memPkg.Path, opts.Debug)
m.Alloc = alloc
if gs.GetMemPackage(memPkg.Path) == nil {
m.RunMemPackage(memPkg, true)
} else {
m.SetActivePackage(gs.GetPackage(memPkg.Path, false))
}
pv := m.Package
m.RunFiles(files.Files...)
for _, tf := range tests {
// TODO(morgan): we could theoretically use wrapping on the baseStore
// and gno store to achieve per-test isolation. However, that requires
// some deeper changes, as ideally we'd:
// - Run the MemPackage independently (so it can also be run as a
// consequence of an import)
// - Run the test files before this for loop (but persist it to store;
// RunFiles doesn't do that currently)
// - Wrap here.
m = Machine(gs, opts.WriterForStore(), memPkg.Path, opts.Debug)
m.Alloc = alloc.Reset()
m.SetActivePackage(pv)
testingpv := m.Store.GetPackage("testing", false)
testingtv := gno.TypedValue{T: &gno.PackageType{}, V: testingpv}
testingcx := &gno.ConstExpr{TypedValue: testingtv}
if opts.Debug {
fileContent := func(ppath, name string) string {
p := filepath.Join(opts.RootDir, ppath, name)
b, err := os.ReadFile(p)
if err != nil {
p = filepath.Join(opts.RootDir, "gnovm", "stdlibs", ppath, name)
b, err = os.ReadFile(p)
}
if err != nil {
p = filepath.Join(opts.RootDir, "examples", ppath, name)
b, err = os.ReadFile(p)
}
return string(b)
}
m.Debugger.Enable(os.Stdin, os.Stdout, fileContent)
}
eval := m.Eval(gno.Call(
gno.Sel(testingcx, "RunTest"), // Call testing.RunTest
gno.Str(opts.RunFlag), // run flag
gno.Nx(strconv.FormatBool(opts.Verbose)), // is verbose?
&gno.CompositeLitExpr{ // Third param, the testing.InternalTest
Type: gno.Sel(testingcx, "InternalTest"),
Elts: gno.KeyValueExprs{
{Key: gno.X("Name"), Value: gno.Str(tf.Name)},
{Key: gno.X("F"), Value: gno.Nx(tf.Name)},
},
},
))
if opts.Events {
events := m.Context.(*teststd.TestExecContext).EventLogger.Events()
if events != nil {
res, err := json.Marshal(events)
if err != nil {
panic(err)
}
fmt.Fprintf(opts.Error, "EVENTS: %s\n", string(res))
}
}
ret := eval[0].GetString()
if ret == "" {
err := fmt.Errorf("failed to execute unit test: %q", tf.Name)
errs = multierr.Append(errs, err)
fmt.Fprintf(opts.Error, "--- FAIL: %s [internal gno testing error]", tf.Name)
continue
}
// TODO: replace with amino or send native type?
var rep report
err := json.Unmarshal([]byte(ret), &rep)
if err != nil {
errs = multierr.Append(errs, err)
fmt.Fprintf(opts.Error, "--- FAIL: %s [internal gno testing error]", tf.Name)
continue
}
if rep.Failed {
err := fmt.Errorf("failed: %q", tf.Name)
errs = multierr.Append(errs, err)
}
if opts.Metrics {
// XXX: store changes
// XXX: max mem consumption
allocsVal := "n/a"
if m.Alloc != nil {
maxAllocs, allocs := m.Alloc.Status()
allocsVal = fmt.Sprintf("%s(%.2f%%)",
prettySize(allocs),
float64(allocs)/float64(maxAllocs)*100,
)
}
fmt.Fprintf(opts.Error, "--- runtime: cycle=%s allocs=%s\n",
prettySize(m.Cycles),
allocsVal,
)
}
}
return errs
}
// report is a mirror of Gno's stdlibs/testing.Report.
type report struct {
Failed bool
Skipped bool
}
type testFunc struct {
Package string
Name string
}
func loadTestFuncs(pkgName string, tfiles *gno.FileSet) (rt []testFunc) {
for _, tf := range tfiles.Files {
for _, d := range tf.Decls {
if fd, ok := d.(*gno.FuncDecl); ok {
fname := string(fd.Name)
if strings.HasPrefix(fname, "Test") {
tf := testFunc{
Package: pkgName,
Name: fname,
}
rt = append(rt, tf)
}
}
}
}
return
}
// parseMemPackageTests parses test files (skipping filetests) in the memPkg.
func parseMemPackageTests(memPkg *gnovm.MemPackage) (tset, itset *gno.FileSet, itfiles, ftfiles []*gnovm.MemFile) {
tset = &gno.FileSet{}
itset = &gno.FileSet{}
var errs error
for _, mfile := range memPkg.Files {
if !strings.HasSuffix(mfile.Name, ".gno") {
continue // skip this file.
}
n, err := gno.ParseFile(mfile.Name, mfile.Body)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if n == nil {
panic("should not happen")
}
switch {
case strings.HasSuffix(mfile.Name, "_filetest.gno"):
ftfiles = append(ftfiles, mfile)
case strings.HasSuffix(mfile.Name, "_test.gno") && memPkg.Name == string(n.PkgName):
tset.AddFiles(n)
case strings.HasSuffix(mfile.Name, "_test.gno") && memPkg.Name+"_test" == string(n.PkgName):
itset.AddFiles(n)
itfiles = append(itfiles, mfile)
case memPkg.Name == string(n.PkgName):
// normal package file
default:
panic(fmt.Sprintf(
"expected package name [%s] or [%s_test] but got [%s] file [%s]",
memPkg.Name, memPkg.Name, n.PkgName, mfile))
}
}
if errs != nil {
panic(errs)
}
return
}
func shouldRun(filter filterMatch, path string) bool {
if filter == nil {
return true
}
elem := strings.Split(path, "/")
ok, _ := filter.matches(elem, matchString)
return ok
}
// Adapted from https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func prettySize(nb int64) string {
const unit = 1000
if nb < unit {
return fmt.Sprintf("%d", nb)
}
div, exp := int64(unit), 0
for n := nb / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%c", float64(nb)/float64(div), "kMGTPE"[exp])
}
func fmtDuration(d time.Duration) string {
return fmt.Sprintf("%.2fs", d.Seconds())
}