unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 08781d2eb041f93fa2169a952beb97baa3776d9f 20440 bytes (raw)
name: test/lisp/erc/erc-networks-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
 
;;; erc-networks-tests.el --- Tests for erc-networks.  -*- lexical-binding:t -*-

;; Copyright (C) 2020-2021 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert-x) ; cl-lib

(require 'erc-networks)

;; FIXME maybe create common helpers file; these three are copied from
;; test/lisp/erc/erc-tests.el

(defun erc-tests--create-live-proc (&optional buf)
  (let ((proc (start-process "sleep" (or buf (current-buffer)) "sleep" "1")))
    (set-process-query-on-exit-flag proc nil)
    proc))

(defun erc-tests--create-dead-proc (&optional buf)
  (let ((p (start-process "true" (or buf (current-buffer)) "true")))
    (while (process-live-p p) (sit-for 0.1))
    p))

(defun erc-tests--clean-bufs ()
  (let (erc-kill-channel-hook
        erc-kill-server-hook
        erc-kill-buffer-hook)
    (dolist (buf (erc-buffer-list))
      (kill-buffer buf))))

(ert-deftest erc-networks--set-name ()
  (with-current-buffer (get-buffer-create "localhost:6667")
    (let (erc-server-announced-name
          erc-isupport-parameters
          erc-network
          calls)
      (erc-mode)
      (cl-letf (((symbol-function 'erc-display-line-1)
                 (lambda (&rest r) (push r calls))))
        (ert-info ("Errors when `erc-server-announced-name' unset")
          (should-error (erc-networks--set-name nil (make-erc-response))))
        (should-not calls)
        (setq erc-server-announced-name "irc.fake.gnu.org")
        (ert-info ("Errors out when table empty and NETWORK param unset")
          (let ((err (should-error (erc-networks--set-name
                                    nil (make-erc-response)))))
            (should (string-match-p "failed" (cadr err)))
            (should (eq (car err) 'error)))
          (should (string-match-p "*** Failed" (car (pop calls)))))))
    (erc-tests--clean-bufs)))

(ert-deftest erc-networks--rename-server-buffer--no-existing--orphan ()
  (with-current-buffer (get-buffer-create "#chan")
    (erc-mode)
    (setq erc-network 'FooNet
          erc-server-current-nick "tester"
          erc--buffer-target (erc--target-from-string "#chan")
          erc--session (erc--sid-create nil)))

  (with-current-buffer (get-buffer-create "irc.foonet.org")
    (erc-mode)
    (setq erc-network 'FooNet
          erc-server-current-nick "tester"
          erc-server-process (erc-tests--create-live-proc)
          erc--session (erc--sid-create nil))
    (should-not (erc-networks--rename-server-buffer erc-server-process))
    (should (string= (buffer-name) "FooNet")))

  (ert-info ("Channel buffer reassociated")
    (erc-server-process-alive "#chan")
    (with-current-buffer "#chan"
      (should erc-server-connected)
      (erc-with-server-buffer
        (should (string= (buffer-name) "FooNet")))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--existing--reuse ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc--session (erc--sid-create nil)
            erc--buffer-target (erc--target-from-string "#chan")))

    (ert-info ("New buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-process (erc-tests--create-live-proc)
              erc--session (erc--sid-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer reassociated")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--existing--noreuse ()
  (should erc-reuse-buffers) ; default
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-dead-proc old-buf))
         erc-reuse-buffers)
    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil)))
    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc--session (erc--sid-create nil)
            erc--buffer-target (erc--target-from-string "#chan")))

    (ert-info ("Server buffer uniquely renamed")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-process (erc-tests--create-live-proc)
              erc--session (erc--sid-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet<2>"))
        (goto-char (point-min))
        (should-not (search-forward "Old buf" nil t))))

    (ert-info ("Channel buffer reassociated")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet<2>")))))

    (ert-info ("Old buffer still around")
      (should (buffer-live-p old-buf))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--reconnecting ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc--buffer-target (erc--target-from-string "#chan")
            erc--session (erc--sid-create nil)))

    (ert-info ("No new buffer")
      (with-current-buffer old-buf
        (setq erc-server-process (erc-tests--create-live-proc))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer updated with live proc")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet"))))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--session-id ()
  (let* ((old-buf (get-buffer-create "MySession"))
         (old-proc (erc-tests--create-dead-proc old-buf)))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc--session (erc--sid-create 'MySession)
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc--session (erc--sid-create 'MySession)
            erc-server-process old-proc
            erc--buffer-target (erc--target-from-string "#chan")))

    (ert-info ("No new buffer")
      (with-current-buffer old-buf
        (setq erc-server-process (erc-tests--create-live-proc))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "MySession"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer updated with live proc")
      (erc-server-process-alive "#chan")
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "MySession"))))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--existing--live ()
  (let* (erc-kill-server-hook
         erc-insert-modify-hook
         (old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-live-proc old-buf))) ; live

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil))
      (should (erc-server-process-alive)))

    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc--session (erc--sid-create nil)
            erc-server-connected t
            erc--buffer-target (erc--target-from-string "#chan")))

    (ert-info ("New buffer rejected, abandoned, not killed")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-insert-marker (set-marker (make-marker) (point-max))
              erc-server-process (erc-tests--create-live-proc)
              erc--session (erc--sid-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (eq erc-active-buffer old-buf))
        (should-not (erc-server-process-alive))
        (should (string= (buffer-name) "irc.foonet.org"))
        (goto-char (point-min))
        (search-forward "still connected")))

    (ert-info ("Channel buffer updated with live proc")
      (should (erc-server-process-alive "#chan"))
      (with-current-buffer "#chan"
        (should erc-server-connected)
        (should (erc-server-buffer-live-p))
        (should (eq erc-server-process old-proc))
        (should (buffer-live-p (process-buffer erc-server-process)))
        (with-current-buffer (process-buffer erc-server-process)
          (should (eq (current-buffer) (get-buffer "FooNet")))
          (should (eq (current-buffer) old-buf))))))

  (should (get-buffer "FooNet"))
  (should (get-buffer "irc.foonet.org"))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--rename-server-buffer--local-match ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-dead-proc old-buf))
         (erc-isupport-parameters '((CHANTYPES "&#"))))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-server-announced-name "us-east.foonet.org"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil)))

    (with-current-buffer (get-buffer-create "&chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-server-announced-name "us-east.foonet.org"
            erc--buffer-target (erc--target-from-string "&chan")
            erc--session (erc--sid-create nil)))

    (ert-info ("New server buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-announced-name "us-east.foonet.org"
              erc-server-process (erc-tests--create-live-proc)
              erc--session (erc--sid-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer reassociated when &local server matches")
      (should (erc-server-process-alive "&chan"))
      (with-current-buffer "&chan"
        (should erc-server-connected)
        (should-not (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf)))

    (erc-tests--clean-bufs)))

(ert-deftest erc-networks--rename-server-buffer--local-nomatch ()
  (let* ((old-buf (get-buffer-create "FooNet"))
         (old-proc (erc-tests--create-dead-proc old-buf))
         (erc-isupport-parameters '((CHANTYPES "&#"))))

    (with-current-buffer old-buf
      (erc-mode)
      (insert "*** Old buf")
      (setq erc-network 'FooNet
            erc-server-current-nick "tester"
            erc-server-announced-name "us-west.foonet.org"
            erc-insert-marker (set-marker (make-marker) (point-max))
            erc-server-process old-proc
            erc--session (erc--sid-create nil)))

    (with-current-buffer (get-buffer-create "&chan")
      (erc-mode)
      (setq erc-network 'FooNet
            erc-server-process old-proc
            erc-server-announced-name "us-west.foonet.org" ; west
            erc--buffer-target (erc--target-from-string "&chan")
            erc--session (erc--sid-create nil)))

    (ert-info ("New server buffer steals name, content")
      (with-current-buffer (get-buffer-create "irc.foonet.org")
        (erc-mode)
        (setq erc-network 'FooNet
              erc-server-current-nick "tester"
              erc-server-announced-name "us-east.foonet.org" ; east
              erc-server-process (erc-tests--create-live-proc)
              erc--session (erc--sid-create nil))
        (should-not (erc-networks--rename-server-buffer erc-server-process))
        (should (string= (buffer-name) "FooNet"))
        (goto-char (point-min))
        (should (search-forward "Old buf"))))

    (ert-info ("Channel buffer now orphaned even though network matches")
      (should-not (erc-server-process-alive "&chan"))
      (with-current-buffer "&chan"
        (should-not erc-server-connected)
        (should (eq erc-server-process old-proc))
        (erc-with-server-buffer
          (should (string= (buffer-name) "FooNet")))))

    (ert-info ("Original buffer killed off")
      (should-not (buffer-live-p old-buf)))

    (erc-tests--clean-bufs)))

(ert-deftest erc-networks--update-server-session--double-existing ()
  (with-temp-buffer
    (erc-mode)
    (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "#chan@foonet/bob")
      (erc-mode)
      (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 2)))
    (with-current-buffer (get-buffer-create "foonet/alice")
      (erc-mode)
      (setq erc--session
            (make-erc--sid-dynamic :parts [foonet "alice"] :len 2)))

    (ert-info ("Adopt equivalent session")
      (should (eq (erc-networks--update-server-session)
                  (with-current-buffer "#chan@foonet/bob" erc--session))))

    (ert-info ("Ignore non-matches")
      (should-not (erc-networks--update-server-session))
      (should (eq erc--session
                  (with-current-buffer "#chan@foonet/bob" erc--session)))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--update-server-session--double-new ()
  (with-temp-buffer
    (erc-mode)
    (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet/alice")
      (erc-mode)
      (setq erc--session
            (make-erc--sid-dynamic :parts [foonet "alice"] :len 2)))
    (with-current-buffer (get-buffer-create "#chan@foonet/alice")
      (erc-mode)
      (setq erc--session (with-current-buffer "foonet/alice" erc--session)))

    (ert-info ("Evolve session to prevent ambiguity")
      (should-not (erc-networks--update-server-session))
      (should (= (erc--sid-dynamic-len erc--session) 2))
      (should (eq (erc--sid-symbol erc--session) 'foonet/bob))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--update-server-session--double-bounded ()
  (with-temp-buffer
    (erc-mode)
    (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet/alice/home")
      (erc-mode)
      (setq erc--session (make-erc--sid-dynamic :parts [foonet "alice" home]
                                               :len 3)))
    (with-current-buffer (get-buffer-create "#chan@foonet/alice/home")
      (erc-mode)
      (setq erc--session (with-current-buffer "foonet/alice/home"
                          erc--session)))

    (ert-info ("Evolve session to prevent ambiguity")
      (should-not (erc-networks--update-server-session))
      (should (= (erc--sid-dynamic-len erc--session) 2))
      (should (eq (erc--sid-symbol erc--session) 'foonet/bob))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--update-server-session--double-even ()
  (with-temp-buffer
    (erc-mode)
    (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob"] :len 1))

    (with-current-buffer (get-buffer-create "foonet")
      (erc-mode)
      (setq erc--session
            (make-erc--sid-dynamic :parts [foonet "alice"] :len 1)))
    (with-current-buffer (get-buffer-create "#chan")
      (erc-mode)
      (setq erc--buffer-target (erc--target-from-string "#chan"))
      (setq erc--session (with-current-buffer "foonet" erc--session)))

    (ert-info ("Evolve session to prevent ambiguity")
      (should-not (erc-networks--update-server-session))
      (should (= (erc--sid-dynamic-len erc--session) 2))
      (should (eq (erc--sid-symbol erc--session) 'foonet/bob)))

    (ert-info ("Collision renamed")
      (with-current-buffer "foonet/alice"
        (should (eq (erc--sid-symbol erc--session) 'foonet/alice)))

      (with-current-buffer "#chan@foonet/alice"
        (should (eq (erc--sid-symbol erc--session) 'foonet/alice)))))

  (erc-tests--clean-bufs))

(ert-deftest erc-networks--update-server-session--triple-new ()
  (with-temp-buffer
    (erc-mode)
    (setq erc--session
          (make-erc--sid-dynamic :parts [foonet "bob" home] :len 1))

    (with-current-buffer (get-buffer-create "foonet/bob/office")
      (erc-mode)
      (setq erc--session (make-erc--sid-dynamic :parts [foonet "bob" office]
                                                :len 3)))
    (with-current-buffer (get-buffer-create "#chan@foonet/bob/office")
      (erc-mode)
      (setq erc--session (with-current-buffer "foonet/bob/office"
                           erc--session)))

    (ert-info ("Extend our session name so that it's unique")
      (should-not (erc-networks--update-server-session))
      (should (= (erc--sid-dynamic-len erc--session) 3))))

  (erc-tests--clean-bufs))

;;; erc-networks-tests.el ends here

debug log:

solving 08781d2eb0 ...
found 08781d2eb0 in https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/

applying [1/1] https://yhetil.org/emacs-bugs/874k8i4tb6.fsf@neverwas.me/
diff --git a/test/lisp/erc/erc-networks-tests.el b/test/lisp/erc/erc-networks-tests.el
new file mode 100644
index 0000000000..08781d2eb0

Checking patch test/lisp/erc/erc-networks-tests.el...
Applied patch test/lisp/erc/erc-networks-tests.el cleanly.

index at:
100644 08781d2eb041f93fa2169a952beb97baa3776d9f	test/lisp/erc/erc-networks-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).