|
14 | 14 | (require (for-syntax racket/base |
15 | 15 | racket/match |
16 | 16 | resyntax/test/private/statement) |
| 17 | + racket/runtime-path |
| 18 | + racket/splicing |
17 | 19 | rackunit |
18 | 20 | rebellion/base/comparator |
19 | 21 | rebellion/base/range |
|
227 | 229 | (define-syntax (resyntax-test-module-begin stx) |
228 | 230 | (syntax-parse stx |
229 | 231 | [(_ body ...) |
230 | | - (define has-require? (has-require-statements? (attribute body))) |
231 | | - (if has-require? |
232 | | - #`(racket-module-begin (module+ test body ...)) |
233 | | - #`(racket-module-begin |
234 | | - (module+ test |
235 | | - (add-suite-under-test! default-recommendations) |
236 | | - body ...)))])) |
| 232 | + #:do [(define has-require? (has-require-statements? (attribute body))) |
| 233 | + (define has-src-path? (and (syntax-source stx) (path? (syntax-source stx))))] |
| 234 | + #:attr default-require (and (not has-require?) #'(add-suite-under-test! default-recommendations)) |
| 235 | + #:attr stx-path (and has-src-path? #`'#,(build-path (syntax-source stx) 'up)) |
| 236 | + #'(racket-module-begin |
| 237 | + (module+ test |
| 238 | + ;; We always clear the suites under test first in case this test file is executing in the |
| 239 | + ;; same (dynamic) module namespace as another test file. If we didn't do this, because the |
| 240 | + ;; suites under test are stored in a global parameter, then a test runner that reuses |
| 241 | + ;; namespaces across files might accidentally run extra refactoring rules that the test file |
| 242 | + ;; didn't specify. The `raco cover` tool is one such test runner: without this reset, |
| 243 | + ;; coverage reporting with `raco cover` stops working. |
| 244 | + (clear-suites-under-test!) |
| 245 | + ;; Similarly, we also clear the test header global parameter. |
| 246 | + (clear-header!) |
| 247 | + (~? (~@ (define-runtime-path here stx-path) |
| 248 | + ;; We also force the current directory to the module's source location's directory. |
| 249 | + ;; Cover doesn't do this automatically like raco test does for some reason, and not |
| 250 | + ;; doing it breaks tests that use relative path imports inside refactored test code. |
| 251 | + (splicing-parameterize ([current-directory here]) |
| 252 | + (~? default-require) |
| 253 | + body ...)) |
| 254 | + (~@ (~? default-require) body ...))))])) |
237 | 255 |
|
238 | 256 |
|
239 | 257 | ;@---------------------------------------------------------------------------------------------------- |
|
0 commit comments