aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Zlatanov2017-08-18 18:30:37 -0400
committerTed Zlatanov2017-08-18 18:31:00 -0400
commit3565437bf27373fe053d93dbb0c295f221834b07 (patch)
tree272e48b5cbbc0e594ca608735e571b1f8fa1b54e
parente962ca57e0bfe3bc2e319bb03dc0c6a9b1a7c5ee (diff)
downloademacs-3565437bf27373fe053d93dbb0c295f221834b07.tar.gz
emacs-3565437bf27373fe053d93dbb0c295f221834b07.zip
Add auth-source tests and codify its API better
The auth-source behavior was unclear in some API use cases, so these extra tests codify and test it. For details see https://github.com/DamienCassou/auth-password-store/issues/29 * lisp/files.el (make-temp-file): Add new initial TEXT parameter. * test/lisp/auth-source-tests.el (auth-source-test-searches): Add auth-source tests and simplify them with the new `make-temp-file'.
-rw-r--r--lisp/files.el9
-rw-r--r--test/lisp/auth-source-tests.el66
2 files changed, 72 insertions, 3 deletions
diff --git a/lisp/files.el b/lisp/files.el
index b05d453b0e7..4dc1238f955 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1397,13 +1397,16 @@ the variable `temporary-file-directory' is returned."
1397 default-directory 1397 default-directory
1398 temporary-file-directory)))) 1398 temporary-file-directory))))
1399 1399
1400(defun make-temp-file (prefix &optional dir-flag suffix) 1400(defun make-temp-file (prefix &optional dir-flag suffix text)
1401 "Create a temporary file. 1401 "Create a temporary file.
1402The returned file name (created by appending some random characters at the end 1402The returned file name (created by appending some random characters at the end
1403of PREFIX, and expanding against `temporary-file-directory' if necessary), 1403of PREFIX, and expanding against `temporary-file-directory' if necessary),
1404is guaranteed to point to a newly created empty file. 1404is guaranteed to point to a newly created file.
1405You can then use `write-region' to write new data into the file. 1405You can then use `write-region' to write new data into the file.
1406 1406
1407If TEXT is non-nil, it will be inserted in the new
1408file. Otherwise the file will be empty.
1409
1407If DIR-FLAG is non-nil, create a new empty directory instead of a file. 1410If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1408 1411
1409If SUFFIX is non-nil, add that at the end of the file name." 1412If SUFFIX is non-nil, add that at the end of the file name."
@@ -1431,7 +1434,7 @@ This implementation works on magic file names."
1431 (setq file (concat file suffix))) 1434 (setq file (concat file suffix)))
1432 (if dir-flag 1435 (if dir-flag
1433 (make-directory file) 1436 (make-directory file)
1434 (write-region "" nil file nil 'silent nil 'excl)) 1437 (write-region (or text "") nil file nil 'silent nil 'excl))
1435 nil) 1438 nil)
1436 (file-already-exists t)) 1439 (file-already-exists t))
1437 ;; the file was somehow created by someone else between 1440 ;; the file was somehow created by someone else between
diff --git a/test/lisp/auth-source-tests.el b/test/lisp/auth-source-tests.el
index 9753029f198..eb56e94af2c 100644
--- a/test/lisp/auth-source-tests.el
+++ b/test/lisp/auth-source-tests.el
@@ -228,5 +228,71 @@
228 (should-not (auth-source-remembered-p '(:host "xedd"))) 228 (should-not (auth-source-remembered-p '(:host "xedd")))
229 (should-not (auth-source-remembered-p '(:host t))))) 229 (should-not (auth-source-remembered-p '(:host t)))))
230 230
231(ert-deftest auth-source-test-searches ()
232 "Test auth-source searches with various parameters"
233 :tags '(auth-source auth-source/netrc)
234 (let* ((entries '("machine a1 port a2 user a3 password a4"
235 "machine b1 port b2 user b3 password b4"
236 "machine c1 port c2 user c3 password c4"))
237 ;; First element: test description.
238 ;; Second element: expected return data, serialized to a string.
239 ;; Rest of elements: the parameters for `auth-source-search'.
240 (tests '(("any host, max 1"
241 "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))"
242 :max 1 :host t)
243 ("any host, default max is 1"
244 "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))"
245 :host t)
246 ("any host, boolean return"
247 "t"
248 :host t :max 0)
249 ("no parameters, default max is 1"
250 "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))"
251 )
252 ("host c1, default max is 1"
253 "((:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))"
254 :host "c1")
255 ("host list of (c1), default max is 1"
256 "((:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))"
257 :host ("c1"))
258 ("any host, max 4"
259 "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\") (:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\") (:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))"
260 :host t :max 4)
261 ("host b1, default max is 1"
262 "((:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\"))"
263 :host "b1")
264 ("host b1, port b2, user b3, default max is 1"
265 "((:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\"))"
266 :host "b1" :port "b2" :user "b3")
267 ))
268
269 (text (string-join entries "\n"))
270 (netrc-file (make-temp-file
271 "auth-source-test"
272 nil nil
273 (string-join entries "\n")))
274 (auth-sources (list netrc-file))
275 (auth-source-do-cache nil))
276
277 (dolist (test tests)
278 (let ((testname (car test))
279 (needed (cadr test))
280 (parameters (cddr test))
281 found found-as-string)
282
283 (setq found (apply #'auth-source-search parameters))
284 (when (listp found)
285 (dolist (f found)
286 (setf f (plist-put f :secret
287 (let ((secret (plist-get f :secret)))
288 (if (functionp secret)
289 (funcall secret)
290 secret))))))
291
292 (setq found-as-string (format "%s: %S" testname found))
293 ;; (message "With parameters %S found: [%s] needed: [%s]" parameters found-as-string needed)
294 (should (equal found-as-string (concat testname ": " needed)))))
295 (delete-file netrc-file)))
296
231(provide 'auth-source-tests) 297(provide 'auth-source-tests)
232;;; auth-source-tests.el ends here 298;;; auth-source-tests.el ends here