-
Notifications
You must be signed in to change notification settings - Fork 14
/
run-tests.rkt
executable file
·451 lines (362 loc) · 16.5 KB
/
run-tests.rkt
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
#!/usr/bin/env racket
#lang racket/base
;; This script is almost exactly like compiler-service, but is only meant
;; to be used to run the test suite.
(require web-server/servlet
web-server/servlet-env
scheme/runtime-path
racket/path
racket/port
scheme/match
scheme/list
racket/cmdline
"this-runtime-version.rkt"
;; profile
"find-paren-loc.rkt"
"src/compiler/mzscheme-vm/write-support.ss"
"src/compiler/mzscheme-vm/compile.ss"
"src/compiler/mzscheme-vm/private/json.ss"
"src/compiler/moby-failure.ss"
"src/compiler/pinfo.ss"
"src/collects/moby/runtime/permission-struct.ss"
"src/collects/moby/runtime/error-struct.ss"
"src/collects/moby/runtime/error-struct-to-dom.ss"
"src/collects/moby/runtime/stx.ss"
"js-runtime/src/sexp.ss"
"src/collects/moby/runtime/dom-helpers.ss")
(define-runtime-path test-htdocs "tests/test-htdocs")
(define-runtime-path misc-runtime "js-runtime/lib")
(define-runtime-path htdocs "servlet-htdocs")
(define-runtime-path compat
"js-runtime/lib/compat")
(define-runtime-path easyxdm "support/easyXDM")
;; make-port-response: (values response/incremental output-port)
;; Creates a response that's coupled to an output-port: whatever you
;; write into the output will be pushed into the response.
(define (make-port-response #:mime-type (mime-type #"application/octet-stream")
#:headers (headers '()))
(let-values ([(in out) (make-pipe)]
[(CHUNK-SIZE) 1024])
(values (response
200 #"OK"
(current-seconds)
mime-type
headers
(lambda (op)
(let loop ()
(let ([some-bytes (read-bytes CHUNK-SIZE in)])
(unless (eof-object? some-bytes)
(write-bytes some-bytes op)
(loop))))))
out)))
(define (url-matches? a-url text)
(for/or ([chunk (url-path a-url)])
(and (string? (path/param-path chunk))
(string=? (path/param-path chunk) text))))
;; has-program-text?: request -> boolean
;; returns true if the request includes program text.
(define (has-program-text? request)
(exists-binding? 'program (request-bindings request)))
;; get-program-text: request -> string
;; Returns the textual content of the program.
(define (get-program-text request)
(extract-binding/single 'program (request-bindings request)))
;; Web service consuming programs and producing bytecode.
(define (start request)
(with-handlers ([void
(lambda (exn-or-moby-error)
(define the-exn (if (exn? exn-or-moby-error)
exn-or-moby-error
(make-moby-failure
(format "~a"
(string-append
(dom-string-content
(error-struct->dom-sexp exn-or-moby-error #f))
"\n"
(Loc->string (moby-error-location exn-or-moby-error))))
(current-continuation-marks)
exn-or-moby-error)))
(cond
[(jsonp-request? request)
(handle-json-exception-response request the-exn)]
[else
(handle-exception-response request the-exn)]))])
(cond
[(url-matches? (request-uri request) "listTestPrograms")
(list-test-programs request)]
[(url-matches? (request-uri request) "getTestProgram")
(get-test-program request)]
[else
(let*-values ([(program-name)
(string->symbol
(extract-binding/single 'name (request-bindings request)))]
[(program-text)
(extract-binding/single 'program (request-bindings request))]
[(program-input-port) (open-input-string program-text)])
;; To support JSONP:
(cond [(jsonp-request? request)
(handle-json-response request program-name program-input-port)]
[else
(call-with-semaphore
lock
(lambda ()
(handle-response request program-name program-input-port)))]))])))
(define lock (make-semaphore 1))
(define (list-test-programs request)
(let-values ([(response output-port) (make-port-response #:mime-type #"text/plain")])
(define test-programs (for/list ([p (directory-list test-htdocs)]
#:when (regexp-match #px".scm$" (path->string p)))
(path->string p)))
(write-json test-programs output-port)
(close-output-port output-port)
response))
;; get-test-program: request -> response
;; Write out the content of the test file as the response.
(define (get-test-program request)
(let-values ([(response output-port)
(make-port-response #:mime-type #"text/plain")])
(define base-name
(file-name-from-path
(extract-binding/single 'name (request-bindings request))))
(define filename (build-path test-htdocs base-name))
(when (file-exists? filename)
(call-with-input-file filename
(lambda (ip)
(copy-port ip output-port))))
(close-output-port output-port)
response))
;; jsonp-request?: request -> boolean
;; Does the request look like a jsonp request?
(define (jsonp-request? request)
(exists-binding? 'callback (request-bindings request)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; jsonp stuff
;; handle-json-response: -> response
(define (handle-json-response request program-name program-input-port)
(let-values ([(response output-port) (make-port-response #:mime-type #"text/javascript")]
[(compiled-program-port) (open-output-bytes)])
(let ([pinfo (compile/port program-input-port compiled-program-port
#:name program-name
#:runtime-version THIS-RUNTIME-VERSION)])
(fprintf output-port "~a(~a);"
(extract-binding/single 'callback (request-bindings request))
(format-output (get-output-bytes compiled-program-port)
pinfo
request))
(close-output-port output-port)
response)))
;; format-output: bytes pinfo request -> bytes
;; Print out output. If in json format, output is in the form:
;; { 'bytecode' : string,
;; 'permissions' : arrayof string }
;;
;; Otherwise, just returns the bytecode.
(define (format-output output-bytecode pinfo request)
(cond
[(wants-json-output? request)
(let ([op (open-output-bytes)])
(write-json (make-hash `((bytecode . ,(bytes->string/utf-8 output-bytecode))
(permissions . ,(get-android-permissions pinfo))))
op)
(get-output-bytes op))]
[else
(format "(~a)"
;; Raw version just spits the output bytecode.
output-bytecode)]))
(define (get-android-permissions pinfo)
(apply append
(map permission->android-permissions
(pinfo-permissions pinfo))))
;; handle-json-exception-response: exn -> response
(define (handle-json-exception-response request exn)
(case (compiler-version request)
[(0)
(let-values ([(response output-port) (make-port-response #:mime-type #"text/javascript")])
(let ([payload
(format "~a(~a);\n" (extract-binding/single 'on-error (request-bindings request))
(sexp->js (exn-message exn)))])
(fprintf output-port "~a" payload)
(close-output-port output-port)
response))]
[(1)
(let-values ([(response output-port) (make-port-response #:mime-type #"text/javascript")])
(let ([payload
(format "~a(~a);\n" (extract-binding/single 'on-error (request-bindings request))
(jsexpr->json (exn->json-structured-output request exn)))])
(fprintf output-port "~a" payload)
(close-output-port output-port)
response))]))
;;paren->loc: paren -> loc
;;converts a paren to a loc
(define (paren->loc p)
(match p
[(struct paren (text type paren-type p-start p-end))
(make-Loc p-start 0 0 (- p-end p-start) "<definitions>")]))
;;paren->oppParen: paren -> string
;;takes in a paren and outputs the opposite paren as a string
(define (paren->oppParen p)
(match p
[(struct paren (text type paren-type p-start p-end))
(get-match text)]))
;;parenCheck takes as input a string, and outputs a boolean.
;;The string that is input should be either ) } or ], which will return true. Else, false.
(define (parenCheck paren)
(or (string=? paren ")")
(string=? paren "}")
(string=? paren "]")))
;; exn->structured-output: exception -> jsexpr
;; Given an exception, tries to get back a jsexpr-structured value that can be passed back to
;; the user.
;; exn->structured-output: exception -> jsexpr
;; Given an exception, tries to get back a jsexpr-structured value that can be passed back to
;; the user.
(define (exn->json-structured-output request an-exn)
(define (on-moby-failure-val failure-val)
(make-hash `(("type" . "moby-failure")
("dom-message" .
,(dom->jsexpr
(error-struct->dom-sexp failure-val #f)))
("structured-error" .
,(jsexpr->json (make-hash `(("location" . ,(loc->jsexpr (moby-error-location failure-val)))
("message" . ,(error-struct->jsexpr failure-val)))))))))
(cond
[(exn:fail:read? an-exn)
(define program (get-program-text request))
(define input-port (open-input-string program))
(define parens (paren-problem input-port)) ;;assuming that it is a paren problem
(let ([translated-srclocs
(map srcloc->Loc (exn:fail:read-srclocs an-exn))])
(on-moby-failure-val
(make-moby-error (if (empty? translated-srclocs)
;; Defensive: translated-srclocs should not be empty, but
;; if read doesn't give us any useful location to point to,
;; we'd better not die here.
(make-Loc 0 1 0 0 "")
(first translated-srclocs))
(cond [(empty? parens)
(make-moby-error-type:generic-read-error
(exn-message an-exn)
(if (empty? translated-srclocs)
empty
(rest translated-srclocs)))]
[else
(make-Message "read: expected a "
(get-match (paren-text (first parens)))
(if (parenCheck (get-match (paren-text (first parens))))
" to close "
" to open "
)
(make-ColoredPart (paren-text (first parens)) (paren->loc (first parens)))
(if (not (empty? (rest parens))) " but found a " "")
(if (not (empty? (rest parens))) (make-ColoredPart (paren-text (second parens)) (paren->loc (second parens))) "")
)]))))]
[(moby-failure? an-exn)
(on-moby-failure-val (moby-failure-val an-exn))]
[else
(exn-message an-exn)]))
;; dom->jsexpr: dom -> jsexpr
;; Translate a dom structure to one that can pass through. The dom is treated as a nested list.
(define (dom->jsexpr a-dom)
(match a-dom
[(list head-name attribs body ...)
`(,(symbol->string head-name)
,(map (lambda (k+v)
(list (symbol->string (first k+v))
(second k+v)))
attribs)
,@(map dom->jsexpr body))]
[else
a-dom]))
;; srcloc->Loc: srcloc -> jsexp
;; Converts a source location (as stored in exceptions) into one that we can
;; store in error structure values.
(define (srcloc->Loc a-srcloc)
(make-Loc (srcloc-position a-srcloc)
(srcloc-line a-srcloc)
(srcloc-column a-srcloc)
(srcloc-span a-srcloc)
(format "~a" (srcloc-source a-srcloc))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; non jsonp stuff: use with xmlhttprequest
(define (handle-response request program-name program-input-port)
(let-values ([(response output-port) (make-port-response #:mime-type #"text/plain")]
[(program-output-port) (open-output-bytes)])
(let ([pinfo (compile/port program-input-port program-output-port
#:name program-name
#:runtime-version THIS-RUNTIME-VERSION)])
(display (format-output (get-output-bytes program-output-port) pinfo request) output-port)
(close-output-port output-port)
response)))
;; wants-json-output?: request -> boolean
;; Produces true if the client wants json output.
;; json output is allowed to generate both the bytecode
;; and permissions strings
(define (wants-json-output? req)
(let ([bindings (request-bindings req)])
(and (exists-binding? 'format bindings)
(string=? (extract-binding/single 'format bindings)
"json"))))
;; handle-exception-response: exn -> response
(define (handle-exception-response request exn)
(case (compiler-version request)
[(0)
(response/full 500
#"Internal Server Error"
(current-seconds)
#"application/octet-stream"
(list)
(list (string->bytes/utf-8 (exn-message exn))))]
[(1)
(response/full 500
#"Internal Server Error"
(current-seconds)
#"application/octet-stream"
(list)
(list (string->bytes/utf-8
(jsexpr->json (exn->json-structured-output request exn)))))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;; compiler-version: request -> number
;; versions should be interpreted as
;;
;; 0: no support for structured error messages.
;; 1: support for structured error messages.
(define (compiler-version request)
(cond
[(exists-binding? 'compiler-version (request-bindings request))
(string->number (extract-binding/single 'compiler-version (request-bindings request)))]
[else
0]))
(define (Loc->string a-loc)
(format "Location: line ~a, column ~a, span ~a, offset ~a, id ~s"
(Loc-line a-loc)
(Loc-column a-loc)
(Loc-span a-loc)
(Loc-offset a-loc)
(Loc-id a-loc)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Write out a fresh copy of the support library.
(call-with-output-file (build-path htdocs "support.js")
(lambda (op)
(write-support "browser" op))
#:exists 'replace)
;; Also, write out the collections
(unless (directory-exists? (build-path htdocs "collects"))
(make-directory (build-path htdocs "collects")))
(write-collections/dir (build-path htdocs "collects"))
;;(call-with-output-file (build-path htdocs "collections.js")
;; (lambda (op)
;; (write-collections op))
;; #:exists 'replace)
(define port 8000)
(void (command-line #:program "servlet"
#:once-each
[("-p" "--port") p "Port (default 8000)" (set! port (string->number p))]))
(serve/servlet start
#:port port
#:servlet-path "/"
#:servlet-regexp #px"^/(servlets/standalone.ss|listTestPrograms|getTestProgram)"
#:extra-files-paths (list test-htdocs misc-runtime htdocs compat easyxdm)
#:launch-browser? #t
#:listen-ip #f)