From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jens Schmidt via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#66546: 30.0.50; save-buffer to write-protected file without backup fails Date: Sat, 04 Nov 2023 13:30:37 +0100 Message-ID: <87msvt1z0y.fsf@sappc2.fritz.box> References: <87wmvmfi68.fsf@sappc2.fritz.box> <83o7gxo771.fsf@gnu.org> <87ttqpgg8l.fsf@sappc2.fritz.box> <83cyxcnp2f.fsf@gnu.org> <87sf671xe3.fsf@sappc2.fritz.box> <83y1fzmdhm.fsf@gnu.org> <87zg0ez57o.fsf@sappc2.fritz.box> <835y31u8t0.fsf@gnu.org> <87edhnamg3.fsf@sappc2.fritz.box> <83a5sbre7x.fsf@gnu.org> <87bkcrafxt.fsf@sappc2.fritz.box> <834jijqmnd.fsf@gnu.org> <878r7v9ei7.fsf@sappc2.fritz.box> <838r7qkgdp.fsf@gnu.org> <5097ddb3-c8a9-47ce-a6da-4ab73ca31172@vodafonemail.de> <838r7lbih3.fsf@gnu.org> <87wmv1wawz.fsf@sappc2.fritz.box> <83bkcc8xe5.fsf@gnu.org> <87fs1mh7p0.fsf@sappc2.fritz.box> <8334xl6gk5.fsf@gnu.org> Reply-To: Jens Schmidt Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27989"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Cc: 66546@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 04 13:31:55 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qzFog-00075b-NF for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 04 Nov 2023 13:31:54 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qzFoN-0006KI-53; Sat, 04 Nov 2023 08:31:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qzFoE-0006Jc-4R for bug-gnu-emacs@gnu.org; Sat, 04 Nov 2023 08:31:26 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qzFoD-0000l8-Sg for bug-gnu-emacs@gnu.org; Sat, 04 Nov 2023 08:31:25 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qzFon-0000Pu-WB for bug-gnu-emacs@gnu.org; Sat, 04 Nov 2023 08:32:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jens Schmidt Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 04 Nov 2023 12:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66546 X-GNU-PR-Package: emacs Original-Received: via spool by 66546-submit@debbugs.gnu.org id=B66546.16991010931564 (code B ref 66546); Sat, 04 Nov 2023 12:32:01 +0000 Original-Received: (at 66546) by debbugs.gnu.org; 4 Nov 2023 12:31:33 +0000 Original-Received: from localhost ([127.0.0.1]:33057 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qzFoK-0000P9-MV for submit@debbugs.gnu.org; Sat, 04 Nov 2023 08:31:33 -0400 Original-Received: from mr3.vodafonemail.de ([145.253.228.163]:59604) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qzFoJ-0000Ou-M4 for 66546@debbugs.gnu.org; Sat, 04 Nov 2023 08:31:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vodafonemail.de; s=vfde-mb-mr2-23sep; t=1699101049; bh=TANrn2AcUuGfPisB8teQKdVeTW4L/jzAbB2+0wiphC4=; h=From:To:Subject:References:Date:In-Reply-To:Message-ID:User-Agent: Content-Type:From; b=cbsslR9smPZgV6UMnrSD2gcqYfAiDK5zWHhzu5u6PnlB3+mb6Br+2wK/BnYZKBcpz S4JEoc156uneUbN7yRrPGOZA3/kzEsHTBmIflIorNbieQif24fpv0NF51Wr1njuq20 /XKn/dum0+omWgwaOa7CBRCoEt9vu5qjScBl9Z2I= Original-Received: from smtp.vodafone.de (unknown [10.0.0.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by mr3.vodafonemail.de (Postfix) with ESMTPS id 4SMxm92l0Hz1yYn; Sat, 4 Nov 2023 12:30:49 +0000 (UTC) Original-Received: from sappc2 (port-92-196-12-179.dynamic.as20676.net [92.196.12.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp.vodafone.de (Postfix) with ESMTPSA id 4SMxm13qMLz9sD2; Sat, 4 Nov 2023 12:30:38 +0000 (UTC) In-Reply-To: <8334xl6gk5.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 04 Nov 2023 10:58:18 +0200") X-purgate-type: clean X-purgate: clean X-purgate-size: 7951 X-purgate-ID: 155817::1699101044-78FF67FF-2631B585/0/0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:273777 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: > I think one nit is still missing: Right you are. Fixed in next version. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Add-tests-for-saving-to-write-protected-files.patch >From b9ed2833818f68d8e0b6fa71ded4599dce9d3247 Mon Sep 17 00:00:00 2001 From: Jens Schmidt Date: Wed, 1 Nov 2023 19:56:06 +0100 Subject: [PATCH] Add tests for saving to write-protected files * test/lisp/files-tests.el (files-tests--with-yes-or-no-p): Add macro. (files-tests-save-buffer-read-only-file): Add test for writing to write-protected files with `save-buffer'. (Bug#66546) --- test/lisp/files-tests.el | 148 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index 63ce4dab7eb..77a4c22ed6a 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1717,6 +1717,154 @@ files-tests--save-some-buffers (set-buffer-modified-p nil) (kill-buffer buf))))))) +(defmacro files-tests--with-yes-or-no-p (reply &rest body) + "Execute BODY, providing replies to `yes-or-no-p' queries. +REPLY should be a cons (PROMPT . VALUE), and during execution of +BODY this macro provides VALUE as return value to all +`yes-or-no-p' calls prompting for PROMPT and nil to all other +`yes-or-no-p' calls. After execution of BODY, this macro ensures +that exactly one `yes-or-no-p' call prompting for PROMPT has been +executed during execution of BODY." + (declare (indent 1) (debug (sexp body))) + `(cl-letf* + ((reply ,reply) + (prompts nil) + ((symbol-function 'yes-or-no-p) + (lambda (prompt) + (let ((reply (cdr (assoc prompt (list reply))))) + (push (cons prompt reply) prompts) + reply)))) + ,@body + (should (equal prompts (list reply))))) + +(ert-deftest files-tests-save-buffer-read-only-file () + "Test writing to write-protected files with `save-buffer'. +Ensure that the issues from bug#66546 are fixed." + (ert-with-temp-directory dir + (cl-flet (;; Define convenience functions. + (file-contents (file) + (if (file-exists-p file) + (condition-case err + (with-temp-buffer + (insert-file-contents-literally file) + (buffer-string)) + (error err)) + 'missing)) + (signal-write-failed (&rest _) + (signal 'file-error "Write failed"))) + + (let* (;; Sanitize environment. + (coding-system-for-read 'no-conversion) + (coding-system-for-write 'no-conversion) + (auto-save-default nil) + (backup-enable-predicate nil) + (before-save-hook nil) + (write-contents-functions nil) + (write-file-functions nil) + (after-save-hook nil) + + ;; Set the name of the game. + (base "read-only-test") + (file (expand-file-name base dir)) + (backup (make-backup-file-name file)) + + (override-read-only-prompt + (format "File %s is write-protected; try to save anyway? " + base))) + + ;; Ensure that set-file-modes renders our test file read-only, + ;; otherwise skip this test. Use `file-writable-p' to test + ;; for read-only-ness, because that's what function + ;; `save-buffer' uses as well. + (with-temp-file file (insert "foo\n")) + (skip-unless (file-writable-p file)) + (set-file-modes file (logand (file-modes file) + (lognot #o0222))) + (skip-unless (not (file-writable-p file))) + + (with-current-buffer (find-file-noselect file) + ;; Prepare for tests backing up the file. + (setq buffer-read-only nil) + (goto-char (point-min)) + (insert "bar\n") + + ;; Save to read-only file with backup, declining prompt. + (files-tests--with-yes-or-no-p + (cons override-read-only-prompt nil) + (should-error + (save-buffer) + ;; "Attempt to save to a file that you aren't allowed to write" + :type 'error)) + (should-not buffer-backed-up) + (should (buffer-modified-p)) + (should-not (file-writable-p file)) + (should (equal (file-contents file) "foo\n")) + (should (equal (file-contents backup) 'missing)) + + ;; Save to read-only file with backup, accepting prompt, + ;; experiencing a write error. + (files-tests--with-yes-or-no-p + (cons override-read-only-prompt t) + (should-error + (cl-letf (((symbol-function 'write-region) + #'signal-write-failed)) + (save-buffer)) + ;; "Write failed" + :type 'file-error)) + (should-not buffer-backed-up) + (should (buffer-modified-p)) + (should-not (file-writable-p file)) + (should (equal (file-contents file) "foo\n")) + (should (equal (file-contents backup) 'missing)) + + ;; Save to read-only file with backup, accepting prompt. + (files-tests--with-yes-or-no-p + (cons override-read-only-prompt t) + (save-buffer)) + (should buffer-backed-up) + (should-not (buffer-modified-p)) + (should-not (file-writable-p file)) + (should-not (file-writable-p backup)) + (should (equal (file-contents file) "bar\nfoo\n")) + (should (equal (file-contents backup) "foo\n")) + + ;; Prepare for tests not backing up the file. + (setq buffer-backed-up nil) + (delete-file backup) + (goto-char (point-min)) + (insert "baz\n") + + ;; Save to read-only file without backup, accepting prompt, + ;; experiencing a write error. This tests that issue B of + ;; bug#66546 is fixed. The results of the "with backup" and + ;; "without backup" subtests are identical when a write + ;; error occurs, but the code paths to reach these results + ;; are not. In other words, this subtest is not redundant. + (files-tests--with-yes-or-no-p + (cons override-read-only-prompt t) + (should-error + (cl-letf (((symbol-function 'write-region) + #'signal-write-failed)) + (save-buffer 0)) + ;; "Write failed" + :type 'file-error)) + (should-not buffer-backed-up) + (should (buffer-modified-p)) + (should-not (file-writable-p file)) + (should (equal (file-contents file) "bar\nfoo\n")) + (should (equal (file-contents backup) 'missing)) + + ;; Save to read-only file without backup, accepting prompt. + ;; This tests that issue A of bug#66546 is fixed. + (files-tests--with-yes-or-no-p + (cons override-read-only-prompt t) + (save-buffer 0)) + (should-not buffer-backed-up) + (should-not (buffer-modified-p)) + (should-not (file-writable-p file)) + (should (equal (file-contents file) "baz\nbar\nfoo\n")) + (should (equal (file-contents backup) 'missing))))))) + (ert-deftest files-tests-save-some-buffers () "Test `save-some-buffers'. Test the 3 cases for the second argument PRED, i.e., nil, t, or -- 2.30.2 --=-=-=--