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 (package-user-dir package-test-user-dir
)
93 (package-archives `(("gnu" .
,package-test-data-dir
)))
94 (old-yes-no-defn (symbol-function 'yes-or-no-p
))
95 (default-directory package-test-file-dir
)
99 '(package-update-news-on-upload t
)
102 '((package-test-archive-upload-base (make-temp-file "pkg-archive-base-" t
))
103 (package-archive-upload-base package-test-archive-upload-base
))
104 (list (cl-gensym)))) ;; Dummy value so `let' doesn't try to bind `nil'
107 ,(if basedir
`(cd ,basedir
))
108 (setf (symbol-function 'yes-or-no-p
) #'(lambda (&rest r
) t
))
109 (unless (file-directory-p package-user-dir
)
110 (mkdir package-user-dir
))
112 `((package-initialize)
113 (package-refresh-contents)
114 (mapc 'package-install
,install
)))
117 `(insert-file-contents ,file
))
120 (when (file-directory-p package-test-user-dir
)
121 (delete-directory package-test-user-dir t
))
123 (when (and (boundp 'package-test-archive-upload-base
)
124 (file-directory-p package-test-archive-upload-base
))
125 (delete-directory package-test-archive-upload-base t
))
126 (setf (symbol-function 'yes-or-no-p
) old-yes-no-defn
))))
128 (defmacro with-fake-help-buffer
(&rest body
)
129 "Execute BODY in a temp buffer which is treated as the \"*Help*\" buffer."
132 ;; Trick `help-buffer' into using the temp buffer.
133 (let ((help-xref-following t
))
136 (defun package-test-strip-version (dir)
137 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir
)))
139 (defun package-test-suffix-matches (base suffix-list
)
140 "Return file names matching BASE concatenated with each item in SUFFIX-LIST"
142 '(lambda (item) (file-expand-wildcards (concat base item
)))
145 (defvar tar-parse-info
)
146 (declare-function tar-header-name
"tar-mode" (cl-x) t
) ; defstruct
148 (defun package-test-search-tar-file (filename)
149 "Search the current buffer's `tar-parse-info' variable for FILENAME.
151 Must called from within a `tar-mode' buffer."
152 (cl-dolist (header tar-parse-info
)
153 (let ((tar-name (tar-header-name header
)))
154 (when (string= tar-name filename
)
157 (defun package-test-desc-version-string (desc)
158 "Return the package version as a string."
159 (package-version-join (package-desc-version desc
)))
161 (ert-deftest package-test-desc-from-buffer
()
162 "Parse an elisp buffer to get a `package-desc' object."
163 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
164 (should (equal (package-buffer-info) simple-single-desc
)))
165 (with-package-test (:basedir
"data/package" :file
"simple-depend-1.0.el")
166 (should (equal (package-buffer-info) simple-depend-desc
)))
167 (with-package-test (:basedir
"data/package"
168 :file
"multi-file-0.2.3.tar")
170 (should (equal (package-tar-file-info) multi-file-desc
))))
172 (ert-deftest package-test-install-single
()
173 "Install a single file without using an archive."
174 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
175 (should (package-install-from-buffer))
177 (should (package-installed-p 'simple-single
))
178 (let* ((simple-pkg-dir (file-name-as-directory
181 package-test-user-dir
)))
182 (autoloads-file (expand-file-name "simple-single-autoloads.el"
184 (should (file-directory-p simple-pkg-dir
))
186 (insert-file-contents (expand-file-name "simple-single-pkg.el"
188 (should (string= (buffer-string)
189 (concat ";;; -*- no-byte-compile: t -*-\n"
190 "(define-package \"simple-single\" \"1.3\" "
191 "\"A single-file package "
192 "with no dependencies\" 'nil "
193 ":url \"http://doodles.au\""
195 (should (file-exists-p autoloads-file
))
196 (should-not (get-file-buffer autoloads-file
)))))
198 (ert-deftest package-test-install-dependency
()
199 "Install a package which includes a dependency."
200 (with-package-test ()
202 (package-refresh-contents)
203 (package-install 'simple-depend
)
204 (should (package-installed-p 'simple-single
))
205 (should (package-installed-p 'simple-depend
))))
207 (ert-deftest package-test-install-two-dependencies
()
208 "Install a package which includes a dependency."
209 (with-package-test ()
211 (package-refresh-contents)
212 (package-install 'simple-two-depend
)
213 (should (package-installed-p 'simple-single
))
214 (should (package-installed-p 'simple-depend
))
215 (should (package-installed-p 'simple-two-depend
))))
217 (ert-deftest package-test-refresh-contents
()
218 "Parse an \"archive-contents\" file."
219 (with-package-test ()
221 (package-refresh-contents)
222 (should (eq 4 (length package-archive-contents
)))))
224 (ert-deftest package-test-install-single-from-archive
()
225 "Install a single package from a package archive."
226 (with-package-test ()
228 (package-refresh-contents)
229 (package-install 'simple-single
)))
231 (ert-deftest package-test-install-multifile
()
232 "Check properties of the installed multi-file package."
233 (with-package-test (:basedir
"data/package" :install
'(multi-file))
235 (expand-file-name "multi-file-autoloads.el"
238 package-test-user-dir
)))
239 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
240 "multi-file-autoloads.el" "multi-file.elc"))
241 (autoload-forms '("^(defvar multi-file-custom-var"
242 "^(custom-autoload 'multi-file-custom-var"
243 "^(autoload 'multi-file-mode"))
244 (pkg-dir (file-name-as-directory
247 package-test-user-dir
))))
248 (package-refresh-contents)
249 (should (package-installed-p 'multi-file
))
251 (insert-file-contents-literally autoload-file
)
252 (dolist (fn installed-files
)
253 (should (file-exists-p (expand-file-name fn pkg-dir
))))
254 (dolist (re autoload-forms
)
255 (goto-char (point-min))
256 (should (re-search-forward re nil t
)))))))
258 (ert-deftest package-test-update-listing
()
259 "Ensure installed package status is updated."
260 (with-package-test ()
261 (let ((buf (package-list-packages)))
262 (search-forward-regexp "^ +simple-single")
263 (package-menu-mark-install)
264 (package-menu-execute)
265 (should (package-installed-p 'simple-single
))
266 (switch-to-buffer "*Packages*")
267 (goto-char (point-min))
268 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
269 (goto-char (point-min))
270 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
273 (ert-deftest package-test-update-archives
()
274 "Test updating package archives."
275 (with-package-test ()
276 (let ((buf (package-list-packages)))
277 (package-menu-refresh)
278 (search-forward-regexp "^ +simple-single")
279 (package-menu-mark-install)
280 (package-menu-execute)
281 (should (package-installed-p 'simple-single
))
282 (let ((package-test-data-dir
283 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
284 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
285 (package-menu-refresh)
287 ;; New version should be available and old version should be installed
288 (goto-char (point-min))
289 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t
))
290 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
292 (goto-char (point-min))
293 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
295 (package-menu-mark-upgrades)
296 (package-menu-execute)
297 (package-menu-refresh)
298 (should (package-installed-p 'simple-single
'(1 4)))))))
300 (ert-deftest package-test-describe-package
()
301 "Test displaying help for a package."
303 (require 'finder-inf
)
305 (with-fake-help-buffer
306 (describe-package '5x5
)
307 (goto-char (point-min))
308 (should (search-forward "5x5 is a built-in package." nil t
))
309 (should (search-forward "Status: Built-in." nil t
))
310 (should (search-forward "Summary: simple little puzzle game" nil t
))
311 (should (search-forward "The aim of 5x5" nil t
)))
314 (with-package-test ()
316 (package-refresh-contents)
317 (package-install 'simple-single
)
318 (with-fake-help-buffer
319 (describe-package 'simple-single
)
320 (goto-char (point-min))
321 (should (search-forward "simple-single is an installed package." nil t
))
322 (should (search-forward
323 (format "Status: Installed in `%s/' (unsigned)."
324 (expand-file-name "simple-single-1.3" package-user-dir
))
326 (should (search-forward "Version: 1.3" nil t
))
327 (should (search-forward "Summary: A single-file package with no dependencies"
329 (should (search-forward "Homepage: http://doodles.au" nil t
))
330 (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t
))
331 ;; No description, though. Because at this point we don't know
332 ;; what archive the package originated from, and we don't have
333 ;; its readme file saved.
336 (ert-deftest package-test-describe-non-installed-package
()
337 "Test displaying of the readme for non-installed package."
339 (with-package-test ()
341 (package-refresh-contents)
342 (with-fake-help-buffer
343 (describe-package 'simple-single
)
344 (goto-char (point-min))
345 (should (search-forward "Homepage: http://doodles.au" nil t
))
346 (should (search-forward "This package provides a minor mode to frobnicate"
349 (ert-deftest package-test-describe-non-installed-multi-file-package
()
350 "Test displaying of the readme for non-installed multi-file package."
352 (with-package-test ()
354 (package-refresh-contents)
355 (with-fake-help-buffer
356 (describe-package 'multi-file
)
357 (goto-char (point-min))
358 (should (search-forward "Homepage: http://puddles.li" nil t
))
359 (should (search-forward "This is a bare-bones readme file for the multi-file"
362 (ert-deftest package-test-signed
()
363 "Test verifying package signature."
364 :expected-result
(condition-case nil
366 (epg-check-configuration (epg-configuration))
369 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir
))
370 (package-test-data-dir
371 (expand-file-name "data/package/signed" package-test-file-dir
)))
372 (with-package-test ()
374 (package-import-keyring keyring
)
375 (package-refresh-contents)
376 (should (package-install 'signed-good
))
377 (should-error (package-install 'signed-bad
))
378 ;; Check if the installed package status is updated.
379 (let ((buf (package-list-packages)))
380 (package-menu-refresh)
381 (should (re-search-forward "^\\s-+signed-good\\s-+1\\.0\\s-+installed"
383 ;; Check if the package description is updated.
384 (with-fake-help-buffer
385 (describe-package 'signed-good
)
386 (goto-char (point-min))
387 (should (search-forward "signed-good is an installed package." nil t
))
388 (should (search-forward
389 (format "Status: Installed in `%s/'."
390 (expand-file-name "signed-good-1.0" package-user-dir
))
393 (provide 'package-test
)
395 ;;; package-test.el ends here