forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
341 lines (292 loc) · 8.42 KB
/
main.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
// Command tester automates the ability to download a suite of JavaScript libraries from a CDN and check if otto can handle them.
//
// It provides two commands via flags:
// * -fetch = Fetch all libraries from the CDN and store them in local testdata directory.
// * -report [file1 file2 ... fileN] = Report the results of trying to run the given or if empty all libraries in the testdata directory.
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/robertkrimen/otto"
"github.com/robertkrimen/otto/parser"
)
const (
// dataDir is where the libraries are downloaded to.
dataDir = "testdata"
// downloadWorkers is the number of workers that process downloads.
downloadWorkers = 40
// librariesURL is the source for JavaScript libraries for testing.
librariesURL = "http://api.cdnjs.com/libraries"
// requestTimeout is the maximum time we wait for a request to complete.
requestTimeout = time.Second * 20
)
var (
// testWorkers is the number of workers that process report.
testWorkers = min(10, runtime.GOMAXPROCS(0))
// noopConsole is a noopConsole which ignore log requests.
noopConsole = map[string]interface{}{
"log": func(call otto.FunctionCall) otto.Value {
return otto.UndefinedValue()
},
}
)
var (
matchReferenceErrorNotDefined = regexp.MustCompile(`^ReferenceError: \S+ is not defined$`)
matchLookahead = regexp.MustCompile(`Invalid regular expression: re2: Invalid \(\?[=!]\) <lookahead>`)
matchBackReference = regexp.MustCompile(`Invalid regular expression: re2: Invalid \\\d <backreference>`)
matchTypeErrorUndefined = regexp.MustCompile(`^TypeError: Cannot access member '[^']+' of undefined$`)
)
// broken identifies libraries which fail with a fatal error, so must be skipped.
var broken = map[string]string{
"lets-plot.js": "stack overflow",
"knockout-es5.js": "stack overflow",
"sendbird-calls.js": "runtime: out of memory",
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
// libraries represents fetch all libraries response.
type libraries struct {
Results []library `json:"results"`
}
// library represents a single library in a libraries response.
type library struct {
Name string `json:"name"`
Latest string `json:"latest"`
}
// fetch fetches itself and stores it in the dataDir.
func (l library) fetch() error {
if !strings.HasSuffix(l.Latest, ".js") {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, l.Latest, nil)
if err != nil {
return fmt.Errorf("request library %q: %w", l.Name, err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("get library %q: %w", l.Name, err)
}
defer resp.Body.Close() //nolint: errcheck
name := l.Name
if !strings.HasSuffix(name, ".js") {
name += ".js"
}
f, err := os.Create(filepath.Join(dataDir, name)) //nolint: gosec
if err != nil {
return fmt.Errorf("create library %q: %w", l.Name, err)
}
if _, err = io.Copy(f, resp.Body); err != nil {
return fmt.Errorf("write library %q: %w", l.Name, err)
}
return nil
}
// test runs the code from filename returning the time it took and any error
// encountered when running a full parse without IgnoreRegExpErrors in parseError.
func test(filename string) (took time.Duration, parseError, err error) { //nolint: nonamedreturns
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic on %q: %v", filename, r)
}
}()
now := time.Now()
defer func() {
// Always set took.
took = time.Since(now)
}()
if val := broken[filepath.Base(filename)]; val != "" {
return 0, nil, fmt.Errorf("fatal %q", val)
}
script, err := os.ReadFile(filename) //nolint: gosec
if err != nil {
return 0, nil, err
}
vm := otto.New()
if err := vm.Set("console", noopConsole); err != nil {
return 0, nil, fmt.Errorf("set console: %w", err)
}
prog, err := parser.ParseFile(nil, filename, string(script), 0)
if err != nil {
val := err.Error()
switch {
case matchReferenceErrorNotDefined.MatchString(val),
matchTypeErrorUndefined.MatchString(val),
matchLookahead.MatchString(val),
matchBackReference.MatchString(val):
// RegExp issues, retry with IgnoreRegExpErrors.
parseError = err
if _, err = parser.ParseFile(nil, filename, string(script), parser.IgnoreRegExpErrors); err != nil {
return 0, nil, err
}
return 0, parseError, nil
default:
return 0, nil, err
}
}
_, err = vm.Run(prog)
return 0, nil, err
}
// fetchAll fetches all files from src.
func fetchAll(src string) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, librariesURL, nil)
if err != nil {
return fmt.Errorf("request libraries %q: %w", src, err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("get libraries %q: %w", src, err)
}
defer resp.Body.Close() //nolint: errcheck
var libs libraries
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&libs); err != nil {
return fmt.Errorf("json decode: %w", err)
}
if err := os.Mkdir(dataDir, 0o750); err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("mkdir: %w", err)
}
var wg sync.WaitGroup
work := make(chan library, downloadWorkers)
errs := make(chan error, len(libs.Results))
wg.Add(downloadWorkers)
for i := 0; i < downloadWorkers; i++ {
go func() {
defer wg.Done()
for lib := range work {
fmt.Fprint(os.Stdout, ".")
errs <- lib.fetch()
}
}()
}
fmt.Fprintf(os.Stdout, "Downloading %d libraries with %d workers ...", len(libs.Results), downloadWorkers)
wg.Add(1)
go func() {
defer wg.Done()
for _, lib := range libs.Results {
work <- lib
}
close(work)
}()
wg.Wait()
close(errs)
fmt.Fprintln(os.Stdout, " done")
for e := range errs {
if e != nil {
fmt.Fprintln(os.Stderr, e)
err = e
}
}
return err
}
// result represents the result from a test.
type result struct {
filename string
err error
parseError error
took time.Duration
}
// report runs test for all specified files, if none a specified all
// JavaScript files in our dataDir, outputting the results.
func report(files []string) error {
if len(files) == 0 {
var err error
files, err = filepath.Glob(filepath.Join(dataDir, "*.js"))
if err != nil {
return fmt.Errorf("read dir: %w", err)
}
}
var wg sync.WaitGroup
workers := min(testWorkers, len(files))
work := make(chan string, workers)
results := make(chan result, len(files))
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
for f := range work {
fmt.Fprint(os.Stdout, ".")
took, parseError, err := test(f)
results <- result{
filename: f,
err: err,
parseError: parseError,
took: took,
}
}
}()
}
fmt.Fprintf(os.Stdout, "Reporting on %d libs with %d workers...", len(files), workers)
wg.Add(1)
go func() {
defer wg.Done()
for _, f := range files {
work <- f
}
close(work)
}()
wg.Wait()
close(results)
fmt.Fprintln(os.Stdout, " done")
var fail, pass, parse int
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(writer, "Library", "\t| Took", "\t| Status")
fmt.Fprintln(writer, "-------", "\t| ----", "\t| ------")
for res := range results {
switch {
case res.err != nil:
fmt.Fprintf(writer, "%s\t| %v\t| fail: %v\n", res.filename, res.took, res.err)
fail++
case res.parseError != nil:
fmt.Fprintf(writer, "%s\t| %v\t| pass parse: %v\n", res.filename, res.took, res.parseError)
parse++
default:
fmt.Fprintf(writer, "%s\t| %v\t| pass\n", res.filename, res.took)
pass++
}
}
if err := writer.Flush(); err != nil {
return fmt.Errorf("flush: %w", err)
}
fmt.Fprintf(os.Stdout, "\nSummary:\n - %d passes\n - %d parse passes\n - %d fails\n", pass, parse, fail)
return nil
}
func main() {
flagFetch := flag.Bool("fetch", false, "fetch all libraries for testing")
flagReport := flag.Bool("report", false, "test and report the named files or all libraries if non specified")
flag.Parse()
var err error
switch {
case *flagFetch:
err = fetchAll(librariesURL)
case *flagReport:
err = report(flag.Args())
default:
flag.PrintDefaults()
err = fmt.Errorf("missing flag")
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(64)
}
}