summaryrefslogtreecommitdiff
path: root/test/src/process-tests.el
diff options
context:
space:
mode:
authorPhilipp Stephani <phst@google.com>2021-01-18 11:38:58 +0100
committerPhilipp Stephani <phst@google.com>2021-01-18 11:40:34 +0100
commit3b4050154e3f72c06501cd9a5ad83841b92c7bd6 (patch)
tree158350cfcd30ae761e618b67852b6267da2e1b05 /test/src/process-tests.el
parentb215e83a784be1118bb5d729f17597c4f1c62b52 (diff)
downloademacs-3b4050154e3f72c06501cd9a5ad83841b92c7bd6.tar.gz
Replace Unix commands with Emacs in process tests.
That way, the tests only depend on Emacs, and not on utilities that might not be available during test time. * test/src/process-tests.el (process-tests--eval) (process-tests--emacs-command, process-tests--emacs-binary) (process-tests--dump-file) (process-tests--usable-file-for-reinvoke): New helper functions. (process-tests/sentinel-called) (process-tests/sentinel-with-multiple-processes): Use them.
Diffstat (limited to 'test/src/process-tests.el')
-rw-r--r--test/src/process-tests.el80
1 files changed, 71 insertions, 9 deletions
diff --git a/test/src/process-tests.el b/test/src/process-tests.el
index d2a98dc19f2..949f73595b4 100644
--- a/test/src/process-tests.el
+++ b/test/src/process-tests.el
@@ -736,15 +736,16 @@ Return nil if that can't be determined."
(ert-deftest process-tests/sentinel-called ()
"Check that sentinels are called after processes finish"
- (let ((echo (executable-find "echo")))
- (skip-unless echo)
+ (let ((command (process-tests--emacs-command)))
+ (skip-unless command)
(dolist (conn-type '(pipe pty))
(ert-info ((format "Connection type: %s" conn-type))
(process-tests--with-processes processes
(let* ((calls ())
(process (make-process
:name "echo"
- :command (list echo "first")
+ :command (process-tests--eval
+ command '(print "first"))
:noquery t
:connection-type conn-type
:coding 'utf-8-unix
@@ -759,17 +760,16 @@ Return nil if that can't be determined."
(ert-deftest process-tests/sentinel-with-multiple-processes ()
"Check that sentinels are called in time even when other processes
have written output."
- (let ((echo (executable-find "echo"))
- (bash (executable-find "bash")))
- (skip-unless echo)
- (skip-unless bash)
+ (let ((command (process-tests--emacs-command)))
+ (skip-unless command)
(dolist (conn-type '(pipe pty))
(ert-info ((format "Connection type: %s" conn-type))
(process-tests--with-processes processes
(let* ((calls ())
(process (make-process
:name "echo"
- :command (list echo "first")
+ :command (process-tests--eval
+ command '(print "first"))
:noquery t
:connection-type conn-type
:coding 'utf-8-unix
@@ -779,7 +779,9 @@ have written output."
(push process processes)
(push (make-process
:name "bash"
- :command (list bash "-c" "sleep 10 && echo second")
+ :command (process-tests--eval
+ command
+ '(progn (sleep-for 10) (print "second")))
:noquery t
:connection-type conn-type)
processes)
@@ -787,5 +789,65 @@ have written output."
(should (equal calls
(list (list process "finished\n"))))))))))
+(defun process-tests--eval (command form)
+ "Return a command that evaluates FORM in an Emacs subprocess.
+COMMAND must be a list returned by
+`process-tests--emacs-command'."
+ (let ((print-gensym t)
+ (print-circle t)
+ (print-length nil)
+ (print-level nil)
+ (print-escape-control-characters t)
+ (print-escape-newlines t)
+ (print-escape-multibyte t)
+ (print-escape-nonascii t))
+ `(,@command "--quick" "--batch" ,(format "--eval=%S" form))))
+
+(defun process-tests--emacs-command ()
+ "Return a command to reinvoke the current Emacs instance.
+Return nil if that doesn't appear to be possible."
+ (when-let ((binary (process-tests--emacs-binary))
+ (dump (process-tests--dump-file)))
+ (cons binary
+ (unless (eq dump :not-needed)
+ (list (concat "--dump-file="
+ (file-name-unquote dump)))))))
+
+(defun process-tests--emacs-binary ()
+ "Return the filename of the currently running Emacs binary.
+Return nil if that can't be determined."
+ (and (stringp invocation-name)
+ (not (file-remote-p invocation-name))
+ (not (file-name-absolute-p invocation-name))
+ (stringp invocation-directory)
+ (not (file-remote-p invocation-directory))
+ (file-name-absolute-p invocation-directory)
+ (when-let ((file (process-tests--usable-file-for-reinvoke
+ (expand-file-name invocation-name
+ invocation-directory))))
+ (and (file-executable-p file) file))))
+
+(defun process-tests--dump-file ()
+ "Return the filename of the dump file used to start Emacs.
+Return nil if that can't be determined. Return `:not-needed' if
+Emacs wasn't started with a dump file."
+ (if-let ((stats (and (fboundp 'pdumper-stats) (pdumper-stats))))
+ (when-let ((file (process-tests--usable-file-for-reinvoke
+ (cdr (assq 'dump-file-name stats)))))
+ (and (file-readable-p file) file))
+ :not-needed))
+
+(defun process-tests--usable-file-for-reinvoke (filename)
+ "Return a version of FILENAME that can be used to reinvoke Emacs.
+Return nil if FILENAME doesn't exist."
+ (when (and (stringp filename)
+ (not (file-remote-p filename)))
+ (cl-callf file-truename filename)
+ (and (stringp filename)
+ (not (file-remote-p filename))
+ (file-name-absolute-p filename)
+ (file-regular-p filename)
+ filename)))
+
(provide 'process-tests)
;;; process-tests.el ends here