emacs/test/lisp/eshell/eshell-tests.el

220 lines
8.3 KiB
EmacsLisp
Raw Normal View History

;;; eshell-tests.el --- Eshell test suite -*- lexical-binding:t -*-
2000-06-23 05:24:10 +00:00
2024-01-02 09:47:10 +08:00
;; Copyright (C) 1999-2024 Free Software Foundation, Inc.
2000-06-23 05:24:10 +00:00
2000-10-16 12:27:09 +00:00
;; Author: John Wiegley <johnw@gnu.org>
2000-06-23 05:24:10 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
2000-06-23 05:24:10 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2000-06-23 05:24:10 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
2000-06-23 05:24:10 +00:00
;;; Commentary:
2013-05-26 19:54:01 +12:00
;; Eshell test suite.
2000-06-23 05:24:10 +00:00
;;; Code:
2013-05-26 19:54:01 +12:00
(require 'ert)
2021-11-08 01:21:06 +01:00
(require 'ert-x)
(require 'esh-mode)
2013-05-26 19:54:01 +12:00
(require 'eshell)
(require 'eshell-tests-helpers
(expand-file-name "eshell-tests-helpers"
(file-name-directory (or load-file-name
default-directory))))
(defvar eshell-test-value nil)
2013-05-26 19:54:01 +12:00
;;; Tests:
(ert-deftest eshell-test/eshell-command/simple ()
"Test that the `eshell-command' function writes to the current buffer."
(skip-unless (executable-find "echo"))
(ert-with-temp-directory eshell-directory-name
(let ((eshell-history-file-name nil))
(with-temp-buffer
(eshell-command "*echo hi" t)
(should (equal (buffer-string) "hi\n"))))))
(ert-deftest eshell-test/eshell-command/pipeline ()
"Test that the `eshell-command' function writes to the current buffer.
This test uses a pipeline for the command."
(skip-unless (and (executable-find "echo")
(executable-find "cat")))
(ert-with-temp-directory eshell-directory-name
(let ((eshell-history-file-name nil))
(with-temp-buffer
(eshell-command "*echo hi | *cat" t)
(should (equal (buffer-string) "hi\n"))))))
Support Eshell iterative evaluation in the background This really just generalizes Eshell's previous support for iterative evaluation of a single current command to a list of multiple commands, of which at most one can be in the foreground (bug#66066). * lisp/eshell/esh-cmd.el (eshell-last-async-procs) (eshell-current-command): Make obsolete in favor of... (eshell-foreground-command): ... this (eshell-background-commands): New variable. (eshell-interactive-process-p): Make obsolete. (eshell-head-process, eshell-tail-process): Use 'eshell-foreground-command'. (eshell-cmd-initialize): Initialize new variables. (eshell-add-command, eshell-remove-command) (eshell-commands-for-process): New functions. (eshell-parse-command): Make 'eshell-do-subjob' the outermost call. (eshell-do-subjob): Call 'eshell-resume-eval' to split this command off from its parent forms. (eshell-eval-command): Use 'eshell-add-command'. (eshell-resume-command): Use 'eshell-commands-for-process'. (eshell-resume-eval): Take a COMMAND argument. Return ':eshell-background' form for deferred background commands. (eshell-do-eval): Remove check for 'eshell-current-subjob-p'. This is handled differently now. * lisp/eshell/eshell.el (eshell-command): Wait for all processes to exit when running synchronously. * lisp/eshell/esh-mode.el (eshell-intercept-commands) (eshell-watch-for-password-prompt): * lisp/eshell/em-cmpl.el (eshell-complete-parse-arguments): * lisp/eshell/em-smart.el (eshell-smart-display-move): Use 'eshell-foreground-command'. * test/lisp/eshell/esh-cmd-tests.el (esh-cmd-test/background/simple-command) (esh-cmd-test/background/subcommand): New tests. (esh-cmd-test/throw): Use 'eshell-foreground-command'. * test/lisp/eshell/eshell-tests.el (eshell-test/queue-input): Use 'eshell-foreground-command'. * test/lisp/eshell/em-script-tests.el (em-script-test/source-script/background): Make the test script more complex. * test/lisp/eshell/eshell-tests.el (eshell-test/eshell-command/pipeline-wait): New test. * doc/misc/eshell.texi (Bugs and ideas): Remove implemented feature.
2023-09-23 11:36:11 -07:00
(ert-deftest eshell-test/eshell-command/pipeline-wait ()
"Check that `eshell-command' waits for all its processes before returning."
(skip-unless (and (executable-find "echo")
(executable-find "sh")
(executable-find "rev")))
(ert-with-temp-directory eshell-directory-name
(let ((eshell-history-file-name nil))
(with-temp-buffer
(eshell-command
"*echo hello | sh -c 'sleep 1; rev' 1>&2 | *echo goodbye" t)
(should (equal (buffer-string) "goodbye\nolleh\n"))))))
(ert-deftest eshell-test/eshell-command/background ()
"Test that `eshell-command' works for background commands."
(skip-unless (executable-find "echo"))
(ert-with-temp-directory eshell-directory-name
(let ((orig-processes (process-list))
(eshell-history-file-name nil))
(with-temp-buffer
(eshell-command "*echo hi &" t)
(eshell-wait-for (lambda () (equal (process-list) orig-processes)))
(should (equal (buffer-string) "hi\n"))))))
(ert-deftest eshell-test/eshell-command/background-pipeline ()
"Test that `eshell-command' works for background commands.
This test uses a pipeline for the command."
(skip-unless (and (executable-find "echo")
(executable-find "cat")))
(ert-with-temp-directory eshell-directory-name
(let ((orig-processes (process-list))
(eshell-history-file-name nil))
(with-temp-buffer
(eshell-command "*echo hi | *cat &" t)
(eshell-wait-for (lambda () (equal (process-list) orig-processes)))
(should (equal (buffer-string) "hi\n"))))))
(ert-deftest eshell-test/eshell-command/output-buffer/sync ()
"Test that the `eshell-command' function writes to its output buffer."
(skip-unless (executable-find "echo"))
(ert-with-temp-directory eshell-directory-name
(let ((eshell-history-file-name nil))
(eshell-command "*echo 'hi\nbye'")
(with-current-buffer "*Eshell Command Output*"
(should (equal (buffer-string) "hi\nbye")))
(kill-buffer "*Eshell Command Output*"))))
(ert-deftest eshell-test/eshell-command/output-buffer/async ()
"Test that the `eshell-command' function writes to its async output buffer."
(skip-unless (executable-find "echo"))
(ert-with-temp-directory eshell-directory-name
(let ((orig-processes (process-list))
(eshell-history-file-name nil))
(eshell-command "*echo hi &")
(eshell-wait-for (lambda () (equal (process-list) orig-processes)))
(with-current-buffer "*Eshell Async Command Output*"
(goto-char (point-min))
(forward-line)
(should (looking-at "hi\n"))))))
2013-05-26 19:54:01 +12:00
(ert-deftest eshell-test/command-running-p ()
"Modeline should show no command running"
(with-temp-eshell
(let ((eshell-status-in-mode-line t))
(should (memq 'eshell-command-running-string mode-line-format))
(should (equal eshell-command-running-string "--")))))
(ert-deftest eshell-test/forward-arg ()
"Test moving across command arguments"
(with-temp-eshell
(eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
(let ((end (point)) begin)
(beginning-of-line)
2013-05-26 19:54:01 +12:00
(setq begin (point))
(eshell-forward-argument 4)
(should (= end (point)))
2013-05-26 19:54:01 +12:00
(eshell-backward-argument 4)
(should (= begin (point))))))
2013-05-26 19:54:01 +12:00
(ert-deftest eshell-test/queue-input ()
Fix reference-counting of Eshell I/O handles This ensures that output targets in Eshell are only closed when Eshell is actually done with them. In particular, this means that "{ echo foo; echo bar } | rev" prints "raboof" as expected (bug#59545). * lisp/eshell/esh-io.el (eshell-create-handles): Structure the handles differently so the targets and their ref-count can be shared. (eshell-duplicate-handles): Reimplement this to share targets between the original and new handle sets. Add STEAL-P argument. (eshell-protect-handles, eshell-copy-output-handle) (eshell-interactive-output-p, eshell-output-object): Account for changes to the handle structure. (eshell-close-handle): New function... (eshell-close-handles, eshell-set-output-handle): ... use it. (eshell-get-targets): Remove. This only existed to make the previous implementation of 'eshell-duplicate-handles' work. * lisp/eshell/esh-cmd.el (eshell-with-copied-handles): New argument STEAL-P. (eshell-do-pipelines): Use STEAL-P for the last item in the pipeline. (eshell-parse-command): Don't copy handles for the last command in the list; explain why we can't use STEAL-P here. (eshell-eval-command): When queuing input, set 'eshell-command-body' and 'eshell-test-body' for the 'if' conditional (see 'eshell-do-eval'). * test/lisp/eshell/esh-io-tests.el (esh-io-test/redirect-pipe): Split into... (esh-io-test/pipeline/default, esh-io-test/pipeline/all): ... these. (esh-io-test/pipeline/subcommands): New test. * test/lisp/eshell/esh-cmd-tests.el (esh-cmd-test/for-loop-pipe) (esh-cmd-test/while-loop-pipe, esh-cmd-test/if-statement-pipe) esh-cmd-test/if-else-statement-pipe): New tests. (esh-cmd-test/while-loop): Use 'pop' to simplify the test a bit. * test/lisp/eshell/eshell-test-helpers.el (eshell-test--max-subprocess-time): Rename to... (eshell-test--max-wait-time): ... this. (eshell-wait-for): New function... (eshell-wait-for-subprocess): ... use it. * test/lisp/eshell/eshell-tests.el (eshell-test/queue-input): Fix this test. Previously, it didn't correctly verify that the original command completed. * test/lisp/eshell/em-tramp-tests.el (em-tramp-test/should-replace-command): New macro... (em-tramp-test/su-default, em-tramp-test/su-user) (em-tramp-test/su-login, em-tramp-test/sudo-shell) (em-tramp-test/sudo-user-shell, em-tramp-test/doas-shell) (em-tramp-test/doas-user-shell): ... use it.
2022-12-24 14:31:50 -08:00
"Test queuing command input.
This should let the current command finish, then automatically
insert the queued one at the next prompt, and finally run it."
2013-05-26 19:54:01 +12:00
(with-temp-eshell
Fix reference-counting of Eshell I/O handles This ensures that output targets in Eshell are only closed when Eshell is actually done with them. In particular, this means that "{ echo foo; echo bar } | rev" prints "raboof" as expected (bug#59545). * lisp/eshell/esh-io.el (eshell-create-handles): Structure the handles differently so the targets and their ref-count can be shared. (eshell-duplicate-handles): Reimplement this to share targets between the original and new handle sets. Add STEAL-P argument. (eshell-protect-handles, eshell-copy-output-handle) (eshell-interactive-output-p, eshell-output-object): Account for changes to the handle structure. (eshell-close-handle): New function... (eshell-close-handles, eshell-set-output-handle): ... use it. (eshell-get-targets): Remove. This only existed to make the previous implementation of 'eshell-duplicate-handles' work. * lisp/eshell/esh-cmd.el (eshell-with-copied-handles): New argument STEAL-P. (eshell-do-pipelines): Use STEAL-P for the last item in the pipeline. (eshell-parse-command): Don't copy handles for the last command in the list; explain why we can't use STEAL-P here. (eshell-eval-command): When queuing input, set 'eshell-command-body' and 'eshell-test-body' for the 'if' conditional (see 'eshell-do-eval'). * test/lisp/eshell/esh-io-tests.el (esh-io-test/redirect-pipe): Split into... (esh-io-test/pipeline/default, esh-io-test/pipeline/all): ... these. (esh-io-test/pipeline/subcommands): New test. * test/lisp/eshell/esh-cmd-tests.el (esh-cmd-test/for-loop-pipe) (esh-cmd-test/while-loop-pipe, esh-cmd-test/if-statement-pipe) esh-cmd-test/if-else-statement-pipe): New tests. (esh-cmd-test/while-loop): Use 'pop' to simplify the test a bit. * test/lisp/eshell/eshell-test-helpers.el (eshell-test--max-subprocess-time): Rename to... (eshell-test--max-wait-time): ... this. (eshell-wait-for): New function... (eshell-wait-for-subprocess): ... use it. * test/lisp/eshell/eshell-tests.el (eshell-test/queue-input): Fix this test. Previously, it didn't correctly verify that the original command completed. * test/lisp/eshell/em-tramp-tests.el (em-tramp-test/should-replace-command): New macro... (em-tramp-test/su-default, em-tramp-test/su-user) (em-tramp-test/su-login, em-tramp-test/sudo-shell) (em-tramp-test/sudo-user-shell, em-tramp-test/doas-shell) (em-tramp-test/doas-user-shell): ... use it.
2022-12-24 14:31:50 -08:00
(eshell-insert-command "sleep 1; echo slept")
(eshell-insert-command "echo alpha" #'eshell-queue-input)
(let ((start (marker-position (eshell-beginning-of-output))))
Support Eshell iterative evaluation in the background This really just generalizes Eshell's previous support for iterative evaluation of a single current command to a list of multiple commands, of which at most one can be in the foreground (bug#66066). * lisp/eshell/esh-cmd.el (eshell-last-async-procs) (eshell-current-command): Make obsolete in favor of... (eshell-foreground-command): ... this (eshell-background-commands): New variable. (eshell-interactive-process-p): Make obsolete. (eshell-head-process, eshell-tail-process): Use 'eshell-foreground-command'. (eshell-cmd-initialize): Initialize new variables. (eshell-add-command, eshell-remove-command) (eshell-commands-for-process): New functions. (eshell-parse-command): Make 'eshell-do-subjob' the outermost call. (eshell-do-subjob): Call 'eshell-resume-eval' to split this command off from its parent forms. (eshell-eval-command): Use 'eshell-add-command'. (eshell-resume-command): Use 'eshell-commands-for-process'. (eshell-resume-eval): Take a COMMAND argument. Return ':eshell-background' form for deferred background commands. (eshell-do-eval): Remove check for 'eshell-current-subjob-p'. This is handled differently now. * lisp/eshell/eshell.el (eshell-command): Wait for all processes to exit when running synchronously. * lisp/eshell/esh-mode.el (eshell-intercept-commands) (eshell-watch-for-password-prompt): * lisp/eshell/em-cmpl.el (eshell-complete-parse-arguments): * lisp/eshell/em-smart.el (eshell-smart-display-move): Use 'eshell-foreground-command'. * test/lisp/eshell/esh-cmd-tests.el (esh-cmd-test/background/simple-command) (esh-cmd-test/background/subcommand): New tests. (esh-cmd-test/throw): Use 'eshell-foreground-command'. * test/lisp/eshell/eshell-tests.el (eshell-test/queue-input): Use 'eshell-foreground-command'. * test/lisp/eshell/em-script-tests.el (em-script-test/source-script/background): Make the test script more complex. * test/lisp/eshell/eshell-tests.el (eshell-test/eshell-command/pipeline-wait): New test. * doc/misc/eshell.texi (Bugs and ideas): Remove implemented feature.
2023-09-23 11:36:11 -07:00
(eshell-wait-for (lambda () (not eshell-foreground-command)))
Fix reference-counting of Eshell I/O handles This ensures that output targets in Eshell are only closed when Eshell is actually done with them. In particular, this means that "{ echo foo; echo bar } | rev" prints "raboof" as expected (bug#59545). * lisp/eshell/esh-io.el (eshell-create-handles): Structure the handles differently so the targets and their ref-count can be shared. (eshell-duplicate-handles): Reimplement this to share targets between the original and new handle sets. Add STEAL-P argument. (eshell-protect-handles, eshell-copy-output-handle) (eshell-interactive-output-p, eshell-output-object): Account for changes to the handle structure. (eshell-close-handle): New function... (eshell-close-handles, eshell-set-output-handle): ... use it. (eshell-get-targets): Remove. This only existed to make the previous implementation of 'eshell-duplicate-handles' work. * lisp/eshell/esh-cmd.el (eshell-with-copied-handles): New argument STEAL-P. (eshell-do-pipelines): Use STEAL-P for the last item in the pipeline. (eshell-parse-command): Don't copy handles for the last command in the list; explain why we can't use STEAL-P here. (eshell-eval-command): When queuing input, set 'eshell-command-body' and 'eshell-test-body' for the 'if' conditional (see 'eshell-do-eval'). * test/lisp/eshell/esh-io-tests.el (esh-io-test/redirect-pipe): Split into... (esh-io-test/pipeline/default, esh-io-test/pipeline/all): ... these. (esh-io-test/pipeline/subcommands): New test. * test/lisp/eshell/esh-cmd-tests.el (esh-cmd-test/for-loop-pipe) (esh-cmd-test/while-loop-pipe, esh-cmd-test/if-statement-pipe) esh-cmd-test/if-else-statement-pipe): New tests. (esh-cmd-test/while-loop): Use 'pop' to simplify the test a bit. * test/lisp/eshell/eshell-test-helpers.el (eshell-test--max-subprocess-time): Rename to... (eshell-test--max-wait-time): ... this. (eshell-wait-for): New function... (eshell-wait-for-subprocess): ... use it. * test/lisp/eshell/eshell-tests.el (eshell-test/queue-input): Fix this test. Previously, it didn't correctly verify that the original command completed. * test/lisp/eshell/em-tramp-tests.el (em-tramp-test/should-replace-command): New macro... (em-tramp-test/su-default, em-tramp-test/su-user) (em-tramp-test/su-login, em-tramp-test/sudo-shell) (em-tramp-test/sudo-user-shell, em-tramp-test/doas-shell) (em-tramp-test/doas-user-shell): ... use it.
2022-12-24 14:31:50 -08:00
(should (string-match "^slept\n.*echo alpha\nalpha\n$"
(buffer-substring-no-properties
start (eshell-end-of-output)))))))
2013-05-26 19:54:01 +12:00
(ert-deftest eshell-test/flush-output ()
"Test flushing of previous output"
(with-temp-eshell
(eshell-insert-command "echo alpha")
(eshell-delete-output)
(should (eshell-match-output
(concat "^" (regexp-quote "*** output flushed ***\n") "$")))))
2013-05-26 19:54:01 +12:00
(ert-deftest eshell-test/get-old-input ()
"Test that we can get the input of a previous command."
2013-05-26 19:54:01 +12:00
(with-temp-eshell
(eshell-insert-command "echo alpha")
(goto-char eshell-last-input-start)
(should (string= (eshell-get-old-input) "echo alpha"))
;; Make sure that `eshell-get-old-input' works even if the point is
;; inside the prompt.
(let ((inhibit-field-text-motion t))
(beginning-of-line))
(should (string= (eshell-get-old-input) "echo alpha"))))
(ert-deftest eshell-test/get-old-input/rerun-command ()
"Test that we can rerun an old command when point is on it."
(with-temp-eshell
(let ((eshell-test-value "first"))
(eshell-match-command-output "echo $eshell-test-value" "first"))
;; Go to the previous prompt.
(forward-line -2)
(let ((inhibit-field-text-motion t))
(end-of-line))
;; Rerun the command, but with a different variable value.
(let ((eshell-test-value "second"))
(eshell-send-input))
(eshell-match-output "second")))
(ert-deftest eshell-test/get-old-input/run-output ()
"Test that we can run a line of output as a command when point is on it."
(with-temp-eshell
(eshell-match-command-output "echo \"echo there\"" "echo there")
;; Go to the output, and insert "hello" after "echo".
(forward-line -1)
(forward-word)
(insert " hello")
;; Run the line as a command.
(eshell-send-input)
(eshell-match-output "(\"hello\" \"there\")")))
(ert-deftest eshell-test/yank-output ()
"Test that yanking a line of output into the next prompt works (bug#66469)."
(with-temp-eshell
(eshell-insert-command "echo hello")
;; Go to the output and kill the line of text.
(forward-line -1)
(kill-line)
;; Go to the last prompt and yank the previous output.
(goto-char (point-max))
(yank)
;; Go to the beginning of the prompt and add some text.
(move-beginning-of-line 1)
(insert-and-inherit "echo ")
;; Make sure when we go to the beginning of the line, we go to the
;; right spot (before the "echo").
(move-end-of-line 1)
(move-beginning-of-line 1)
(should (looking-at "echo hello"))))
(provide 'eshell-tests)
;;; eshell-tests.el ends here