Fix redirecting Eshell output to symbols in some places
authorJim Porter <jporterbugs@gmail.com>
Thu, 29 Aug 2024 01:53:03 +0000 (18:53 -0700)
committerJim Porter <jporterbugs@gmail.com>
Fri, 30 Aug 2024 04:51:25 +0000 (21:51 -0700)
Do not merge to master.

* lisp/eshell/esh-io.el (eshell-output-object-to-target): Don't require
TARGET to be bound.

* lisp/eshell/em-script.el (eshell-execute-file): Quote the output/error
targets.

* test/lisp/eshell/em-script-tests.el (eshell-execute-file-output): New
variable.
(em-script-test/execute-file/output-file)
(em-script-test/execute-file/output-symbol): New tests.

* test/lisp/eshell/esh-io-tests.el (eshell-test-file-string): Move to...
* test/lisp/eshell/eshell-tests-helpers.el (eshell-test-file-string):
... here.

lisp/eshell/em-script.el
lisp/eshell/esh-io.el
test/lisp/eshell/em-script-tests.el
test/lisp/eshell/esh-io-tests.el
test/lisp/eshell/eshell-tests-helpers.el

index ba020d2eb5b9d800fb78c22e4673d5d4335e27b0..ebba0440d68bccd7c22041311388dfcebefd54a6 100644 (file)
@@ -119,7 +119,7 @@ Comments begin with `#'."
       (eshell-mode)
       (eshell-do-eval
        `(let ((eshell-current-handles
-               (eshell-create-handles ,stdout 'insert))
+               (eshell-create-handles ',stdout 'insert))
               (eshell-current-subjob-p))
           ,(eshell--source-file file args))
        t))))
index 9de9cc4509acad278aec9ce1cdd2da769a015aeb..570ace2ebb8260e1ac286fc9320297381df36936 100644 (file)
@@ -713,7 +713,7 @@ Returns what was actually sent, or nil if nothing was sent.")
 
 (cl-defmethod eshell-output-object-to-target (object (target symbol))
   "Output OBJECT to the value of the symbol TARGET."
-  (if (not (symbol-value target))
+  (if (not (and (boundp target) (symbol-value target)))
       (set target object)
     (setq object (eshell-stringify object))
     (if (not (stringp (symbol-value target)))
index 86a78e4302638b9c7bbc1b544f00b0fa106fae38..5e5eb80f215db953ddb78e3807cfd8188f4b2f45 100644 (file)
@@ -33,6 +33,9 @@
          (expand-file-name "eshell-tests-helpers"
                            (file-name-directory (or load-file-name
                                                     default-directory))))
+
+(defvar eshell-execute-file-output)
+
 ;;; Tests:
 
 (ert-deftest em-script-test/source-script ()
         (eshell-execute-file temp-file '(1 2 3) t))
       (should (equal (buffer-string) "6")))))
 
+(ert-deftest em-script-test/execute-file/output-file ()
+  "Test `eshell-execute-file' redirecting to a file."
+  (ert-with-temp-file temp-file :text "echo more"
+    (ert-with-temp-file output-file :text "initial"
+      (with-temp-eshell-settings
+        (eshell-execute-file temp-file nil output-file))
+      (should (equal (eshell-test-file-string output-file) "moreinitial")))))
+
+(ert-deftest em-script-test/execute-file/output-symbol ()
+  "Test `eshell-execute-file' redirecting to a symbol."
+  (ert-with-temp-file temp-file :text "echo hi\necho bye"
+    (with-temp-eshell-settings
+      (eshell-execute-file temp-file nil 'eshell-execute-file-output))
+    (should (equal eshell-execute-file-output "hibye"))))
+
 (ert-deftest em-script-test/batch-file ()
   "Test running an Eshell script file as a batch script."
   (ert-with-temp-file temp-file
index b4e8c0b4a9aac97de4ebba84cf9615ec7bd43d92..6add14c05fa73c4c70857846f0d8d7388528d1e1 100644 (file)
 (defvar eshell-test-value-with-fun nil)
 (defun eshell-test-value-with-fun ())
 
-(defun eshell-test-file-string (file)
-  "Return the contents of FILE as a string."
-  (with-temp-buffer
-    (insert-file-contents file)
-    (buffer-string)))
-
 (defun eshell/test-output ()
   "Write some test output separately to stdout and stderr."
   (eshell-printn "stdout")
index bfd829c95e9640e9470973934456ae1b0a7b4266..def04be0577e9e8aacd144617520cb2f1f974696 100644 (file)
@@ -139,6 +139,12 @@ After inserting, call FUNC.  If FUNC is nil, instead call
   (buffer-substring-no-properties
    (eshell-beginning-of-output) (eshell-end-of-output)))
 
+(defun eshell-test-file-string (file)
+  "Return the contents of FILE as a string."
+  (with-temp-buffer
+    (insert-file-contents file)
+    (buffer-string)))
+
 (defun eshell-match-output (regexp)
   "Test whether the output of the last command matches REGEXP."
   (string-match-p regexp (eshell-last-output)))