(module test mzscheme
(require (lib "file.ss"))
(define-syntax in-this-directory
(syntax-rules ()
[(_ e1 e2 ...)
(parameterize ([current-directory (this-expression-source-directory)])
e1 e2 ...)]))
(define (rm-rf path)
(when (or (file-exists? path) (directory-exists? path))
(delete-directory/files path)))
(define keep-new-directories?
(make-parameter #f (lambda (new-b)
(if (not (boolean? new-b))
(raise-type-error 'keep-new-directories? "boolean" new-b)
new-b))))
(define-syntax in-new-directory
(syntax-rules ()
[(_ dir-e e1 e2 ...)
(let ([dir dir-e])
(dynamic-wind
void
(lambda ()
(rm-rf dir)
(make-directory dir)
(parameterize ([current-directory dir])
e1 e2 ...))
(lambda ()
(unless (keep-new-directories?) (rm-rf dir)))))]))
(provide keep-new-directories? in-new-directory))