Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / test / automated / package-test.el
blob34a3ce25a483ebabfc47635111154d78261e7219
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>
6 ;; Version: 1.0
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/>.
23 ;;; Commentary:
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
33 ;;; Code:
35 (require 'package)
36 (require 'ert)
37 (require 'cl-lib)
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
43 buffer-file-name))
44 "Directory of the actual \"package-test.el\" file.")
46 (defvar simple-single-desc
47 (package-desc-create :name 'simple-single
48 :version '(1 3)
49 :summary "A single-file package with no dependencies"
50 :kind 'single
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
56 :version '(1 0)
57 :summary "A single-file package with a dependency."
58 :kind 'single
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
64 :version '(0 2 3)
65 :summary "Example of a multi-file tar package"
66 :kind 'tar
67 :extras '((:url . "http://puddles.li")))
68 "Expected `package-desc' from \"multi-file-0.2.3.tar\".")
70 (defvar new-pkg-desc
71 (package-desc-create :name 'new-pkg
72 :version '(1 0)
73 :kind 'single)
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
84 basedir
85 install
86 update-news
87 upload-base)
88 &rest body)
89 "Set up temporary locations and variables for testing."
90 (declare (indent 1))
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)
96 package--initialized
97 package-alist
98 ,@(if update-news
99 '(package-update-news-on-upload t)
100 (list (cl-gensym)))
101 ,@(if upload-base
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'
105 (unwind-protect
106 (progn
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))
111 ,@(when install
112 `((package-initialize)
113 (package-refresh-contents)
114 (mapc 'package-install ,install)))
115 (with-temp-buffer
116 ,(if file
117 `(insert-file-contents ,file))
118 ,@body))
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."
130 `(with-temp-buffer
131 (help-mode)
132 ;; Trick `help-buffer' into using the temp buffer.
133 (let ((help-xref-following t))
134 ,@body)))
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"
141 (cl-mapcan
142 '(lambda (item) (file-expand-wildcards (concat base item)))
143 suffix-list))
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)
155 (cl-return t)))))
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")
169 (tar-mode)
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))
176 (package-initialize)
177 (should (package-installed-p 'simple-single))
178 (let* ((simple-pkg-dir (file-name-as-directory
179 (expand-file-name
180 "simple-single-1.3"
181 package-test-user-dir)))
182 (autoloads-file (expand-file-name "simple-single-autoloads.el"
183 simple-pkg-dir)))
184 (should (file-directory-p simple-pkg-dir))
185 (with-temp-buffer
186 (insert-file-contents (expand-file-name "simple-single-pkg.el"
187 simple-pkg-dir))
188 (should (string= (buffer-string)
189 (concat "(define-package \"simple-single\" \"1.3\" "
190 "\"A single-file package "
191 "with no dependencies\" 'nil "
192 ":url \"http://doodles.au\""
193 ")\n"))))
194 (should (file-exists-p autoloads-file))
195 (should-not (get-file-buffer autoloads-file)))))
197 (ert-deftest package-test-install-dependency ()
198 "Install a package which includes a dependency."
199 (with-package-test ()
200 (package-initialize)
201 (package-refresh-contents)
202 (package-install 'simple-depend)
203 (should (package-installed-p 'simple-single))
204 (should (package-installed-p 'simple-depend))))
206 (ert-deftest package-test-refresh-contents ()
207 "Parse an \"archive-contents\" file."
208 (with-package-test ()
209 (package-initialize)
210 (package-refresh-contents)
211 (should (eq 3 (length package-archive-contents)))))
213 (ert-deftest package-test-install-single-from-archive ()
214 "Install a single package from a package archive."
215 (with-package-test ()
216 (package-initialize)
217 (package-refresh-contents)
218 (package-install 'simple-single)))
220 (ert-deftest package-test-install-multifile ()
221 "Check properties of the installed multi-file package."
222 (with-package-test (:basedir "data/package" :install '(multi-file))
223 (let ((autoload-file
224 (expand-file-name "multi-file-autoloads.el"
225 (expand-file-name
226 "multi-file-0.2.3"
227 package-test-user-dir)))
228 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
229 "multi-file-autoloads.el" "multi-file.elc"))
230 (autoload-forms '("^(defvar multi-file-custom-var"
231 "^(custom-autoload 'multi-file-custom-var"
232 "^(autoload 'multi-file-mode"))
233 (pkg-dir (file-name-as-directory
234 (expand-file-name
235 "multi-file-0.2.3"
236 package-test-user-dir))))
237 (package-refresh-contents)
238 (should (package-installed-p 'multi-file))
239 (with-temp-buffer
240 (insert-file-contents-literally autoload-file)
241 (dolist (fn installed-files)
242 (should (file-exists-p (expand-file-name fn pkg-dir))))
243 (dolist (re autoload-forms)
244 (goto-char (point-min))
245 (should (re-search-forward re nil t)))))))
247 (ert-deftest package-test-update-listing ()
248 "Ensure installed package status is updated."
249 (with-package-test ()
250 (let ((buf (package-list-packages)))
251 (search-forward-regexp "^ +simple-single")
252 (package-menu-mark-install)
253 (package-menu-execute)
254 (should (package-installed-p 'simple-single))
255 (switch-to-buffer "*Packages*")
256 (goto-char (point-min))
257 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+unsigned" nil t))
258 (goto-char (point-min))
259 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t))
260 (kill-buffer buf))))
262 (ert-deftest package-test-update-archives ()
263 "Test updating package archives."
264 (with-package-test ()
265 (let ((buf (package-list-packages)))
266 (package-menu-refresh)
267 (search-forward-regexp "^ +simple-single")
268 (package-menu-mark-install)
269 (package-menu-execute)
270 (should (package-installed-p 'simple-single))
271 (let ((package-test-data-dir
272 (expand-file-name "data/package/newer-versions" package-test-file-dir)))
273 (setq package-archives `(("gnu" . ,package-test-data-dir)))
274 (package-menu-refresh)
276 ;; New version should be available and old version should be installed
277 (goto-char (point-min))
278 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t))
279 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+unsigned" nil t))
281 (goto-char (point-min))
282 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t))
284 (package-menu-mark-upgrades)
285 (package-menu-execute)
286 (package-menu-refresh)
287 (should (package-installed-p 'simple-single '(1 4)))))))
289 (ert-deftest package-test-describe-package ()
290 "Test displaying help for a package."
292 (require 'finder-inf)
293 ;; Built-in
294 (with-fake-help-buffer
295 (describe-package '5x5)
296 (goto-char (point-min))
297 (should (search-forward "5x5 is a built-in package." nil t))
298 (should (search-forward "Status: Built-in." nil t))
299 (should (search-forward "Summary: simple little puzzle game" nil t))
300 (should (search-forward "The aim of 5x5" nil t)))
302 ;; Installed
303 (with-package-test ()
304 (package-initialize)
305 (package-refresh-contents)
306 (package-install 'simple-single)
307 (with-fake-help-buffer
308 (describe-package 'simple-single)
309 (goto-char (point-min))
310 (should (search-forward "simple-single is an unsigned package." nil t))
311 (should (search-forward
312 (format "Status: Installed in `%s/' (unsigned)."
313 (expand-file-name "simple-single-1.3" package-user-dir))
314 nil t))
315 (should (search-forward "Version: 1.3" nil t))
316 (should (search-forward "Summary: A single-file package with no dependencies"
317 nil t))
318 (should (search-forward "Homepage: http://doodles.au" nil t))
319 ;; No description, though. Because at this point we don't know
320 ;; what archive the package originated from, and we don't have
321 ;; its readme file saved.
324 (ert-deftest package-test-describe-non-installed-package ()
325 "Test displaying of the readme for non-installed package."
327 (with-package-test ()
328 (package-initialize)
329 (package-refresh-contents)
330 (with-fake-help-buffer
331 (describe-package 'simple-single)
332 (goto-char (point-min))
333 (should (search-forward "Homepage: http://doodles.au" nil t))
334 (should (search-forward "This package provides a minor mode to frobnicate"
335 nil t)))))
337 (ert-deftest package-test-describe-non-installed-multi-file-package ()
338 "Test displaying of the readme for non-installed multi-file package."
340 (with-package-test ()
341 (package-initialize)
342 (package-refresh-contents)
343 (with-fake-help-buffer
344 (describe-package 'multi-file)
345 (goto-char (point-min))
346 (should (search-forward "Homepage: http://puddles.li" nil t))
347 (should (search-forward "This is a bare-bones readme file for the multi-file"
348 nil t)))))
350 (ert-deftest package-test-signed ()
351 "Test verifying package signature."
352 :expected-result (condition-case nil
353 (progn
354 (epg-check-configuration (epg-configuration))
355 :passed)
356 (error :failed))
357 (let* ((keyring (expand-file-name "key.pub" package-test-data-dir))
358 (package-test-data-dir
359 (expand-file-name "data/package/signed" package-test-file-dir)))
360 (with-package-test ()
361 (package-initialize)
362 (package-import-keyring keyring)
363 (package-refresh-contents)
364 (should (package-install 'signed-good))
365 (should-error (package-install 'signed-bad))
366 ;; Check if the installed package status is updated.
367 (let ((buf (package-list-packages)))
368 (package-menu-refresh)
369 (should (re-search-forward "^\\s-+signed-good\\s-+1\\.0\\s-+installed"
370 nil t)))
371 ;; Check if the package description is updated.
372 (with-fake-help-buffer
373 (describe-package 'signed-good)
374 (goto-char (point-min))
375 (should (search-forward "signed-good is an installed package." nil t))
376 (should (search-forward
377 (format "Status: Installed in `%s/'."
378 (expand-file-name "signed-good-1.0" package-user-dir))
379 nil t))))))
381 (provide 'package-test)
383 ;;; package-test.el ends here