]> git.eshelyaron.com Git - emacs.git/commitdiff
Use 'eshell-with-handles' in a few more places
authorJim Porter <jporterbugs@gmail.com>
Thu, 29 Aug 2024 01:30:29 +0000 (18:30 -0700)
committerEshel Yaron <me@eshelyaron.com>
Wed, 4 Sep 2024 07:51:42 +0000 (09:51 +0200)
* lisp/eshell/em-alias.el (eshell-write-aliases-list):
* lisp/eshell/em-script.el (eshell-batch-file):
* lisp/eshell/esh-cmd.el (eshell-command-to-value):
* lisp/eshell/eshell.el (eshell-command): Use 'eshell-with-handles'.

* 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.

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

(cherry picked from commit 5c68545a936ab42df90c8498ca77207a5e6aff1f)

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

index aa6eb2d4efbe398383ca5ae9030fec6169bb9fa0..e5bf8d5fe823d8f08e948310dabcdfc4ed31c99a 100644 (file)
@@ -208,11 +208,9 @@ This is useful after manually editing the contents of the file."
   "Write out the current aliases into `eshell-aliases-file'."
   (when (and eshell-aliases-file
              (file-writable-p (file-name-directory eshell-aliases-file)))
-    (let ((eshell-current-handles
-           (eshell-create-handles eshell-aliases-file 'overwrite)))
+    (eshell-with-handles (eshell-aliases-file 'overwrite)
       (eshell/alias)
-      (eshell-set-exit-info 0 nil)
-      (eshell-close-handles))))
+      (eshell-set-exit-info 0 nil))))
 
 (defsubst eshell-lookup-alias (name)
   "Check whether NAME is aliased.  Return the alias if there is one."
index a8662c4a2b79a0349dc8b1014946899ba59fe812..8cdaa994cc56b25fc2c1935042015788d231fc44 100644 (file)
@@ -175,11 +175,9 @@ top in order to make it into an executable script:
     (with-temp-buffer
       (eshell-mode)
       (eshell-do-eval
-       `(let ((eshell-current-handles
-               (eshell-create-handles "/dev/stdout" 'append
-                                      "/dev/stderr" 'append))
-              (eshell-current-subjob-p))
-          ,(eshell--source-file file args))
+       `(eshell-with-handles ("/dev/stdout" 'append "/dev/stderr" 'append)
+          (let ((eshell-current-subjob-p))
+            ,(eshell--source-file file args)))
        t))))
 
 (defun eshell/source (file &rest args)
index 0a68859fc0a71a8da4fd6d0cb8cc7c6f93c0cd0d..09fc65522ade15d4bb7220723485c8db3d4d0eea 100644 (file)
@@ -901,11 +901,10 @@ This avoids the need to use `let*'."
 (defmacro eshell-command-to-value (command)
   "Run an Eshell COMMAND synchronously, returning its output."
   (let ((value (make-symbol "eshell-temp")))
-    `(let ((eshell-in-pipeline-p nil)
-           (eshell-current-handles
-           (eshell-create-handles ',value 'overwrite)))
-       ,command
-       ,value)))
+    `(eshell-with-handles (',value 'overwrite)
+       (let ((eshell-in-pipeline-p nil))
+         ,command
+         ,value))))
 
 ;;;_* Iterative evaluation
 ;;
index 6637ff36a2c61b3567213015fa0217c450379b27..41e0348f3cdf8fdf7224f23387b50911777a3b8b 100644 (file)
@@ -354,11 +354,9 @@ buffer is already taken by another running shell command."
          (eshell-non-interactive-p t))
       (eshell-mode)
       (let* ((proc (eshell-eval-command
-                    `(let ((eshell-current-handles
-                            (eshell-create-handles ,stdout 'insert
-                                                   ,stderr 'insert))
-                           (eshell-current-subjob-p))
-                      ,(eshell-parse-command command))
+                    `(eshell-with-handles (,stdout 'insert ,stderr 'insert)
+                       (let ((eshell-current-subjob-p))
+                        ,(eshell-parse-command command)))
                     command))
              (async (eq (car-safe proc) :eshell-background))
              (bufname (cond
index 86a78e4302638b9c7bbc1b544f00b0fa106fae38..b9f96fa86db135dc82a975c77c6cfc658748fd23 100644 (file)
         (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/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)))
index f16c28cd1ae66a27c6652d7dcd7990221da9d92d..c84af62fdbd181248fad16b5f6e9bf0f55bad124 100644 (file)
@@ -138,6 +138,12 @@ This test uses a pipeline for the command."
         (forward-line)
         (should (looking-at "bye\n"))))))
 
+(ert-deftest eshell-test/eshell-command/output-file ()
+  "Test that `eshell-command' can write to a file."
+  (ert-with-temp-file temp-file :text "initial"
+    (eshell-command "echo more" temp-file)
+    (should (equal (eshell-test-file-string temp-file) "moreinitial"))))
+
 (ert-deftest eshell-test/command-running-p ()
   "Modeline should show no command running"
   (with-temp-eshell