update unidata-gen.el
[bpt/emacs.git] / test / automated / package-test.el
1 ;;; package-test.el --- Tests for the Emacs package system
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Daniel Hackney <dan@haxney.org>
6 ;; Version: 1.0
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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.
28
29 ;; Run this in a clean Emacs session using:
30 ;;
31 ;; $ emacs -Q --batch -L . -l package-test.el -l ert -f ert-run-tests-batch-and-exit
32
33 ;;; Code:
34
35 (require 'package)
36 (require 'ert)
37 (require 'cl-lib)
38
39 (defvar package-test-user-dir nil
40 "Directory to use for installing packages during testing.")
41
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.")
45
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.")
53
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.")
61
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\".")
69
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.")
75
76 (defvar package-test-data-dir (expand-file-name "data/package" package-test-file-dir)
77 "Base directory of package test files.")
78
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\".")
82
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))
119
120 (when (file-directory-p package-test-user-dir)
121 (delete-directory package-test-user-dir t))
122
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))))
127
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)))
135
136 (defun package-test-strip-version (dir)
137 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir)))
138
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))
144
145 (defvar tar-parse-info)
146 (declare-function tar-header-name "tar-mode" (cl-x) t) ; defstruct
147
148 (defun package-test-search-tar-file (filename)
149 "Search the current buffer's `tar-parse-info' variable for FILENAME.
150
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)))))
156
157 (defun package-test-desc-version-string (desc)
158 "Return the package version as a string."
159 (package-version-join (package-desc-version desc)))
160
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))))
171
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 ";;; -*- 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\""
194 ")\n"))))
195 (should (file-exists-p autoloads-file))
196 (should-not (get-file-buffer autoloads-file)))))
197
198 (ert-deftest package-test-install-dependency ()
199 "Install a package which includes a dependency."
200 (with-package-test ()
201 (package-initialize)
202 (package-refresh-contents)
203 (package-install 'simple-depend)
204 (should (package-installed-p 'simple-single))
205 (should (package-installed-p 'simple-depend))))
206
207 (ert-deftest package-test-install-two-dependencies ()
208 "Install a package which includes a dependency."
209 (with-package-test ()
210 (package-initialize)
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))))
216
217 (ert-deftest package-test-refresh-contents ()
218 "Parse an \"archive-contents\" file."
219 (with-package-test ()
220 (package-initialize)
221 (package-refresh-contents)
222 (should (eq 4 (length package-archive-contents)))))
223
224 (ert-deftest package-test-install-single-from-archive ()
225 "Install a single package from a package archive."
226 (with-package-test ()
227 (package-initialize)
228 (package-refresh-contents)
229 (package-install 'simple-single)))
230
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))
234 (let ((autoload-file
235 (expand-file-name "multi-file-autoloads.el"
236 (expand-file-name
237 "multi-file-0.2.3"
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
245 (expand-file-name
246 "multi-file-0.2.3"
247 package-test-user-dir))))
248 (package-refresh-contents)
249 (should (package-installed-p 'multi-file))
250 (with-temp-buffer
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)))))))
257
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))
271 (kill-buffer buf))))
272
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)
286
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))
291
292 (goto-char (point-min))
293 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t))
294
295 (package-menu-mark-upgrades)
296 (package-menu-execute)
297 (package-menu-refresh)
298 (should (package-installed-p 'simple-single '(1 4)))))))
299
300 (ert-deftest package-test-describe-package ()
301 "Test displaying help for a package."
302
303 (require 'finder-inf)
304 ;; Built-in
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)))
312
313 ;; Installed
314 (with-package-test ()
315 (package-initialize)
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))
325 nil t))
326 (should (search-forward "Version: 1.3" nil t))
327 (should (search-forward "Summary: A single-file package with no dependencies"
328 nil t))
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.
334 )))
335
336 (ert-deftest package-test-describe-non-installed-package ()
337 "Test displaying of the readme for non-installed package."
338
339 (with-package-test ()
340 (package-initialize)
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"
347 nil t)))))
348
349 (ert-deftest package-test-describe-non-installed-multi-file-package ()
350 "Test displaying of the readme for non-installed multi-file package."
351
352 (with-package-test ()
353 (package-initialize)
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"
360 nil t)))))
361
362 (ert-deftest package-test-signed ()
363 "Test verifying package signature."
364 :expected-result (condition-case nil
365 (progn
366 (epg-check-configuration (epg-configuration))
367 :passed)
368 (error :failed))
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 ()
373 (package-initialize)
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"
382 nil t)))
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))
391 nil t))))))
392
393 (provide 'package-test)
394
395 ;;; package-test.el ends here