From 01987c093e2cfbd46115cc58e4ff9f789efb9d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 7 Sep 2023 18:03:40 +0200 Subject: Enable let-env to unset variables. --- doc/ref/guile/util.texi | 3 +++ module/hnh/util/env.scm | 6 +++++- tests/test/let-env.scm | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/ref/guile/util.texi b/doc/ref/guile/util.texi index 72463407..7536a9bc 100644 --- a/doc/ref/guile/util.texi +++ b/doc/ref/guile/util.texi @@ -315,6 +315,9 @@ Converts @var{any} to a string, as per @var{display}. @defmac let-env bindings body ... Similar to @var{let}, but sets environment variables for the code in body. Restores the old values once we leave. + +A variable can also be removed from the environment, by setting its +value to @code{#f}. @end defmac @defmac catch* thunk (symbol proc) ... diff --git a/module/hnh/util/env.scm b/module/hnh/util/env.scm index 18ec0543..bb42d966 100644 --- a/module/hnh/util/env.scm +++ b/module/hnh/util/env.scm @@ -14,7 +14,11 @@ (list n new-value (getenv n))) (list (symbol->string (quote name)) ...) (list value ...))) - (for-each (lambda (pair) (setenv (car pair) (cadr pair))) + (for-each (lambda (pair) + (if (cadr pair) + (setenv (car pair) + (cadr pair)) + (unsetenv (car pair)))) env-pairs)) (lambda () body ...) (lambda () diff --git a/tests/test/let-env.scm b/tests/test/let-env.scm index e3dc5927..a989776a 100644 --- a/tests/test/let-env.scm +++ b/tests/test/let-env.scm @@ -41,3 +41,8 @@ (getenv "CALP_TEST_ENV")) +(test-group "Unsetting environment" + (setenv "TEST" "A") + (let-env ((TEST #f)) + (test-assert (not (getenv "TEST")))) + (test-equal "A" (getenv "TEST"))) -- cgit v1.2.3