unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: 25951@debbugs.gnu.org, npostavs@users.sourceforge.net,
	emacs-devel@gnu.org, eliz@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: bug#25951: [PATCH] Fix quoted files for 'verify-visited-file-modtime'
Date: Sat, 29 Apr 2017 14:20:27 +0200	[thread overview]
Message-ID: <20170429122027.39318-1-phst__17532.0142567672$1493468477$gmane$org@google.com> (raw)
In-Reply-To: <83fugvpw24.fsf@gnu.org>

Fixes Bug#25951.

* lisp/files.el (file-name-non-special): Set the file name for the
correct buffer.

* test/lisp/files-tests.el (files-tests--file-name-non-special--buffers):
Add unit test.
(files-tests--with-advice, files-tests--with-temp-file): New helper
macros.
---
 lisp/files.el            |  9 ++++++-
 test/lisp/files-tests.el | 64 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/lisp/files.el b/lisp/files.el
index 6848818cad..2e9ab1aad1 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -28,6 +28,8 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'cl-lib))
+
 (defvar font-lock-keywords)
 
 (defgroup backup nil
@@ -6987,7 +6989,12 @@ file-name-non-special
            (when (and visit buffer-file-name)
              (setq buffer-file-name (concat "/:" buffer-file-name))))))
       (`unquote-then-quote
-       (let ((buffer-file-name (substring buffer-file-name 2)))
+       (cl-letf* ((buffer (or (car arguments) (current-buffer)))
+                  ((buffer-local-value 'buffer-file-name buffer)
+                   (substring (buffer-file-name buffer) 2)))
+         ;; `unquote-then-quote' is only used for the
+         ;; `verify-visited-file-modtime' action, which takes a buffer
+         ;; as only optional argument.
          (apply operation arguments)))
       (_
        (apply operation arguments)))))
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index 80bbeb1bc5..4583b1af3c 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -1,4 +1,4 @@
-;;; files-tests.el --- tests for files.el.
+;;; files-tests.el --- tests for files.el.  -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
 
@@ -20,6 +20,7 @@
 ;;; Code:
 
 (require 'ert)
+(require 'nadvice)
 
 ;; Set to t if the local variable was set, `query' if the query was
 ;; triggered.
@@ -251,5 +252,66 @@ files-test-bug-18141-file
                       (start-file-process "foo" nil "true"))))
   (should (eq (let ((default-directory "/:/")) (shell-command "true")) 0)))
 
+(defmacro files-tests--with-advice (symbol where function &rest body)
+  (declare (indent 3))
+  (cl-check-type symbol symbol)
+  (cl-check-type where keyword)
+  (cl-check-type function function)
+  (macroexp-let2 nil function function
+    `(progn
+       (advice-add #',symbol ,where ,function)
+       (unwind-protect
+           (progn ,@body)
+         (advice-remove #',symbol ,function)))))
+
+(defmacro files-tests--with-temp-file (name &rest body)
+  (declare (indent 1))
+  (cl-check-type name symbol)
+  `(let ((,name (make-temp-file "emacs")))
+     (unwind-protect
+         (progn ,@body)
+       (delete-file ,name))))
+
+(ert-deftest files-tests--file-name-non-special--buffers ()
+  "Check that Bug#25951 is fixed.
+We call `verify-visited-file-modtime' on a buffer visiting a file
+with a quoted name.  We use two different variants: first with
+the buffer current and a nil argument, second passing the buffer
+object explicitly.  In both cases no error should be raised and
+the `file-name-non-special' handler for quoted file names should
+be invoked with the right arguments."
+  (files-tests--with-temp-file temp-file-name
+    (with-temp-buffer
+     (let* ((buffer-visiting-file (current-buffer))
+            (actual-args ())
+            (log (lambda (&rest args) (push args actual-args))))
+       (insert-file-contents (concat "/:" temp-file-name) :visit)
+       (should (stringp buffer-file-name))
+       (should (string-prefix-p "/:" buffer-file-name))
+       (should (consp (visited-file-modtime)))
+       (should (equal (find-file-name-handler buffer-file-name
+                                              #'verify-visited-file-modtime)
+                      #'file-name-non-special))
+       (files-tests--with-advice file-name-non-special :before log
+         ;; This should call the file name handler with the right
+         ;; buffer and not signal an error.  The file hasn't been
+         ;; modified, so `verify-visited-file-modtime' should return
+         ;; t.
+         (should (equal (verify-visited-file-modtime) t))
+         (with-temp-buffer
+           (should (stringp (buffer-file-name buffer-visiting-file)))
+           ;; This should call the file name handler with the right
+           ;; buffer and not signal an error.  The file hasn't been
+           ;; modified, so `verify-visited-file-modtime' should return
+           ;; t.
+           (should (equal (verify-visited-file-modtime buffer-visiting-file)
+                          t))))
+       ;; Verify that the handler was actually called.  We called
+       ;; `verify-visited-file-modtime' twice, so both calls should be
+       ;; recorded in reverse order.
+       (should (equal actual-args
+                      `((verify-visited-file-modtime ,buffer-visiting-file)
+                        (verify-visited-file-modtime nil))))))))
+
 (provide 'files-tests)
 ;;; files-tests.el ends here
-- 
2.12.2






       reply	other threads:[~2017-04-29 12:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <83fugvpw24.fsf@gnu.org>
2017-04-29 12:20 ` Philipp Stephani [this message]
     [not found] ` <20170429122027.39318-1-phst@google.com>
2017-05-06 19:27   ` bug#25951: [PATCH] Fix quoted files for 'verify-visited-file-modtime' Philipp
     [not found]   ` <CAArVCkQ_mFGskvFHG9cVw=2fTxfROkDEshaxgGjrgk=oRBe4jA@mail.gmail.com>
2017-05-06 20:28     ` Glenn Morris
2017-05-06 20:41       ` npostavs
2017-05-06 21:21         ` bug#25951: [PATCH] Fix bootstrap build of files.el Philipp
2017-05-06 21:27         ` bug#25951: [PATCH] Fix quoted files for 'verify-visited-file-modtime' Philipp
     [not found]         ` <20170506212157.62158-1-phst@google.com>
2017-05-07  1:24           ` bug#25951: [PATCH] Fix bootstrap build of files.el Glenn Morris
     [not found]           ` <5gfugho3vw.fsf@fencepost.gnu.org>
2017-05-07 11:35             ` Philipp
2017-04-23 16:54 bug#25951: 26.0.50; Error when ediffing files that are visited using quoted file names Philipp Stephani
2017-04-29 11:49 ` bug#25951: [PATCH] Fix quoted files for 'verify-visited-file-modtime' Philipp Stephani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='20170429122027.39318-1-phst__17532.0142567672$1493468477$gmane$org@google.com' \
    --to=p.stephani2@gmail.com \
    --cc=25951@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=npostavs@users.sourceforge.net \
    --cc=phst@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).