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
| | ;;; erc-scenarios-common.el --- common helpers for ERC scenarios -*- lexical-binding: t -*-
;; Copyright (C) 2021 Free Software Foundation, Inc.
;;
;; This file is part of GNU Emacs.
;;
;; This program 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.
;;
;; This program 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 this program. If not, see
;; <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file should not contain any test cases.
(require 'ert-x) ; cl-lib
(eval-and-compile (let ((dir (getenv "EMACS_TEST_DIRECTORY")))
(when dir
(load (concat dir "/lisp/erc/erc-d/erc-d-t") nil t)
(load (concat dir "/lisp/erc/erc-d/erc-d") nil t))))
(require 'erc-d)
(require 'erc-d-t)
(require 'erc-backend)
(defvar erc-scenarios-common--resources-dir
(expand-file-name (concat (ert-resource-directory)
"../erc-scenarios-resources/")))
;; Because teardown is already inhibited when running interactively,
;; which prevents subsequent tests from succeeding, we might as well
;; treat inspection as the goal.
(unless noninteractive
(setq erc-server-auto-reconnect nil))
(defvar erc-scenarios-common-dialog nil)
(defvar erc-scenarios-common-extra-teardown nil)
(defun erc-scenarios-common--add-silence ()
(advice-add #'erc-login :around #'erc-d-t-silence-around)
(advice-add #'erc-handle-login :around #'erc-d-t-silence-around)
(advice-add #'erc-server-connect :around #'erc-d-t-silence-around))
(defun erc-scenarios-common--remove-silence ()
(advice-remove #'erc-login #'erc-d-t-silence-around)
(advice-remove #'erc-handle-login #'erc-d-t-silence-around)
(advice-remove #'erc-server-connect #'erc-d-t-silence-around))
(defun erc-scenarios-common--print-trace ()
(when (and (boundp 'trace-buffer) (get-buffer trace-buffer))
(with-current-buffer trace-buffer
(message "%S" (buffer-string))
(kill-buffer))))
(defun erc-scenarios-common--make-bindings (bindings)
`((erc-d-u-canned-dialog-dir (expand-file-name
(or erc-scenarios-common-dialog
(cadr (assq 'erc-scenarios-common-dialog
',bindings)))
erc-scenarios-common--resources-dir))
(erc-d-spec-vars `(,@erc-d-spec-vars
(quit . ,(erc-quit/part-reason-default))
(erc-version . ,erc-version)))
(erc-modules (copy-sequence erc-modules))
(auth-source-do-cache nil)
(erc-autojoin-channels-alist nil)
(erc-server-auto-reconnect nil)
,@bindings))
(defmacro erc-scenarios-common-with-cleanup (bindings &rest body)
"Provide boilerplate cleanup tasks after calling BODY with BINDINGS.
If an `erc-d' process exists, wait for it to start before running BODY.
If `erc-autojoin-mode' mode is bound, restore it during cleanup if
disabled by BODY. Other defaults common to these test cases are added
below and can be overridden, except when wanting the \"real\" default
value, which must be looked up or captured outside of the calling form.
Dialog resource directories are located by expanding the variable
`erc-scenarios-common-dialog' or its value in BINDINGS."
(declare (indent 1))
(let* ((orig-autojoin-mode (make-symbol "orig-autojoin-mode"))
(combind `((,orig-autojoin-mode (bound-and-true-p erc-autojoin-mode))
,@(erc-scenarios-common--make-bindings bindings))))
`(erc-d-t-with-cleanup (,@combind)
(ert-info ("Restore autojoin, etc., kill ERC buffers")
(dolist (buf (buffer-list))
(when-let ((erc-d-u--process-buffer)
(proc (get-buffer-process buf)))
(erc-d-t-wait-for 5 "Dumb server dies on its own"
(not (process-live-p proc)))))
(erc-scenarios-common--remove-silence)
(when erc-scenarios-common-extra-teardown
(ert-info ("Running extra teardown")
(funcall erc-scenarios-common-extra-teardown)))
(when (and (boundp 'erc-autojoin-mode)
(not (eq erc-autojoin-mode ,orig-autojoin-mode)))
(erc-autojoin-mode (if ,orig-autojoin-mode +1 -1)))
(when noninteractive
(erc-scenarios-common--print-trace)
(erc-d-t-kill-related-buffers)))
(erc-scenarios-common--add-silence)
(ert-info ("Wait for dumb server")
(dolist (buf (buffer-list))
(with-current-buffer buf
(when erc-d-u--process-buffer
(erc-d-t-search-for 3 "Starting")))))
(ert-info ("Activate erc-debug-irc-protocol")
(unless (and noninteractive (not erc-debug-irc-protocol))
(erc-toggle-debug-irc-protocol)))
,@body)))
(defun erc-scenarios-common-assert-initial-buf-name (id port)
;; Assert no limbo period when explicit ID given
(should (string= (if id
(symbol-name id)
(format "127.0.0.1:%d" port))
(buffer-name))))
(defun erc-scenarios-common-buflist (prefix)
"Return list of buffers with names sharing PREFIX."
(let (case-fold-search)
(erc-networks--id-sort-buffers
(delq nil
(mapcar (lambda (b)
(when (string-prefix-p prefix (buffer-name b)) b))
(buffer-list))))))
(provide 'erc-scenarios-common)
;;; erc-scenarios-common.el ends here
|