(SETF (UIOP:GETENV VAR) NIL) should unset environment variable VAR
When an environment variable is not set, then UIOP:GETENV returns NIL when trying to retrieve the value of that variable. However NIL is not a supported value for (SETF UIOP:GETENV) so that the following code does not work as expected:
(flet ((some-action (value)
(declare (ignore value)))
(ensure-environment-is-clean ()
(unless (eq nil (uiop:getenv "VAR"))
(error "The variable VAR should not be bound."))))
(let ((saved-value
(uiop:getenv "VAR")))
(ensure-environment-is-clean)
(setf (uiop:getenv "VAR") "alternative value")
(unwind-protect (some-action (uiop:getenv "VAR"))
(setf (uiop:getenv "VAR") saved-value))
(ensure-environment-is-clean)))
The expected behaviour is that the snippet of code above completes successfully but on SBCL it returns an error. I am not sure what is the behaviour of other implementations.