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
'((:url .
"http://doodles.au")))
54 "Expected `package-desc' parsed from simple-single-1.3.el.")
56 (defvar simple-depend-desc
57 (package-desc-create :name
'simple-depend
59 :summary
"A single-file package with a dependency."
61 :reqs
'((simple-single (1 3))))
62 "Expected `package-desc' parsed from simple-depend-1.0.el.")
64 (defvar multi-file-desc
65 (package-desc-create :name
'multi-file
67 :summary
"Example of a multi-file tar package"
69 :extras
'((:url .
"http://puddles.li")))
70 "Expected `package-desc' from \"multi-file-0.2.3.tar\".")
73 (package-desc-create :name
'new-pkg
76 "Expected `package-desc' parsed from new-pkg-1.0.el.")
78 (defvar simple-depend-desc-1
79 (package-desc-create :name
'simple-depend-1
81 :summary
"A single-file package with a dependency."
83 :reqs
'((simple-depend (1 0))
85 "`package-desc' used for testing dependencies.")
87 (defvar simple-depend-desc-2
88 (package-desc-create :name
'simple-depend-2
90 :summary
"A single-file package with a dependency."
92 :reqs
'((simple-depend-1 (1 0))
94 "`package-desc' used for testing dependencies.")
96 (defvar package-test-data-dir
(expand-file-name "data/package" package-test-file-dir
)
97 "Base directory of package test files.")
99 (defvar package-test-fake-contents-file
100 (expand-file-name "archive-contents" package-test-data-dir
)
101 "Path to a static copy of \"archive-contents\".")
103 (cl-defmacro with-package-test
((&optional
&key file
110 "Set up temporary locations and variables for testing."
112 `(let* ((package-test-user-dir (make-temp-file "pkg-test-user-dir-" t
))
113 (process-environment (cons (format "HOME=%s" package-test-user-dir
)
114 process-environment
))
115 (package-user-dir package-test-user-dir
)
116 (package-archives `(("gnu" .
,(or ,location package-test-data-dir
))))
117 (default-directory package-test-file-dir
)
122 '(package-update-news-on-upload t
)
125 '((package-test-archive-upload-base (make-temp-file "pkg-archive-base-" t
))
126 (package-archive-upload-base package-test-archive-upload-base
))
127 (list (cl-gensym)))) ;; Dummy value so `let' doesn't try to bind `nil'
128 (let ((buf (get-buffer "*Packages*")))
129 (when (buffer-live-p buf
)
133 ,(if basedir
`(cd ,basedir
))
134 (unless (file-directory-p package-user-dir
)
135 (mkdir package-user-dir
))
136 (cl-letf (((symbol-function 'yes-or-no-p
) (lambda (&rest r
) t
))
137 ((symbol-function 'y-or-n-p
) (lambda (&rest r
) t
)))
139 `((package-initialize)
140 (package-refresh-contents)
141 (mapc 'package-install
,install
)))
144 `(insert-file-contents ,file
))
147 (when (file-directory-p package-test-user-dir
)
148 (delete-directory package-test-user-dir t
))
150 (when (and (boundp 'package-test-archive-upload-base
)
151 (file-directory-p package-test-archive-upload-base
))
152 (delete-directory package-test-archive-upload-base t
)))))
154 (defmacro with-fake-help-buffer
(&rest body
)
155 "Execute BODY in a temp buffer which is treated as the \"*Help*\" buffer."
158 ;; Trick `help-buffer' into using the temp buffer.
159 (let ((help-xref-following t
))
162 (defun package-test-strip-version (dir)
163 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir
)))
165 (defun package-test-suffix-matches (base suffix-list
)
166 "Return file names matching BASE concatenated with each item in SUFFIX-LIST"
168 '(lambda (item) (file-expand-wildcards (concat base item
)))
171 (defvar tar-parse-info
)
172 (declare-function tar-header-name
"tar-mode" (cl-x) t
) ; defstruct
174 (defun package-test-search-tar-file (filename)
175 "Search the current buffer's `tar-parse-info' variable for FILENAME.
177 Must called from within a `tar-mode' buffer."
178 (cl-dolist (header tar-parse-info
)
179 (let ((tar-name (tar-header-name header
)))
180 (when (string= tar-name filename
)
183 (defun package-test-desc-version-string (desc)
184 "Return the package version as a string."
185 (package-version-join (package-desc-version desc
)))
187 (ert-deftest package-test-desc-from-buffer
()
188 "Parse an elisp buffer to get a `package-desc' object."
189 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
190 (should (equal (package-buffer-info) simple-single-desc
)))
191 (with-package-test (:basedir
"data/package" :file
"simple-depend-1.0.el")
192 (should (equal (package-buffer-info) simple-depend-desc
)))
193 (with-package-test (:basedir
"data/package"
194 :file
"multi-file-0.2.3.tar")
196 (should (equal (package-tar-file-info) multi-file-desc
))))
198 (ert-deftest package-test-install-single
()
199 "Install a single file without using an archive."
200 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
201 (should (package-install-from-buffer))
203 (should (package-installed-p 'simple-single
))
204 (let* ((simple-pkg-dir (file-name-as-directory
207 package-test-user-dir
)))
208 (autoloads-file (expand-file-name "simple-single-autoloads.el"
210 (should (file-directory-p simple-pkg-dir
))
212 (insert-file-contents (expand-file-name "simple-single-pkg.el"
214 (should (string= (buffer-string)
215 (concat ";;; -*- no-byte-compile: t -*-\n"
216 "(define-package \"simple-single\" \"1.3\" "
217 "\"A single-file package "
218 "with no dependencies\" 'nil "
219 ":url \"http://doodles.au\""
221 (should (file-exists-p autoloads-file
))
222 (should-not (get-file-buffer autoloads-file
)))))
224 (ert-deftest package-test-install-dependency
()
225 "Install a package which includes a dependency."
226 (with-package-test ()
228 (package-refresh-contents)
229 (package-install 'simple-depend
)
230 (should (package-installed-p 'simple-single
))
231 (should (package-installed-p 'simple-depend
))))
233 (ert-deftest package-test-install-two-dependencies
()
234 "Install a package which includes a dependency."
235 (with-package-test ()
237 (package-refresh-contents)
238 (package-install 'simple-two-depend
)
239 (should (package-installed-p 'simple-single
))
240 (should (package-installed-p 'simple-depend
))
241 (should (package-installed-p 'simple-two-depend
))))
243 (ert-deftest package-test-refresh-contents
()
244 "Parse an \"archive-contents\" file."
245 (with-package-test ()
247 (package-refresh-contents)
248 (should (eq 4 (length package-archive-contents
)))))
250 (ert-deftest package-test-install-single-from-archive
()
251 "Install a single package from a package archive."
252 (with-package-test ()
254 (package-refresh-contents)
255 (package-install 'simple-single
)))
257 (ert-deftest package-test-install-prioritized
()
258 "Install a lower version from a higher-prioritized archive."
259 (with-package-test ()
260 (let* ((newer-version (expand-file-name "data/package/newer-versions"
261 package-test-file-dir
))
262 (package-archives `(("older" .
,package-test-data-dir
)
263 ("newer" .
,newer-version
)))
264 (package-archive-priorities '(("older" .
100))))
267 (package-refresh-contents)
268 (package-install 'simple-single
)
270 (let ((installed (cadr (assq 'simple-single package-alist
))))
271 (should (version-list-= '(1 3)
272 (package-desc-version installed
)))))))
274 (ert-deftest package-test-install-multifile
()
275 "Check properties of the installed multi-file package."
276 (with-package-test (:basedir
"data/package" :install
'(multi-file))
278 (expand-file-name "multi-file-autoloads.el"
281 package-test-user-dir
)))
282 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
283 "multi-file-autoloads.el" "multi-file.elc"))
284 (autoload-forms '("^(defvar multi-file-custom-var"
285 "^(custom-autoload 'multi-file-custom-var"
286 "^(autoload 'multi-file-mode"))
287 (pkg-dir (file-name-as-directory
290 package-test-user-dir
))))
291 (package-refresh-contents)
292 (should (package-installed-p 'multi-file
))
294 (insert-file-contents-literally autoload-file
)
295 (dolist (fn installed-files
)
296 (should (file-exists-p (expand-file-name fn pkg-dir
))))
297 (dolist (re autoload-forms
)
298 (goto-char (point-min))
299 (should (re-search-forward re nil t
)))))))
301 (ert-deftest package-test-update-listing
()
302 "Ensure installed package status is updated."
303 (with-package-test ()
304 (let ((buf (package-list-packages)))
305 (search-forward-regexp "^ +simple-single")
306 (package-menu-mark-install)
307 (package-menu-execute)
308 (should (package-installed-p 'simple-single
))
309 (switch-to-buffer "*Packages*")
310 (goto-char (point-min))
311 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
312 (goto-char (point-min))
313 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
316 (ert-deftest package-test-update-archives
()
317 "Test updating package archives."
318 (with-package-test ()
319 (let ((buf (package-list-packages)))
320 (package-menu-refresh)
321 (search-forward-regexp "^ +simple-single")
322 (package-menu-mark-install)
323 (package-menu-execute)
324 (should (package-installed-p 'simple-single
))
325 (let ((package-test-data-dir
326 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
327 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
328 (package-menu-refresh)
330 ;; New version should be available and old version should be installed
331 (goto-char (point-min))
332 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+available" nil t
))
333 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
335 (goto-char (point-min))
336 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
338 (package-menu-mark-upgrades)
339 (package-menu-execute)
340 (package-menu-refresh)
341 (should (package-installed-p 'simple-single
'(1 4)))))))
343 (ert-deftest package-test-update-archives-async
()
344 "Test updating package archives asynchronously."
345 (skip-unless (executable-find "python2"))
346 (with-package-test (:basedir
347 package-test-data-dir
348 :location
"http://0.0.0.0:8000/")
349 (let* ((package-menu-async t
)
350 (process (start-process
351 "package-server" "package-server-buffer"
352 (executable-find "python2")
353 (expand-file-name "package-test-server.py"))))
357 (should package--downloads-in-progress
)
358 (should mode-line-process
)
360 (with-timeout (10 'timeout
)
361 (while package--downloads-in-progress
362 (accept-process-output nil
1))
364 (goto-char (point-min))
366 (search-forward-regexp "^ +simple-single" nil t
)))
367 (kill-process process
)))))
369 (ert-deftest package-test-describe-package
()
370 "Test displaying help for a package."
372 (require 'finder-inf
)
374 (with-fake-help-buffer
375 (describe-package '5x5
)
376 (goto-char (point-min))
377 (should (search-forward "5x5 is a built-in package." nil t
))
378 (should (search-forward "Status: Built-in." nil t
))
379 (should (search-forward "Summary: simple little puzzle game" nil t
))
380 (should (search-forward "The aim of 5x5" nil t
)))
383 (with-package-test ()
385 (package-refresh-contents)
386 (package-install 'simple-single
)
387 (with-fake-help-buffer
388 (describe-package 'simple-single
)
389 (goto-char (point-min))
390 (should (search-forward "simple-single is an installed package." nil t
))
391 (should (search-forward
392 "Status: Installed in `~/simple-single-1.3/' (unsigned)."
394 (should (search-forward "Version: 1.3" nil t
))
395 (should (search-forward "Summary: A single-file package with no dependencies"
397 (should (search-forward "Homepage: http://doodles.au" nil t
))
398 (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
))
399 ;; No description, though. Because at this point we don't know
400 ;; what archive the package originated from, and we don't have
401 ;; its readme file saved.
404 (ert-deftest package-test-describe-non-installed-package
()
405 "Test displaying of the readme for non-installed package."
407 (with-package-test ()
409 (package-refresh-contents)
410 (with-fake-help-buffer
411 (describe-package 'simple-single
)
412 (goto-char (point-min))
413 (should (search-forward "Homepage: http://doodles.au" nil t
))
414 (should (search-forward "This package provides a minor mode to frobnicate"
417 (ert-deftest package-test-describe-non-installed-multi-file-package
()
418 "Test displaying of the readme for non-installed multi-file package."
420 (with-package-test ()
422 (package-refresh-contents)
423 (with-fake-help-buffer
424 (describe-package 'multi-file
)
425 (goto-char (point-min))
426 (should (search-forward "Homepage: http://puddles.li" nil t
))
427 (should (search-forward "This is a bare-bones readme file for the multi-file"
430 (ert-deftest package-test-signed
()
431 "Test verifying package signature."
432 (skip-unless (ignore-errors
433 (let ((homedir (make-temp-file "package-test" t
)))
435 (let ((process-environment
436 (cons (format "HOME=%s" homedir
)
437 process-environment
)))
438 (epg-check-configuration (epg-configuration))
440 (delete-directory homedir t
)))))
441 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
442 (package-test-data-dir
443 (expand-file-name "data/package/signed" package-test-file-dir
)))
444 (with-package-test ()
446 (package-import-keyring keyring
)
447 (package-refresh-contents)
448 (should (package-install 'signed-good
))
449 (should-error (package-install 'signed-bad
))
450 ;; Check if the installed package status is updated.
451 (let ((buf (package-list-packages)))
452 (package-menu-refresh)
453 (should (re-search-forward
454 "^\\s-+signed-good\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-"
456 (should (string-equal (match-string-no-properties 1) "1.0"))
457 (should (string-equal (match-string-no-properties 2) "installed")))
458 ;; Check if the package description is updated.
459 (with-fake-help-buffer
460 (describe-package 'signed-good
)
461 (goto-char (point-min))
462 (should (re-search-forward "signed-good is an? \\(\\S-+\\) package." nil t
))
463 (should (string-equal (match-string-no-properties 1) "installed"))
464 (should (search-forward
465 "Status: Installed in `~/signed-good-1.0/'."
470 ;;; Tests for package-x features.
474 (defvar package-x-test--single-archive-entry-1-3
476 (package-make-ac-desc '(1 3) nil
477 "A single-file package with no dependencies"
479 '((:url .
"http://doodles.au"))))
480 "Expected contents of the archive entry from the \"simple-single\" package.")
482 (defvar package-x-test--single-archive-entry-1-4
484 (package-make-ac-desc '(1 4) nil
485 "A single-file package with no dependencies"
488 "Expected contents of the archive entry from the updated \"simple-single\" package.")
490 (ert-deftest package-x-test-upload-buffer
()
491 "Test creating an \"archive-contents\" file"
492 (with-package-test (:basedir
"data/package"
493 :file
"simple-single-1.3.el"
495 (package-upload-buffer)
496 (should (file-exists-p (expand-file-name "archive-contents"
497 package-archive-upload-base
)))
498 (should (file-exists-p (expand-file-name "simple-single-1.3.el"
499 package-archive-upload-base
)))
500 (should (file-exists-p (expand-file-name "simple-single-readme.txt"
501 package-archive-upload-base
)))
503 (let (archive-contents)
505 (insert-file-contents
506 (expand-file-name "archive-contents"
507 package-archive-upload-base
))
508 (setq archive-contents
509 (package-read-from-string
510 (buffer-substring (point-min) (point-max)))))
511 (should (equal archive-contents
512 (list 1 package-x-test--single-archive-entry-1-3
))))))
514 (ert-deftest package-x-test-upload-new-version
()
515 "Test uploading a new version of a package"
516 (with-package-test (:basedir
"data/package"
517 :file
"simple-single-1.3.el"
519 (package-upload-buffer)
521 (insert-file-contents "newer-versions/simple-single-1.4.el")
522 (package-upload-buffer))
524 (let (archive-contents)
526 (insert-file-contents
527 (expand-file-name "archive-contents"
528 package-archive-upload-base
))
529 (setq archive-contents
530 (package-read-from-string
531 (buffer-substring (point-min) (point-max)))))
532 (should (equal archive-contents
533 (list 1 package-x-test--single-archive-entry-1-4
))))))
535 (ert-deftest package-test-get-deps
()
536 "Test `package--get-deps' with complex structures."
538 (mapcar (lambda (p) (list (package-desc-name p
) p
))
539 (list simple-single-desc
544 simple-depend-desc-2
))))
546 (equal (package--get-deps 'simple-depend
)
549 (equal (package--get-deps 'simple-depend
'indirect
)
552 (equal (package--get-deps 'simple-depend
'direct
)
555 (equal (package--get-deps 'simple-depend-2
)
556 '(simple-depend-1 multi-file simple-depend simple-single
)))
558 (equal (package--get-deps 'simple-depend-2
'indirect
)
559 '(simple-depend multi-file simple-single
)))
561 (equal (package--get-deps 'simple-depend-2
'direct
)
562 '(simple-depend-1 multi-file
)))))
564 (ert-deftest package-test-sort-by-dependence
()
565 "Test `package--sort-by-dependence' with complex structures."
567 (mapcar (lambda (p) (list (package-desc-name p
) p
))
568 (list simple-single-desc
573 simple-depend-desc-2
)))
575 (list simple-single-desc
580 simple-depend-desc-2
)))
582 (equal (package--sort-by-dependence delete-list
)
583 (list simple-depend-desc-2 simple-depend-desc-1 new-pkg-desc
584 multi-file-desc simple-depend-desc simple-single-desc
)))
586 (equal (package--sort-by-dependence (reverse delete-list
))
587 (list new-pkg-desc simple-depend-desc-2 simple-depend-desc-1
588 multi-file-desc simple-depend-desc simple-single-desc
)))))
590 (provide 'package-test
)
592 ;;; package-test.el ends here