Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
383 lines (340 loc) · 10.6 KB
/
Copy pathmain.go
File metadata and controls
383 lines (340 loc) · 10.6 KB
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
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"path"
"runtime"
"strconv"
"strings"
"webtyp.com/devbrowser"
"webtyp.com/devbrowser/cdproto/inspector"
"webtyp.com/devbrowser/cdproto/profiler"
cdpruntime "webtyp.com/devbrowser/cdproto/runtime"
"webtyp.com/devbrowser/cdproto/target"
"webtyp.com/devbrowser/chromedp"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := run(ctx, os.Args, os.Stderr, flag.CommandLine)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(ctx context.Context, args []string, errOutput io.Writer, flagSet *flag.FlagSet) (returnedErr error) {
logger := log.New(errOutput, "[wasmbrowsertest]: ", log.LstdFlags|log.Lshortfile)
defer func() {
r := recover()
if r != nil {
returnedErr = fmt.Errorf("Panicked: %+v", r)
}
}()
if len(args) < 2 {
return errors.New("Please pass a wasm file as a parameter")
}
// Parse flags first
// We need to parse flags from args[1:] because args[0] is the program name
// However, the flag package expects to parse from os.Args (or a slice provided to Parse).
// gentleParse effectively parses flags. We should use it to extract flags and finding the non-flag args.
cpuProfile := flagSet.String("test.cpuprofile", "", "")
coverageProfile := flagSet.String("test.coverprofile", "", "")
quiet := flagSet.Bool("quiet", false, "disable printing of passing test logs")
tinygoMode := flagSet.Bool("tinygo", false, "the wasm binary was built by TinyGo: serve TinyGo's wasm_exec.js instead of the Go toolchain's")
// Separate flags and other args
var wasmFile string
var passon []string
// manually parse valid arguments to bypass flagSet.Parse error on unknown flags
// We need to find the wasm file first to separate "args before wasm file" (flags for us)
// and "args after/including wasm file" (flags for wasm binary? or gentleParse handles it?)
// Revert to a simpler strategy: identifying wasm file first, then handle flags.
// We scan for the first argument that ends in .wasm or .test and doesn't start with -
wasmFileIndex := -1
for i, arg := range args {
if i == 0 {
continue
} // skip program name
if !strings.HasPrefix(arg, "-") && (strings.HasSuffix(arg, ".wasm") || strings.HasSuffix(arg, ".test")) {
wasmFileIndex = i
break
}
}
if wasmFileIndex == -1 {
// Fallback or error?
return errors.New("Please pass a wasm file as a parameter")
}
// Handle our flags manually in the args before wasmFile?
// Or just clean args to remove our flags and parse?
var cleanArgs []string
cleanArgs = append(cleanArgs, args[0])
foundQuiet := false
foundTinygo := false
for i, arg := range args {
if i == 0 {
continue
}
if arg == "-quiet" || arg == "--quiet" {
foundQuiet = true
continue
}
// Like -quiet, this flag is ours and arrives before the binary
// (`go test -exec 'wasmbrowsertest -tinygo'`), so the wasm binary must
// never see it.
if arg == "-tinygo" || arg == "--tinygo" {
foundTinygo = true
continue
}
// Also strip cpuprofile/coverprofile if handled manually?
// No, let gentleParse and flagSet handle standard go test flags.
cleanArgs = append(cleanArgs, arg)
}
*quiet = foundQuiet
*tinygoMode = foundTinygo
args = cleanArgs
// Recalculate index in clean args
wasmFileIndex = -1
for i, arg := range cleanArgs {
if i == 0 {
continue
} // skip program name
if !strings.HasPrefix(arg, "-") && (strings.HasSuffix(arg, ".wasm") || strings.HasSuffix(arg, ".test")) {
wasmFileIndex = i
break
}
}
if wasmFileIndex == -1 {
return errors.New("Please pass a wasm file as a parameter")
}
wasmFile = cleanArgs[wasmFileIndex]
if *tinygoMode {
// The binary go test just handed us was built by the Go toolchain, so it
// cannot prove TinyGo compatibility. Rebuild the package with TinyGo and
// run that instead.
tinygoFile, cleanup, err := tinygoBuildTest()
if err != nil {
return err
}
defer cleanup()
wasmFile = tinygoFile
}
ext := path.Ext(wasmFile)
if ext == ".test" {
newWasmFile := strings.ReplaceAll(wasmFile, ext, ".wasm")
err := copyFile(wasmFile, newWasmFile)
if err != nil {
return err
}
defer os.Remove(newWasmFile)
wasmFile = newWasmFile
}
// gentleParse expects (flagSet, args).
// Usage in original: passon, err := gentleParse(flagSet, args[2:])
// Original assumed args[1] is file.
// We should pass everything AFTER the wasm file to gentleParse?
// OR does gentleParse handle flags that come BEFORE the wasm file too?
// "args[2:]" in original implied everything after the file.
// If wasmFileIndex is where the file is, then arguments for the WASM binary (flags) are likely after it.
passonArgs := cleanArgs[wasmFileIndex+1:]
var err error // Declare err here to avoid shadowing
passon, err = gentleParse(flagSet, passonArgs)
if err != nil {
return err
}
passon = append([]string{wasmFile}, passon...)
// We need to verify if cpuProfile/coverageProfile were set by gentleParse?
// gentleParse calls flagSet.Parse().
// If our flags are in passonArgs, gentleParse will see them.
// BUT if `go test` passes them BEFORE the wasm file?
// `go test -exec` behavior:
// `wasmbrowsertest [exec-flags] <test-binary> [test-flags]`
// If we run `go test -exec 'wasmbrowsertest -quiet'`, it becomes:
// `wasmbrowsertest -quiet <binary> -test.v ...`
// So `-quiet` is before the binary. `-test.v` is after.
// We already extracted `quiet`.
// now we need to make sure `test.cpuprofile` etc (which are standard Go flags) are caught.
// They usually come AFTER the binary.
// So `gentleParse` on `passonArgs` (args after binary) should work.
if *coverageProfile != "" {
passon = append(passon, "-test.coverprofile="+*coverageProfile)
}
if *cpuProfile != "" {
passon = append(passon, "-test.cpuprofile="+*cpuProfile)
}
// Setup web server.
handler, err := NewWASMServer(wasmFile, passon, *coverageProfile, *tinygoMode, logger)
if err != nil {
return err
}
url, shutdownHTTPServer, err := startHTTPServer(ctx, handler, logger)
if err != nil {
return err
}
defer shutdownHTTPServer()
var browserCtx context.Context
if os.Getenv("WASM_HEADLESS") == "off" {
opts := chromedp.DefaultExecAllocatorOptions[:]
opts = append(opts,
chromedp.ExecPath(devbrowser.ResolveChromeExecPath()),
chromedp.Flag("headless", false),
)
// WSL needs the GPU disabled. See issue #10
if runtime.GOOS == "linux" && isWSL() {
opts = append(opts, chromedp.DisableGPU)
}
allocCtx, cancelAllocCtx := chromedp.NewExecAllocator(ctx, opts...)
defer cancelAllocCtx()
var cancelCtx context.CancelFunc
browserCtx, cancelCtx = chromedp.NewContext(allocCtx)
defer cancelCtx()
} else {
lease, err := devbrowser.AcquireHeadless(ctx)
if err != nil {
return err
}
defer lease.Close()
browserCtx = lease.Context()
}
chromedp.ListenTarget(browserCtx, func(ev interface{}) {
handleEvent(browserCtx, ev, logger, *quiet)
})
var exitCode int
tasks := []chromedp.Action{
chromedp.Navigate(url),
chromedp.WaitEnabled(`#doneButton`),
chromedp.Evaluate(`exitCode;`, &exitCode),
}
if *cpuProfile != "" {
// Prepend and append profiling tasks
tasks = append([]chromedp.Action{
profiler.Enable(),
profiler.Start(),
}, tasks...)
tasks = append(tasks, chromedp.ActionFunc(func(ctx context.Context) error {
profile, err := profiler.Stop().Do(ctx)
if err != nil {
return err
}
outF, err := os.Create(*cpuProfile)
if err != nil {
return err
}
defer func() {
err = outF.Close()
if err != nil {
logger.Println(err)
}
}()
funcMap, err := getFuncMap(wasmFile)
if err != nil {
return err
}
return WriteProfile(profile, outF, funcMap)
}))
}
err = chromedp.Run(browserCtx, tasks...)
if *quiet {
if consoleFilter == nil {
// Initialize if not done (though handleEvent should have done it if events fired)
consoleFilter = NewConsoleFilter(*quiet, func(s string) {
fmt.Printf("%s\n", s)
})
}
consoleFilter.Flush()
}
if err != nil {
// Browser did not exit cleanly. Likely failed with an uncaught error.
return err
}
if exitCode != 0 {
return fmt.Errorf("exit with status %d", exitCode)
}
return nil
}
func copyFile(src, dst string) error {
srdFd, err := os.Open(src)
if err != nil {
return fmt.Errorf("error in copying %s to %s: %v", src, dst, err)
}
defer srdFd.Close()
dstFd, err := os.Create(dst)
if err != nil {
return fmt.Errorf("error in copying %s to %s: %v", src, dst, err)
}
defer dstFd.Close()
_, err = io.Copy(dstFd, srdFd)
if err != nil {
return fmt.Errorf("error in copying %s to %s: %v", src, dst, err)
}
return nil
}
// handleEvent responds to different events from the browser and takes
// appropriate action.
var (
consoleFilter *ConsoleFilter
)
func handleEvent(ctx context.Context, ev interface{}, logger *log.Logger, quiet bool) {
if consoleFilter == nil {
consoleFilter = NewConsoleFilter(quiet, func(s string) {
fmt.Printf("%s\n", s)
})
}
switch ev := ev.(type) {
case *cdpruntime.EventConsoleAPICalled:
for _, arg := range ev.Args {
line := string([]byte(arg.Value))
if line == "" { // If Value is not found, look for Description.
line = arg.Description
}
// Any string content is quoted with double-quotes.
// So need to treat it specially.
s, err := strconv.Unquote(line)
if err != nil {
// Probably some numeric content, print it as is.
// fmt.Printf("%s\n", line)
s = line
}
consoleFilter.Add(s)
}
case *cdpruntime.EventExceptionThrown:
if ev.ExceptionDetails != nil {
consoleFilter.Flush()
details := ev.ExceptionDetails
fmt.Printf("%s:%d:%d %s\n", details.URL, details.LineNumber, details.ColumnNumber, details.Text)
if details.Exception != nil {
fmt.Printf("%s\n", details.Exception.Description)
}
}
case *target.EventTargetCrashed:
consoleFilter.Flush()
fmt.Printf("target crashed: status: %s, error code:%d\n", ev.Status, ev.ErrorCode)
err := chromedp.Cancel(ctx)
if err != nil {
logger.Printf("error in cancelling context: %v\n", err)
}
case *inspector.EventDetached:
consoleFilter.Flush()
fmt.Println("inspector detached: ", ev.Reason)
err := chromedp.Cancel(ctx)
if err != nil {
logger.Printf("error in cancelling context: %v\n", err)
}
}
}
// isWSL returns true if the OS is WSL, false otherwise.
// This method of checking for WSL has worked since mid 2016:
// https://github.com/microsoft/WSL/issues/423#issuecomment-328526847
func isWSL() bool {
buf, err := os.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
return false
}
// if there was an error opening the file it must not be WSL, so ignore the error
return bytes.Contains(buf, []byte("Microsoft"))
}