Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby versions

From: Peter Oliver <[email-masked]>
Date: Tue, 27 Apr 2021 16:29:17 +0100 (BST)

On Tue, 27 Apr 2021, Dmitry Gutov wrote:

> On 26.04.2021 21:28, Peter Oliver wrote:
>
>>  Two patches are attached to address this:
>>
>>  - The first patch adds a new choice to ruby-insert-encoding-magic-comment,
>>  unless-utf8, which causes the magic comment not to be inserted if the
>>  encoding is UTF-8.
>>
>>  - The second patch, perhaps more controversially, makes this the default.
>
> Both changes make sense to me.
>
> However, I've looked at the existing code and found a prior change which 
> intended for this to be more customizable already, yet had a minor bug.
>
> Please try out the following patch:

That works for me, and I think is more straightforward than my approach.  Thanks.

Attached is an additional patch which adapts the tests added in my patch for your patch.

-- 
Peter Oliver

From 675c08cee899444f33113b806d6709b569c44790 Mon Sep 17 00:00:00 2001
From: Peter Oliver <[email-masked]>
Date: Tue, 27 Apr 2021 16:24:58 +0100
Subject: [PATCH] Test ruby-mode-set-encoding with a few different encodings
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Follows on from Dmitry Gutov’s patch in
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=48043#8.
---
 test/lisp/progmodes/ruby-mode-tests.el | 30 ++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el
index 42a011c8bc..fec7d86a95 100644
--- a/test/lisp/progmodes/ruby-mode-tests.el
+++ b/test/lisp/progmodes/ruby-mode-tests.el
@@ -32,6 +32,12 @@ ruby-with-temp-buffer
      (ruby-mode)
      ,@body))
 
+(defmacro ruby-with-temp-file (contents &rest body)
+  `(ruby-with-temp-buffer ,contents
+     (set-visited-file-name "ruby-mode-tests")
+     ,@body
+     (delete-file buffer-file-name)))
+
 (defun ruby-should-indent (content column)
   "Assert indentation COLUMN on the last line of CONTENT."
   (ruby-with-temp-buffer content
@@ -844,6 +850,30 @@ ruby--insert-coding-comment-custom-style
       (ruby--insert-coding-comment "utf-8")
       (should (string= "# encoding: utf-8\n\n" (buffer-string))))))
 
+(ert-deftest ruby--set-encoding-when-ascii ()
+  (ruby-with-temp-file "ascii"
+    (let ((ruby-encoding-magic-comment-style 'ruby)
+          (ruby-insert-encoding-magic-comment t))
+      (setq save-buffer-coding-system 'us-ascii)
+      (ruby-mode-set-encoding)
+      (should (string= "ascii" (buffer-string))))))
+
+(ert-deftest ruby--set-encoding-when-utf8 ()
+  (ruby-with-temp-file "💎"
+    (let ((ruby-encoding-magic-comment-style 'ruby)
+          (ruby-insert-encoding-magic-comment t))
+      (setq save-buffer-coding-system 'utf-8)
+      (ruby-mode-set-encoding)
+      (should (string= "💎" (buffer-string))))))
+
+(ert-deftest ruby--set-encoding-when-latin-15 ()
+  (ruby-with-temp-file "Ⓡ"
+    (let ((ruby-encoding-magic-comment-style 'ruby)
+          (ruby-insert-encoding-magic-comment t))
+      (setq save-buffer-coding-system 'iso-8859-15)
+      (ruby-mode-set-encoding)
+      (should (string= "# coding: iso-8859-15\nⓇ" (buffer-string))))))
+
 (ert-deftest ruby--indent/converted-from-manual-test ()
   :tags '(:expensive-test)
   ;; Converted from manual test.
-- 
2.26.3