summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorF. Jason Park <jp@neverwas.me>2025-10-19 05:29:18 -0700
committerF. Jason Park <jp@neverwas.me>2026-04-30 15:46:42 -0700
commitcf9728c4be8f50c4a176b78cee6e23e1e485788a (patch)
tree708e2ecf7a3b4d9585a915335affcd7c1defb174 /test
parentba2a150740691146454955055067d3285edc52d5 (diff)
Only perform erc-log-insert-log-on-open setup once
* etc/ERC-NEWS: Add entry for option `erc-log-insert-log-on-open'. Mention deprecation of `erc-log-setup-logging'. * lisp/erc/erc-log.el (erc-log-insert-log-on-open): Expand type from boolean to choice of boolean, predicate, and new function item `erc-log-new-target-buffer-p'. (erc-log-mode, erc-log-enable, erc-log-disable): Replace `erc-log-setup-logging' on `erc-connect-pre-hook' with `erc-log--insert-log-on-open' at depth 80. Replace calls to `erc-log-setup-logging' with ones to `erc-log--setup'. (erc-log-new-target-buffer-p): New function. While the name could perhaps do more to indicate that it's only useful when called from `erc-open', that's the only place ERC typically sets up new target buffers. (erc-log-setup-logging): Deprecate and replace body with adapter that calls `erc-log--setup' and `erc-log--insert-log-on-open'. (erc-log--setup): New function whose body is mostly from the "nondestructive" portion of `erc-log-setup-logging'. Instead of moving the `erc-saved-last-position' marker to the old value, expect it not to have been initialized by `erc-initialize-log-marker' because `erc-connect-pre-hook' now runs after this code, which is now incorporated more judiciously into the module's "enable body". (erc-log--insert-log-on-open): New function whose body is mostly from the "destructive" portion of the previous incarnation of `erc-log-setup-logging'. Unlike the original, if option `erc-log-insert-log-on-open' is a function, call it to decide whether to insert a log file atop its buffer. Also, don't advance the marker `erc-last-saved-position' to the prompt area, where it can get stuck and prevent logs from being saved when `erc-log-insert-log-on-open' is non-nil. Thanks to Libera user Lionyx for reporting the bug, which has been around since at least ERC 5.4 and Emacs 28.1. (erc-log-disable-logging): Remove `erc-save-buffer-in-logs' from `write-file-functions' to undo its buffer-local addition in what is now `erc--log-setup'. * test/lisp/erc/erc-scenarios-log-options.el: New file. (Bug#79665)
Diffstat (limited to 'test')
-rw-r--r--test/lisp/erc/erc-scenarios-log-options.el214
1 files changed, 214 insertions, 0 deletions
diff --git a/test/lisp/erc/erc-scenarios-log-options.el b/test/lisp/erc/erc-scenarios-log-options.el
new file mode 100644
index 00000000000..c1fe946e844
--- /dev/null
+++ b/test/lisp/erc/erc-scenarios-log-options.el
@@ -0,0 +1,214 @@
+;;; erc-scenarios-log-options.el --- erc-log options scenarios -*- lexical-binding: t -*-
+
+;; Copyright (C) 2026 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/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'ert-x)
+(eval-and-compile
+ (let ((load-path (cons (ert-resource-directory) load-path)))
+ (require 'erc-scenarios-common)))
+
+(require 'erc-log)
+
+;; This checks that logs are only inserted in new buffers.
+(defun erc-scenarios-log-options--insert-on-open (test-fn)
+
+ (erc-scenarios-common-with-cleanup
+ ((erc-scenarios-common-dialog "join/reconnect")
+ (dumb-server (erc-d-run "localhost" t 'foonet 'foonet-again))
+ (tempdir (make-temp-file "erc-tests-log." t nil nil))
+ (erc-log-channels-directory tempdir)
+ (erc-modules `(log ,@erc-modules))
+ (erc-timestamp-format-left "\n[@@DATE__STAMP@@]\n")
+ (port (process-contact dumb-server :service))
+ (erc-server-auto-reconnect t)
+ (erc-server-flood-penalty 0.1)
+ (expect (erc-d-t-make-expecter))
+ ;; Bind these so they'll be killed on teardown.
+ (server-log-buffer (get-buffer-create "*erc-log FooNet*"))
+ (chan-log-buffer (get-buffer-create "*erc-log #chan*"))
+ (uh-suffix (format "!tester@127.0.0.1:%d.txt" port)))
+
+ (let ((file (concat "127.0.0.1:" (number-to-string port) uh-suffix)))
+ (with-temp-file (expand-file-name file tempdir)
+ (insert "\n@@OLD__BEG@@\n" "file: " file "\n@@OLD__END@@\n\n")))
+
+ (with-temp-file (expand-file-name (concat "foonet" uh-suffix) tempdir)
+ (insert "\n@@OLD__BEG@@\n"
+ "file: " (concat "foonet" uh-suffix)
+ "\n@@OLD__END@@\n\n"))
+
+ (with-temp-file (expand-file-name (concat "#chan" uh-suffix) tempdir)
+ (insert "\n@@OLD__BEG@@\n"
+ "file: " (concat "#chan" uh-suffix)
+ "\n@@OLD__END@@\n\n"))
+
+ (ert-info ("Connect")
+ (with-current-buffer (erc :server "127.0.0.1"
+ :port port
+ :nick "tester"
+ :password "changeme"
+ :full-name "tester")
+ (funcall expect 10 "debug mode")))
+
+ (ert-info ("#chan populated")
+ (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
+ (funcall expect 10 "@@DATE__STAMP@@")
+ (funcall expect 10 "<alice> tester, welcome")))
+
+ (ert-info ("Reconnect")
+ (with-current-buffer "FooNet"
+ (funcall expect 10 "Connection failed!")
+ (funcall expect 10 "Reconnecting")
+ (funcall expect 10 "Welcome")
+ (funcall expect 10 "debug mode")))
+
+ (with-current-buffer "#chan"
+ (funcall expect -0.01 "@@DATE__STAMP@@")
+ (funcall expect 10 "<alice> bob: Well, this"))
+
+ (with-current-buffer "FooNet"
+ (erc-scenarios-common-say "/quit")
+ (funcall expect 10 "ERROR"))
+
+ ;; Ensure no redundant logging.
+ (with-current-buffer "FooNet"
+ (let ((file (erc-current-logfile (current-buffer))))
+ (with-current-buffer server-log-buffer
+ (insert-file-contents file)
+ (funcall expect 1 "@@DATE__STAMP@@")
+ (funcall expect -0.01 "@@DATE__STAMP@@")
+ ;; Full output again on reconnect.
+ (funcall expect 1 "*** Welcome to the foonet")
+ (funcall expect 1 "debug mode")
+ (funcall expect 1 "*** Connection failed!")
+ (funcall expect 1 "*** Welcome to the foonet")
+ (funcall expect 1 "debug mode"))))
+
+ (with-current-buffer "#chan"
+ (let ((file (erc-current-logfile (current-buffer))))
+ (with-current-buffer chan-log-buffer
+ (insert-file-contents file)
+ (funcall expect 1 "@@OLD__BEG@@")
+ (funcall expect -0.01 "@@OLD__BEG@@") ; only appearance
+ (funcall expect 1 "file: #chan")
+ (funcall expect 1 "@@OLD__END@@")
+ (funcall expect 1 "@@DATE__STAMP@@")
+ (funcall expect -0.01 "@@DATE__STAMP@@")
+ ;; Full output again on reconnect.
+ (funcall expect 1 "*** You have joined channel #chan")
+ (funcall expect 1 "<alice> tester, welcome!")
+ (funcall expect -0.01 "@@DATE__STAMP@@")
+ (funcall expect 1 "*** You have joined channel #chan")
+ (funcall expect 1 "<alice> bob: Well, this is the forest"))))
+
+ (funcall test-fn expect)
+ (erc-log-mode -1)
+
+ (if noninteractive
+ (delete-directory tempdir :recursive)
+ (add-hook 'kill-emacs-hook
+ (lambda () (delete-directory tempdir :recursive))))))
+
+;; This shows that the legacy default value of t ends up inserting
+;; existing log content on reconnect as well, leading to redundant
+;; insertions.
+(ert-deftest erc-scenarios-log-options--insert-on-open/default ()
+ (let ((erc-log-insert-log-on-open t))
+ (erc-scenarios-log-options--insert-on-open
+ (lambda (expect)
+
+ (with-current-buffer "*erc-log FooNet*"
+ (funcall expect 1 "@@OLD__BEG@@" (point-min))
+ (funcall expect -0.01 "@@OLD__BEG@@") ; not repeated
+ (funcall expect 1 "file: foonet")
+ (funcall expect 1 "@@OLD__END@@")
+ (funcall expect 1 "@@DATE__STAMP@@"))
+
+ ;; For server buffers, the file name changes because the buffer
+ ;; is renamed after the network is announced during the initial
+ ;; session.
+ (with-current-buffer "FooNet"
+
+ (funcall expect 1 "@@OLD__BEG@@" (point-min))
+ ;; Existing log contents inserted once per connection (most
+ ;; recent first).
+ (funcall expect 1 "file: foonet")
+ (funcall expect 1 "@@OLD__END@@")
+
+ ;; Insertion from initial connection last.
+ (funcall expect 1 "@@OLD__BEG@@")
+ (funcall expect 1 "file: 127.0.0.1")
+ (funcall expect -0.01 "@@OLD__BEG@@")
+ (funcall expect 1 "@@OLD__END@@")
+
+ (funcall expect 1 "@@DATE__STAMP@@"))
+
+ (ert-info ("Repeated in #chan")
+ (with-current-buffer "#chan"
+ (funcall expect 1 "@@OLD__BEG@@" (point-min))
+ (funcall expect 1 "file: #chan")
+ (funcall expect 1 "@@OLD__END@@")
+
+ ;; Existing log is indeed repeated in full once per connection.
+ (funcall expect 1 "@@OLD__BEG@@")
+ (funcall expect -0.01 "@@OLD__BEG@@")
+ (funcall expect 1 "file: #chan")
+ (funcall expect 1 "@@OLD__END@@")
+
+ (funcall expect 1 "@@DATE__STAMP@@")))))))
+
+(ert-deftest erc-scenarios-log-options--insert-on-open/target-p ()
+ (let ((erc-log-insert-log-on-open #'erc-log-new-target-buffer-p))
+ (erc-scenarios-log-options--insert-on-open
+ (lambda (expect)
+
+ (ert-info ("Absent from server buffer")
+ (with-current-buffer "FooNet"
+ ;; No insertions in the server buffer.
+ (funcall expect -0.01 "@@OLD__BEG@@" (point-min))
+ (funcall expect 1 "@@DATE__STAMP@@")))
+
+ (ert-info ("Once in server log")
+ ;; No redundancies in the server's log file, though previously
+ ;; existing content is obviously present.
+ (with-current-buffer "*erc-log FooNet*"
+ (funcall expect 1 "@@OLD__BEG@@" (point-min))
+ (funcall expect -0.01 "@@OLD__BEG@@") ; no repeats
+ (funcall expect 1 "file: foonet")
+ (funcall expect 1 "@@OLD__END@@")
+ (funcall expect 1 "@@DATE__STAMP@@")))
+
+ ;; Also, as asserted in the fixture body, the associated log file
+ ;; for #chan has no redundancy.
+ (ert-info ("Not repeated in #chan")
+ (with-current-buffer "#chan"
+ (funcall expect 1 "@@OLD__BEG@@" (point-min))
+ (funcall expect 1 "file: #chan")
+ (funcall expect 1 "@@OLD__END@@")
+
+ ;; Existing scrollback only inserted at most once per buffer.
+ (funcall expect -0.01 "@@OLD__BEG@@")
+ (funcall expect 1 "@@DATE__STAMP@@")))))))
+
+(require 'erc-log)
+
+;;; erc-scenarios-log-options.el ends here