forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
271 lines (236 loc) · 7.33 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
package main
import (
"context"
"flag"
"fmt"
goio "io"
"log"
"path/filepath"
"strings"
"time"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/test"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/random"
)
type testCfg struct {
verbose bool
rootDir string
run string
timeout time.Duration
updateGoldenTests bool
printRuntimeMetrics bool
printEvents bool
}
func newTestCmd(io commands.IO) *commands.Command {
cfg := &testCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "test",
ShortUsage: "test [flags] <package> [<package>...]",
ShortHelp: "test packages",
LongHelp: `Runs the tests for the specified packages.
'gno test' recompiles each package along with any files with names matching the
file pattern "*_test.gno" or "*_filetest.gno".
The <package> can be directory or file path (relative or absolute).
- "*_test.gno" files work like "*_test.go" files, but they contain only test
functions. Benchmark and fuzz functions aren't supported yet. Similarly, only
tests that belong to the same package are supported for now (no "xxx_test").
The package path used to execute the "*_test.gno" file is fetched from the
module name found in 'gno.mod', or else it is randomly generated like
"gno.land/r/XXXXXXXX".
- "*_filetest.gno" files on the other hand are kind of unique. They exist to
provide a way to interact and assert a gno contract, thanks to a set of
specific directives that can be added using code comments.
"*_filetest.gno" must be declared in the 'main' package and so must have a
'main' function, that will be executed to test the target contract.
These single-line directives can set "input parameters" for the machine used
to perform the test:
- "PKGPATH:" is a single line directive that can be used to define the
package used to interact with the tested package. If not specified, "main" is
used.
- "MAXALLOC:" is a single line directive that can be used to define a limit
to the VM allocator. If this limit is exceeded, the VM will panic. Default to
0, no limit.
- "SEND:" is a single line directive that can be used to send an amount of
token along with the transaction. The format is for example "1000000ugnot".
Default is empty.
These directives, instead, match the comment that follows with the result
of the GnoVM, acting as a "golden test":
- "Output:" tests the following comment with the standard output of the
filetest.
- "Error:" tests the following comment with any panic, or other kind of
error that the filetest generates (like a parsing or preprocessing error).
- "Realm:" tests the following comment against the store log, which can show
what realm information is stored.
- "Stacktrace:" can be used to verify the following lines against the
stacktrace of the error.
- "Events:" can be used to verify the emitted events against a JSON.
To speed up execution, imports of pure packages are processed separately from
the execution of the tests. This makes testing faster, but means that the
initialization of imported pure packages cannot be checked in filetests.
`,
},
cfg,
func(_ context.Context, args []string) error {
return execTest(cfg, args, io)
},
)
}
func (c *testCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.verbose,
"v",
false,
"verbose output when running",
)
fs.BoolVar(
&c.updateGoldenTests,
"update-golden-tests",
false,
`writes actual as wanted for "golden" directives in filetests`,
)
fs.StringVar(
&c.rootDir,
"root-dir",
"",
"clone location of github.com/gnolang/gno (gno tries to guess it)",
)
fs.StringVar(
&c.run,
"run",
"",
"test name filtering pattern",
)
fs.DurationVar(
&c.timeout,
"timeout",
0,
"max execution time",
)
fs.BoolVar(
&c.printRuntimeMetrics,
"print-runtime-metrics",
false,
"print runtime metrics (gas, memory, cpu cycles)",
)
fs.BoolVar(
&c.printEvents,
"print-events",
false,
"print emitted events",
)
}
func execTest(cfg *testCfg, args []string, io commands.IO) error {
// Default to current directory if no args provided
if len(args) == 0 {
args = []string{"."}
}
// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.RootDir()
}
paths, err := targetsFromPatterns(args)
if err != nil {
return fmt.Errorf("list targets from patterns: %w", err)
}
if len(paths) == 0 {
io.ErrPrintln("no packages to test")
return nil
}
if cfg.timeout > 0 {
go func() {
time.Sleep(cfg.timeout)
panic("test timed out after " + cfg.timeout.String())
}()
}
subPkgs, err := gnomod.SubPkgsFromPaths(paths)
if err != nil {
return fmt.Errorf("list sub packages: %w", err)
}
// Set up options to run tests.
stdout := goio.Discard
if cfg.verbose {
stdout = io.Out()
}
opts := test.NewTestOptions(cfg.rootDir, io.In(), stdout, io.Err())
opts.RunFlag = cfg.run
opts.Sync = cfg.updateGoldenTests
opts.Verbose = cfg.verbose
opts.Metrics = cfg.printRuntimeMetrics
opts.Events = cfg.printEvents
buildErrCount := 0
testErrCount := 0
for _, pkg := range subPkgs {
if len(pkg.TestGnoFiles) == 0 && len(pkg.FiletestGnoFiles) == 0 {
io.ErrPrintfln("? %s \t[no test files]", pkg.Dir)
continue
}
// Determine gnoPkgPath by reading gno.mod
var gnoPkgPath string
modfile, err := gnomod.ParseAt(pkg.Dir)
if err == nil {
gnoPkgPath = modfile.Module.Mod.Path
} else {
gnoPkgPath = pkgPathFromRootDir(pkg.Dir, cfg.rootDir)
if gnoPkgPath == "" {
// unable to read pkgPath from gno.mod, generate a random realm path
io.ErrPrintfln("--- WARNING: unable to read package path from gno.mod or gno root directory; try creating a gno.mod file")
gnoPkgPath = "gno.land/r/" + strings.ToLower(random.RandStr(8)) // XXX: gno.land hardcoded for convenience.
}
}
memPkg := gno.MustReadMemPackage(pkg.Dir, gnoPkgPath)
startedAt := time.Now()
hasError := catchRuntimeError(gnoPkgPath, io.Err(), func() {
err = test.Test(memPkg, pkg.Dir, opts)
})
duration := time.Since(startedAt)
dstr := fmtDuration(duration)
if hasError || err != nil {
if err != nil {
io.ErrPrintfln("%s: test pkg: %v", pkg.Dir, err)
}
io.ErrPrintfln("FAIL")
io.ErrPrintfln("FAIL %s \t%s", pkg.Dir, dstr)
io.ErrPrintfln("FAIL")
testErrCount++
} else {
io.ErrPrintfln("ok %s \t%s", pkg.Dir, dstr)
}
}
if testErrCount > 0 || buildErrCount > 0 {
io.ErrPrintfln("FAIL")
return fmt.Errorf("FAIL: %d build errors, %d test errors", buildErrCount, testErrCount)
}
return nil
}
// attempts to determine the full gno pkg path by analyzing the directory.
func pkgPathFromRootDir(pkgPath, rootDir string) string {
abPkgPath, err := filepath.Abs(pkgPath)
if err != nil {
log.Printf("could not determine abs path: %v", err)
return ""
}
abRootDir, err := filepath.Abs(rootDir)
if err != nil {
log.Printf("could not determine abs path: %v", err)
return ""
}
abRootDir += string(filepath.Separator)
if !strings.HasPrefix(abPkgPath, abRootDir) {
return ""
}
impPath := strings.ReplaceAll(abPkgPath[len(abRootDir):], string(filepath.Separator), "/")
for _, prefix := range [...]string{
"examples/",
"gnovm/stdlibs/",
"gnovm/tests/stdlibs/",
} {
if strings.HasPrefix(impPath, prefix) {
return impPath[len(prefix):]
}
}
return ""
}