summaryrefslogtreecommitdiff
path: root/test/epl-test.el
blob: 31a88ace78af15e69ae159a6fd8c38053d310d7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;; epl-test.el --- EPL: Test suite -*- lexical-binding: t; -*-

;; Copyright (C) 2013-2015  Sebastian Wiesner

;; Author: Sebastian Wiesner <swiesner@lunaryorn.com>
;; Maintainer: Johan Andersson <johan.rejeep@gmail.com>
;;     Sebastian Wiesner <swiesner@lunaryorn.com>
;; URL: http://github.com/cask/epl

;; This file is NOT part of GNU Emacs.

;; Author: Sebastian Wiesner <swiesner@lunaryorn.com>
;; Keywords: convenience

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Test EPL

;;; Code:

(require 'epl)
(require 'f)
(require 'ert)


;;;; Directories
(defconst epl-test-directory (f-parent (f-this-file))
  "The directory of the test suite.")

(defconst epl-sandbox-directory (f-expand "sandbox" epl-test-directory)
  "The sandbox directory of the test suite.")


;;;; Resource handling
(defconst epl-test-resource-directory (f-join epl-test-directory "resources")
  "The directory of test resource files.")

(defun epl-test-resource-file-name (resource)
  "Get the file name of a RESOURCE."
  (f-join epl-test-resource-directory resource))

(defmacro epl-test/with-sandbox (&rest body)
  "Run BODY in a sandbox environment.

In the sandbox, packages that are installed, are installed in the
directory `epl-sandbox-directory'.  The sandbox directory never
exist when entering the sandbox environment."
  `(let ((package-user-dir epl-sandbox-directory))
     (when (f-dir? epl-sandbox-directory)
       (f-delete epl-sandbox-directory 'force))
     ,@body))


;;;; Package structures

(ert-deftest epl-package-as-description/variable-must-be-a-symbol ()
  ;; We explicitly `eval' the expression here, to avoid eager macro expansion
  ;; kicking in, and triggering the error before the test gets executed
  (let* ((expr '(epl-package-as-description "foo" (message "bar")))
         (err-and-data (should-error (eval expr)))
         (data (cdr err-and-data)))
    (should (eq (car err-and-data) 'wrong-type-argument))
    (should (eq (car data) #'symbolp))
    (should (equal (cadr data) "foo"))))

(ert-deftest epl-package-as-description/variable-must-be-bound-to-epl-package ()
  (let* ((foo "bar")
         (err-and-data (should-error (epl-package-as-description foo
                                       (message "bar"))))
         (data (cdr err-and-data)))
    (should (eq (car err-and-data) 'wrong-type-argument))
    (should (eq (car data) #'epl-package-p))
    (should (equal (cadr data) "bar"))))

(ert-deftest epl-package-from-buffer/invalid-lisp-package ()
  (with-temp-buffer
    (insert "
foo.el --- Foo

Version: 1
Package-Requires: ((foo

;;; foo.el ends here")
    (should-error (epl-package-from-buffer) :type '(epl-invalid-package))))

(ert-deftest epl-package-from-lisp-file/invalid-lisp-package ()
  (let* ((file-name (epl-test-resource-file-name "invalid-package.el"))
         (err (should-error (epl-package-from-lisp-file file-name)
                              :type '(epl-invalid-package-file))))
    (should (equal (cadr err) file-name))))

(ert-deftest epl-package-from-file/valid-lisp-package ()
  (let* ((file (epl-test-resource-file-name "dummy-package.el"))
         (package (epl-package-from-file file))
         (summary "EPL: Dummy package for unit tests"))
    (when (< emacs-major-version 24)
      ;; Emacs 23 package.el does not strip local variable lines from summary
      (setq summary (concat summary "  -*- lexical-binding: t; -*-")))
    (should (epl-package-p package))
    (should (string= (epl-package-name package) 'dummy-package))
    (should (string= (epl-package-summary package) summary))
    (should (equal (epl-package-version package) '(4 3 1 2 -3)))
    (should (equal (epl-package-requirements package)
                   (list (epl-requirement-create :name 'foo :version '(1 2))
                         (epl-requirement-create :name 'bar :version '(2 2)))))))

(ert-deftest epl-package-from-file-tar/tar-file-without-package-descriptor ()
  "Test a TAR package without package descriptor."
  (should-error
   (epl-package-from-file
    (epl-test-resource-file-name "dummy-package-4.3.1.2alpha.tar"))))

(ert-deftest epl-package-from-file/valid-tar-package ()
  (let* ((file (epl-test-resource-file-name "dummy-package-4.3.2.tar"))
         (package (epl-package-from-file file)))
    (should (epl-package-p package))
    (should (string= (epl-package-name package) 'dummy-package))
    (should (string= (epl-package-summary package) "EPL dummy package"))
    (should (equal (epl-package-version package) '(4 3 2)))
    (should (equal (epl-package-requirements package)
                   (list (epl-requirement-create :name 'foo :version '(0 3))
                         (epl-requirement-create :name 'spam :version '(0 4)))))))

(ert-deftest epl-package-from-file/tar-file-does-not-exist ()
  (should-error
   (epl-package-from-file
    (epl-test-resource-file-name "no-such-package.tar"))))

(ert-deftest epl-package-from-file/lisp-file-does-not-exist ()
  (should-error
   (epl-package-from-file
    (epl-test-resource-file-name "no-such-package.el"))))

(ert-deftest epl-package-from-descriptor-file/should-not-define-the-package ()
  "Loading a package descriptor should not affect the database.

package.el tends to have such unfortunate side effects."
  (let ((file (epl-test-resource-file-name "dummy-package-pkg.el")))
    (epl-package-from-descriptor-file file)
    (should-not (assq 'dummy-package package-alist))))

(ert-deftest epl-package-from-descriptor-file/valid-descriptor ()
  (let* ((file (epl-test-resource-file-name "dummy-package-pkg.el"))
         (package (epl-package-from-descriptor-file file)))
    (should (epl-package-p package))
    (should (string= (epl-package-name package) 'dummy-package))
    (should (string= (epl-package-summary package) "EPL dummy package"))
    (should (equal (epl-package-version package) '(4 3 6)))
    (should (equal (epl-package-requirements package)
                   (list (epl-requirement-create :name 'bar :version '(8 1 -3))
                         (epl-requirement-create :name 'spam :version '(0 4)))))))

(ert-deftest epl-package-from-descriptor/descriptor-file-does-not-exist ()
  (should-error
   (epl-package-from-file
    (epl-test-resource-file-name "no-such-descriptor-pkg.el"))))

(ert-deftest epl-package-from-descriptor/package-file-is-invalid ()
  (should-error
   (epl-package-from-file
    (epl-test-resource-file-name "invalid-package-pkg.el"))))

(ert-deftest epl-package-directory/should-work ()
  (epl-test/with-sandbox
   (epl-install-file (epl-test-resource-file-name "smartie-package.el"))
   (let ((package (epl-find-installed-package 'smartie-package)))
     (should (equal (file-name-nondirectory (epl-package-directory package))
                    "smartie-package-1.2.3"))
     (epl-package-delete package))))


;;; Package database
(ert-deftest epl-built-in-packages/catches-all ()
  ;; Make sure that `package--builtins' is filled for our test
  (package-built-in-p 'foo)
  (should package--builtins)
  (should (equal (length (epl-built-in-packages)) (length package--builtins))))


;;; Package operations
(ert-deftest epl-package-delete/should-not-be-installed ()
  (epl-test/with-sandbox
   (let ((smartie-package (epl-test-resource-file-name "smartie-package.el")))
     (epl-install-file smartie-package)
     (let ((package (car (epl-find-installed-packages 'smartie-package))))
       (should (epl-package-installed-p package))
       (epl-package-delete package)
       (should-not (epl-package-installed-p package))))))

(ert-deftest epl-package-installed-p/min-version ()
  (epl-test/with-sandbox
   (let ((package-file (epl-test-resource-file-name "versioned-package.el"))
         (new-package-file (epl-test-resource-file-name "versioned-package-new.el")))
     (epl-install-file package-file)
     (let ((package (car (epl-find-installed-packages 'versioned-package))))
       (should (epl-package-installed-p package))
       (should (epl-package-installed-p package (version-to-list "8.2.10")))
       (should (epl-package-installed-p package (version-to-list "8.2.9")))
       (should (epl-package-installed-p package (version-to-list "8.2")))
       (should (epl-package-installed-p package (version-to-list "8")))
       (should-not (epl-package-installed-p package (version-to-list "8.2.11")))
       (should-not (epl-package-installed-p package (version-to-list "8.3.10")))
       (should-not (epl-package-installed-p package (version-to-list "9.1.6")))
       (should-not (epl-package-installed-p package (version-to-list "999999")))))))

(ert-deftest epl-package-installed-p/min-version-upgrade ()
  (epl-test/with-sandbox
   (let ((package-file (epl-test-resource-file-name "versioned-package.el"))
         (new-package-file (epl-test-resource-file-name "versioned-package-new.el")))
     (epl-install-file package-file)
     (let ((package (car (epl-find-installed-packages 'versioned-package))))
       (should (epl-package-installed-p package))
       (should-not (epl-package-installed-p package (version-to-list "9.1.6")))
     (epl-install-file new-package-file)
     (let ((package (car (epl-find-installed-packages 'versioned-package))))
       (should (epl-package-installed-p package))
       (should (epl-package-installed-p package (version-to-list "8.2.10")))
       (should (epl-package-installed-p package (version-to-list "8.2.9")))
       (should (epl-package-installed-p package (version-to-list "8.2")))
       (should (epl-package-installed-p package (version-to-list "8")))
       (should (epl-package-installed-p package (version-to-list "8.2.11")))
       (should (epl-package-installed-p package (version-to-list "8.3.10")))
       (should (epl-package-installed-p package (version-to-list "9.1.6")))
       (should-not (epl-package-installed-p package (version-to-list "999999"))))))))

(provide 'epl-test)

;;; epl-test.el ends here