aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-09-07 18:03:40 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-09-11 18:00:15 +0200
commit01987c093e2cfbd46115cc58e4ff9f789efb9d0d (patch)
tree841382c97a62e9108dcabb0e5c4e27c5ab459d19
parentFixes to xdg basedir. (diff)
downloadcalp-01987c093e2cfbd46115cc58e4ff9f789efb9d0d.tar.gz
calp-01987c093e2cfbd46115cc58e4ff9f789efb9d0d.tar.xz
Enable let-env to unset variables.
-rw-r--r--doc/ref/guile/util.texi3
-rw-r--r--module/hnh/util/env.scm6
-rw-r--r--tests/test/let-env.scm5
3 files changed, 13 insertions, 1 deletions
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")))