Subject: bug#53617: 28.0.90; duplicate checks in erc--switch-to-buffer

From: "J.P." <[email-masked]>
Date: Mon, 14 Mar 2022 06:33:48 -0700

"J.P." <[email-masked]> writes:

> I've added the customary tweaks to the log message if someone wants to
> glance at it (cc. bandali, Lars).

This now includes a unit test.

Unless anyone objects or I'm preempted, I'll add this to 29 on or around
March 19th (~5 days from now), and leave ERC's version number unchanged
at 5.4.1. Thanks.

>From 751e93d57ac00399dfb528b270fe753a3e8cfc91 Mon Sep 17 00:00:00 2001
From: Guy Gastineau <[email-masked]>
Date: Fri, 28 Jan 2022 23:12:42 -0500
Subject: [PATCH 1/2] Remove redundant checks in erc--switch-to-buffer

* lisp/erc/erc.el (erc--switch-to-buffer): Commit
f925fc93bac41d7622d1af927e33b0e738ff55b0 "Add `predicate' arg to
`read-buffer' and use it for erc-iswitchb" meant to remove this, but
it was left behind. (Bug#53617).

Copyright-paperwork-exempt: yes
---
 lisp/erc/erc.el | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 9ee8d38b02..52fe106f2d 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -1761,12 +1761,7 @@ erc--switch-to-buffer
 		 (lambda (bufname)
 		   (let ((buf (if (consp bufname)
 				  (cdr bufname) (get-buffer bufname))))
-		     (when buf
-		       (erc--buffer-p buf (lambda () t) proc)
-		       (with-current-buffer buf
-			 (and (derived-mode-p 'erc-mode)
-			      (or (null proc)
-				  (eq proc erc-server-process))))))))))
+                     (and buf (erc--buffer-p buf (lambda () t) proc)))))))
 (defun erc-switch-to-buffer (&optional arg)
   "Prompt for an ERC buffer to switch to.
 When invoked with prefix argument, use all ERC buffers.  Without
-- 
2.35.1

>From d0c6ef217a18c98e18e4c29e2cba2e9ab63af970 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email-masked]>
Date: Sun, 13 Mar 2022 22:39:36 -0700
Subject: [PATCH 2/2] [DROP ME] Add eventual unit test for
 erc--switch-to-buffer

* test/lisp/erc/erc-tests.el (erc--switch-to-buffer): Add regression
test for Bug#53617.  Prefer postponing addition until after #48598.
---
 test/lisp/erc/erc-tests.el | 73 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index 5603e76454..a14c4c0460 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -114,6 +114,79 @@ erc-with-all-buffers-of-server
     (should (get-buffer "#spam"))
     (kill-buffer "#spam")))
 
+;; DELETE ME BEG (just a demo, for now)
+(require 'ert-x)
+(defvar erc-modified-channels-alist)
+
+(ert-deftest erc--switch-to-buffer ()
+  (unless (version< "28" emacs-version)
+    (ert-skip "Depends on symbol `minibuffer-quit'"))
+
+  (let ((proc (start-process "aNet" (current-buffer) "true"))
+        (erc-modified-channels-alist `(("fake") (,(messages-buffer))))
+        ;;
+        erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook)
+
+    (with-current-buffer (get-buffer-create "server")
+      (erc-mode)
+      (set-process-buffer (setq erc-server-process proc) (current-buffer))
+      (with-current-buffer (get-buffer-create "#chan")
+        (erc-mode)
+        (setq erc-server-process proc))
+      (with-current-buffer (get-buffer-create "#foo")
+        (erc-mode)
+        (setq erc-server-process proc))
+
+      (ert-info ("Channel #chan selectable from server buffer")
+        (ert-simulate-keys (list ?# ?c ?h ?a ?n ?\C-m)
+          (should (string= "#chan" (erc--switch-to-buffer))))))
+
+    (ert-info ("Channel #foo selectable from non-ERC buffer")
+      (ert-simulate-keys (list ?# ?f ?o ?o ?\C-m)
+        (should (string= "#foo" (erc--switch-to-buffer)))))
+
+    (ert-info ("Default selectable")
+      (ert-simulate-keys (list ?\C-m)
+        (should (string= "*Messages*" (erc--switch-to-buffer)))))
+
+    ;; Can't use `should-error' here because `minibuffer-quit' derives
+    ;; from `quit' instead of `error', and only the latter is trapped.
+    (ert-info ("Extant but non-ERC buffer not selectable")
+      (get-buffer-create "#fake") ; not ours
+      (let (ran)
+        (condition-case s
+            (ert-simulate-keys (list ?# ?f ?a ?k ?e ?\C-m)
+              (erc--switch-to-buffer))
+          (minibuffer-quit (setq ran s)))
+        (should (equal ran '(minibuffer-quit)))))
+
+    (with-current-buffer (get-buffer-create "other")
+      (erc-mode)
+      (setq erc-server-process
+            (start-process "bNet" (current-buffer) "true")))
+
+    (ert-info ("Foreign ERC buffer not selectable")
+      (let (ran)
+        (condition-case s
+            (ert-simulate-keys (list ?o ?t ?h ?e ?r ?\C-m)
+              (with-current-buffer "server"
+                (erc--switch-to-buffer)))
+          (minibuffer-quit (setq ran s)))
+        (should (equal ran '(minibuffer-quit)))))
+
+    (ert-info ("Any ERC-buffer selectable from non-ERC buffer")
+      (should-not (eq major-mode 'erc-mode))
+      (ert-simulate-keys (list ?o ?t ?h ?e ?r ?\C-m)
+        (should (string= "other" (erc--switch-to-buffer)))))
+
+    (kill-buffer "server")
+    (kill-buffer "other")
+    (kill-buffer "#chan")
+    (kill-buffer "#foo")
+    (kill-buffer "#fake")))
+
+;; DELETE ME END (just a demo, for now)
+
 (ert-deftest erc-lurker-maybe-trim ()
   (let (erc-lurker-trim-nicks
         (erc-lurker-ignore-chars "_`"))
-- 
2.35.1