branch: elpa/buttercup
commit e9636f8ba44dd393a79ec05ff6023910eb04c340
Author: Ola Nilsson <[email protected]>
Commit: Ola Nilsson <[email protected]>

    tests: Extend with-local-buttercup to handle envvars
    
    Use with-environment-variables to bind the NO_COLOR and GITHUB_ACTION
    variables.  Both default to nil, but can be set to other values with
    the :no-color and :github-action keys respectively.
---
 buttercup-compat.el     | 20 ++++++++++++++++++++
 tests/test-buttercup.el | 34 ++++++++++++++++++++++++----------
 2 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/buttercup-compat.el b/buttercup-compat.el
index 40ec25409b..6ff1cfe6a6 100644
--- a/buttercup-compat.el
+++ b/buttercup-compat.el
@@ -95,6 +95,26 @@ This is the time of the last change to the file's contents, 
and
 is a Lisp timestamp in the style of `current-time'."
        (nth 5 attributes)))
 
+;;;;;;;;;;;;;;;;;;;;;;
+;;; Introduced in 28.1
+
+(unless (fboundp 'with-environment-variables)
+  (defmacro with-environment-variables (variables &rest body)
+    "Set VARIABLES in the environment and execute BODY.
+VARIABLES is a list of variable settings of the form (VAR VALUE),
+where VAR is the name of the variable (a string) and VALUE
+is its value (also a string).
+
+The previous values will be restored upon exit."
+    (declare (indent 1) (debug (sexp body)))
+    (unless (consp variables)
+      (error "Invalid VARIABLES: %s" variables))
+    `(let ((process-environment (copy-sequence process-environment)))
+       ,@(mapcar (lambda (elem)
+                   `(setenv ,(car elem) ,(cadr elem)))
+                 variables)
+       ,@body)))
+
 ;;;;;;;;;;;;;;;;;;;;;;
 ;;; Introduced in 29.1
 
diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index a805870689..1509835157 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -36,23 +36,33 @@
 (defmacro with-local-buttercup (&rest body)
   "Execute BODY with local buttercup state variables.
 Keyword arguments kan be used to override the values of certain
-variables:
- :color       -> `buttercup-color'
- :frame-style -> `buttercup-stack-frame-style'
- :reporter    -> `buttercup-reporter'
- :suites      -> `buttercup-suites'
- :quiet       -> `buttercup-reporter-batch-quiet-statuses'
-\n(fn &keys COLOR SUITES REPORTER &rest BODY)"
+variables or environment variables while executing BODY:
+ :color         -> `buttercup-color'
+ :frame-style   -> `buttercup-stack-frame-style'
+ :quiet         -> `buttercup-reporter-batch-quiet-statuses'
+ :reporter      -> `buttercup-reporter'
+ :suites        -> `buttercup-suites'
+ :no-color      -> `NO_COLOR'
+ :github-action -> `GITHUB_ACTION'
+\n(fn &keys COLOR FRAME-STYLE QUIET REPORTER SUITES
+          NO-COLOR GITHUB-ACTION
+    &rest BODY)"
   (declare (debug t) (indent defun))
   ;; extract keyword arguments
   (let ((keys '(:color buttercup-color
                        :frame-style buttercup-stack-frame-style
                        :reporter buttercup-reporter
                        :suites buttercup-suites
-                       :quiet buttercup-reporter-batch-quiet-statuses))
+                       :quiet buttercup-reporter-batch-quiet-statuses
+                       :no-color "NO_COLOR"
+                       :github-action "GITHUB_ACTION"))
+        env-vars
         extra-vars)
     (while (plist-member keys (car body))
-      (push (list (plist-get keys (pop body)) (pop body)) extra-vars))
+      (let* ((key (pop body))
+             (var (plist-get keys key)))
+        (push (list var (pop body))
+              (if (symbolp var) extra-vars env-vars))))
   `(let (buttercup--after-each
          buttercup--before-each
          (buttercup--cleanup-functions :invalid)
@@ -66,7 +76,11 @@ variables:
          (buttercup-stack-frame-style 'crop)
          (buttercup-warning-buffer-name " *ignored buttercup warnings*")
          ,@(nreverse extra-vars))
-     ,@body)))
+     (with-environment-variables (("NO_COLOR" nil)
+                                  ("GITHUB_ACTION" nil)
+                                  ,@(nreverse env-vars)
+                                  )
+       ,@body))))
 
 (defmacro buttercup--test-with-tempdir (files &rest body)
   "Create FILES and execute BODY in a temporary directory.

Reply via email to