1 ;;; package-test.el --- Tests for the Emacs package system
3 ;; Copyright (C) 2013 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 "Expected `package-desc' parsed from simple-single-1.3.el.")
53 (defvar simple-single-desc-1-4
54 (package-desc-create :name
'simple-single
56 :summary
"A single-file package with no dependencies"
58 "Expected `package-desc' parsed from simple-single-1.4.el.")
60 (defvar simple-depend-desc
61 (package-desc-create :name
'simple-depend
63 :summary
"A single-file package with a dependency."
65 :reqs
'((simple-single (1 3))))
66 "Expected `package-desc' parsed from simple-depend-1.0.el.")
68 (defvar multi-file-desc
69 (package-desc-create :name
'multi-file
71 :summary
"Example of a multi-file tar package"
73 "Expected `package-desc' from \"multi-file-0.2.3.tar\".")
76 (package-desc-create :name
'new-pkg
79 "Expected `package-desc' parsed from new-pkg-1.0.el.")
81 (defvar package-test-data-dir
(expand-file-name "data/package" package-test-file-dir
)
82 "Base directory of package test files.")
84 (defvar package-test-fake-contents-file
85 (expand-file-name "archive-contents" package-test-data-dir
)
86 "Path to a static copy of \"archive-contents\".")
88 (defvar package-test-built-file-suffixes
'(".tar" "/dir" "/*.info")
89 "Remove these files when cleaning up a built package.")
91 (cl-defmacro with-package-test
((&optional
&key file
97 "Set up temporary locations and variables for testing."
99 `(let* ((package-test-user-dir (make-temp-file "pkg-test-user-dir-" t
))
100 (package-user-dir package-test-user-dir
)
101 (package-archives `(("gnu" .
,package-test-data-dir
)))
102 (old-yes-no-defn (symbol-function 'yes-or-no-p
))
103 (old-pwd default-directory
)
107 '(package-update-news-on-upload t
)
110 '((package-test-archive-upload-base (make-temp-file "pkg-archive-base-" t
))
111 (package-archive-upload-base package-test-archive-upload-base
))
112 (list (cl-gensym)))) ;; Dummy value so `let' doesn't try to bind `nil'
115 ,(if basedir
`(cd ,basedir
))
116 (setf (symbol-function 'yes-or-no-p
) #'(lambda (&rest r
) t
))
117 (unless (file-directory-p package-user-dir
)
118 (mkdir package-user-dir
))
120 `((package-initialize)
121 (package-refresh-contents)
122 (mapc 'package-install
,install
)))
125 `(insert-file-contents ,file
))
128 (when (file-directory-p package-test-user-dir
)
129 (delete-directory package-test-user-dir t
))
131 (when (and (boundp 'package-test-archive-upload-base
)
132 (file-directory-p package-test-archive-upload-base
))
133 (delete-directory package-test-archive-upload-base t
))
134 (setf (symbol-function 'yes-or-no-p
) old-yes-no-defn
)
137 (defmacro with-fake-help-buffer
(&rest body
)
138 "Execute BODY in a temp buffer which is treated as the \"*Help*\" buffer."
141 ;; Trick `help-buffer' into using the temp buffer.
142 (let ((help-xref-following t
))
145 (autoload 'makeinfo-buffer
"makeinfo")
146 (defvar compilation-in-progress
)
148 (defun package-test-install-texinfo (file)
149 "Install from texinfo FILE.
151 FILE should be a .texinfo file relative to the current
154 (let* ((full-file (expand-file-name file
))
155 (info-file (replace-regexp-in-string "\\.texi\\'" ".info" full-file
))
156 (old-info-defn (symbol-function 'Info-revert-find-node
)))
158 (setf (symbol-function 'Info-revert-find-node
) #'ignore
)
159 (with-current-buffer (find-file-literally full-file
)
163 ;; Give `makeinfo-buffer' a chance to finish
164 (while compilation-in-progress
166 (call-process "ginstall-info" nil nil nil
167 (format "--info-dir=%s" default-directory
)
168 (format "%s" info-file
)))
170 (setf (symbol-function 'Info-revert-find-node
) old-info-defn
)))))
172 (defun package-test-strip-version (dir)
173 (replace-regexp-in-string "-pkg\\.el\\'" "" (package--description-file dir
)))
175 (defun package-test-suffix-matches (base suffix-list
)
176 "Return file names matching BASE concatenated with each item in SUFFIX-LIST"
178 '(lambda (item) (file-expand-wildcards (concat base item
)))
181 (defun package-test-cleanup-built-files (dir)
182 "Remove files which were the result of creating a tar archive.
184 DIR is the base name of the package directory, without the trailing slash"
185 (let* ((pkg-dirname (file-name-nondirectory dir
)))
186 (dolist (file (package-test-suffix-matches dir package-test-built-file-suffixes
))
187 (delete-file file
))))
189 (defvar tar-parse-info
)
190 (declare-function tar-header-name
"tar-mode" (cl-x) t
) ; defstruct
192 (defun package-test-search-tar-file (filename)
193 "Search the current buffer's `tar-parse-info' variable for FILENAME.
195 Must called from within a `tar-mode' buffer."
196 (cl-dolist (header tar-parse-info
)
197 (let ((tar-name (tar-header-name header
)))
198 (when (string= tar-name filename
)
201 (defun package-test-desc-version-string (desc)
202 "Return the package version as a string."
203 (package-version-join (package-desc-version desc
)))
205 (ert-deftest package-test-desc-from-buffer
()
206 "Parse an elisp buffer to get a `package-desc' object."
207 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
208 (should (equal (package-buffer-info) simple-single-desc
)))
209 (with-package-test (:basedir
"data/package" :file
"simple-depend-1.0.el")
210 (should (equal (package-buffer-info) simple-depend-desc
)))
211 (with-package-test (:basedir
"data/package"
212 :file
"multi-file-0.2.3.tar")
214 (should (equal (package-tar-file-info) multi-file-desc
))))
216 (ert-deftest package-test-install-single
()
217 "Install a single file without using an archive."
218 (with-package-test (:basedir
"data/package" :file
"simple-single-1.3.el")
219 (should (package-install-from-buffer))
221 (should (package-installed-p 'simple-single
))
222 (let* ((simple-pkg-dir (file-name-as-directory
225 package-test-user-dir
)))
226 (autoloads-file (expand-file-name "simple-single-autoloads.el"
228 (should (file-directory-p simple-pkg-dir
))
230 (insert-file-contents (expand-file-name "simple-single-pkg.el"
232 (should (string= (buffer-string)
233 (concat "(define-package \"simple-single\" \"1.3\" "
234 "\"A single-file package "
235 "with no dependencies\" 'nil)\n"))))
236 (should (file-exists-p autoloads-file
))
237 (should-not (get-file-buffer autoloads-file
)))))
239 (ert-deftest package-test-install-dependency
()
240 "Install a package which includes a dependency."
241 (with-package-test ()
243 (package-refresh-contents)
244 (package-install 'simple-depend
)
245 (should (package-installed-p 'simple-single
))
246 (should (package-installed-p 'simple-depend
))))
248 (ert-deftest package-test-refresh-contents
()
249 "Parse an \"archive-contents\" file."
250 (with-package-test ()
252 (package-refresh-contents)
253 (should (eq 3 (length package-archive-contents
)))))
255 (ert-deftest package-test-install-single-from-archive
()
256 "Install a single package from a package archive."
257 (with-package-test ()
259 (package-refresh-contents)
260 (package-install 'simple-single
)))
262 (ert-deftest package-test-install-multifile
()
263 "Check properties of the installed multi-file package."
264 (with-package-test (:basedir
"data/package" :install
'(multi-file))
266 (expand-file-name "multi-file-autoloads.el"
269 package-test-user-dir
)))
270 (installed-files '("dir" "multi-file.info" "multi-file-sub.elc"
271 "multi-file-autoloads.el" "multi-file.elc"))
272 (autoload-forms '("^(defvar multi-file-custom-var"
273 "^(custom-autoload 'multi-file-custom-var"
274 "^(autoload 'multi-file-mode"))
275 (pkg-dir (file-name-as-directory
278 package-test-user-dir
))))
279 (package-refresh-contents)
280 (should (package-installed-p 'multi-file
))
282 (insert-file-contents-literally autoload-file
)
283 (dolist (fn installed-files
)
284 (should (file-exists-p (expand-file-name fn pkg-dir
))))
285 (dolist (re autoload-forms
)
286 (goto-char (point-min))
287 (should (re-search-forward re nil t
)))))))
289 (ert-deftest package-test-update-listing
()
290 "Ensure installed package status is updated."
291 (with-package-test ()
292 (let ((buf (package-list-packages)))
293 (search-forward-regexp "^ +simple-single")
294 (package-menu-mark-install)
295 (package-menu-execute)
296 (should (package-installed-p 'simple-single
))
297 (switch-to-buffer "*Packages*")
298 (goto-char (point-min))
299 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
300 (goto-char (point-min))
301 (should-not (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+\\(available\\|new\\)" nil t
))
304 (ert-deftest package-test-update-archives
()
305 "Test updating package archives."
306 (with-package-test ()
307 (let ((buf (package-list-packages)))
308 (package-menu-refresh)
309 (search-forward-regexp "^ +simple-single")
310 (package-menu-mark-install)
311 (package-menu-execute)
312 (should (package-installed-p 'simple-single
))
313 (let ((package-test-data-dir
314 (expand-file-name "data/package/newer-versions" package-test-file-dir
)))
315 (setq package-archives
`(("gnu" .
,package-test-data-dir
)))
316 (package-menu-refresh)
318 ;; New version should be available and old version should be installed
319 (goto-char (point-min))
320 (should (re-search-forward "^\\s-+simple-single\\s-+1.4\\s-+new" nil t
))
321 (should (re-search-forward "^\\s-+simple-single\\s-+1.3\\s-+installed" nil t
))
323 (goto-char (point-min))
324 (should (re-search-forward "^\\s-+new-pkg\\s-+1.0\\s-+\\(available\\|new\\)" nil t
))
326 (package-menu-mark-upgrades)
327 (package-menu-execute)
328 (package-menu-refresh)
329 (should (package-installed-p 'simple-single
'(1 4)))))))
331 (ert-deftest package-test-describe-package
()
332 "Test displaying help for a package."
334 (require 'finder-inf
)
336 (with-fake-help-buffer
337 (describe-package '5x5
)
338 (goto-char (point-min))
339 (should (search-forward "5x5 is a built-in package." nil t
))
340 (should (search-forward "Status: Built-in." nil t
))
341 (should (search-forward "Summary: simple little puzzle game" nil t
))
342 (should (search-forward "The aim of 5x5" nil t
)))
345 (with-package-test ()
347 (package-refresh-contents)
348 (package-install 'simple-single
)
349 (with-fake-help-buffer
350 (describe-package 'simple-single
)
351 (goto-char (point-min))
352 (should (search-forward "simple-single is an installed package." nil t
))
353 (should (search-forward
354 (format "Status: Installed in `%s/'."
355 (expand-file-name "simple-single-1.3" package-user-dir
))
357 (should (search-forward "Version: 1.3" nil t
))
358 (should (search-forward "Summary: A single-file package with no dependencies"
360 ;; No description, though. Because at this point we don't know
361 ;; what archive the package originated from, and we don't have
362 ;; its readme file saved.
365 (ert-deftest package-test-describe-not-installed-package
()
366 "Test displaying of the readme for not-installed package."
368 (with-package-test ()
370 (package-refresh-contents)
371 (with-fake-help-buffer
372 (describe-package 'simple-single
)
373 (goto-char (point-min))
374 (should (search-forward "This package provides a minor mode to frobnicate"
377 (ert-deftest package-test-describe-non-installed-package
()
378 "Test displaying of the readme for non-installed package."
380 (with-package-test ()
382 (package-refresh-contents)
383 (with-fake-help-buffer
384 (describe-package 'simple-single
)
385 (goto-char (point-min))
386 (should (search-forward "This package provides a minor mode to frobnicate"
389 (ert-deftest package-test-describe-non-installed-multi-file-package
()
390 "Test displaying of the readme for non-installed multi-file package."
392 (with-package-test ()
394 (package-refresh-contents)
395 (with-fake-help-buffer
396 (describe-package 'multi-file
)
397 (goto-char (point-min))
398 (should (search-forward "This is a bare-bones readme file for the multi-file"
401 (provide 'package-test
)
403 ;;; package-test.el ends here