1 ;;; package-test.el --- Tests for the Emacs package system
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Daniel Hackney <dan@haxney.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; You may want to run this from a separate Emacs instance from your
26 ;; main one, because a bug in the code below could mess with your
27 ;; installed packages.
29 ;; Run this in a clean Emacs session using:
31 ;; $ emacs -Q --batch -L . -l package-test.el -l ert -f ert-run-tests-batch-and-exit
39 (setq package-menu-async nil
)
41 (defvar package-test-user-dir nil
42 "Directory to use for installing packages during testing.")
44 (defvar package-test-file-dir
(file-name-directory (or load-file-name
46 "Directory of the actual \"package-test.el\" file.")
48 (defvar simple-single-desc
49 (package-desc-create :name
'simple-single
51 :summary
"A single-file package with no dependencies"
53 :extras
'((:authors
("J. R. Hacker" .
"jrh@example.com"))
54 (:maintainer
"J. R. Hacker" .
"jrh@example.com")
55 (:url .
"http://doodles.au")))
56 "Expected `package-desc' parsed from simple-single-1.3.el.")
58 (defvar simple-depend-desc
59 (package-desc-create :name
'simple-depend
61 :summary
"A single-file package with a dependency."
63 :reqs
'((simple-single (1 3)))
64 :extras
'((:authors
("J. R. Hacker" .
"jrh@example.com"))
65 (:maintainer
"J. R. Hacker" .
"jrh@example.com")))
66 "Expected `package-desc' parsed from simple-depend-1.0.el.")
68 (defvar multi-file-desc
69 (package-desc-create :name
'multi-file
71 :summary
"Example of a multi-file tar package"
73 :extras
'((:url .
"http://puddles.li")))
74 "Expected `package-desc' from \"multi-file-0.2.3.tar\".")
77 (package-desc-create :name
'new-pkg
80 "Expected `package-desc' parsed from new-pkg-1.0.el.")
82 (defvar simple-depend-desc-1
83 (package-desc-create :name
'simple-depend-1
85 :summary
"A single-file package with a dependency."
87 :reqs
'((simple-depend (1 0))
89 "`package-desc' used for testing dependencies.")
91 (defvar simple-depend-desc-2
92 (package-desc-create :name
'simple-depend-2
94 :summary
"A single-file package with a dependency."
96 :reqs
'((simple-depend-1 (1 0))
98 "`package-desc' used for testing dependencies.")
100 (defvar package-test-data-dir
(expand-file-name "data/package" package-test-file-dir
)
101 "Base directory of package test files.")
103 (defvar package-test-fake-contents-file
104 (expand-file-name "archive-contents" package-test-data-dir
)
105 "Path to a static copy of \"archive-contents\".")
107 (cl-defmacro with-package-test
((&optional
&key file
114 "Set up temporary locations and variables for testing."
116 `(let* ((package-test-user-dir (make-temp-file "pkg-test-user-dir-" t
))
117 (process-environment (cons (format "HOME=%s" package-test-user-dir
)
118 process-environment
))
119 (package-user-dir package-test-user-dir
)
120 (package-archives `(("gnu" .
,(or ,location package-test-data-dir
))))
121 (default-directory package-test-file-dir
)
126 '(package-update-news-on-upload t
)
129 '((package-test-archive-upload-base (make-temp-file "pkg-archive-base-" t
))
130 (package-archive-upload-base package-test-archive-upload-base
))
131 (list (cl-gensym)))) ;; Dummy value so `let' doesn't try to bind nil
132 (let ((buf (get-buffer "*Packages*")))
133 (when (buffer-live-p buf
)
137 ,(if basedir
`(cd ,basedir
))
138 (unless (file-directory-p package-user-dir
)
139 (mkdir package-user-dir
))
140 (cl-letf (((symbol-function 'yes-or-no-p
) (lambda (&rest r
) t
))
141 ((symbol-function 'y-or-n-p
) (lambda (&rest r
) t
)))
143 `((package-initialize)
144 (package-refresh-contents)
145 (mapc 'package-install
,install
)))
148 `(insert-file-contents ,file
))
151 (when (file-directory-p package-test-user-dir
)
152 (delete-directory package-test-user-dir t
))
154 (when (and (boundp 'package-test-archive-upload-base
)
155 (file-directory-p package-test-archive-upload-base
))
156 (delete-directory package-test-archive-upload-base t
)))))
158 (defmacro with-fake-help-buffer
(&rest body
)
159 "Execute BODY in a temp buffer which is treated as the \"*Help*\" buffer."
162 ;; Trick `help-buffer' into using the temp buffer.
163 (let ((help-xref-following t
))
166 (defun package-test-strip-version (dir)
167 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir
)))
169 (defun package-test-suffix-matches (base suffix-list
)
170 "Return file names matching BASE concatenated with each item in SUFFIX-LIST"
172 '(lambda (item) (file-expand-wildcards (concat base item
)))
175 (defvar tar-parse-info
)
176 (declare-function tar-header-name
"tar-mode" (cl-x) t
) ; defstruct
178 (defun package-test-search-tar-file (filename)
179 "Search the current buffer's `tar-parse-info' variable for FILENAME.
181 Must called from within a `tar-mode' buffer."
182 (cl-dolist (header tar-parse-info
)
183 (let ((tar-name (tar-header-name header
)))
184 (when (string= tar-name filename
)
187 (defun package-test-desc-version-string (desc)
188 "Return the package version as a string."
189 (package-version-join (package-desc-version desc
)))
191 (ert-deftest package-test-desc-from-buffer
()
192 "Parse an elisp buffer to get a `package-desc' object."
193 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
194 (should (equal (package-buffer-info) simple-single-desc
)))
195 (with-package-test (:basedir
"data/package" :file
"simple-depend-1.0.el")
196 (should (equal (package-buffer-info) simple-depend-desc
)))
197 (with-package-test (:basedir
"data/package"
198 :file
"multi-file-0.2.3.tar")
200 (should (equal (package-tar-file-info) multi-file-desc
))))
202 (ert-deftest package-test-install-single
()
203 "Install a single file without using an archive."
204 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
205 (should (package-install-from-buffer))
207 (should (package-installed-p 'simple-single
))
208 ;; Check if we properly report an "already installed".
209 (package-install 'simple-single
)
210 (with-current-buffer "*Messages*"
211 (should (string-match "^[`‘']simple-single[’'] is already installed\n?\\'"
213 (should (package-installed-p 'simple-single
))
214 (let* ((simple-pkg-dir (file-name-as-directory
217 package-test-user-dir
)))
218 (autoloads-file (expand-file-name "simple-single-autoloads.el"
220 (should (file-directory-p simple-pkg-dir
))
222 (insert-file-contents (expand-file-name "simple-single-pkg.el"
224 (should (string= (buffer-string)
225 (concat ";;; -*- no-byte-compile: t -*-\n"
226 "(define-package \"simple-single\" \"1.3\" "
227 "\"A single-file package "
228 "with no dependencies\" 'nil "
229 ":authors '((\"J. R. Hacker\" . \"jrh@example.com\")) "
230 ":maintainer '(\"J. R. Hacker\" . \"jrh@example.com\") "
231 ":url \"http://doodles.au\""
233 (should (file-exists-p autoloads-file
))
234 (should-not (get-file-buffer autoloads-file
)))))
236 (ert-deftest package-test-install-dependency
()
237 "Install a package which includes a dependency."
238 (with-package-test ()
240 (package-refresh-contents)
241 (package-install 'simple-depend
)
242 (should (package-installed-p 'simple-single
))
243 (should (package-installed-p 'simple-depend
))))
245 (ert-deftest package-test-install-two-dependencies
()
246 "Install a package which includes a dependency."
247 (with-package-test ()
249 (package-refresh-contents)
250 (package-install 'simple-two-depend
)
251 (should (package-installed-p 'simple-single
))
252 (should (package-installed-p 'simple-depend
))
253 (should (package-installed-p 'simple-two-depend
))))
255 (ert-deftest package-test-refresh-contents
()
256 "Parse an \"archive-contents\" file."
257 (with-package-test ()
259 (package-refresh-contents)
260 (should (eq 4 (length package-archive-contents
)))))
262 (ert-deftest package-test-install-single-from-archive
()
263 "Install a single package from a package archive."
264 (with-package-test ()
266 (package-refresh-contents)
267 (package-install 'simple-single
)))
269 (ert-deftest package-test-install-prioritized
()
270 "Install a lower version from a higher-prioritized archive."
271 (with-package-test ()
272 (let* ((newer-version (expand-file-name "data/package/newer-versions"
273 package-test-file-dir
))
274 (package-archives `(("older" .
,package-test-data-dir
)
275 ("newer" .
,newer-version
)))
276 (package-archive-priorities '(("older" .
100))))
279 (package-refresh-contents)
280 (package-install 'simple-single
)
282 (let ((installed (cadr (assq 'simple-single package-alist
))))
283 (should (version-list-= '(1 3)
284 (package-desc-version installed
)))))))
286 (ert-deftest package-test-install-multifile
()
287 "Check properties of the installed multi-file package."
288 (with-package-test (:basedir
"data/package" :install
'(multi-file))
290 (expand-file-name "multi-file-autoloads.el"
293 package-test-user-dir
)))
294 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
295 "multi-file-autoloads.el" "multi-file.elc"))
296 (autoload-forms '("^(defvar multi-file-custom-var"
297 "^(custom-autoload 'multi-file-custom-var"
298 "^(autoload 'multi-file-mode"))
299 (pkg-dir (file-name-as-directory
302 package-test-user-dir
))))
303 (package-refresh-contents)
304 (should (package-installed-p 'multi-file
))
306 (insert-file-contents-literally autoload-file
)
307 (dolist (fn installed-files
)
308 (should (file-exists-p (expand-file-name fn pkg-dir
))))
309 (dolist (re autoload-forms
)
310 (goto-char (point-min))
311 (should (re-search-forward re nil t
)))))))
313 (ert-deftest package-test-update-listing
()
314 "Ensure installed package status is updated."
315 (with-package-test ()
316 (let ((buf (package-list-packages)))
317 (search-forward-regexp "^ +simple-single")
318 (package-menu-mark-install)
319 (package-menu-execute)
320 (run-hooks 'post-command-hook
)
321 (should (package-installed-p 'simple-single
))
322 (switch-to-buffer "*Packages*")
323 (goto-char (point-min))
324 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
325 (goto-char (point-min))
326 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
329 (ert-deftest package-test-update-archives
()
330 "Test updating package archives."
331 (with-package-test ()
332 (let ((buf (package-list-packages)))
333 (package-menu-refresh)
334 (search-forward-regexp "^ +simple-single")
335 (package-menu-mark-install)
336 (package-menu-execute)
337 (should (package-installed-p 'simple-single
))
338 (let ((package-test-data-dir
339 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
340 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
341 (package-menu-refresh)
343 ;; New version should be available and old version should be installed
344 (goto-char (point-min))
345 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+available" nil t
))
346 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
348 (goto-char (point-min))
349 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
351 (package-menu-mark-upgrades)
352 (package-menu-execute)
353 (package-menu-refresh)
354 (should (package-installed-p 'simple-single
'(1 4)))))))
356 (ert-deftest package-test-update-archives-async
()
357 "Test updating package archives asynchronously."
358 (skip-unless (executable-find "python2"))
359 ;; For some reason this test doesn't work reliably on hydra.nixos.org.
360 (skip-unless (not (getenv "NIX_STORE")))
361 (with-package-test (:basedir
362 package-test-data-dir
363 :location
"http://0.0.0.0:8000/")
364 (let* ((package-menu-async t
)
365 (process (start-process
366 "package-server" "package-server-buffer"
367 (executable-find "python2")
368 (expand-file-name "package-test-server.py"))))
372 (should package--downloads-in-progress
)
373 (should mode-line-process
)
375 (with-timeout (10 'timeout
)
376 (while package--downloads-in-progress
377 (accept-process-output nil
1))
379 ;; If the server process died, there's some non-Emacs problem.
380 ;; Eg maybe the port was already in use.
381 (skip-unless (process-live-p process
))
382 (goto-char (point-min))
384 (search-forward-regexp "^ +simple-single" nil t
)))
385 (if (process-live-p process
) (kill-process process
))))))
387 (ert-deftest package-test-describe-package
()
388 "Test displaying help for a package."
390 (require 'finder-inf
)
392 (with-fake-help-buffer
393 (describe-package '5x5
)
394 (goto-char (point-min))
395 (should (search-forward "5x5 is a built-in package." nil t
))
396 ;; Don't assume the descriptions are in any particular order.
397 (save-excursion (should (search-forward "Status: Built-in." nil t
)))
398 (save-excursion (should (search-forward "Summary: simple little puzzle game" nil t
)))
399 (should (search-forward "The aim of 5x5" nil t
)))
402 (with-package-test ()
404 (package-refresh-contents)
405 (package-install 'simple-single
)
406 (with-fake-help-buffer
407 (describe-package 'simple-single
)
408 (goto-char (point-min))
409 (should (search-forward "simple-single is an installed package." nil t
))
410 (save-excursion (should (re-search-forward "Status: Installed in ['`‘]simple-single-1.3/['’] (unsigned)." nil t
)))
411 (save-excursion (should (search-forward "Version: 1.3" nil t
)))
412 (save-excursion (should (search-forward "Summary: A single-file package with no dependencies" nil t
)))
413 (save-excursion (should (search-forward "Homepage: http://doodles.au" nil t
)))
414 (save-excursion (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
)))
415 ;; No description, though. Because at this point we don't know
416 ;; what archive the package originated from, and we don't have
417 ;; its readme file saved.
420 (ert-deftest package-test-describe-non-installed-package
()
421 "Test displaying of the readme for non-installed package."
423 (with-package-test ()
425 (package-refresh-contents)
426 (with-fake-help-buffer
427 (describe-package 'simple-single
)
428 (goto-char (point-min))
429 (should (search-forward "Homepage: http://doodles.au" nil t
))
430 (should (search-forward "This package provides a minor mode to frobnicate"
433 (ert-deftest package-test-describe-non-installed-multi-file-package
()
434 "Test displaying of the readme for non-installed multi-file package."
436 (with-package-test ()
438 (package-refresh-contents)
439 (with-fake-help-buffer
440 (describe-package 'multi-file
)
441 (goto-char (point-min))
442 (should (search-forward "Homepage: http://puddles.li" nil t
))
443 (should (search-forward "This is a bare-bones readme file for the multi-file"
446 (ert-deftest package-test-signed
()
447 "Test verifying package signature."
448 (skip-unless (ignore-errors
449 (let ((homedir (make-temp-file "package-test" t
)))
451 (let ((process-environment
452 (cons (format "HOME=%s" homedir
)
453 process-environment
)))
454 (epg-check-configuration (epg-configuration))
456 (delete-directory homedir t
)))))
457 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
458 (package-test-data-dir
459 (expand-file-name "data/package/signed" package-test-file-dir
)))
460 (with-package-test ()
462 (package-import-keyring keyring
)
463 (package-refresh-contents)
464 (should (package-install 'signed-good
))
465 (should-error (package-install 'signed-bad
))
466 ;; Check if the installed package status is updated.
467 (let ((buf (package-list-packages)))
468 (package-menu-refresh)
469 (should (re-search-forward
470 "^\\s-+signed-good\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-"
472 (should (string-equal (match-string-no-properties 1) "1.0"))
473 (should (string-equal (match-string-no-properties 2) "installed")))
474 ;; Check if the package description is updated.
475 (with-fake-help-buffer
476 (describe-package 'signed-good
)
477 (goto-char (point-min))
478 (should (re-search-forward "signed-good is an? \\(\\S-+\\) package." nil t
))
479 (should (string-equal (match-string-no-properties 1) "installed"))
480 (should (re-search-forward
481 "Status: Installed in ['`‘]signed-good-1.0/['’]."
486 ;;; Tests for package-x features.
490 (defvar package-x-test--single-archive-entry-1-3
492 (package-make-ac-desc '(1 3) nil
493 "A single-file package with no dependencies"
495 '((:authors
("J. R. Hacker" .
"jrh@example.com"))
496 (:maintainer
"J. R. Hacker" .
"jrh@example.com")
497 (:url .
"http://doodles.au"))))
498 "Expected contents of the archive entry from the \"simple-single\" package.")
500 (defvar package-x-test--single-archive-entry-1-4
502 (package-make-ac-desc '(1 4) nil
503 "A single-file package with no dependencies"
505 '((:authors
("J. R. Hacker" .
"jrh@example.com"))
506 (:maintainer
"J. R. Hacker" .
"jrh@example.com"))))
507 "Expected contents of the archive entry from the updated \"simple-single\" package.")
509 (ert-deftest package-x-test-upload-buffer
()
510 "Test creating an \"archive-contents\" file"
511 (with-package-test (:basedir
"data/package"
512 :file
"simple-single-1.3.el"
514 (package-upload-buffer)
515 (should (file-exists-p (expand-file-name "archive-contents"
516 package-archive-upload-base
)))
517 (should (file-exists-p (expand-file-name "simple-single-1.3.el"
518 package-archive-upload-base
)))
519 (should (file-exists-p (expand-file-name "simple-single-readme.txt"
520 package-archive-upload-base
)))
522 (let (archive-contents)
524 (insert-file-contents
525 (expand-file-name "archive-contents"
526 package-archive-upload-base
))
527 (setq archive-contents
528 (package-read-from-string
529 (buffer-substring (point-min) (point-max)))))
530 (should (equal archive-contents
531 (list 1 package-x-test--single-archive-entry-1-3
))))))
533 (ert-deftest package-x-test-upload-new-version
()
534 "Test uploading a new version of a package"
535 (with-package-test (:basedir
"data/package"
536 :file
"simple-single-1.3.el"
538 (package-upload-buffer)
540 (insert-file-contents "newer-versions/simple-single-1.4.el")
541 (package-upload-buffer))
543 (let (archive-contents)
545 (insert-file-contents
546 (expand-file-name "archive-contents"
547 package-archive-upload-base
))
548 (setq archive-contents
549 (package-read-from-string
550 (buffer-substring (point-min) (point-max)))))
551 (should (equal archive-contents
552 (list 1 package-x-test--single-archive-entry-1-4
))))))
554 (ert-deftest package-test-get-deps
()
555 "Test `package--get-deps' with complex structures."
557 (mapcar (lambda (p) (list (package-desc-name p
) p
))
558 (list simple-single-desc
563 simple-depend-desc-2
))))
565 (equal (package--get-deps 'simple-depend
)
568 (equal (package--get-deps 'simple-depend
'indirect
)
571 (equal (package--get-deps 'simple-depend
'direct
)
574 (equal (package--get-deps 'simple-depend-2
)
575 '(simple-depend-1 multi-file simple-depend simple-single
)))
577 (equal (package--get-deps 'simple-depend-2
'indirect
)
578 '(simple-depend multi-file simple-single
)))
580 (equal (package--get-deps 'simple-depend-2
'direct
)
581 '(simple-depend-1 multi-file
)))))
583 (ert-deftest package-test-sort-by-dependence
()
584 "Test `package--sort-by-dependence' with complex structures."
586 (mapcar (lambda (p) (list (package-desc-name p
) p
))
587 (list simple-single-desc
592 simple-depend-desc-2
)))
594 (list simple-single-desc
599 simple-depend-desc-2
)))
601 (equal (package--sort-by-dependence delete-list
)
602 (list simple-depend-desc-2 simple-depend-desc-1 new-pkg-desc
603 multi-file-desc simple-depend-desc simple-single-desc
)))
605 (equal (package--sort-by-dependence (reverse delete-list
))
606 (list new-pkg-desc simple-depend-desc-2 simple-depend-desc-1
607 multi-file-desc simple-depend-desc simple-single-desc
)))))
609 (provide 'package-test
)
611 ;;; package-test.el ends here