]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
test: use emacsclient(1) for Emacs tests
[notmuch] / test / test-lib.el
1 ;; test-lib.el --- auxiliary stuff for Notmuch Emacs tests.
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch test suit.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
22
23 ;; avoid crazy 10-column default of --batch
24 (set-frame-width (window-frame (get-buffer-window)) 80)
25
26 ;; `read-file-name' by default uses `completing-read' function to read
27 ;; user input.  It does not respect `standard-input' variable which we
28 ;; use in tests to provide user input.  So replace it with a plain
29 ;; `read' call.
30 (setq read-file-name-function (lambda (&rest _) (read)))
31
32 (defun notmuch-test-wait ()
33   "Wait for process completion."
34   (while (get-buffer-process (current-buffer))
35     (sleep-for 0.1)))
36
37 (defun test-output (&optional filename)
38   "Save current buffer to file FILENAME.  Default FILENAME is OUTPUT."
39   (write-region (point-min) (point-max) (or filename "OUTPUT")))
40
41 (defun test-visible-output (&optional filename)
42   "Save visible text in current buffer to file FILENAME.  Default
43 FILENAME is OUTPUT."
44   (let ((text (visible-buffer-string)))
45     (with-temp-file (or filename "OUTPUT") (insert text))))
46
47 (defun visible-buffer-string ()
48   "Same as `buffer-string', but excludes invisible text."
49   (visible-buffer-substring (point-min) (point-max)))
50
51 (defun visible-buffer-substring (start end)
52   "Same as `buffer-substring', but excludes invisible text."
53   (let (str)
54     (while (< start end)
55       (let ((next-pos (next-char-property-change start end)))
56         (when (not (invisible-p start))
57           (setq str (concat str (buffer-substring start next-pos))))
58         (setq start next-pos)))
59     str))
60
61 (defun orphan-watchdog (pid)
62   "Periodically check that the process with id PID is still
63 running, quit if it terminated."
64   (if (not (process-attributes pid))
65       (kill-emacs)
66     (run-at-time "1 min" nil 'orphan-watchdog pid)))