unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 828f4207e1ba9711eae1cbc5674d02540dffd88f 12833 bytes (raw)
name: test/src/process-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
 
;;; process-tests.el --- Testing the process facilities

;; Copyright (C) 2013-2018 Free Software Foundation, Inc.

;; This program is free software; you can redistribute it and/or modify
;; 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.

;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ert)

;; Timeout in seconds; the test fails if the timeout is reached.
(defvar process-test-sentinel-wait-timeout 2.0)

;; Start a process that exits immediately.  Call WAIT-FUNCTION,
;; possibly multiple times, to wait for the process to complete.
(defun process-test-sentinel-wait-function-working-p (wait-function)
  (let ((proc (start-process "test" nil "bash" "-c" "exit 20"))
	(sentinel-called nil)
	(start-time (float-time)))
    (set-process-sentinel proc (lambda (proc msg)
				 (setq sentinel-called t)))
    (while (not (or sentinel-called
		    (> (- (float-time) start-time)
		       process-test-sentinel-wait-timeout)))
      (funcall wait-function))
    (cl-assert (eq (process-status proc) 'exit))
    (cl-assert (= (process-exit-status proc) 20))
    sentinel-called))

(ert-deftest process-test-sentinel-accept-process-output ()
  (skip-unless (executable-find "bash"))
  (should (process-test-sentinel-wait-function-working-p
           #'accept-process-output)))

(ert-deftest process-test-sentinel-sit-for ()
  (skip-unless (executable-find "bash"))
  (should
   (process-test-sentinel-wait-function-working-p (lambda () (sit-for 0.01 t)))))

(when (eq system-type 'windows-nt)
  (ert-deftest process-test-quoted-batfile ()
    "Check that Emacs hides CreateProcess deficiency (bug#18745)."
    (let (batfile)
      (unwind-protect
          (progn
            ;; CreateProcess will fail when both the bat file and 1st
            ;; argument are quoted, so include spaces in both of those
            ;; to force quoting.
            (setq batfile (make-temp-file "echo args" nil ".bat"))
            (with-temp-file batfile
              (insert "@echo arg1=%1, arg2=%2\n"))
            (with-temp-buffer
              (call-process batfile nil '(t t) t "x &y")
              (should (string= (buffer-string) "arg1=\"x &y\", arg2=\n")))
            (with-temp-buffer
              (call-process-shell-command
               (mapconcat #'shell-quote-argument (list batfile "x &y") " ")
               nil '(t t) t)
              (should (string= (buffer-string) "arg1=\"x &y\", arg2=\n"))))
        (when batfile (delete-file batfile))))))

(ert-deftest process-test-stderr-buffer ()
  (skip-unless (executable-find "bash"))
  (let* ((stdout-buffer (generate-new-buffer "*stdout*"))
	 (stderr-buffer (generate-new-buffer "*stderr*"))
	 (proc (make-process :name "test"
			     :command (list "bash" "-c"
					    (concat "echo hello stdout!; "
						    "echo hello stderr! >&2; "
						    "exit 20"))
			     :buffer stdout-buffer
			     :stderr stderr-buffer))
	 (sentinel-called nil)
	 (start-time (float-time)))
    (set-process-sentinel proc (lambda (proc msg)
				 (setq sentinel-called t)))
    (while (not (or sentinel-called
		    (> (- (float-time) start-time)
		       process-test-sentinel-wait-timeout)))
      (accept-process-output))
    (cl-assert (eq (process-status proc) 'exit))
    (cl-assert (= (process-exit-status proc) 20))
    (should (with-current-buffer stdout-buffer
	      (goto-char (point-min))
	      (looking-at "hello stdout!")))
    (should (with-current-buffer stderr-buffer
	      (goto-char (point-min))
	      (looking-at "hello stderr!")))))

(ert-deftest process-test-stderr-filter ()
  (skip-unless (executable-find "bash"))
  (let* ((sentinel-called nil)
	 (stderr-sentinel-called nil)
	 (stdout-output nil)
	 (stderr-output nil)
	 (stdout-buffer (generate-new-buffer "*stdout*"))
	 (stderr-buffer (generate-new-buffer "*stderr*"))
	 (stderr-proc (make-pipe-process :name "stderr"
					 :buffer stderr-buffer))
	 (proc (make-process :name "test" :buffer stdout-buffer
			     :command (list "bash" "-c"
					    (concat "echo hello stdout!; "
						    "echo hello stderr! >&2; "
						    "exit 20"))
			     :stderr stderr-proc))
	 (start-time (float-time)))
    (set-process-filter proc (lambda (proc input)
			       (push input stdout-output)))
    (set-process-sentinel proc (lambda (proc msg)
				 (setq sentinel-called t)))
    (set-process-filter stderr-proc (lambda (proc input)
				      (push input stderr-output)))
    (set-process-sentinel stderr-proc (lambda (proc input)
					(setq stderr-sentinel-called t)))
    (while (not (or sentinel-called
		    (> (- (float-time) start-time)
		       process-test-sentinel-wait-timeout)))
      (accept-process-output))
    (cl-assert (eq (process-status proc) 'exit))
    (cl-assert (= (process-exit-status proc) 20))
    (should sentinel-called)
    (should (equal 1 (with-current-buffer stdout-buffer
		       (point-max))))
    (should (equal "hello stdout!\n"
		   (mapconcat #'identity (nreverse stdout-output) "")))
    (should stderr-sentinel-called)
    (should (equal 1 (with-current-buffer stderr-buffer
		       (point-max))))
    (should (equal "hello stderr!\n"
		   (mapconcat #'identity (nreverse stderr-output) "")))))

(ert-deftest start-process-should-not-modify-arguments ()
  "`start-process' must not modify its arguments in-place."
  ;; See bug#21831.
  (let* ((path (pcase system-type
                 ((or 'windows-nt 'ms-dos)
                  ;; Make sure the file name uses forward slashes.
                  ;; The original bug was that 'start-process' would
                  ;; convert forward slashes to backslashes.
                  (expand-file-name (executable-find "attrib.exe")))
                 (_ "/bin//sh")))
         (samepath (copy-sequence path)))
    ;; Make sure 'start-process' actually goes all the way and invokes
    ;; the program.
    (should (process-live-p (condition-case nil
                                (start-process "" nil path)
                              (error nil))))
    (should (equal path samepath))))

(ert-deftest make-process/noquery-stderr ()
  "Checks that Bug#30031 is fixed."
  (skip-unless (executable-find "sleep"))
  (with-temp-buffer
    (let* ((previous-processes (process-list))
           (process (make-process :name "sleep"
                                  :command '("sleep" "1h")
                                  :noquery t
                                  :connection-type 'pipe
                                  :stderr (current-buffer))))
      (unwind-protect
          (let ((new-processes (cl-set-difference (process-list)
                                                  previous-processes
                                                  :test #'eq)))
            (should new-processes)
            (dolist (process new-processes)
              (should-not (process-query-on-exit-flag process))))
        (kill-process process)))))

(ert-deftest make-process/mix-stderr ()
  "Check that ‘make-process’ mixes the output streams if STDERR is nil."
  (skip-unless (executable-find shell-file-name))
  (with-temp-buffer
    (let ((process (make-process
                    :name "mix-stderr"
                    :command (list shell-file-name shell-command-switch
                                   "echo stdout; echo stderr >&2")
                    :buffer (current-buffer)
                    :sentinel #'ignore
                    :noquery t
                    :connection-type 'pipe)))
      (while (process-live-p process)
        (accept-process-output process))
      (should (eq (process-status process) 'exit))
      (should (eq (process-exit-status process) 0))
      (should (equal (buffer-string) "stdout\nstderr\n")))))

(ert-deftest make-process/stderr-sentinel ()
  "Check that `make-process' doesn't install the default sentinel for stderr."
  (skip-unless (executable-find shell-file-name))
  (with-temp-buffer
    (let ((process (make-process
                    :name "stderr-sentinel"
                    :command (list shell-file-name shell-command-switch
                                   "echo stderr >&2")
                    :buffer nil
                    :stderr (current-buffer)
                    :noquery t
                    :connection-type 'pipe)))
      (while (process-live-p process)
        (accept-process-output process))
      (should (eq (process-status process) 'exit))
      (should (eq (process-exit-status process) 0))
      (should (equal (buffer-string) "stderr\n")))))

(ert-deftest make-process/stderr-coding ()
  "Check that `make-process' correctly encodes standard error."
  (skip-unless (executable-find shell-file-name))
  ;; The byte sequences printed to standard output and error are valid
  ;; UTF-8 code unit sequences for "ä" and "ö", respectively.
  ;; However, `make-process' should decode them both using
  ;; `iso-latin-1-dos', as specified below.
  (with-temp-buffer
    (let ((stdout (current-buffer))
          ;; `coding-system-for-read' should be ignored.
          (coding-system-for-read 'utf-8-unix))
      (with-temp-buffer
        (let ((process (make-process
                        :name "stderr-coding"
                        :command (list shell-file-name shell-command-switch
                                       (concat "echo -e '\\xC3\\xA4\\r'; "
                                               "echo -e '\\xC3\\xB6\\r' >&2"))
                        :buffer stdout
                        :stderr (current-buffer)
                        :sentinel #'ignore
                        :coding 'iso-latin-1-dos
                        :noquery t
                        :connection-type 'pipe)))
          (while (process-live-p process)
            (accept-process-output process))
          (let ((stderr-process (get-buffer-process (current-buffer))))
            ;; FIXME: The following form should not be required.
            (while (process-live-p stderr-process)
              (accept-process-output stderr-process)))
          (should (eq (process-status process) 'exit))
          (should (eq (process-exit-status process) 0))
          (should (equal (buffer-string) "\u00C3\u00B6\n"))))
      (should (equal (buffer-string) "\u00C3\u00A4\n")))))

(ert-deftest make-process/mixed-output-coding ()
  "Check that `make-process' allows different coding systems for
the output streams."
  (skip-unless (executable-find shell-file-name))
  ;; The byte sequences printed to standard output and error are valid
  ;; UTF-8 code unit sequences for "ä" and "ö", respectively.
  ;; However, `make-process' should decode them using the coding
  ;; systems passed to `make-process' and `make-pipe-process',
  ;; respectively.
  (with-temp-buffer
    (let ((stdout (current-buffer))
          ;; `coding-system-for-read' should be ignored.
          (coding-system-for-read 'utf-8-unix))
      (with-temp-buffer
        (let* ((stderr-process (make-pipe-process
                                :name "stderr-coding stderr"
                                :buffer (current-buffer)
                                :sentinel #'ignore
                                :coding 'iso-latin-1-unix
                                :noquery t))
               (process (make-process
                         :name "stderr-coding"
                         :command (list shell-file-name shell-command-switch
                                        (concat "echo -e '\\xC3\\xA4\\r'; "
                                                "echo -e '\\xC3\\xB6\\r' >&2"))
                         :buffer stdout
                         :stderr stderr-process
                         :sentinel #'ignore
                         :coding 'iso-latin-1-dos
                         :noquery t
                         :connection-type 'pipe)))
          (while (process-live-p process)
            (accept-process-output process))
          ;; FIXME: Should the following be required?
          (while (process-live-p stderr-process)
            (accept-process-output stderr-process))
          (should (eq (process-status process) 'exit))
          (should (eq (process-exit-status process) 0))
          (should (eq (process-status stderr-process) 'closed))
          (should (equal (buffer-string) "\u00C3\u00B6\r\n"))))
      (should (equal (buffer-string) "\u00C3\u00A4\n")))))

(provide 'process-tests)
;; process-tests.el ends here.

debug log:

solving 828f4207e1 ...
found 828f4207e1 in https://yhetil.org/emacs-devel/20180404120218.257212-3-phst@google.com/
found 838ba78acb in https://yhetil.org/emacs-devel/20180404120218.257212-2-phst@google.com/
found b1b4665c3c in https://yhetil.org/emacs-devel/20180404120218.257212-1-phst@google.com/
found 7d35560229 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 7d355602297c7fe1380012c25e2dbbecb86952c0	test/src/process-tests.el

applying [1/3] https://yhetil.org/emacs-devel/20180404120218.257212-1-phst@google.com/
diff --git a/test/src/process-tests.el b/test/src/process-tests.el
index 7d35560229..b1b4665c3c 100644


applying [2/3] https://yhetil.org/emacs-devel/20180404120218.257212-2-phst@google.com/
diff --git a/test/src/process-tests.el b/test/src/process-tests.el
index b1b4665c3c..838ba78acb 100644


applying [3/3] https://yhetil.org/emacs-devel/20180404120218.257212-3-phst@google.com/
diff --git a/test/src/process-tests.el b/test/src/process-tests.el
index 838ba78acb..828f4207e1 100644

Checking patch test/src/process-tests.el...
Applied patch test/src/process-tests.el cleanly.
Checking patch test/src/process-tests.el...
Applied patch test/src/process-tests.el cleanly.
Checking patch test/src/process-tests.el...
Applied patch test/src/process-tests.el cleanly.

index at:
100644 828f4207e1ba9711eae1cbc5674d02540dffd88f	test/src/process-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).