From 300e912747fe7d66d21f2f43af24004bf2eeed20 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Wed, 28 Aug 2024 18:30:29 -0700 Subject: [PATCH] Use 'eshell-with-handles' in a few more places * 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 | 6 ++---- lisp/eshell/em-script.el | 8 +++----- lisp/eshell/esh-cmd.el | 9 ++++----- lisp/eshell/eshell.el | 8 +++----- test/lisp/eshell/em-script-tests.el | 8 ++++++++ test/lisp/eshell/esh-io-tests.el | 6 ------ test/lisp/eshell/eshell-tests-helpers.el | 6 ++++++ test/lisp/eshell/eshell-tests.el | 6 ++++++ 8 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lisp/eshell/em-alias.el b/lisp/eshell/em-alias.el index aa6eb2d4efb..e5bf8d5fe82 100644 --- a/lisp/eshell/em-alias.el +++ b/lisp/eshell/em-alias.el @@ -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." diff --git a/lisp/eshell/em-script.el b/lisp/eshell/em-script.el index a8662c4a2b7..8cdaa994cc5 100644 --- a/lisp/eshell/em-script.el +++ b/lisp/eshell/em-script.el @@ -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) diff --git a/lisp/eshell/esh-cmd.el b/lisp/eshell/esh-cmd.el index 0a68859fc0a..09fc65522ad 100644 --- a/lisp/eshell/esh-cmd.el +++ b/lisp/eshell/esh-cmd.el @@ -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 ;; diff --git a/lisp/eshell/eshell.el b/lisp/eshell/eshell.el index 6637ff36a2c..41e0348f3cd 100644 --- a/lisp/eshell/eshell.el +++ b/lisp/eshell/eshell.el @@ -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 diff --git a/test/lisp/eshell/em-script-tests.el b/test/lisp/eshell/em-script-tests.el index 86a78e43026..b9f96fa86db 100644 --- a/test/lisp/eshell/em-script-tests.el +++ b/test/lisp/eshell/em-script-tests.el @@ -113,6 +113,14 @@ (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 diff --git a/test/lisp/eshell/esh-io-tests.el b/test/lisp/eshell/esh-io-tests.el index b4e8c0b4a9a..6add14c05fa 100644 --- a/test/lisp/eshell/esh-io-tests.el +++ b/test/lisp/eshell/esh-io-tests.el @@ -34,12 +34,6 @@ (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") diff --git a/test/lisp/eshell/eshell-tests-helpers.el b/test/lisp/eshell/eshell-tests-helpers.el index bfd829c95e9..def04be0577 100644 --- a/test/lisp/eshell/eshell-tests-helpers.el +++ b/test/lisp/eshell/eshell-tests-helpers.el @@ -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))) diff --git a/test/lisp/eshell/eshell-tests.el b/test/lisp/eshell/eshell-tests.el index f16c28cd1ae..c84af62fdbd 100644 --- a/test/lisp/eshell/eshell-tests.el +++ b/test/lisp/eshell/eshell-tests.el @@ -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 -- 2.39.5