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 "(define-package \"simple-single\" \"1.3\" "
192 "\"A single-file package "
193 "with no dependencies\" 'nil "
194 ":url \"http://doodles.au\""
196 (should (file-exists-p autoloads-file
))
197 (should-not (get-file-buffer autoloads-file
)))))
199 (ert-deftest package-test-install-dependency
()
200 "Install a package which includes a dependency."
201 (with-package-test ()
203 (package-refresh-contents)
204 (package-install 'simple-depend
)
205 (should (package-installed-p 'simple-single
))
206 (should (package-installed-p 'simple-depend
))))
208 (ert-deftest package-test-install-two-dependencies
()
209 "Install a package which includes a dependency."
210 (with-package-test ()
212 (package-refresh-contents)
213 (package-install 'simple-two-depend
)
214 (should (package-installed-p 'simple-single
))
215 (should (package-installed-p 'simple-depend
))
216 (should (package-installed-p 'simple-two-depend
))))
218 (ert-deftest package-test-refresh-contents
()
219 "Parse an \"archive-contents\" file."
220 (with-package-test ()
222 (package-refresh-contents)
223 (should (eq 4 (length package-archive-contents
)))))
225 (ert-deftest package-test-install-single-from-archive
()
226 "Install a single package from a package archive."
227 (with-package-test ()
229 (package-refresh-contents)
230 (package-install 'simple-single
)))
232 (ert-deftest package-test-install-multifile
()
233 "Check properties of the installed multi-file package."
234 (with-package-test (:basedir
"data/package" :install
'(multi-file))
236 (expand-file-name "multi-file-autoloads.el"
239 package-test-user-dir
)))
240 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
241 "multi-file-autoloads.el" "multi-file.elc"))
242 (autoload-forms '("^(defvar multi-file-custom-var"
243 "^(custom-autoload 'multi-file-custom-var"
244 "^(autoload 'multi-file-mode"))
245 (pkg-dir (file-name-as-directory
248 package-test-user-dir
))))
249 (package-refresh-contents)
250 (should (package-installed-p 'multi-file
))
252 (insert-file-contents-literally autoload-file
)
253 (dolist (fn installed-files
)
254 (should (file-exists-p (expand-file-name fn pkg-dir
))))
255 (dolist (re autoload-forms
)
256 (goto-char (point-min))
257 (should (re-search-forward re nil t
)))))))
259 (ert-deftest package-test-update-listing
()
260 "Ensure installed package status is updated."
261 (with-package-test ()
262 (let ((buf (package-list-packages)))
263 (search-forward-regexp "^ +simple-single")
264 (package-menu-mark-install)
265 (package-menu-execute)
266 (should (package-installed-p 'simple-single
))
267 (switch-to-buffer "*Packages*")
268 (goto-char (point-min))
269 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
270 (goto-char (point-min))
271 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
274 (ert-deftest package-test-update-archives
()
275 "Test updating package archives."
276 (with-package-test ()
277 (let ((buf (package-list-packages)))
278 (package-menu-refresh)
279 (search-forward-regexp "^ +simple-single")
280 (package-menu-mark-install)
281 (package-menu-execute)
282 (should (package-installed-p 'simple-single
))
283 (let ((package-test-data-dir
284 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
285 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
286 (package-menu-refresh)
288 ;; New version should be available and old version should be installed
289 (goto-char (point-min))
290 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t
))
291 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
293 (goto-char (point-min))
294 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
296 (package-menu-mark-upgrades)
297 (package-menu-execute)
298 (package-menu-refresh)
299 (should (package-installed-p 'simple-single
'(1 4)))))))
301 (ert-deftest package-test-describe-package
()
302 "Test displaying help for a package."
304 (require 'finder-inf
)
306 (with-fake-help-buffer
307 (describe-package '5x5
)
308 (goto-char (point-min))
309 (should (search-forward "5x5 is a built-in package." nil t
))
310 (should (search-forward "Status: Built-in." nil t
))
311 (should (search-forward "Summary: simple little puzzle game" nil t
))
312 (should (search-forward "The aim of 5x5" nil t
)))
315 (with-package-test ()
317 (package-refresh-contents)
318 (package-install 'simple-single
)
319 (with-fake-help-buffer
320 (describe-package 'simple-single
)
321 (goto-char (point-min))
322 (should (search-forward "simple-single is an installed package." nil t
))
323 (should (search-forward
324 (format "Status: Installed in `%s/' (unsigned)."
325 (expand-file-name "simple-single-1.3" package-user-dir
))
327 (should (search-forward "Version: 1.3" nil t
))
328 (should (search-forward "Summary: A single-file package with no dependencies"
330 (should (search-forward "Homepage: http://doodles.au" nil t
))
331 (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
))
332 ;; No description, though. Because at this point we don't know
333 ;; what archive the package originated from, and we don't have
334 ;; its readme file saved.
337 (ert-deftest package-test-describe-non-installed-package
()
338 "Test displaying of the readme for non-installed package."
340 (with-package-test ()
342 (package-refresh-contents)
343 (with-fake-help-buffer
344 (describe-package 'simple-single
)
345 (goto-char (point-min))
346 (should (search-forward "Homepage: http://doodles.au" nil t
))
347 (should (search-forward "This package provides a minor mode to frobnicate"
350 (ert-deftest package-test-describe-non-installed-multi-file-package
()
351 "Test displaying of the readme for non-installed multi-file package."
353 (with-package-test ()
355 (package-refresh-contents)
356 (with-fake-help-buffer
357 (describe-package 'multi-file
)
358 (goto-char (point-min))
359 (should (search-forward "Homepage: http://puddles.li" nil t
))
360 (should (search-forward "This is a bare-bones readme file for the multi-file"
363 (ert-deftest package-test-signed
()
364 "Test verifying package signature."
365 (skip-unless (ignore-errors
366 (let ((homedir (make-temp-file "package-test" t
)))
368 (let ((process-environment
369 (cons (format "HOME=%s" homedir
)
370 process-environment
)))
371 (epg-check-configuration (epg-configuration))
373 (delete-directory homedir t
)))))
374 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
375 (package-test-data-dir
376 (expand-file-name "data/package/signed" package-test-file-dir
)))
377 (with-package-test ()
379 (package-import-keyring keyring
)
380 (package-refresh-contents)
381 (should (package-install 'signed-good
))
382 (should-error (package-install 'signed-bad
))
383 ;; Check if the installed package status is updated.
384 (let ((buf (package-list-packages)))
385 (package-menu-refresh)
386 (should (re-search-forward "^\\s-+signed-good\\s-+1\\.0\\s-+installed"
388 ;; Check if the package description is updated.
389 (with-fake-help-buffer
390 (describe-package 'signed-good
)
391 (goto-char (point-min))
392 (should (search-forward "signed-good is an installed package." nil t
))
393 (should (search-forward
394 (format "Status: Installed in `%s/'."
395 (expand-file-name "signed-good-1.0" package-user-dir
))
398 (provide 'package-test
)
400 ;;; package-test.el ends here