Test Utilities

_Test Utilities_

This collection provides one file:

 _test.ss_: utilities for unit testing


> (source-directory-of-expression e) :: syntax

Expands to an expression that evaluates to the name of the directory of the file
containing the argument expression. Library modules can splice arbitrary syntax
objects into this macro to create macros that use the directory of the *client*
module that uses the macro.

Compare with _this-expression-source-directory_ in, which always expands
to the source directory of the module in which it is applied.

> (in-this-directory e1 e2 ...) :: syntax

Evaluates its subexpressions with the value of the _current-directory_ parameter
set to the directory in which the source file of the client module resides.

> (in-new-directory dir e1 e2 ...) :: syntax

A special form that creates a new directory with the path specified by `dir' and
evaluates the subexpressions with the value of the _current-directory_ parameter
set to the new directory for the duration of the evaluation. After evaluation
terminates, either by completing normally, raising an exception, or invoking a
continuation, if the value of the `keep-new-directories?' is #f, the temporary
directory is deleted.

Temporary directories created by further invocations of `in-new-directory'
during the extent of the evaluation are *not* deleted, so that they may be
accessed during the test.

You can prevent any directories from being deleted by wrapping a top-level
instance of `in-new-directory' with

    (parameterize ([keep-new-directories #t])

> keep-new-directories? :: (parameterof boolean)

When the evaluation of an instance of the `in-new-directory' form terminates, it
checks the current value of this parameter to determine whether to delete the
temporary directory it created.

This parameter is initially set to #f, so that at the top level of any
particular test, the entire directory tree created by `in-new-directory' will be
deleted. It is set to #t for all subsequent tests nested within an initial

WARNING --------------------------------------------------------------

Note that if you capture the continuation during the extent of an
`in-new-directory' and reinvoke it after its initial evaluation has terminated,
all bets are off. In particular, the directory will have already been deleted.

EXAMPLE --------------------------------------------------------------

(in-new-directory "sandbox"
  (in-new-directory "subdir-1"
    ;; make a bunch of files ...
  (in-new-directory "subdir-2"
    ;; make a bunch more files ...
  ... (directory-list "subdir-1") ...
  ... (directory-list "subdir-1") ...

(directory-exists? "sandbox")