Repository navigation
Expand file tree
/
Copy pathtest-rendering.rkt
More file actions
313 lines (281 loc) · 14.8 KB
/
Copy pathtest-rendering.rkt
File metadata and controls
313 lines (281 loc) · 14.8 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
#lang racket/base
;; Tests for the DrDr web interface.
;; Uses synthetic data and web-server/test to exercise page rendering.
(require rackunit
racket/file
racket/list
racket/match
racket/path
racket/promise
net/url
xml/path
web-server/test
web-server/http
web-server/servlet-dispatch
(only-in web-server/dispatchers/dispatch exn:dispatcher?)
"dirstruct.rkt"
"rendering.rkt"
"status.rkt"
"scm.rkt"
"path-utils.rkt"
"cache.rkt"
"analyze.rkt"
"make-archive-lib.rkt"
"render.rkt")
;; --- Synthetic data setup ---
(define test-file-path "cs/pkgs/racket-test/tests/example.rkt")
(define (make-test-revision rev
#:status [status-type 'success]
#:duration-ms [dur-ms 5000]
#:changed? [changed? #f]
#:author [author "tester"])
(define log-dir (revision-log-dir rev))
(define log-pth (build-path log-dir test-file-path))
(make-directory* (path-only log-pth))
;; Write commit-msg
(define short-hash (substring (string-append (make-string 40 #\a)) 0 40))
(define commit
(make-git-diff* "refs/heads/master"
short-hash
author
"2026-02-15 10:00:00 +0000"
(list (format "Test commit for rev ~a" rev))
(list "collects/tests/test.rkt")))
(write-to-file* (make-git-push rev author (list commit)) (revision-commit-msg rev))
;; Write analyzed marker
(write-to-file* (current-seconds) (build-path (revision-dir rev) "analyzed"))
;; Write log (status struct)
(define start-ms (* 1000000000.0))
(define end-ms (+ start-ms dur-ms))
(define the-log
(case status-type
[(timeout)
(make-timeout start-ms
end-ms
(list "raco" "test" test-file-path)
(list (make-stdout #"running tests...\n") (make-stderr #"timeout exceeded\n")))]
[(failure)
(make-exit start-ms
end-ms
(list "raco" "test" test-file-path)
(list (make-stdout #"running tests...\n") (make-stderr #"FAILURE: test failed\n"))
1)]
[(stderr)
(make-exit start-ms
end-ms
(list "raco" "test" test-file-path)
(list (make-stdout #"running tests...\n") (make-stderr #"warning: something\n"))
0)]
[else
(make-exit start-ms
end-ms
(list "raco" "test" test-file-path)
(list (make-stdout #"running tests...\nall tests passed\n"))
0)]))
(write-to-file* the-log log-pth)
;; Run analysis to create all analyze cache files
(parameterize ([current-rev rev])
(analyze-logs rev)))
(define (call-with-test-data thunk)
(define test-dir (make-temporary-file "drdr-test-~a" 'directory))
(dynamic-wind
(lambda ()
(plt-directory test-dir)
(make-directory* (plt-build-directory))
(make-directory* (plt-future-build-directory))
(make-directory* (plt-data-directory))
;; Use 'cache mode during setup so analyze-logs can write cache files
(cache/file-mode 'cache)
(make-test-revision 70100 #:status 'success #:duration-ms 3200 #:author "alice")
(make-test-revision 70101 #:status 'success #:duration-ms 3400 #:changed? #t #:author "bob")
(make-test-revision 70102 #:status 'failure #:duration-ms 4100 #:changed? #t #:author "alice")
(make-test-revision 70103 #:status 'timeout #:duration-ms 90000 #:changed? #t #:author "bob")
;; Switch to 'no-cache for tests, matching the web server's mode
(cache/file-mode 'no-cache))
thunk
(lambda () (delete-directory/files test-dir #:must-exist? #f))))
;; --- Test helpers ---
(define (test-request/url url-str)
(make-request #"GET"
(string->url url-str)
empty
(delay
empty)
#f
"127.0.0.1"
80
"127.0.0.1"))
(define (dispatch-request url-str)
(parameterize ([drdr-start-request (current-inexact-milliseconds)])
(top-dispatch (test-request/url url-str))))
(define (response-body resp)
(define out (open-output-bytes))
((response-output resp) out)
(bytes->string/utf-8 (get-output-bytes out)))
;; --- Tests ---
(define render-tests
(test-suite "DrDr web rendering"
(test-case "front page lists revisions"
(call-with-test-data (lambda ()
(define resp (dispatch-request "http://localhost/"))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"70103" body)
(check-regexp-match #rx"70102" body)
(check-regexp-match #rx"70101" body)
(check-regexp-match #rx"70100" body)
(check-regexp-match #rx"alice" body)
(check-regexp-match #rx"bob" body))))
(test-case "revision page shows directory listing"
(call-with-test-data (lambda ()
(define resp (dispatch-request "http://localhost/70101/"))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"bob" body)
(check-regexp-match #rx"Test commit for rev 70101" body))))
(test-case "file result page shows test output"
(call-with-test-data (lambda ()
(define resp
(dispatch-request (format "http://localhost/70101/~a" test-file-path)))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"all tests passed" body)
(check-regexp-match #rx"3\\.40s" body)
(check-regexp-match #rx"Exit Code.*0" body))))
(test-case "failure file result shows stderr"
(call-with-test-data (lambda ()
(define resp
(dispatch-request (format "http://localhost/70102/~a" test-file-path)))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"FAILURE" body)
(check-regexp-match #rx"Exit Code.*1" body))))
(test-case "timeout file result shows timeout"
(call-with-test-data (lambda ()
(define resp
(dispatch-request (format "http://localhost/70103/~a" test-file-path)))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"timeout exceeded" body)
(check-regexp-match #rx"10004" body)))) ; checkmark entity for timeout
(test-case "help page renders"
(call-with-test-data (lambda ()
(define resp (dispatch-request "http://localhost/help"))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"What is DrDr" body))))
(test-case "a revision that names no build matches no rule"
(call-with-test-data
(lambda ()
(for ([url (in-list '("http://localhost/1489401258/"
"http://localhost/1489401258/pkgs/x.rkt"
"http://localhost/previous-change/1489401258/pkgs/x.rkt"))])
(check-exn exn:dispatcher? (lambda () (dispatch-request url)))))))
(test-case "nonexistent file returns not found message"
(call-with-test-data (lambda ()
(define resp (dispatch-request "http://localhost/70101/no/such/file.rkt"))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"does not exist" body))))
(test-case "file history page lists all revisions"
(call-with-test-data (lambda ()
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
(check-regexp-match #rx"File History" body)
(check-regexp-match #rx"70100" body)
(check-regexp-match #rx"70101" body)
(check-regexp-match #rx"70102" body)
(check-regexp-match #rx"70103" body))))
(test-case "file history page shows status for each revision"
(call-with-test-data (lambda ()
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"Success" body)
(check-regexp-match #rx"Failure \\+ Stderr" body)
(check-regexp-match #rx"Timeout" body))))
(test-case "file history page shows stderr status for exit-0 with stderr"
(call-with-test-data (lambda ()
(parameterize ([cache/file-mode 'cache])
(make-test-revision 70104 #:status 'stderr #:author "carol"))
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"Stderr" body))))
(test-case "file history page shows exit codes"
(call-with-test-data (lambda ()
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"Exit Code" body)
;; Success revisions show exit code 0
(check-regexp-match #rx"<td>0</td>" body)
;; Failure revision shows exit code 1
(check-regexp-match #rx"<td>1</td>" body))))
(test-case "file history page shows missing for revisions without file"
(call-with-test-data (lambda ()
;; Create a revision with no log for test-file-path
(make-directory* (revision-log-dir 99))
(make-directory* (revision-analyze-dir 99))
(write-to-file* (current-seconds)
(build-path (revision-dir 99) "analyzed"))
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"99" body)
(check-regexp-match #rx"Missing" body))))
(test-case "file history page shows pending for incomplete revisions"
(call-with-test-data (lambda ()
;; Create a revision directory with no "analyzed" marker
(make-directory* (revision-log-dir 70104))
(make-directory* (revision-analyze-dir 70104))
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"70104" body)
(check-regexp-match #rx"Pending" body)
;; Should NOT show "Missing" for rev 70104
;; (Missing should only appear for completed rev 99 if present)
)))
(test-case "file result page links to file history"
(call-with-test-data (lambda ()
(define resp
(dispatch-request (format "http://localhost/70101/~a" test-file-path)))
(define body (response-body resp))
(check-regexp-match #rx"file-history" body)
(check-regexp-match #rx"All results for this file" body))))
(test-case "file history page reads archived revisions correctly"
;; Regression test: archived revisions used to show as "Pending" because
;; show-file-history called file-exists? on the analyze and "analyzed"
;; marker files, which fails after archiving deletes them. The fix is to
;; use read-cache* which falls through to the archive.
(call-with-test-data (lambda ()
;; Archive revs 70101, 70102, 70103 (70101 is success,
;; 70102 is failure, 70103 is timeout). Rev 70100 is
;; skipped because make-archive doesn't archive
;; multiples of 100.
(make-archive 70101)
(make-archive 70102)
(make-archive 70103)
(define resp
(dispatch-request
(format "http://localhost/file-history/~a" test-file-path)))
(check-equal? (response-code resp) 200)
(define body (response-body resp))
;; Archived revisions must show their real status,
;; not "Pending".
(check-regexp-match #rx"Success" body)
(check-regexp-match #rx"Failure \\+ Stderr" body)
(check-regexp-match #rx"Timeout" body)
(check-false (regexp-match? #rx"Pending" body)))))))
(module+ test
(require rackunit/text-ui)
(run-tests render-tests))