1 ;;; package-test.el --- Tests for the Emacs package system
3 ;; Copyright (C) 2013-2014 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 (defvar package-test-user-dir nil
40 "Directory to use for installing packages during testing.")
42 (defvar package-test-file-dir
(file-name-directory (or load-file-name
44 "Directory of the actual \"package-test.el\" file.")
46 (defvar simple-single-desc
47 (package-desc-create :name
'simple-single
49 :summary
"A single-file package with no dependencies"
51 :extras
'((:url .
"http://doodles.au")))
52 "Expected `package-desc' parsed from simple-single-1.3.el.")
54 (defvar simple-depend-desc
55 (package-desc-create :name
'simple-depend
57 :summary
"A single-file package with a dependency."
59 :reqs
'((simple-single (1 3))))
60 "Expected `package-desc' parsed from simple-depend-1.0.el.")
62 (defvar multi-file-desc
63 (package-desc-create :name
'multi-file
65 :summary
"Example of a multi-file tar package"
67 :extras
'((:url .
"http://puddles.li")))
68 "Expected `package-desc' from \"multi-file-0.2.3.tar\".")
71 (package-desc-create :name
'new-pkg
74 "Expected `package-desc' parsed from new-pkg-1.0.el.")
76 (defvar package-test-data-dir
(expand-file-name "data/package" package-test-file-dir
)
77 "Base directory of package test files.")
79 (defvar package-test-fake-contents-file
80 (expand-file-name "archive-contents" package-test-data-dir
)
81 "Path to a static copy of \"archive-contents\".")
83 (cl-defmacro with-package-test
((&optional
&key file
89 "Set up temporary locations and variables for testing."
91 `(let* ((package-test-user-dir (make-temp-file "pkg-test-user-dir-" t
))
92 (process-environment (cons (format "HOME=%s" package-test-user-dir
)
94 (package-user-dir package-test-user-dir
)
95 (package-archives `(("gnu" .
,package-test-data-dir
)))
96 (old-yes-no-defn (symbol-function 'yes-or-no-p
))
97 (default-directory package-test-file-dir
)
101 '(package-update-news-on-upload t
)
104 '((package-test-archive-upload-base (make-temp-file "pkg-archive-base-" t
))
105 (package-archive-upload-base package-test-archive-upload-base
))
106 (list (cl-gensym)))) ;; Dummy value so `let' doesn't try to bind `nil'
109 ,(if basedir
`(cd ,basedir
))
110 (setf (symbol-function 'yes-or-no-p
) #'(lambda (&rest r
) t
))
111 (unless (file-directory-p package-user-dir
)
112 (mkdir package-user-dir
))
114 `((package-initialize)
115 (package-refresh-contents)
116 (mapc 'package-install
,install
)))
119 `(insert-file-contents ,file
))
122 (when (file-directory-p package-test-user-dir
)
123 (delete-directory package-test-user-dir t
))
125 (when (and (boundp 'package-test-archive-upload-base
)
126 (file-directory-p package-test-archive-upload-base
))
127 (delete-directory package-test-archive-upload-base t
))
128 (setf (symbol-function 'yes-or-no-p
) old-yes-no-defn
))))
130 (defmacro with-fake-help-buffer
(&rest body
)
131 "Execute BODY in a temp buffer which is treated as the \"*Help*\" buffer."
134 ;; Trick `help-buffer' into using the temp buffer.
135 (let ((help-xref-following t
))
138 (defun package-test-strip-version (dir)
139 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir
)))
141 (defun package-test-suffix-matches (base suffix-list
)
142 "Return file names matching BASE concatenated with each item in SUFFIX-LIST"
144 '(lambda (item) (file-expand-wildcards (concat base item
)))
147 (defvar tar-parse-info
)
148 (declare-function tar-header-name
"tar-mode" (cl-x) t
) ; defstruct
150 (defun package-test-search-tar-file (filename)
151 "Search the current buffer's `tar-parse-info' variable for FILENAME.
153 Must called from within a `tar-mode' buffer."
154 (cl-dolist (header tar-parse-info
)
155 (let ((tar-name (tar-header-name header
)))
156 (when (string= tar-name filename
)
159 (defun package-test-desc-version-string (desc)
160 "Return the package version as a string."
161 (package-version-join (package-desc-version desc
)))
163 (ert-deftest package-test-desc-from-buffer
()
164 "Parse an elisp buffer to get a `package-desc' object."
165 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
166 (should (equal (package-buffer-info) simple-single-desc
)))
167 (with-package-test (:basedir
"data/package" :file
"simple-depend-1.0.el")
168 (should (equal (package-buffer-info) simple-depend-desc
)))
169 (with-package-test (:basedir
"data/package"
170 :file
"multi-file-0.2.3.tar")
172 (should (equal (package-tar-file-info) multi-file-desc
))))
174 (ert-deftest package-test-install-single
()
175 "Install a single file without using an archive."
176 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
177 (should (package-install-from-buffer))
179 (should (package-installed-p 'simple-single
))
180 (let* ((simple-pkg-dir (file-name-as-directory
183 package-test-user-dir
)))
184 (autoloads-file (expand-file-name "simple-single-autoloads.el"
186 (should (file-directory-p simple-pkg-dir
))
188 (insert-file-contents (expand-file-name "simple-single-pkg.el"
190 (should (string= (buffer-string)
191 (concat ";;; -*- no-byte-compile: t -*-\n"
192 "(define-package \"simple-single\" \"1.3\" "
193 "\"A single-file package "
194 "with no dependencies\" 'nil "
195 ":url \"http://doodles.au\""
197 (should (file-exists-p autoloads-file
))
198 (should-not (get-file-buffer autoloads-file
)))))
200 (ert-deftest package-test-install-dependency
()
201 "Install a package which includes a dependency."
202 (with-package-test ()
204 (package-refresh-contents)
205 (package-install 'simple-depend
)
206 (should (package-installed-p 'simple-single
))
207 (should (package-installed-p 'simple-depend
))))
209 (ert-deftest package-test-install-two-dependencies
()
210 "Install a package which includes a dependency."
211 (with-package-test ()
213 (package-refresh-contents)
214 (package-install 'simple-two-depend
)
215 (should (package-installed-p 'simple-single
))
216 (should (package-installed-p 'simple-depend
))
217 (should (package-installed-p 'simple-two-depend
))))
219 (ert-deftest package-test-refresh-contents
()
220 "Parse an \"archive-contents\" file."
221 (with-package-test ()
223 (package-refresh-contents)
224 (should (eq 4 (length package-archive-contents
)))))
226 (ert-deftest package-test-install-single-from-archive
()
227 "Install a single package from a package archive."
228 (with-package-test ()
230 (package-refresh-contents)
231 (package-install 'simple-single
)))
233 (ert-deftest package-test-install-multifile
()
234 "Check properties of the installed multi-file package."
235 (with-package-test (:basedir
"data/package" :install
'(multi-file))
237 (expand-file-name "multi-file-autoloads.el"
240 package-test-user-dir
)))
241 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
242 "multi-file-autoloads.el" "multi-file.elc"))
243 (autoload-forms '("^(defvar multi-file-custom-var"
244 "^(custom-autoload 'multi-file-custom-var"
245 "^(autoload 'multi-file-mode"))
246 (pkg-dir (file-name-as-directory
249 package-test-user-dir
))))
250 (package-refresh-contents)
251 (should (package-installed-p 'multi-file
))
253 (insert-file-contents-literally autoload-file
)
254 (dolist (fn installed-files
)
255 (should (file-exists-p (expand-file-name fn pkg-dir
))))
256 (dolist (re autoload-forms
)
257 (goto-char (point-min))
258 (should (re-search-forward re nil t
)))))))
260 (ert-deftest package-test-update-listing
()
261 "Ensure installed package status is updated."
262 (with-package-test ()
263 (let ((buf (package-list-packages)))
264 (search-forward-regexp "^ +simple-single")
265 (package-menu-mark-install)
266 (package-menu-execute)
267 (should (package-installed-p 'simple-single
))
268 (switch-to-buffer "*Packages*")
269 (goto-char (point-min))
270 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
271 (goto-char (point-min))
272 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
275 (ert-deftest package-test-update-archives
()
276 "Test updating package archives."
277 (with-package-test ()
278 (let ((buf (package-list-packages)))
279 (package-menu-refresh)
280 (search-forward-regexp "^ +simple-single")
281 (package-menu-mark-install)
282 (package-menu-execute)
283 (should (package-installed-p 'simple-single
))
284 (let ((package-test-data-dir
285 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
286 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
287 (package-menu-refresh)
289 ;; New version should be available and old version should be installed
290 (goto-char (point-min))
291 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t
))
292 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
294 (goto-char (point-min))
295 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
297 (package-menu-mark-upgrades)
298 (package-menu-execute)
299 (package-menu-refresh)
300 (should (package-installed-p 'simple-single
'(1 4)))))))
302 (ert-deftest package-test-describe-package
()
303 "Test displaying help for a package."
305 (require 'finder-inf
)
307 (with-fake-help-buffer
308 (describe-package '5x5
)
309 (goto-char (point-min))
310 (should (search-forward "5x5 is a built-in package." nil t
))
311 (should (search-forward "Status: Built-in." nil t
))
312 (should (search-forward "Summary: simple little puzzle game" nil t
))
313 (should (search-forward "The aim of 5x5" nil t
)))
316 (with-package-test ()
318 (package-refresh-contents)
319 (package-install 'simple-single
)
320 (with-fake-help-buffer
321 (describe-package 'simple-single
)
322 (goto-char (point-min))
323 (should (search-forward "simple-single is an installed package." nil t
))
324 (should (search-forward
325 (format "Status: Installed in `%s/' (unsigned)."
326 (expand-file-name "simple-single-1.3" package-user-dir
))
328 (should (search-forward "Version: 1.3" nil t
))
329 (should (search-forward "Summary: A single-file package with no dependencies"
331 (should (search-forward "Homepage: http://doodles.au" nil t
))
332 (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
))
333 ;; No description, though. Because at this point we don't know
334 ;; what archive the package originated from, and we don't have
335 ;; its readme file saved.
338 (ert-deftest package-test-describe-non-installed-package
()
339 "Test displaying of the readme for non-installed package."
341 (with-package-test ()
343 (package-refresh-contents)
344 (with-fake-help-buffer
345 (describe-package 'simple-single
)
346 (goto-char (point-min))
347 (should (search-forward "Homepage: http://doodles.au" nil t
))
348 (should (search-forward "This package provides a minor mode to frobnicate"
351 (ert-deftest package-test-describe-non-installed-multi-file-package
()
352 "Test displaying of the readme for non-installed multi-file package."
354 (with-package-test ()
356 (package-refresh-contents)
357 (with-fake-help-buffer
358 (describe-package 'multi-file
)
359 (goto-char (point-min))
360 (should (search-forward "Homepage: http://puddles.li" nil t
))
361 (should (search-forward "This is a bare-bones readme file for the multi-file"
364 (ert-deftest package-test-signed
()
365 "Test verifying package signature."
366 (skip-unless (ignore-errors
367 (let ((homedir (make-temp-file "package-test" t
)))
369 (let ((process-environment
370 (cons (format "HOME=%s" homedir
)
371 process-environment
)))
372 (epg-check-configuration (epg-configuration))
374 (delete-directory homedir t
)))))
375 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
376 (package-test-data-dir
377 (expand-file-name "data/package/signed" package-test-file-dir
)))
378 (with-package-test ()
380 (package-import-keyring keyring
)
381 (package-refresh-contents)
382 (should (package-install 'signed-good
))
383 (should-error (package-install 'signed-bad
))
384 ;; Check if the installed package status is updated.
385 (let ((buf (package-list-packages)))
386 (package-menu-refresh)
387 (should (re-search-forward "^\\s-+signed-good\\s-+1\\.0\\s-+installed"
389 ;; Check if the package description is updated.
390 (with-fake-help-buffer
391 (describe-package 'signed-good
)
392 (goto-char (point-min))
393 (should (search-forward "signed-good is an installed package." nil t
))
394 (should (search-forward
395 (format "Status: Installed in `%s/'."
396 (expand-file-name "signed-good-1.0" package-user-dir
))
401 ;;; Tests for package-x features.
405 (defvar package-x-test--single-archive-entry-1-3
407 (package-make-ac-desc '(1 3) nil
408 "A single-file package with no dependencies"
410 '((:url .
"http://doodles.au"))))
411 "Expected contents of the archive entry from the \"simple-single\" package.")
413 (defvar package-x-test--single-archive-entry-1-4
415 (package-make-ac-desc '(1 4) nil
416 "A single-file package with no dependencies"
419 "Expected contents of the archive entry from the updated \"simple-single\" package.")
421 (ert-deftest package-x-test-upload-buffer
()
422 "Test creating an \"archive-contents\" file"
423 (with-package-test (:basedir
"data/package"
424 :file
"simple-single-1.3.el"
426 (package-upload-buffer)
427 (should (file-exists-p (expand-file-name "archive-contents"
428 package-archive-upload-base
)))
429 (should (file-exists-p (expand-file-name "simple-single-1.3.el"
430 package-archive-upload-base
)))
431 (should (file-exists-p (expand-file-name "simple-single-readme.txt"
432 package-archive-upload-base
)))
434 (let (archive-contents)
436 (insert-file-contents
437 (expand-file-name "archive-contents"
438 package-archive-upload-base
))
439 (setq archive-contents
440 (package-read-from-string
441 (buffer-substring (point-min) (point-max)))))
442 (should (equal archive-contents
443 (list 1 package-x-test--single-archive-entry-1-3
))))))
445 (ert-deftest package-x-test-upload-new-version
()
446 "Test uploading a new version of a package"
447 (with-package-test (:basedir
"data/package"
448 :file
"simple-single-1.3.el"
450 (package-upload-buffer)
452 (insert-file-contents "newer-versions/simple-single-1.4.el")
453 (package-upload-buffer))
455 (let (archive-contents)
457 (insert-file-contents
458 (expand-file-name "archive-contents"
459 package-archive-upload-base
))
460 (setq archive-contents
461 (package-read-from-string
462 (buffer-substring (point-min) (point-max)))))
463 (should (equal archive-contents
464 (list 1 package-x-test--single-archive-entry-1-4
))))))
466 (provide 'package-test
)
468 ;;; package-test.el ends here