From 21888a94648df813cef354a22b92be9b23673358 Mon Sep 17 00:00:00 2001 From: Jonathan Tomer Date: Wed, 8 May 2019 09:13:58 +0200 Subject: [PATCH] Don't rewrite buffer contents after saving by rename (Bug#35497) * lisp/files.el (basic-save-buffer-2): Don't rewrite file contents after saving-by-renaming. (Bug#35497) * test/lisp/files-tests.el (files-tests-dont-rewrite-precious-files): * test/lisp/net/tramp-tests.el (tramp-test10-write-region-file-precious-flag): Regression tests for this change. --- lisp/files.el | 4 ++-- test/lisp/files-tests.el | 15 +++++++++++++++ test/lisp/net/tramp-tests.el | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lisp/files.el b/lisp/files.el index 518394fdcd7..8477c227bcc 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5258,7 +5258,7 @@ Before and after saving the buffer, this function runs (set-file-extended-attributes buffer-file-name (nth 1 setmodes))) (set-file-modes buffer-file-name - (logior (car setmodes) 128)))))) + (logior (car setmodes) 128))))) (let (success) (unwind-protect (progn @@ -5274,7 +5274,7 @@ Before and after saving the buffer, this function runs (and setmodes (not success) (progn (rename-file (nth 2 setmodes) buffer-file-name t) - (setq buffer-backed-up nil)))))) + (setq buffer-backed-up nil))))))) setmodes)) (declare-function diff-no-select "diff" diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index ae8ea41a797..fe2e958f1c3 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1244,5 +1244,20 @@ See ." (executable-find (file-name-nondirectory tmpfile)))))) (delete-file tmpfile)))) +(ert-deftest files-tests-dont-rewrite-precious-files () + "Test that `file-precious-flag' forces files to be saved by +renaming only, rather than modified in-place." + (let* ((temp-file-name (make-temp-file "files-tests")) + (advice (lambda (_start _end filename &rest _r) + (should-not (string= filename temp-file-name))))) + (unwind-protect + (with-current-buffer (find-file-noselect temp-file-name) + (advice-add #'write-region :before advice) + (setq-local file-precious-flag t) + (insert "foobar") + (should (null (save-buffer)))) + (ignore-errors (advice-remove #'write-region advice)) + (ignore-errors (delete-file temp-file-name))))) + (provide 'files-tests) ;;; files-tests.el ends here diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index 7d3c43408d4..e35be0e3121 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -41,6 +41,7 @@ ;;; Code: +(require 'cl-seq) (require 'dired) (require 'ert) (require 'ert-x) @@ -2270,6 +2271,34 @@ This checks also `file-name-as-directory', `file-name-directory', ;; Cleanup. (ignore-errors (delete-file tmp-name)))))) +(ert-deftest tramp-test10-write-region-file-precious-flag () + "Check that `file-precious-flag' is respected with Tramp in use." + (skip-unless (tramp--test-enabled)) + (skip-unless (tramp--test-sh-p)) + + (let* ((tmp-name (tramp--test-make-temp-name)) + written-files + (advice (lambda (_start _end filename &rest _r) + (push filename written-files)))) + + (unwind-protect + (with-current-buffer (find-file-noselect tmp-name) + ;; Write initial contents. Adapt `visited-file-modtime' + ;; in order to suppress confirmation. + (insert "foo") + (write-region nil nil tmp-name) + (set-visited-file-modtime) + ;; Run the test. + (advice-add 'write-region :before advice) + (setq-local file-precious-flag t) + (insert "bar") + (should (null (save-buffer))) + (should-not (cl-member tmp-name written-files :test #'string=))) + + ;; Cleanup. + (ignore-errors (advice-remove 'write-region advice)) + (ignore-errors (delete-file tmp-name))))) + (ert-deftest tramp-test11-copy-file () "Check `copy-file'." (skip-unless (tramp--test-enabled)) -- 2.39.2