aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorF. Jason Park2023-01-23 20:48:24 -0800
committerF. Jason Park2023-04-08 14:23:51 -0700
commitd5435a0d822e95bfb18f3cdf4fac83611ec17972 (patch)
tree129b4603e4680b2b78355aac7a62057305000ca9 /test
parent05f6fdb9e7893329baff675bd31fb36ad64c756d (diff)
downloademacs-d5435a0d822e95bfb18f3cdf4fac83611ec17972.tar.gz
emacs-d5435a0d822e95bfb18f3cdf4fac83611ec17972.zip
Refactor marker initialization in erc-open
* lisp/erc/erc.el (erc--initialize-markers): New helper to ensure prompt and its associated markers are set up correctly. (erc-open): When determining whether a session is a logical continuation, leverage the work already performed by the `erc-networks' library to that effect. Its verdicts are based on network context and thus reliable even when a user dials anew from an entry-point, which is not a simple reconnection because the user expects a clean slate for everything except an existing buffer's messages, meaning `erc--server-reconnecting' will be nil and local-module state variables need resetting. Also remove the check for `erc-reuse-buffers' and instead trust that `erc-get-buffer-create' always does the right thing. Replace all code involving marker and prompt setup by deferring to a new helper, `erc--initialize markers'. * test/lisp/erc/erc-scenarios-base-local-module-modes.el: New file. * test/lisp/erc/erc-scenarios-base-local-modules.el (erc-scenarios-base-local-modules--mode-persistence): Move test to separate file to help with parallel "-j" runs. * test/lisp/erc/erc-tests.el (erc-tests--send-prep): Replace redundant prompt-setup code. (erc--initialize-markers): New test. (Bug#60936.)
Diffstat (limited to 'test')
-rw-r--r--test/lisp/erc/erc-scenarios-base-local-module-modes.el211
-rw-r--r--test/lisp/erc/erc-scenarios-base-local-modules.el99
-rw-r--r--test/lisp/erc/erc-tests.el79
3 files changed, 285 insertions, 104 deletions
diff --git a/test/lisp/erc/erc-scenarios-base-local-module-modes.el b/test/lisp/erc/erc-scenarios-base-local-module-modes.el
new file mode 100644
index 00000000000..7b91e28dc83
--- /dev/null
+++ b/test/lisp/erc/erc-scenarios-base-local-module-modes.el
@@ -0,0 +1,211 @@
1;;; erc-scenarios-base-local-module-modes.el --- More local-mod ERC tests -*- lexical-binding: t -*-
2
3;; Copyright (C) 2023 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; A local module doubles as a minor mode whose mode variable and
23;; associated local data can withstand service disruptions.
24;; Unfortunately, the current implementation is too unwieldy to be
25;; made public because it doesn't perform any of the boiler plate
26;; needed to save and restore buffer-local and "network-local" copies
27;; of user options. Ultimately, a user-friendly framework must fill
28;; this void if third-party local modules are ever to become
29;; practical.
30;;
31;; The following tests all use `sasl' because, as of ERC 5.5, it's the
32;; only local module.
33
34;;; Code:
35
36(require 'ert-x)
37(eval-and-compile
38 (let ((load-path (cons (ert-resource-directory) load-path)))
39 (require 'erc-scenarios-common)))
40
41(require 'erc-sasl)
42
43;; After quitting a session for which `sasl' is enabled, you
44;; disconnect and toggle `erc-sasl-mode' off. You then reconnect
45;; using an alternate nickname. You again disconnect and reconnect,
46;; this time immediately, and the mode stays disabled. Finally, you
47;; once again disconnect, toggle the mode back on, and reconnect. You
48;; are authenticated successfully, just like in the initial session.
49;;
50;; This is meant to show that a user's local mode settings persist
51;; between sessions. It also happens to show (in round four, below)
52;; that a server renicking a user on 001 after a 903 is handled just
53;; like a user-initiated renick, although this is not the main thrust.
54
55(ert-deftest erc-scenarios-base-local-module-modes--reconnect ()
56 :tags '(:expensive-test)
57 (erc-scenarios-common-with-cleanup
58 ((erc-scenarios-common-dialog "base/local-modules")
59 (erc-server-flood-penalty 0.1)
60 (dumb-server (erc-d-run "localhost" t 'first 'second 'third 'fourth))
61 (port (process-contact dumb-server :service))
62 (erc-modules (cons 'sasl erc-modules))
63 (expect (erc-d-t-make-expecter))
64 (server-buffer-name (format "127.0.0.1:%d" port)))
65
66 (ert-info ("Round one, initial authentication succeeds as expected")
67 (with-current-buffer (erc :server "127.0.0.1"
68 :port port
69 :nick "tester"
70 :user "tester"
71 :password "changeme"
72 :full-name "tester")
73 (should (string= (buffer-name) server-buffer-name))
74 (funcall expect 10 "You are now logged in as tester"))
75
76 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "foonet"))
77 (funcall expect 10 "This server is in debug mode")
78 (erc-cmd-JOIN "#chan")
79
80 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
81 (funcall expect 20 "She is Lavinia, therefore must"))
82
83 (erc-cmd-QUIT "")
84 (funcall expect 10 "finished")))
85
86 (ert-info ("Round two, nick rejected, alternate granted")
87 (with-current-buffer "foonet"
88
89 (ert-info ("Toggle mode off, reconnect")
90 (erc-sasl-mode -1)
91 (erc-cmd-RECONNECT))
92
93 (funcall expect 10 "User modes for tester`")
94 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
95 (should (equal (buffer-name) "foonet"))
96 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
97
98 (with-current-buffer "#chan"
99 (funcall expect 10 "Some enigma, some riddle"))
100
101 (erc-cmd-QUIT "")
102 (funcall expect 10 "finished")))
103
104 (ert-info ("Round three, send alternate nick initially")
105 (with-current-buffer "foonet"
106
107 (ert-info ("Keep mode off, reconnect")
108 (should-not erc-sasl-mode)
109 (should (local-variable-p 'erc-sasl-mode))
110 (erc-cmd-RECONNECT))
111
112 (funcall expect 10 "User modes for tester`")
113 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
114 (should (equal (buffer-name) "foonet"))
115 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
116
117 (with-current-buffer "#chan"
118 (funcall expect 10 "Let our reciprocal vows be remembered."))
119
120 (erc-cmd-QUIT "")
121 (funcall expect 10 "finished")))
122
123 (ert-info ("Round four, authenticated successfully again")
124 (with-current-buffer "foonet"
125
126 (ert-info ("Toggle mode on, reconnect")
127 (should-not erc-sasl-mode)
128 (should (local-variable-p 'erc-sasl-mode))
129 (erc-sasl-mode +1)
130 (erc-cmd-RECONNECT))
131
132 (funcall expect 10 "User modes for tester")
133 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
134 (should (equal (buffer-name) "foonet"))
135 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
136
137 (with-current-buffer "#chan"
138 (funcall expect 10 "Well met; good morrow, Titus and Hortensius."))
139
140 (erc-cmd-QUIT "")))))
141
142;; In contrast to the mode-persistence test above, this one
143;; demonstrates that a user reinvoking an entry point declares their
144;; intention to reset local-module state for the server buffer.
145;; Whether a local-module's state variable is also reset in target
146;; buffers up to the module. That is, by default, they're left alone.
147
148(ert-deftest erc-scenarios-base-local-module-modes--entrypoint ()
149 :tags '(:expensive-test)
150 (erc-scenarios-common-with-cleanup
151 ((erc-scenarios-common-dialog "base/local-modules")
152 (erc-server-flood-penalty 0.1)
153 (dumb-server (erc-d-run "localhost" t 'first 'first))
154 (port (process-contact dumb-server :service))
155 (erc-modules (cons 'sasl erc-modules))
156 (expect (erc-d-t-make-expecter))
157 (server-buffer-name (format "127.0.0.1:%d" port)))
158
159 (ert-info ("Round one, initial authentication succeeds as expected")
160 (with-current-buffer (erc :server "127.0.0.1"
161 :port port
162 :nick "tester"
163 :user "tester"
164 :password "changeme"
165 :full-name "tester")
166 (should (string= (buffer-name) server-buffer-name))
167 (funcall expect 10 "You are now logged in as tester"))
168
169 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "foonet"))
170 (funcall expect 10 "This server is in debug mode")
171 (erc-cmd-JOIN "#chan")
172
173 (ert-info ("Toggle local-module off in target buffer")
174 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
175 (funcall expect 20 "She is Lavinia, therefore must")
176 (erc-sasl-mode -1)))
177
178 (erc-cmd-QUIT "")
179 (funcall expect 10 "finished")
180
181 (ert-info ("Toggle mode off")
182 (erc-sasl-mode -1)
183 (should (local-variable-p 'erc-sasl-mode)))))
184
185 (ert-info ("Reconnecting via entry point discards `erc-sasl-mode' value.")
186 ;; If you were to /RECONNECT here, no PASS changeme would be
187 ;; sent instead of CAP SASL, resulting in a failure.
188 (with-current-buffer (erc :server "127.0.0.1"
189 :port port
190 :nick "tester"
191 :user "tester"
192 :password "changeme"
193 :full-name "tester")
194 (should (string= (buffer-name) server-buffer-name))
195 (funcall expect 10 "You are now logged in as tester")
196
197 (erc-d-t-wait-for 10 (equal (buffer-name) "foonet"))
198 (funcall expect 10 "User modes for tester")
199 (should erc-sasl-mode)) ; obviously
200
201 ;; No other foonet buffer exists, e.g., foonet<2>
202 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
203
204 (ert-info ("Target buffer retains local-module state")
205 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
206 (funcall expect 20 "She is Lavinia, therefore must")
207 (should-not erc-sasl-mode)
208 (should (local-variable-p 'erc-sasl-mode))
209 (erc-cmd-QUIT ""))))))
210
211;;; erc-scenarios-base-local-module-modes.el ends here
diff --git a/test/lisp/erc/erc-scenarios-base-local-modules.el b/test/lisp/erc/erc-scenarios-base-local-modules.el
index 1318207a3bf..d6dbd87c8cc 100644
--- a/test/lisp/erc/erc-scenarios-base-local-modules.el
+++ b/test/lisp/erc/erc-scenarios-base-local-modules.el
@@ -82,105 +82,6 @@
82 (erc-cmd-QUIT "") 82 (erc-cmd-QUIT "")
83 (funcall expect 10 "finished"))))) 83 (funcall expect 10 "finished")))))
84 84
85;; After quitting a session for which `sasl' is enabled, you
86;; disconnect and toggle `erc-sasl-mode' off. You then reconnect
87;; using an alternate nickname. You again disconnect and reconnect,
88;; this time immediately, and the mode stays disabled. Finally, you
89;; once again disconnect, toggle the mode back on, and reconnect. You
90;; are authenticated successfully, just like in the initial session.
91;;
92;; This is meant to show that a user's local mode settings persist
93;; between sessions. It also happens to show (in round four, below)
94;; that a server renicking a user on 001 after a 903 is handled just
95;; like a user-initiated renick, although this is not the main thrust.
96
97(ert-deftest erc-scenarios-base-local-modules--mode-persistence ()
98 :tags '(:expensive-test)
99 (erc-scenarios-common-with-cleanup
100 ((erc-scenarios-common-dialog "base/local-modules")
101 (erc-server-flood-penalty 0.1)
102 (dumb-server (erc-d-run "localhost" t 'first 'second 'third 'fourth))
103 (port (process-contact dumb-server :service))
104 (erc-modules (cons 'sasl erc-modules))
105 (expect (erc-d-t-make-expecter))
106 (server-buffer-name (format "127.0.0.1:%d" port)))
107
108 (ert-info ("Round one, initial authentication succeeds as expected")
109 (with-current-buffer (erc :server "127.0.0.1"
110 :port port
111 :nick "tester"
112 :user "tester"
113 :password "changeme"
114 :full-name "tester")
115 (should (string= (buffer-name) server-buffer-name))
116 (funcall expect 10 "You are now logged in as tester"))
117
118 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "foonet"))
119 (funcall expect 10 "This server is in debug mode")
120 (erc-cmd-JOIN "#chan")
121
122 (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
123 (funcall expect 20 "She is Lavinia, therefore must"))
124
125 (erc-cmd-QUIT "")
126 (funcall expect 10 "finished")))
127
128 (ert-info ("Round two, nick rejected, alternate granted")
129 (with-current-buffer "foonet"
130
131 (ert-info ("Toggle mode off, reconnect")
132 (erc-sasl-mode -1)
133 (erc-cmd-RECONNECT))
134
135 (funcall expect 10 "User modes for tester`")
136 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
137 (should (equal (buffer-name) "foonet"))
138 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
139
140 (with-current-buffer "#chan"
141 (funcall expect 10 "Some enigma, some riddle"))
142
143 (erc-cmd-QUIT "")
144 (funcall expect 10 "finished")))
145
146 (ert-info ("Round three, send alternate nick initially")
147 (with-current-buffer "foonet"
148
149 (ert-info ("Keep mode off, reconnect")
150 (should-not erc-sasl-mode)
151 (should (local-variable-p 'erc-sasl-mode))
152 (erc-cmd-RECONNECT))
153
154 (funcall expect 10 "User modes for tester`")
155 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
156 (should (equal (buffer-name) "foonet"))
157 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
158
159 (with-current-buffer "#chan"
160 (funcall expect 10 "Let our reciprocal vows be remembered."))
161
162 (erc-cmd-QUIT "")
163 (funcall expect 10 "finished")))
164
165 (ert-info ("Round four, authenticated successfully again")
166 (with-current-buffer "foonet"
167
168 (ert-info ("Toggle mode on, reconnect")
169 (should-not erc-sasl-mode)
170 (should (local-variable-p 'erc-sasl-mode))
171 (erc-sasl-mode +1)
172 (erc-cmd-RECONNECT))
173
174 (funcall expect 10 "User modes for tester")
175 (should-not (cdr (erc-scenarios-common-buflist "foonet")))
176 (should (equal (buffer-name) "foonet"))
177 (should-not (cdr (erc-scenarios-common-buflist "#chan")))
178
179 (with-current-buffer "#chan"
180 (funcall expect 10 "Well met; good morrow, Titus and Hortensius."))
181
182 (erc-cmd-QUIT "")))))
183
184;; For local modules, the twin toggle commands `erc-FOO-enable' and 85;; For local modules, the twin toggle commands `erc-FOO-enable' and
185;; `erc-FOO-disable' affect all buffers of a connection, whereas 86;; `erc-FOO-disable' affect all buffers of a connection, whereas
186;; `erc-FOO-mode' continues to operate only on the current buffer. 87;; `erc-FOO-mode' continues to operate only on the current buffer.
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index b2f24aa718e..6e66de53edd 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -117,11 +117,7 @@
117 ;; Caller should probably shadow `erc-insert-modify-hook' or 117 ;; Caller should probably shadow `erc-insert-modify-hook' or
118 ;; populate user tables for erc-button. 118 ;; populate user tables for erc-button.
119 (erc-mode) 119 (erc-mode)
120 (insert "\n\n") 120 (erc--initialize-markers (point) nil)
121 (setq erc-input-marker (make-marker)
122 erc-insert-marker (make-marker))
123 (set-marker erc-insert-marker (point-max))
124 (erc-display-prompt)
125 (should (= (point) erc-input-marker))) 121 (should (= (point) erc-input-marker)))
126 122
127(defun erc-tests--set-fake-server-process (&rest args) 123(defun erc-tests--set-fake-server-process (&rest args)
@@ -257,6 +253,79 @@
257 (kill-buffer "bob") 253 (kill-buffer "bob")
258 (kill-buffer "ServNet")))) 254 (kill-buffer "ServNet"))))
259 255
256(ert-deftest erc--initialize-markers ()
257 (let ((proc (start-process "true" (current-buffer) "true"))
258 erc-modules
259 erc-connect-pre-hook
260 erc-insert-modify-hook
261 erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook)
262 (set-process-query-on-exit-flag proc nil)
263 (erc-mode)
264 (setq erc-server-process proc
265 erc-networks--id (erc-networks--id-create 'foonet))
266 (erc-open "localhost" 6667 "tester" "Tester" nil
267 "fake" nil "#chan" proc nil "user" nil)
268 (with-current-buffer (should (get-buffer "#chan"))
269 (should (= ?\n (char-after 1)))
270 (should (= ?E (char-after erc-insert-marker)))
271 (should (= 3 (marker-position erc-insert-marker)))
272 (should (= 8 (marker-position erc-input-marker)))
273 (should (= 8 (point-max)))
274 (should (= 8 (point)))
275 ;; These prompt properties are a continual source of confusion.
276 ;; Including the literal defaults here can hopefully serve as a
277 ;; quick reference for anyone operating in that area.
278 (should (equal (buffer-string)
279 #("\n\nERC> "
280 2 6 ( font-lock-face erc-prompt-face
281 rear-nonsticky t
282 erc-prompt t
283 field erc-prompt
284 front-sticky t
285 read-only t)
286 6 7 ( rear-nonsticky t
287 erc-prompt t
288 field erc-prompt
289 front-sticky t
290 read-only t))))
291
292 ;; Simulate some activity by inserting some text before and
293 ;; after the prompt (multiline).
294 (erc-display-error-notice nil "Welcome")
295 (goto-char (point-max))
296 (insert "Hello\nWorld")
297 (goto-char 3)
298 (should (looking-at-p (regexp-quote "*** Welcome"))))
299
300 (ert-info ("Reconnect")
301 (erc-open "localhost" 6667 "tester" "Tester" nil
302 "fake" nil "#chan" proc nil "user" nil)
303 (should-not (get-buffer "#chan<2>")))
304
305 (ert-info ("Existing prompt respected")
306 (with-current-buffer (should (get-buffer "#chan"))
307 (should (= ?\n (char-after 1)))
308 (should (= ?E (char-after erc-insert-marker)))
309 (should (= 15 (marker-position erc-insert-marker)))
310 (should (= 20 (marker-position erc-input-marker)))
311 (should (= 3 (point))) ; point restored
312 (should (equal (buffer-string)
313 #("\n\n*** Welcome\nERC> Hello\nWorld"
314 2 13 (font-lock-face erc-error-face)
315 14 18 ( font-lock-face erc-prompt-face
316 rear-nonsticky t
317 erc-prompt t
318 field erc-prompt
319 front-sticky t
320 read-only t)
321 18 19 ( rear-nonsticky t
322 erc-prompt t
323 field erc-prompt
324 front-sticky t
325 read-only t))))
326 (when noninteractive
327 (kill-buffer))))))
328
260(ert-deftest erc--switch-to-buffer () 329(ert-deftest erc--switch-to-buffer ()
261 (defvar erc-modified-channels-alist) ; lisp/erc/erc-track.el 330 (defvar erc-modified-channels-alist) ; lisp/erc/erc-track.el
262 331