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 "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-macro-compilation
()
246 "Install a package which includes a dependency."
247 (with-package-test (:basedir
"data/package")
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 "data/package/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
"data/package" :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 "data/package/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 ;; For some reason this test doesn't work reliably on hydra.nixos.org.
374 (skip-unless (not (getenv "NIX_STORE")))
375 (with-package-test (:basedir
376 package-test-data-dir
377 :location
"http://0.0.0.0:8000/")
378 (let* ((package-menu-async t
)
379 (process (start-process
380 "package-server" "package-server-buffer"
381 (executable-find "python2")
382 (expand-file-name "package-test-server.py"))))
386 (should package--downloads-in-progress
)
387 (should mode-line-process
)
389 (with-timeout (10 'timeout
)
390 (while package--downloads-in-progress
391 (accept-process-output nil
1))
393 ;; If the server process died, there's some non-Emacs problem.
394 ;; Eg maybe the port was already in use.
395 (skip-unless (process-live-p process
))
396 (goto-char (point-min))
398 (search-forward-regexp "^ +simple-single" nil t
)))
399 (if (process-live-p process
) (kill-process process
))))))
401 (ert-deftest package-test-describe-package
()
402 "Test displaying help for a package."
404 (require 'finder-inf
)
406 (with-fake-help-buffer
407 (describe-package '5x5
)
408 (goto-char (point-min))
409 (should (search-forward "5x5 is a built-in package." nil t
))
410 ;; Don't assume the descriptions are in any particular order.
411 (save-excursion (should (search-forward "Status: Built-in." nil t
)))
412 (save-excursion (should (search-forward "Summary: simple little puzzle game" nil t
)))
413 (should (search-forward "The aim of 5x5" nil t
)))
416 (with-package-test ()
418 (package-refresh-contents)
419 (package-install 'simple-single
)
420 (with-fake-help-buffer
421 (describe-package 'simple-single
)
422 (goto-char (point-min))
423 (should (search-forward "simple-single is an installed package." nil t
))
424 (save-excursion (should (re-search-forward "Status: Installed in ['`‘]simple-single-1.3/['’] (unsigned)." nil t
)))
425 (save-excursion (should (search-forward "Version: 1.3" nil t
)))
426 (save-excursion (should (search-forward "Summary: A single-file package with no dependencies" nil t
)))
427 (save-excursion (should (search-forward "Homepage: http://doodles.au" nil t
)))
428 (save-excursion (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
)))
429 ;; No description, though. Because at this point we don't know
430 ;; what archive the package originated from, and we don't have
431 ;; its readme file saved.
434 (ert-deftest package-test-describe-non-installed-package
()
435 "Test displaying of the readme for non-installed package."
437 (with-package-test ()
439 (package-refresh-contents)
440 (with-fake-help-buffer
441 (describe-package 'simple-single
)
442 (goto-char (point-min))
443 (should (search-forward "Homepage: http://doodles.au" nil t
))
444 (should (search-forward "This package provides a minor mode to frobnicate"
447 (ert-deftest package-test-describe-non-installed-multi-file-package
()
448 "Test displaying of the readme for non-installed multi-file package."
450 (with-package-test ()
452 (package-refresh-contents)
453 (with-fake-help-buffer
454 (describe-package 'multi-file
)
455 (goto-char (point-min))
456 (should (search-forward "Homepage: http://puddles.li" nil t
))
457 (should (search-forward "This is a bare-bones readme file for the multi-file"
460 (ert-deftest package-test-signed
()
461 "Test verifying package signature."
462 (skip-unless (ignore-errors
463 (let ((homedir (make-temp-file "package-test" t
)))
465 (let ((process-environment
466 (cons (format "HOME=%s" homedir
)
467 process-environment
)))
468 (epg-check-configuration (epg-configuration))
470 (delete-directory homedir t
)))))
471 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
472 (package-test-data-dir
473 (expand-file-name "data/package/signed" package-test-file-dir
)))
474 (with-package-test ()
476 (package-import-keyring keyring
)
477 (package-refresh-contents)
478 (should (package-install 'signed-good
))
479 (should-error (package-install 'signed-bad
))
480 ;; Check if the installed package status is updated.
481 (let ((buf (package-list-packages)))
482 (package-menu-refresh)
483 (should (re-search-forward
484 "^\\s-+signed-good\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-"
486 (should (string-equal (match-string-no-properties 1) "1.0"))
487 (should (string-equal (match-string-no-properties 2) "installed")))
488 ;; Check if the package description is updated.
489 (with-fake-help-buffer
490 (describe-package 'signed-good
)
491 (goto-char (point-min))
492 (should (re-search-forward "signed-good is an? \\(\\S-+\\) package." nil t
))
493 (should (string-equal (match-string-no-properties 1) "installed"))
494 (should (re-search-forward
495 "Status: Installed in ['`‘]signed-good-1.0/['’]."
500 ;;; Tests for package-x features.
504 (defvar package-x-test--single-archive-entry-1-3
506 (package-make-ac-desc '(1 3) nil
507 "A single-file package with no dependencies"
509 '((:authors
("J. R. Hacker" .
"jrh@example.com"))
510 (:maintainer
"J. R. Hacker" .
"jrh@example.com")
511 (:url .
"http://doodles.au"))))
512 "Expected contents of the archive entry from the \"simple-single\" package.")
514 (defvar package-x-test--single-archive-entry-1-4
516 (package-make-ac-desc '(1 4) nil
517 "A single-file package with no dependencies"
519 '((:authors
("J. R. Hacker" .
"jrh@example.com"))
520 (:maintainer
"J. R. Hacker" .
"jrh@example.com"))))
521 "Expected contents of the archive entry from the updated \"simple-single\" package.")
523 (ert-deftest package-x-test-upload-buffer
()
524 "Test creating an \"archive-contents\" file"
525 (with-package-test (:basedir
"data/package"
526 :file
"simple-single-1.3.el"
528 (package-upload-buffer)
529 (should (file-exists-p (expand-file-name "archive-contents"
530 package-archive-upload-base
)))
531 (should (file-exists-p (expand-file-name "simple-single-1.3.el"
532 package-archive-upload-base
)))
533 (should (file-exists-p (expand-file-name "simple-single-readme.txt"
534 package-archive-upload-base
)))
536 (let (archive-contents)
538 (insert-file-contents
539 (expand-file-name "archive-contents"
540 package-archive-upload-base
))
541 (setq archive-contents
542 (package-read-from-string
543 (buffer-substring (point-min) (point-max)))))
544 (should (equal archive-contents
545 (list 1 package-x-test--single-archive-entry-1-3
))))))
547 (ert-deftest package-x-test-upload-new-version
()
548 "Test uploading a new version of a package"
549 (with-package-test (:basedir
"data/package"
550 :file
"simple-single-1.3.el"
552 (package-upload-buffer)
554 (insert-file-contents "newer-versions/simple-single-1.4.el")
555 (package-upload-buffer))
557 (let (archive-contents)
559 (insert-file-contents
560 (expand-file-name "archive-contents"
561 package-archive-upload-base
))
562 (setq archive-contents
563 (package-read-from-string
564 (buffer-substring (point-min) (point-max)))))
565 (should (equal archive-contents
566 (list 1 package-x-test--single-archive-entry-1-4
))))))
568 (ert-deftest package-test-get-deps
()
569 "Test `package--get-deps' with complex structures."
571 (mapcar (lambda (p) (list (package-desc-name p
) p
))
572 (list simple-single-desc
577 simple-depend-desc-2
))))
579 (equal (package--get-deps 'simple-depend
)
582 (equal (package--get-deps 'simple-depend
'indirect
)
585 (equal (package--get-deps 'simple-depend
'direct
)
588 (equal (package--get-deps 'simple-depend-2
)
589 '(simple-depend-1 multi-file simple-depend simple-single
)))
591 (equal (package--get-deps 'simple-depend-2
'indirect
)
592 '(simple-depend multi-file simple-single
)))
594 (equal (package--get-deps 'simple-depend-2
'direct
)
595 '(simple-depend-1 multi-file
)))))
597 (ert-deftest package-test-sort-by-dependence
()
598 "Test `package--sort-by-dependence' with complex structures."
600 (mapcar (lambda (p) (list (package-desc-name p
) p
))
601 (list simple-single-desc
606 simple-depend-desc-2
)))
608 (list simple-single-desc
613 simple-depend-desc-2
)))
615 (equal (package--sort-by-dependence delete-list
)
616 (list simple-depend-desc-2 simple-depend-desc-1 new-pkg-desc
617 multi-file-desc simple-depend-desc simple-single-desc
)))
619 (equal (package--sort-by-dependence (reverse delete-list
))
620 (list new-pkg-desc simple-depend-desc-2 simple-depend-desc-1
621 multi-file-desc simple-depend-desc simple-single-desc
)))))
623 (provide 'package-test
)
625 ;;; package-test.el ends here