1 ;;; async-test --- async.el-related tests
3 ;; Copyright (C) 2012 John Wiegley
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 10 Jul 2012
9 ;; X-URL: https://github.com/jwiegley/emacs-async
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Contains tests for all the async modules.
38 (defun async-test-1 ()
40 (message "Starting async-test-1...")
42 ;; What to do in the child process
44 (message "This is a test")
48 ;; What to do when it finishes
50 (message "Async process done, result should be 222: %s" result)))
51 (message "Starting async-test-1...done"))
53 (defun async-test-2 ()
55 (message "Starting async-test-2...")
56 (let ((proc (async-start
57 ;; What to do in the child process
59 (message "This is a test")
62 (message "I'm going to do some work here")
64 (message "Async process done, result should be 222: %s"
67 (defun async-test-3 ()
69 (message "Starting async-test-3...")
71 ;; What to do in the child process
73 (message "This is a test")
75 (error "Error in child process")
78 ;; What to do when it finishes
80 (message "Async process done, result should be 222: %s" result)))
81 (message "Starting async-test-1...done"))
83 (defun async-test-4 ()
85 (message "Starting async-test-4...")
86 (async-start-process "sleep" "sleep"
87 ;; What to do when it finishes
89 (message "Sleep done, exit code was %d"
90 (process-exit-status proc)))
92 (message "Starting async-test-4...done"))
94 (defun async-test-5 ()
96 (message "Starting async-test-5...")
99 ;; What to do in the child process
101 (message "This is a test, sending message")
102 (async-send :hello "world")
103 ;; wait for a message
104 (let ((msg (async-receive)))
105 (message "Child got message: %s"
106 (plist-get msg :goodbye)))
110 ;; What to do when it finishes
112 (if (async-message-p result)
113 (message "Got hello from child process: %s"
114 (plist-get result :hello))
115 (message "Async process done, result should be 222: %s"
117 (async-send proc :goodbye "everyone"))
118 (message "Starting async-test-5...done"))
120 (defun async-test-6 ()
122 (message "Starting async-test-6...")
124 ;; What to do in the child process
126 ,(async-inject-variables "\\`user-mail-address\\'")
127 (format "user-mail-address = %s" user-mail-address))
129 ;; What to do when it finishes
131 (message "Async process done: %s" result))))
133 (defsubst async-file-contents (file)
134 "Return the contents of FILE, as a string."
136 (insert-file-contents file)
139 (defun* async-do-copy-file-test (ok-if-already-exists
140 keep-time preserve-uid-gid
141 preserve-selinux-context
142 &key use-native-commands
144 (let* ((temp-file (make-temp-file "async-do-copy-file-test"))
145 (temp-file2 (concat temp-file ".target")))
149 (insert "async-do-copy-file-test")
150 (write-region (point-min) (point-max) temp-file))
152 (let* ((async-file-use-native-commands use-native-commands)
153 (future (if synchronously
154 (copy-file temp-file temp-file2
158 preserve-selinux-context)
159 (async-copy-file temp-file temp-file2
163 preserve-selinux-context
165 (unless synchronously
166 (if use-native-commands
167 (let ((proc (async-get future)))
168 (should (processp proc))
169 (should (equal 'exit (process-status proc))))
170 (should (equal (async-get future) nil))))
172 (should (file-readable-p temp-file2))
174 (should (equal "async-do-copy-file-test"
175 (async-file-contents temp-file2)))))
177 (if (file-exists-p temp-file) (delete-file temp-file))
178 (if (file-exists-p temp-file2) (delete-file temp-file2)))))
180 (ert-deftest async-copy-file-lisp-sync-1 ()
181 (async-do-copy-file-test t t t nil :synchronously t))
182 (ert-deftest async-copy-file-lisp-1 ()
183 (async-do-copy-file-test t t t nil :use-native-commands nil))
184 (ert-deftest async-copy-file-native-1 ()
185 (async-do-copy-file-test t t t nil :use-native-commands t))
187 (defsubst async-file-make-temp-dir (prefix)
188 "Make a temporary directory using PREFIX.
189 Return the name of the directory."
190 (let ((dir (make-temp-name
191 (expand-file-name prefix temporary-file-directory))))
195 (defsubst async-file-make-file (file contents)
196 "Create a new FILE with the given CONTENTS."
199 (write-region (point-min) (point-max) file)))
201 (defun* async-do-copy-directory-test (keep-time parents copy-contents
202 &key use-native-commands
204 (let* ((temp-dir (async-file-make-temp-dir "async-do-copy-directory-test"))
205 (temp-dir2 (concat temp-dir ".target")))
208 (async-file-make-file (expand-file-name "foo" temp-dir) "foo")
209 (async-file-make-file (expand-file-name "bar" temp-dir) "bar")
211 ;; Shouldn't the parents argument cause this to happen when needed?
212 ;; But if the following is wrapped with "unless parents", even
213 ;; `async-copy-directory-lisp-sync-2' fails.
214 (make-directory temp-dir2)
216 (let* ((async-file-use-native-commands use-native-commands)
217 (future (if synchronously
218 (copy-directory temp-dir temp-dir2
222 (async-copy-directory temp-dir temp-dir2
227 (unless synchronously
228 (if use-native-commands
229 (let ((proc (async-get future)))
230 (should (processp proc))
231 (should (equal 'exit (process-status proc))))
232 ;; Ignore the return value from `copy-directory'
235 (if (and parents copy-contents)
236 (should (file-directory-p temp-dir2)))
238 (let* ((target (if copy-contents
240 (expand-file-name (file-name-nondirectory temp-dir)
242 (foo-file (expand-file-name "foo" target))
243 (bar-file (expand-file-name "bar" target)))
245 (should (file-readable-p foo-file))
246 (should (file-readable-p bar-file))
248 (should (equal "foo" (async-file-contents foo-file)))
249 (should (equal "bar" (async-file-contents bar-file))))))
251 (if (file-directory-p temp-dir) (delete-directory temp-dir t))
252 (if (file-directory-p temp-dir2) (delete-directory temp-dir2 t)))))
254 (ert-deftest async-copy-directory-lisp-sync-1 ()
255 (async-do-copy-directory-test t nil nil :synchronously t))
256 (ert-deftest async-copy-directory-lisp-sync-2 ()
257 (async-do-copy-directory-test t t nil :synchronously t))
258 (ert-deftest async-copy-directory-lisp-sync-3 ()
259 (async-do-copy-directory-test t nil t :synchronously t))
260 (ert-deftest async-copy-directory-lisp-sync-4 ()
261 (async-do-copy-directory-test t t t :synchronously t))
263 (ert-deftest async-copy-directory-lisp-1 ()
264 (async-do-copy-directory-test t nil nil :use-native-commands nil))
265 (ert-deftest async-copy-directory-lisp-2 ()
266 (async-do-copy-directory-test t t nil :use-native-commands nil))
267 (ert-deftest async-copy-directory-lisp-3 ()
268 (async-do-copy-directory-test t nil t :use-native-commands nil))
269 (ert-deftest async-copy-directory-lisp-4 ()
270 (async-do-copy-directory-test t t t :use-native-commands nil))
272 (ert-deftest async-copy-directory-native-1 ()
273 (async-do-copy-directory-test t nil nil :use-native-commands t))
274 (ert-deftest async-copy-directory-native-2 ()
275 (async-do-copy-directory-test t t nil :use-native-commands t))
276 (ert-deftest async-copy-directory-native-3 ()
277 (async-do-copy-directory-test t nil t :use-native-commands t))
278 (ert-deftest async-copy-directory-native-4 ()
279 (async-do-copy-directory-test t t t :use-native-commands t))
281 (provide 'async-test)
283 ;;; async-test.el ends here