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."
(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
(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