Commit | Line | Data |
---|---|---|
9ea5cf9f DG |
1 | ;;; package-test.el --- Tests for the Emacs package system |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2013-2014 Free Software Foundation, Inc. |
9c8d35d5 | 4 | |
9ea5cf9f DG |
5 | ;; Author: Daniel Hackney <dan@haxney.org> |
6 | ;; Version: 1.0 | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
9c8d35d5 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
9ea5cf9f | 11 | ;; it under the terms of the GNU General Public License as published by |
9c8d35d5 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
9ea5cf9f DG |
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 | |
9c8d35d5 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
9ea5cf9f DG |
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" | |
056453c6 | 50 | :kind 'single |
494e898b | 51 | :extras '((:url . "http://doodles.au"))) |
9ea5cf9f DG |
52 | "Expected `package-desc' parsed from simple-single-1.3.el.") |
53 | ||
9ea5cf9f DG |
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" | |
056453c6 | 66 | :kind 'tar |
494e898b | 67 | :extras '((:url . "http://puddles.li"))) |
9ea5cf9f DG |
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 | ||
9ea5cf9f DG |
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)) | |
056453c6 | 95 | (default-directory package-test-file-dir) |
9ea5cf9f DG |
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)) | |
056453c6 | 126 | (setf (symbol-function 'yes-or-no-p) old-yes-no-defn)))) |
9ea5cf9f DG |
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 | ||
9ea5cf9f DG |
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 | ||
03d01981 GM |
145 | (defvar tar-parse-info) |
146 | (declare-function tar-header-name "tar-mode" (cl-x) t) ; defstruct | |
147 | ||
9ea5cf9f DG |
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 "(define-package \"simple-single\" \"1.3\" " | |
190 | "\"A single-file package " | |
056453c6 | 191 | "with no dependencies\" 'nil " |
494e898b | 192 | ":url \"http://doodles.au\"" |
056453c6 | 193 | ")\n")))) |
9ea5cf9f DG |
194 | (should (file-exists-p autoloads-file)) |
195 | (should-not (get-file-buffer autoloads-file))))) | |
196 | ||
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)))) | |
205 | ||
dcf78667 DG |
206 | (ert-deftest package-test-install-two-dependencies () |
207 | "Install a package which includes a dependency." | |
208 | (with-package-test () | |
209 | (package-initialize) | |
210 | (package-refresh-contents) | |
211 | (package-install 'simple-two-depend) | |
212 | (should (package-installed-p 'simple-single)) | |
213 | (should (package-installed-p 'simple-depend)) | |
214 | (should (package-installed-p 'simple-two-depend)))) | |
215 | ||
9ea5cf9f DG |
216 | (ert-deftest package-test-refresh-contents () |
217 | "Parse an \"archive-contents\" file." | |
218 | (with-package-test () | |
219 | (package-initialize) | |
220 | (package-refresh-contents) | |
dcf78667 | 221 | (should (eq 4 (length package-archive-contents))))) |
9ea5cf9f DG |
222 | |
223 | (ert-deftest package-test-install-single-from-archive () | |
224 | "Install a single package from a package archive." | |
225 | (with-package-test () | |
226 | (package-initialize) | |
227 | (package-refresh-contents) | |
228 | (package-install 'simple-single))) | |
229 | ||
230 | (ert-deftest package-test-install-multifile () | |
231 | "Check properties of the installed multi-file package." | |
232 | (with-package-test (:basedir "data/package" :install '(multi-file)) | |
233 | (let ((autoload-file | |
234 | (expand-file-name "multi-file-autoloads.el" | |
235 | (expand-file-name | |
236 | "multi-file-0.2.3" | |
237 | package-test-user-dir))) | |
238 | (installed-files '("dir" "multi-file.info" "multi-file-sub.elc" | |
239 | "multi-file-autoloads.el" "multi-file.elc")) | |
240 | (autoload-forms '("^(defvar multi-file-custom-var" | |
241 | "^(custom-autoload 'multi-file-custom-var" | |
242 | "^(autoload 'multi-file-mode")) | |
243 | (pkg-dir (file-name-as-directory | |
244 | (expand-file-name | |
245 | "multi-file-0.2.3" | |
246 | package-test-user-dir)))) | |
247 | (package-refresh-contents) | |
248 | (should (package-installed-p 'multi-file)) | |
249 | (with-temp-buffer | |
250 | (insert-file-contents-literally autoload-file) | |
251 | (dolist (fn installed-files) | |
252 | (should (file-exists-p (expand-file-name fn pkg-dir)))) | |
253 | (dolist (re autoload-forms) | |
254 | (goto-char (point-min)) | |
255 | (should (re-search-forward re nil t))))))) | |
256 | ||
257 | (ert-deftest package-test-update-listing () | |
258 | "Ensure installed package status is updated." | |
259 | (with-package-test () | |
260 | (let ((buf (package-list-packages))) | |
261 | (search-forward-regexp "^ +simple-single") | |
262 | (package-menu-mark-install) | |
263 | (package-menu-execute) | |
264 | (should (package-installed-p 'simple-single)) | |
265 | (switch-to-buffer "*Packages*") | |
266 | (goto-char (point-min)) | |
acbadd00 | 267 | (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+unsigned" nil t)) |
9ea5cf9f DG |
268 | (goto-char (point-min)) |
269 | (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t)) | |
270 | (kill-buffer buf)))) | |
271 | ||
272 | (ert-deftest package-test-update-archives () | |
273 | "Test updating package archives." | |
274 | (with-package-test () | |
275 | (let ((buf (package-list-packages))) | |
276 | (package-menu-refresh) | |
277 | (search-forward-regexp "^ +simple-single") | |
278 | (package-menu-mark-install) | |
279 | (package-menu-execute) | |
280 | (should (package-installed-p 'simple-single)) | |
281 | (let ((package-test-data-dir | |
282 | (expand-file-name "data/package/newer-versions" package-test-file-dir))) | |
283 | (setq package-archives `(("gnu" . ,package-test-data-dir))) | |
284 | (package-menu-refresh) | |
285 | ||
286 | ;; New version should be available and old version should be installed | |
287 | (goto-char (point-min)) | |
288 | (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t)) | |
acbadd00 | 289 | (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+unsigned" nil t)) |
9ea5cf9f DG |
290 | |
291 | (goto-char (point-min)) | |
292 | (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t)) | |
293 | ||
294 | (package-menu-mark-upgrades) | |
295 | (package-menu-execute) | |
296 | (package-menu-refresh) | |
297 | (should (package-installed-p 'simple-single '(1 4))))))) | |
298 | ||
299 | (ert-deftest package-test-describe-package () | |
300 | "Test displaying help for a package." | |
301 | ||
302 | (require 'finder-inf) | |
303 | ;; Built-in | |
304 | (with-fake-help-buffer | |
305 | (describe-package '5x5) | |
306 | (goto-char (point-min)) | |
307 | (should (search-forward "5x5 is a built-in package." nil t)) | |
308 | (should (search-forward "Status: Built-in." nil t)) | |
309 | (should (search-forward "Summary: simple little puzzle game" nil t)) | |
310 | (should (search-forward "The aim of 5x5" nil t))) | |
311 | ||
312 | ;; Installed | |
313 | (with-package-test () | |
314 | (package-initialize) | |
315 | (package-refresh-contents) | |
316 | (package-install 'simple-single) | |
317 | (with-fake-help-buffer | |
318 | (describe-package 'simple-single) | |
319 | (goto-char (point-min)) | |
acbadd00 | 320 | (should (search-forward "simple-single is an unsigned package." nil t)) |
9ea5cf9f | 321 | (should (search-forward |
acbadd00 | 322 | (format "Status: Installed in `%s/' (unsigned)." |
9ea5cf9f DG |
323 | (expand-file-name "simple-single-1.3" package-user-dir)) |
324 | nil t)) | |
325 | (should (search-forward "Version: 1.3" nil t)) | |
326 | (should (search-forward "Summary: A single-file package with no dependencies" | |
327 | nil t)) | |
056453c6 | 328 | (should (search-forward "Homepage: http://doodles.au" nil t)) |
0ceba22e | 329 | (should (re-search-forward "Keywords: \\[?frobnicate\\]?" nil t)) |
9ea5cf9f DG |
330 | ;; No description, though. Because at this point we don't know |
331 | ;; what archive the package originated from, and we don't have | |
332 | ;; its readme file saved. | |
333 | ))) | |
334 | ||
9ea5cf9f DG |
335 | (ert-deftest package-test-describe-non-installed-package () |
336 | "Test displaying of the readme for non-installed package." | |
337 | ||
338 | (with-package-test () | |
339 | (package-initialize) | |
340 | (package-refresh-contents) | |
341 | (with-fake-help-buffer | |
342 | (describe-package 'simple-single) | |
343 | (goto-char (point-min)) | |
056453c6 | 344 | (should (search-forward "Homepage: http://doodles.au" nil t)) |
9ea5cf9f DG |
345 | (should (search-forward "This package provides a minor mode to frobnicate" |
346 | nil t))))) | |
347 | ||
348 | (ert-deftest package-test-describe-non-installed-multi-file-package () | |
349 | "Test displaying of the readme for non-installed multi-file package." | |
350 | ||
351 | (with-package-test () | |
352 | (package-initialize) | |
353 | (package-refresh-contents) | |
354 | (with-fake-help-buffer | |
355 | (describe-package 'multi-file) | |
356 | (goto-char (point-min)) | |
056453c6 | 357 | (should (search-forward "Homepage: http://puddles.li" nil t)) |
9ea5cf9f DG |
358 | (should (search-forward "This is a bare-bones readme file for the multi-file" |
359 | nil t))))) | |
360 | ||
acbadd00 DU |
361 | (ert-deftest package-test-signed () |
362 | "Test verifying package signature." | |
363 | :expected-result (condition-case nil | |
364 | (progn | |
365 | (epg-check-configuration (epg-configuration)) | |
366 | :passed) | |
367 | (error :failed)) | |
368 | (let* ((keyring (expand-file-name "key.pub" package-test-data-dir)) | |
369 | (package-test-data-dir | |
370 | (expand-file-name "data/package/signed" package-test-file-dir))) | |
371 | (with-package-test () | |
372 | (package-initialize) | |
373 | (package-import-keyring keyring) | |
374 | (package-refresh-contents) | |
375 | (should (package-install 'signed-good)) | |
376 | (should-error (package-install 'signed-bad)) | |
377 | ;; Check if the installed package status is updated. | |
378 | (let ((buf (package-list-packages))) | |
379 | (package-menu-refresh) | |
380 | (should (re-search-forward "^\\s-+signed-good\\s-+1\\.0\\s-+installed" | |
381 | nil t))) | |
382 | ;; Check if the package description is updated. | |
383 | (with-fake-help-buffer | |
384 | (describe-package 'signed-good) | |
385 | (goto-char (point-min)) | |
386 | (should (search-forward "signed-good is an installed package." nil t)) | |
387 | (should (search-forward | |
388 | (format "Status: Installed in `%s/'." | |
389 | (expand-file-name "signed-good-1.0" package-user-dir)) | |
390 | nil t)))))) | |
391 | ||
9ea5cf9f DG |
392 | (provide 'package-test) |
393 | ||
394 | ;;; package-test.el ends here |