From: Ted Zlatanov Date: Fri, 18 Aug 2017 22:30:37 +0000 (-0400) Subject: Add auth-source tests and codify its API better X-Git-Tag: emacs-26.0.90~424 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=3565437bf27373fe053d93dbb0c295f221834b07;p=emacs.git 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'. --- 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." default-directory temporary-file-directory)))) -(defun make-temp-file (prefix &optional dir-flag suffix) +(defun make-temp-file (prefix &optional dir-flag suffix text) "Create a temporary file. The returned file name (created by appending some random characters at the end of PREFIX, and expanding against `temporary-file-directory' if necessary), -is guaranteed to point to a newly created empty file. +is guaranteed to point to a newly created file. You can then use `write-region' to write new data into the file. +If TEXT is non-nil, it will be inserted in the new +file. Otherwise the file will be empty. + If DIR-FLAG is non-nil, create a new empty directory instead of a file. If SUFFIX is non-nil, add that at the end of the file name." @@ -1431,7 +1434,7 @@ This implementation works on magic file names." (setq file (concat file suffix))) (if dir-flag (make-directory file) - (write-region "" nil file nil 'silent nil 'excl)) + (write-region (or text "") nil file nil 'silent nil 'excl)) nil) (file-already-exists t)) ;; 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 @@ (should-not (auth-source-remembered-p '(:host "xedd"))) (should-not (auth-source-remembered-p '(:host t))))) +(ert-deftest auth-source-test-searches () + "Test auth-source searches with various parameters" + :tags '(auth-source auth-source/netrc) + (let* ((entries '("machine a1 port a2 user a3 password a4" + "machine b1 port b2 user b3 password b4" + "machine c1 port c2 user c3 password c4")) + ;; First element: test description. + ;; Second element: expected return data, serialized to a string. + ;; Rest of elements: the parameters for `auth-source-search'. + (tests '(("any host, max 1" + "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))" + :max 1 :host t) + ("any host, default max is 1" + "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))" + :host t) + ("any host, boolean return" + "t" + :host t :max 0) + ("no parameters, default max is 1" + "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\"))" + ) + ("host c1, default max is 1" + "((:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))" + :host "c1") + ("host list of (c1), default max is 1" + "((:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))" + :host ("c1")) + ("any host, max 4" + "((:host \"a1\" :port \"a2\" :user \"a3\" :secret \"a4\") (:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\") (:host \"c1\" :port \"c2\" :user \"c3\" :secret \"c4\"))" + :host t :max 4) + ("host b1, default max is 1" + "((:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\"))" + :host "b1") + ("host b1, port b2, user b3, default max is 1" + "((:host \"b1\" :port \"b2\" :user \"b3\" :secret \"b4\"))" + :host "b1" :port "b2" :user "b3") + )) + + (text (string-join entries "\n")) + (netrc-file (make-temp-file + "auth-source-test" + nil nil + (string-join entries "\n"))) + (auth-sources (list netrc-file)) + (auth-source-do-cache nil)) + + (dolist (test tests) + (let ((testname (car test)) + (needed (cadr test)) + (parameters (cddr test)) + found found-as-string) + + (setq found (apply #'auth-source-search parameters)) + (when (listp found) + (dolist (f found) + (setf f (plist-put f :secret + (let ((secret (plist-get f :secret))) + (if (functionp secret) + (funcall secret) + secret)))))) + + (setq found-as-string (format "%s: %S" testname found)) + ;; (message "With parameters %S found: [%s] needed: [%s]" parameters found-as-string needed) + (should (equal found-as-string (concat testname ": " needed))))) + (delete-file netrc-file))) + (provide 'auth-source-tests) ;;; auth-source-tests.el ends here