>From d12f5f9520153e7d0b7d2b4b9c63d1a582918e16 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Mon, 22 Jul 2019 21:48:45 +0100 Subject: [PATCH 4/5] Fix some minor gravatar.el issues For discussion, see the following thread: https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html * lisp/image/gravatar.el (gravatar-hash): Trim leading and trailing whitespace in given address, as per the Gravatar docs. (gravatar-retrieve-synchronously): Silence call to url-retrieve-synchronously for consistency with gravatar-retrieve. (gravatar-retrieved): Only cache buffer on successful retrieval. * test/lisp/image/gravatar-tests.el: New file. --- lisp/image/gravatar.el | 12 +++++++---- test/lisp/image/gravatar-tests.el | 34 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test/lisp/image/gravatar-tests.el diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index fb539bcdbd..52fd875d68 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el @@ -26,6 +26,8 @@ (require 'url) (require 'url-cache) +(eval-when-compile + (require 'subr-x)) (defgroup gravatar nil "Gravatars." @@ -76,8 +78,9 @@ gravatar-base-url "Base URL for getting gravatars.") (defun gravatar-hash (mail-address) - "Create a hash from MAIL-ADDRESS." - (md5 (downcase mail-address))) + "Return the Gravatar hash for MAIL-ADDRESS." + ;; https://gravatar.com/site/implement/hash/ + (md5 (downcase (string-trim mail-address)))) (defun gravatar-build-url (mail-address) "Return a URL to retrieve MAIL-ADDRESS gravatar." @@ -114,7 +117,7 @@ gravatar-retrieve-synchronously retrieval failed." (let ((url (gravatar-build-url mail-address))) (with-current-buffer (if (url-cache-expired url gravatar-cache-ttl) - (url-retrieve-synchronously url) + (url-retrieve-synchronously url t) (url-fetch-from-cache url)) (gravatar-retrieved () #'identity)))) @@ -125,7 +128,8 @@ gravatar-retrieved This function is intended as a callback for `url-retrieve'." (let ((data (unless (plist-get status :error) (gravatar-get-data)))) - (and url-current-object ; Only cache if not already cached. + (and data ; Only cache on success. + url-current-object ; Only cache if not already cached. gravatar-automatic-caching (url-store-in-cache)) (prog1 (apply cb (if data (create-image data nil t) 'error) cbargs) diff --git a/test/lisp/image/gravatar-tests.el b/test/lisp/image/gravatar-tests.el new file mode 100644 index 0000000000..e6239da008 --- /dev/null +++ b/test/lisp/image/gravatar-tests.el @@ -0,0 +1,34 @@ +;;; gravatar-tests.el --- tests for gravatar.el -*- lexical-binding: t -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; This file is part of GNU Emacs. + +;; GNU Emacs 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. + +;; GNU Emacs 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 GNU Emacs. If not, see . + +;;; Code: + +(require 'ert) +(require 'gravatar) + +(ert-deftest gravatar-hash () + "Test `gravatar-hash'." + (should (equal (gravatar-hash "") "d41d8cd98f00b204e9800998ecf8427e")) + (let ((hash "acbd18db4cc2f85cedef654fccc4a4d8")) + (should (equal (gravatar-hash "foo") hash)) + (should (equal (gravatar-hash "foo ") hash)) + (should (equal (gravatar-hash " foo") hash)) + (should (equal (gravatar-hash " foo ") hash)))) + +;;; gravatar-tests.el ends here -- 2.20.1