1 ;;; package-test.el --- Tests for the Emacs package system
3 ;; Copyright (C) 2013-2016 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 "package-resources" 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 "package-resources" :file "simple-single-1.3.el")
194 (should (equal (package-buffer-info) simple-single-desc)))
195 (with-package-test (:basedir "package-resources" :file "simple-depend-1.0.el")
196 (should (equal (package-buffer-info) simple-depend-desc)))
197 (with-package-test (:basedir "package-resources"
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 "package-resources" :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-macro-compilation ()
246 "Install a package which includes a dependency."
247 (with-package-test (:basedir "package-resources")
248 (package-install-file (expand-file-name "macro-problem-package-1.0/"))
249 (require 'macro-problem)
250 ;; `macro-problem-func' uses a macro from `macro-aux'.
251 (should (equal (macro-problem-func) '(progn a b)))
252 (package-install-file (expand-file-name "macro-problem-package-2.0/"))
253 ;; After upgrading, `macro-problem-func' depends on a new version
254 ;; of the macro from `macro-aux'.
255 (should (equal (macro-problem-func) '(1 b)))
256 ;; `macro-problem-10-and-90' depends on an entirely new macro from `macro-aux'.
257 (should (equal (macro-problem-10-and-90) '(10 90)))))
259 (ert-deftest package-test-install-two-dependencies ()
260 "Install a package which includes a dependency."
261 (with-package-test ()
263 (package-refresh-contents)
264 (package-install 'simple-two-depend)
265 (should (package-installed-p 'simple-single))
266 (should (package-installed-p 'simple-depend))
267 (should (package-installed-p 'simple-two-depend))))
269 (ert-deftest package-test-refresh-contents ()
270 "Parse an \"archive-contents\" file."
271 (with-package-test ()
273 (package-refresh-contents)
274 (should (eq 4 (length package-archive-contents)))))
276 (ert-deftest package-test-install-single-from-archive ()
277 "Install a single package from a package archive."
278 (with-package-test ()
280 (package-refresh-contents)
281 (package-install 'simple-single)))
283 (ert-deftest package-test-install-prioritized ()
284 "Install a lower version from a higher-prioritized archive."
285 (with-package-test ()
286 (let* ((newer-version (expand-file-name "package-resources/newer-versions"
287 package-test-file-dir))
288 (package-archives `(("older" . ,package-test-data-dir)
289 ("newer" . ,newer-version)))
290 (package-archive-priorities '(("older" . 100))))
293 (package-refresh-contents)
294 (package-install 'simple-single)
296 (let ((installed (cadr (assq 'simple-single package-alist))))
297 (should (version-list-= '(1 3)
298 (package-desc-version installed)))))))
300 (ert-deftest package-test-install-multifile ()
301 "Check properties of the installed multi-file package."
302 (with-package-test (:basedir "package-resources" :install '(multi-file))
304 (expand-file-name "multi-file-autoloads.el"
307 package-test-user-dir)))
308 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
309 "multi-file-autoloads.el" "multi-file.elc"))
310 (autoload-forms '("^(defvar multi-file-custom-var"
311 "^(custom-autoload 'multi-file-custom-var"
312 "^(autoload 'multi-file-mode"))
313 (pkg-dir (file-name-as-directory
316 package-test-user-dir))))
317 (package-refresh-contents)
318 (should (package-installed-p 'multi-file))
320 (insert-file-contents-literally autoload-file)
321 (dolist (fn installed-files)
322 (should (file-exists-p (expand-file-name fn pkg-dir))))
323 (dolist (re autoload-forms)
324 (goto-char (point-min))
325 (should (re-search-forward re nil t)))))))
327 (ert-deftest package-test-update-listing ()
328 "Ensure installed package status is updated."
329 (with-package-test ()
330 (let ((buf (package-list-packages)))
331 (search-forward-regexp "^ +simple-single")
332 (package-menu-mark-install)
333 (package-menu-execute)
334 (run-hooks 'post-command-hook)
335 (should (package-installed-p 'simple-single))
336 (switch-to-buffer "*Packages*")
337 (goto-char (point-min))
338 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t))
339 (goto-char (point-min))
340 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t))
343 (ert-deftest package-test-update-archives ()
344 "Test updating package archives."
345 (with-package-test ()
346 (let ((buf (package-list-packages)))
347 (package-menu-refresh)
348 (search-forward-regexp "^ +simple-single")
349 (package-menu-mark-install)
350 (package-menu-execute)
351 (should (package-installed-p 'simple-single))
352 (let ((package-test-data-dir
353 (expand-file-name "package-resources/newer-versions" package-test-file-dir)))
354 (setq package-archives `(("gnu" . ,package-test-data-dir)))
355 (package-menu-refresh)
357 ;; New version should be available and old version should be installed
358 (goto-char (point-min))
359 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+available" nil t))
360 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t))
362 (goto-char (point-min))
363 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t))
365 (package-menu-mark-upgrades)
366 (package-menu-execute)
367 (package-menu-refresh)
368 (should (package-installed-p 'simple-single '(1 4)))))))
370 (ert-deftest package-test-update-archives-async ()
371 "Test updating package archives asynchronously."
372 (skip-unless (executable-find "python2"))
373 (let* ((package-menu-async t)
374 (default-directory package-test-data-dir)
375 (process (start-process
376 "package-server" "package-server-buffer"
377 (executable-find "python2")
378 "package-test-server.py"))
382 (with-current-buffer "package-server-buffer"
384 (with-timeout (10 nil)
386 (accept-process-output nil 1)
387 (goto-char (point-min))
388 (if (re-search-forward "Serving HTTP on .* port \\([0-9]+\\) "
390 (setq port (match-string 1))))
392 (with-package-test (:basedir
393 package-test-data-dir
394 :location (format "http://0.0.0.0:%s/" port))
396 (should package--downloads-in-progress)
397 (should mode-line-process)
399 (with-timeout (10 'timeout)
400 (while package--downloads-in-progress
401 (accept-process-output nil 1))
403 ;; If the server process died, there's some non-Emacs problem.
404 ;; Eg maybe the port was already in use.
405 (skip-unless (process-live-p process))
406 (goto-char (point-min))
408 (search-forward-regexp "^ +simple-single" nil t))))
409 (if (process-live-p process) (kill-process process)))))
411 (ert-deftest package-test-describe-package ()
412 "Test displaying help for a package."
414 (require 'finder-inf)
416 (with-fake-help-buffer
417 (describe-package '5x5)
418 (goto-char (point-min))
419 (should (search-forward "5x5 is a built-in package." nil t))
420 ;; Don't assume the descriptions are in any particular order.
421 (save-excursion (should (search-forward "Status: Built-in." nil t)))
422 (save-excursion (should (search-forward "Summary: simple little puzzle game" nil t)))
423 (should (search-forward "The aim of 5x5" nil t)))
426 (with-package-test ()
428 (package-refresh-contents)
429 (package-install 'simple-single)
430 (with-fake-help-buffer
431 (describe-package 'simple-single)
432 (goto-char (point-min))
433 (should (search-forward "simple-single is an installed package." nil t))
434 (save-excursion (should (re-search-forward "Status: Installed in ['`‘]simple-single-1.3/['’] (unsigned)." nil t)))
435 (save-excursion (should (search-forward "Version: 1.3" nil t)))
436 (save-excursion (should (search-forward "Summary: A single-file package with no dependencies" nil t)))
437 (save-excursion (should (search-forward "Homepage: http://doodles.au" nil t)))
438 (save-excursion (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t)))
439 ;; No description, though. Because at this point we don't know
440 ;; what archive the package originated from, and we don't have
441 ;; its readme file saved.
444 (ert-deftest package-test-describe-non-installed-package ()
445 "Test displaying of the readme for non-installed package."
447 (with-package-test ()
449 (package-refresh-contents)
450 (with-fake-help-buffer
451 (describe-package 'simple-single)
452 (goto-char (point-min))
453 (should (search-forward "Homepage: http://doodles.au" nil t))
454 (should (search-forward "This package provides a minor mode to frobnicate"
457 (ert-deftest package-test-describe-non-installed-multi-file-package ()
458 "Test displaying of the readme for non-installed multi-file package."
460 (with-package-test ()
462 (package-refresh-contents)
463 (with-fake-help-buffer
464 (describe-package 'multi-file)
465 (goto-char (point-min))
466 (should (search-forward "Homepage: http://puddles.li" nil t))
467 (should (search-forward "This is a bare-bones readme file for the multi-file"
470 (ert-deftest package-test-signed ()
471 "Test verifying package signature."
472 (skip-unless (ignore-errors
473 (let ((homedir (make-temp-file "package-test" t)))
475 (let ((process-environment
476 (cons (format "HOME=%s" homedir)
477 process-environment)))
478 (epg-check-configuration (epg-configuration))
479 (epg-find-configuration 'OpenPGP))
480 (delete-directory homedir t)))))
481 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir))
482 (package-test-data-dir
483 (expand-file-name "package-resources/signed" package-test-file-dir)))
484 (with-package-test ()
486 (package-import-keyring keyring)
487 (package-refresh-contents)
488 (let ((package-check-signature 'allow-unsigned))
489 (should (package-install 'signed-good))
490 (should-error (package-install 'signed-bad)))
491 (let ((package-check-signature t))
492 (should (package-install 'signed-good))
493 (should-error (package-install 'signed-bad)))
494 (let ((package-check-signature nil))
495 (should (package-install 'signed-good))
496 (should (package-install 'signed-bad)))
497 ;; Check if the installed package status is updated.
498 (let ((buf (package-list-packages)))
499 (package-menu-refresh)
500 (should (re-search-forward
501 "^\\s-+signed-good\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-"
503 (should (string-equal (match-string-no-properties 1) "1.0"))
504 (should (string-equal (match-string-no-properties 2) "installed")))
505 ;; Check if the package description is updated.
506 (with-fake-help-buffer
507 (describe-package 'signed-good)
508 (goto-char (point-min))
509 (should (re-search-forward "signed-good is an? \\(\\S-+\\) package." nil t))
510 (should (string-equal (match-string-no-properties 1) "installed"))
511 (should (re-search-forward
512 "Status: Installed in ['`‘]signed-good-1.0/['’]."
517 ;;; Tests for package-x features.
521 (defvar package-x-test--single-archive-entry-1-3
523 (package-make-ac-desc '(1 3) nil
524 "A single-file package with no dependencies"
526 '((:authors ("J. R. Hacker" . "jrh@example.com"))
527 (:maintainer "J. R. Hacker" . "jrh@example.com")
528 (:url . "http://doodles.au"))))
529 "Expected contents of the archive entry from the \"simple-single\" package.")
531 (defvar package-x-test--single-archive-entry-1-4
533 (package-make-ac-desc '(1 4) nil
534 "A single-file package with no dependencies"
536 '((:authors ("J. R. Hacker" . "jrh@example.com"))
537 (:maintainer "J. R. Hacker" . "jrh@example.com"))))
538 "Expected contents of the archive entry from the updated \"simple-single\" package.")
540 (ert-deftest package-x-test-upload-buffer ()
541 "Test creating an \"archive-contents\" file"
542 (with-package-test (:basedir "package-resources"
543 :file "simple-single-1.3.el"
545 (package-upload-buffer)
546 (should (file-exists-p (expand-file-name "archive-contents"
547 package-archive-upload-base)))
548 (should (file-exists-p (expand-file-name "simple-single-1.3.el"
549 package-archive-upload-base)))
550 (should (file-exists-p (expand-file-name "simple-single-readme.txt"
551 package-archive-upload-base)))
553 (let (archive-contents)
555 (insert-file-contents
556 (expand-file-name "archive-contents"
557 package-archive-upload-base))
558 (setq archive-contents
559 (package-read-from-string
560 (buffer-substring (point-min) (point-max)))))
561 (should (equal archive-contents
562 (list 1 package-x-test--single-archive-entry-1-3))))))
564 (ert-deftest package-x-test-upload-new-version ()
565 "Test uploading a new version of a package"
566 (with-package-test (:basedir "package-resources"
567 :file "simple-single-1.3.el"
569 (package-upload-buffer)
571 (insert-file-contents "newer-versions/simple-single-1.4.el")
572 (package-upload-buffer))
574 (let (archive-contents)
576 (insert-file-contents
577 (expand-file-name "archive-contents"
578 package-archive-upload-base))
579 (setq archive-contents
580 (package-read-from-string
581 (buffer-substring (point-min) (point-max)))))
582 (should (equal archive-contents
583 (list 1 package-x-test--single-archive-entry-1-4))))))
585 (ert-deftest package-test-get-deps ()
586 "Test `package--get-deps' with complex structures."
588 (mapcar (lambda (p) (list (package-desc-name p) p))
589 (list simple-single-desc
594 simple-depend-desc-2))))
596 (equal (package--get-deps 'simple-depend)
599 (equal (package--get-deps 'simple-depend 'indirect)
602 (equal (package--get-deps 'simple-depend 'direct)
605 (equal (package--get-deps 'simple-depend-2)
606 '(simple-depend-1 multi-file simple-depend simple-single)))
608 (equal (package--get-deps 'simple-depend-2 'indirect)
609 '(simple-depend multi-file simple-single)))
611 (equal (package--get-deps 'simple-depend-2 'direct)
612 '(simple-depend-1 multi-file)))))
614 (ert-deftest package-test-sort-by-dependence ()
615 "Test `package--sort-by-dependence' with complex structures."
617 (mapcar (lambda (p) (list (package-desc-name p) p))
618 (list simple-single-desc
623 simple-depend-desc-2)))
625 (list simple-single-desc
630 simple-depend-desc-2)))
632 (equal (package--sort-by-dependence delete-list)
634 (list simple-depend-desc-2 simple-depend-desc-1 new-pkg-desc
635 multi-file-desc simple-depend-desc simple-single-desc)))
637 (equal (package--sort-by-dependence (reverse delete-list))
638 (list new-pkg-desc simple-depend-desc-2 simple-depend-desc-1
639 multi-file-desc simple-depend-desc simple-single-desc)))))
641 (provide 'package-test)
643 ;;; package-test.el ends here