-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathcheck-test262.go
422 lines (379 loc) · 11.6 KB
/
check-test262.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
// Copyright (C) 2020 Matthew "strager" Glazar
// See end of file for extended copyright information.
// Test quick-lint-js with test262.
//
// https://github.com/tc39/test262
package main
import "bytes"
import "flag"
import "fmt"
import "io"
import "log"
import "os"
import "os/exec"
import "path/filepath"
import "runtime"
import "sort"
import "strings"
import "sync"
import "sync/atomic"
var testTodo = TestTodo{
TodoPaths: []string{
// These files look like tests, but they are not.
// TODO(strager): Check these files for crashes anyway, but
// don't check their assertions.
"tools/generation/test/expected/glob*/normal/*-nested/*.js",
"tools/generation/test/expected/glob*/normal/*.js",
"tools/generation/test/expected/multiple/glob/*.js",
"tools/generation/test/expected/multiple/normal/*-nested/*.js",
"tools/generation/test/expected/multiple/normal/*.js",
"tools/generation/test/expected/normal/*.js",
"tools/generation/test/expected/normal/nested/*.js",
"tools/lint/test/fixtures/*.js",
// TODO(strager): Implement strict mode.
"language/directive-prologue/10.1.1-2gs.js",
"language/directive-prologue/10.1.1-5gs.js",
"language/directive-prologue/10.1.1-8gs.js",
"language/directive-prologue/14.1-4gs.js",
"language/directive-prologue/14.1-5gs.js",
"language/expressions/*/*-eval-strict.js",
"language/expressions/*/eval.js",
"language/expressions/compound-assignment/11.13.2-6-1gs.js",
},
TodoFeatures: [][]byte{},
}
func main() {
quickLintJSExecutable := flag.String(
"quick-lint-js",
"quick-lint-js",
"path to the quick-lint-js executable",
)
stopOnFirstFailure := flag.Bool(
"stop-on-first-failure",
false,
"print only the first failing test (if any)",
)
flag.Parse()
if flag.NArg() == 0 {
os.Stderr.WriteString(fmt.Sprintf("error: missing test fixture directory\n"))
os.Exit(2)
}
var testFiles []TestFile
for _, testDirectory := range flag.Args() {
FindTests(testDirectory, &testFiles)
}
sort.Slice(testFiles, func(i int, j int) bool {
return testFiles[i].Path < testFiles[j].Path
})
threadCount := runtime.NumCPU()
queue := MakeWorkQueue(*quickLintJSExecutable, testFiles, threadCount, *stopOnFirstFailure)
for i := 0; i < queue.threadCount; i++ {
queue.wg.Add(1)
go RunWorker(queue, i)
}
queue.wg.Wait()
if *stopOnFirstFailure {
lowestFailingIndex := -1
var failure *LintResult = nil
for threadIndex, failureIndex := range queue.failureIndexes {
if failureIndex != -1 && (lowestFailingIndex == -1 || failureIndex < lowestFailingIndex) {
lowestFailingIndex = failureIndex
failure = queue.failures[threadIndex]
}
}
if failure != nil {
failure.Dump(os.Stderr)
os.Exit(1)
}
} else {
if atomic.LoadInt64(&queue.minimumFailingIndex) >= 0 {
os.Exit(1)
}
}
}
// If stopOnFirstFailure is true, then output is deterministic. This makes
// development of quick-lint-js easier.
//
// If stopOnFirstFailure is false, then output is non-deterministic. This makes
// testing faster.
type WorkQueue struct {
quickLintJSExecutable string
testFiles []TestFile
threadCount int
wg sync.WaitGroup
stopOnFirstFailure bool
// outputMutex locks dumping test failures.
//
// Used only if !stopOnFirstFailure
outputMutex sync.Mutex
// minimumFailingIndex is -1 or an index into testFiles.
//
// Any thread can *atomically* read from or CAS-write to
// minimumFailingIndex.
minimumFailingIndex int64
// failureIndexes is keyed by thread index. Each value is an index into
// testFiles, or is -1.
//
// Used only if stopOnFirstFailure.
//
// Each thread can non-atomically read and write its own entry in the
// failureIndexes slice. The main thread can read any entry from the
// failureIndexes slice only after all goroutines have finished.
failureIndexes []int
// failures is keyed by thread index.
//
// Used only if stopOnFirstFailure.
//
// Each thread can read and write its own entry in the failures slice.
// The main thread can read any entry from the failures slice only after
// all goroutines have finished.
failures []*LintResult
}
func MakeWorkQueue(quickLintJSExecutable string, testFiles []TestFile, threadCount int, stopOnFirstFailure bool) *WorkQueue {
queue := WorkQueue{
failureIndexes: make([]int, threadCount),
failures: make([]*LintResult, threadCount),
minimumFailingIndex: -1,
quickLintJSExecutable: quickLintJSExecutable,
stopOnFirstFailure: stopOnFirstFailure,
testFiles: testFiles,
threadCount: threadCount,
}
for i := 0; i < len(queue.failureIndexes); i++ {
queue.failureIndexes[i] = -1
}
return &queue
}
func RunWorker(queue *WorkQueue, threadIndex int) {
defer queue.wg.Done()
for i := threadIndex; i < len(queue.testFiles); i += queue.threadCount {
if queue.stopOnFirstFailure && queue.HaveEarlierFailure(i) {
break
}
testFile := queue.testFiles[i]
// TODO(strager): Always ignore warnings.
ignoreWarnings := !testFile.Expectations.EarlyError
result := RunQuickLintJS(queue.quickLintJSExecutable, testFile.Path, ignoreWarnings)
if result.Crashed() ||
(testFile.Expectations.IsTest && !testFile.Expectations.EarlyError && !result.ExitedWithCode(0)) ||
(testFile.Expectations.IsTest && testFile.Expectations.EarlyError && result.ExitedWithCode(0)) {
queue.RecordFailure(threadIndex, i, &result)
if queue.stopOnFirstFailure {
break
}
}
}
}
func (queue *WorkQueue) HaveEarlierFailure(index int) bool {
minimumFailingIndex := atomic.LoadInt64(&queue.minimumFailingIndex)
return minimumFailingIndex != -1 && int(minimumFailingIndex) < index
}
func (queue *WorkQueue) RecordFailure(threadIndex int, index int, result *LintResult) {
queue.failures[threadIndex] = result
queue.failureIndexes[threadIndex] = index
oldMinimumFailingIndex := atomic.LoadInt64(&queue.minimumFailingIndex)
for index < int(oldMinimumFailingIndex) {
if atomic.CompareAndSwapInt64(
&queue.minimumFailingIndex,
oldMinimumFailingIndex,
int64(index),
) {
break
}
oldMinimumFailingIndex = atomic.LoadInt64(&queue.minimumFailingIndex)
}
if !queue.stopOnFirstFailure {
queue.outputMutex.Lock()
defer queue.outputMutex.Unlock()
result.Dump(os.Stderr)
}
}
func FindTests(test262FixtureDirectory string, testFiles *[]TestFile) {
err := filepath.Walk(test262FixtureDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".js") {
testExpectations := ReadTestExpectations(testTodo, path)
if !(testExpectations.IsTodoPath || testExpectations.NeedsTodoFeatures) {
*testFiles = append(*testFiles, TestFile{
Path: path,
Expectations: testExpectations,
})
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
type TestTodo struct {
TodoFeatures [][]byte
TodoPaths []string
}
type TestFile struct {
Path string
Expectations TestExpectations
}
type TestExpectations struct {
EarlyError bool
IsTest bool
IsTodoPath bool
NeedsTodoFeatures bool
}
func ReadTestExpectations(testTodo TestTodo, path string) TestExpectations {
source, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
return ParseTestExpectations(testTodo, source, path)
}
func ParseTestExpectations(testTodo TestTodo, source []byte, path string) TestExpectations {
return TestExpectations{
EarlyError: bytes.Contains(source, []byte("phase: parse")),
IsTest: bytes.Contains(source, []byte("/*---")),
IsTodoPath: pathMatchesAnyPattern(path, testTodo.TodoPaths),
NeedsTodoFeatures: testSourceRequiresFeatures(source, testTodo.TodoFeatures),
}
}
func pathMatchesAnyPattern(path string, patterns []string) bool {
for _, pattern := range patterns {
if MatchPath(pattern, path) {
return true
}
}
return false
}
func testSourceRequiresFeatures(source []byte, features [][]byte) bool {
for _, feature := range features {
if bytes.Contains(source, feature) {
return true
}
}
return false
}
func RunQuickLintJS(quickLintJSExecutable string, jsFile string, ignoreWarnings bool) LintResult {
command := []string{quickLintJSExecutable}
if ignoreWarnings {
command = append(command, "--exit-fail-on=-E0002,-E0003,-E0057,-E0058,-E0059,-E0077,-E0086,-E0179,-E0185,-E0196,-E0212")
}
command = append(command, "--", jsFile)
process := exec.Command(command[0], command[1:]...)
var output bytes.Buffer
process.Stdout = &output
process.Stderr = &output
if err := process.Start(); err != nil {
log.Fatal(err)
}
// TODO(strager): Time out after 10 seconds.
err := process.Wait()
var exitStatus *exec.ExitError
ok := false
if err == nil {
exitStatus = nil
} else if exitStatus, ok = err.(*exec.ExitError); ok {
} else {
log.Fatal(err)
}
return LintResult{
command: command,
exitStatus: exitStatus,
jsFile: jsFile,
output: output,
}
}
type LintResult struct {
command []string
exitStatus *exec.ExitError
jsFile string
output bytes.Buffer
}
func (result *LintResult) Crashed() bool {
return !(result.exitStatus == nil || result.exitStatus.ExitCode() == 1)
}
func (result *LintResult) ExitedWithCode(exitCode int) bool {
if result.exitStatus == nil {
return exitCode == 0
} else {
return result.exitStatus.ExitCode() == exitCode
}
}
func (result *LintResult) UserRunnableCommand() string {
// TODO(strager): Escape components.
return strings.Join(result.command, " ")
}
func (result *LintResult) Dump(out *os.File) {
var failureDescription string
if result.Crashed() {
failureDescription = "command crashed"
} else {
failureDescription = "test failed"
}
_, _ = out.WriteString(fmt.Sprintf(
"error: %s: %s\n",
failureDescription,
result.UserRunnableCommand(),
))
_, _ = out.Write(result.output.Bytes())
_, _ = out.WriteString(fmt.Sprintf("\nContents of %s:\n", result.jsFile))
file, err := os.Open(result.jsFile)
if err != nil {
log.Fatal(err)
}
if _, err = io.Copy(out, file); err != nil {
log.Fatal(err)
}
}
func MatchPath(pattern string, path string) bool {
patternParts := SplitPathComponents(pattern)
pathParts := SplitPathComponents(path)
if len(patternParts) > len(pathParts) {
return false
}
pathParts = pathParts[len(pathParts)-len(patternParts):]
for i := 0; i < len(patternParts); i++ {
matched, err := filepath.Match(patternParts[i], pathParts[i])
if err != nil {
log.Fatalf("error: invalid pattern: %#v: %v", pattern, err)
}
if !matched {
return false
}
}
return true
}
func SplitPathComponents(path string) []string {
var components []string
for path != "" {
parent, file := filepath.Split(path)
components = append(components, file)
path = strings.TrimRight(parent, "\\/")
}
ReverseStringSlice(components)
return components
}
func ReverseStringSlice(items []string) {
middle := len(items) / 2
for i := 0; i < middle; i++ {
j := len(items) - i - 1
items[i], items[j] = items[j], items[i]
}
}
// quick-lint-js finds bugs in JavaScript programs.
// Copyright (C) 2020 Matthew "strager" Glazar
//
// This file is part of quick-lint-js.
//
// quick-lint-js is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// quick-lint-js is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.