unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#31336: 27.0.50; [RFC] Gnus mock testing sessions
@ 2018-05-01 21:56 Eric Abrahamsen
  2018-05-09 18:29 ` bug#31336: " Eric Abrahamsen
  2018-10-09 17:46 ` Eric Abrahamsen
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Abrahamsen @ 2018-05-01 21:56 UTC (permalink / raw)
  To: 31336

[-- Attachment #1: Type: text/plain, Size: 2369 bytes --]

Something I've wanted for a while is a fake Gnus session that I can use
for testing, since it's very hard to get Gnus up and running
meaningfully without substantial customization.

So I made the attached "gnus-mock.el" library. You start "emacs -Q", run
`gnus-mock', and get a repeatable trashable Gnus session you can use for
testing. What I did was put a basic Gnus installation under
./test/data/gnus/mock, and every time you start a mock session that
installation gets copied to a temporary directory, and Gnus operates out
of that directory for the duration of the session. You can abuse it
however you like, and then when you quit it gets cleaned up. It has some
hard-coded config (at present, just one nnmaildir server), and you have
the option of laying your own config over that.

It seems to work just fine. I can imagine all kinds of things we could
do with this, but I wanted to float this here before I went any further,
to get some feedback. Some things:

1. Ideally we'd have a bunch of messages in there, which can serve as
   sort of regression tests, and may also be useful for unit tests as
   well. Is there any concern about the size of data? My understanding
   is that this data will be discarded in an actual install, so maybe it
   doesn't matter so much?
2. I'm currently using `source-directory' to find the data dir. I guess,
   if we're expecting this only to be used for development in the source
   tree, then that's okay.
3. Locally, I wrote a python one-liner as a dummy `sendmail-program',
   that just returns a 0 exit code. A python script isn't really
   practical, and also I haven't done anything for the other mail
   sending protocols. What I'd really like is a little executable that
   accepts the message, and then sticks it in yet another directory
   within the temp installation. Then we can point `mail-sources' to
   that, and have a little boomerang where all sent messages come back
   to you.
4. It's not possible to pre-configure directory-based servers in
   `gnus-server-alist', as the directory paths need to be absolute, and
   at present they change every time `gnus-mock' is started. Not a
   tragedy, though.

I've attached the gnus-mock code, and the dummy .gnus.el config file --
later, once there's some feedback on this, I can push a branch that
contains the whole data dir.

Hope this is welcome,
Eric


[-- Attachment #2: gnus-mock.el --]
[-- Type: text/plain, Size: 4766 bytes --]

;;; gnus-mock.el --- Mock Gnus installation for testing  -*- lexical-binding: t; -*-

;; Copyright (C) 2018  Free Software Foundation

;; Author: Eric Abrahamsen <eric@ericabrahamsen.net>

;; 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 module provides a reproducible mock Gnus installation,
;; including dummy data, that can be used for Gnus development and
;; testing.  Start the mock Gnus session with either `gnus-mock' or
;; `gnus-mock-unplugged'.  Nothing in the mock sessions will make a
;; network connection, and mail sending is stubbed out using
;; `gnus-mock-sendmail-program'.

;; The mock session starts with a predefined nnmaildir server, as well
;; as some dummy mail data.  Other predefined servers may be added in
;; the future.  At startup, all dummy data is copied into a temporary
;; directory.  At shutdown, the temporary directory is deleted.

;; A special config file is used for the mock session; users may add
;; to this config, or shadow its options, by setting
;; `gnus-mock-settings-file' to the name of an additional config file.

;;; Code:

(require 'gnus)
(require 'message)

(defgroup gnus-mock nil
  "Options for the mock Gnus installation."
  :group 'gnus
  :version "27.1")

(defcustom gnus-mock-settings-file nil
  "Path to an additional config file for mock Gnus.
The contents of this file will be appended to gnus-mock's own
config file before Gnus startup, in effect shadowing config
values in the default file."
  :group 'gnus-mock
  :type 'file)

(defcustom gnus-mock-data-dir
  (expand-file-name "test/data/gnus/mock" source-directory)
  "Source directory for Gnus mock data."
  :group 'gnus-mock
  :type 'string)

(defcustom gnus-mock-cleanup-p t
  "When non-nil, delete temporary files after shutdown.
Each Gnus mock session will create a new temporary directory, so
multiple sessions will not conflict if this option is nil."
  :group 'gnus-mock
  :type 'boolean)

(defcustom gnus-mock-sendmail-program
  (nnheader-concat gnus-mock-data-dir "fakesendmail.py")
  "Program used as the value of `sendmail-program'."
  :group 'gnus-mock
  :type 'string)

;; Regular Gnus can also check this, in case of a "dirty" mock
;; session.
(defvar gnus-mock-p nil
  "Non-nil during a mocked Gnus session.")

(defun gnus-mock-setup ()
  "Set up mock data and config."
  (let ((mock-tmp-dir (make-temp-file "emacs-gnus-mock-" t)))
    (setq gnus-home-directory mock-tmp-dir
	  gnus-directory (nnheader-concat gnus-home-directory "News")
	  gnus-agent-directory (nnheader-concat gnus-directory "agent/")
	  nndraft-directory (nnheader-concat gnus-directory "drafts/")
	  gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
	  gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
	  message-directory gnus-home-directory
	  sendmail-program gnus-mock-sendmail-program
	  ;; If Emacs was started with -Q, as it should have been,
	  ;; `init-file-user' will be nil which will prevent reading of
	  ;; `gnus-init-file'.
	  init-file-user "gnus-mock")
    ;; Put our data and config in place.
    (copy-directory
     gnus-mock-data-dir
     (file-name-as-directory gnus-home-directory) nil nil t)
    ;; Possibly insert additional config.
    (when gnus-mock-settings-file
      (with-temp-buffer
	(insert-file-contents gnus-mock-settings-file)
	(append-to-file
	 (point-min) (point-max) gnus-init-file)))))

;;;###autoload
(defun gnus-mock-unplugged ()
  "Start an unplugged mock Gnus session."
  (interactive)
  (gnus-mock t))

;;;###autoload
(defun gnus-mock (&optional unplugged)
  "Start a mock Gnus session."
  (interactive)
  (when (gnus-alive-p)
    (error "First save and quit your running Gnus session"))
  (gnus-mock-setup)
  (when unplugged
    (setq gnus-plugged nil))
  (setq gnus-mock-p t)
  (gnus)
  (add-hook 'gnus-after-exiting-gnus-hook #'gnus-mock-restore))

(defun gnus-mock-restore ()
  "Restore after quitting a mock Gnus session."
  (when gnus-mock-p
    (when gnus-mock-cleanup-p
      (delete-directory gnus-home-directory t))
    (setq gnus-mock-p nil)
    (remove-hook 'gnus-after-exiting-gnus-hook #'gnus-mock-restore)))

(provide 'gnus-mock)
;;; gnus-mock.el ends here

[-- Attachment #3: .gnus.el --]
[-- Type: text/plain, Size: 468 bytes --]

;;; Settings for Gnus mock sessions.  Do not edit this file, instead
;;; add further customizations to the file indicated by
;;; `gnus-mock-settings-file'.

(setq gnus-select-method '(nnmaildir
                           "Test"
                           (directory (expand-file-name "test" gnus-home-directory))))

(setq user-mail-address "mockturtle@gnus.org")
(setq user-full-name "Mock Turtle")

(setq message-send-mail-function #'message-send-mail-with-sendmail)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#31336: [RFC] Gnus mock testing sessions
  2018-05-01 21:56 bug#31336: 27.0.50; [RFC] Gnus mock testing sessions Eric Abrahamsen
@ 2018-05-09 18:29 ` Eric Abrahamsen
  2018-10-09 17:46 ` Eric Abrahamsen
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Abrahamsen @ 2018-05-09 18:29 UTC (permalink / raw)
  To: 31336

Hmm, given the crickets here, maybe this would be better off as an ELPA
package?





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#31336: [RFC] Gnus mock testing sessions
  2018-05-01 21:56 bug#31336: 27.0.50; [RFC] Gnus mock testing sessions Eric Abrahamsen
  2018-05-09 18:29 ` bug#31336: " Eric Abrahamsen
@ 2018-10-09 17:46 ` Eric Abrahamsen
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Abrahamsen @ 2018-10-09 17:46 UTC (permalink / raw)
  To: 31336-done

I'm just going to propose this as an Elpa package.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-10-09 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-01 21:56 bug#31336: 27.0.50; [RFC] Gnus mock testing sessions Eric Abrahamsen
2018-05-09 18:29 ` bug#31336: " Eric Abrahamsen
2018-10-09 17:46 ` Eric Abrahamsen

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).