This requires two things: 1) a macro for creating a new directory and evaluating subexpressions in the context of that directory, and 2) a facility for enabling and disabling the automatic deletion of temporary directories.
Another nice property of this pattern is that if I want to test my test, i.e., to inspect the files it's creating, I can temporarily disable the automatic deletion of the sandbox directory by wrapping the whole thing in a(define keep-new-directories? (make-parameter #f))
(define-syntax in-new-directory
(syntax-rules ()
[(_ dir-e e1 e2 ...)
(let ([dir dir-e])
(rm-rf dir)
(make-directory dir)
(let ([result (parameterize ([current-directory dir]
[keep-new-directories? #t])
e1 e2 ...)])
(unless (keep-new-directories?)
(rm-rf dir))
result))]))
(parameterize ([keep-new-directories? #t]) ...)
.Meta-conclusion: dynamic scope is useful.
No comments:
Post a Comment