summaryrefslogtreecommitdiff
path: root/src/mailman/testing/documentation.py
blob: ee37aa1ed8e741aab1334d4b2b9ecc45fe7e922f (plain)
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
# Copyright (C) 2007-2017 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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 Mailman 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 Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Harness for testing Mailman's documentation.

Note that doctest extraction does not currently work for zip file
distributions.  doctest discovery currently requires file system traversal.
"""

import os
import sys

from click.testing import CliRunner
from contextlib import ExitStack
from importlib import import_module
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.testing.helpers import (
    call_api, get_queue_messages, specialized_message_from_string, subscribe)
from mailman.testing.layers import SMTPLayer
from public import public
from subprocess import PIPE, STDOUT, run


DOT = '.'
COMMASPACE = ', '


def stop():
    """Call into pdb.set_trace()"""
    # Do the import here so that you get the wacky special hacked pdb instead
    # of Python's normal pdb.
    import pdb
    pdb.set_trace()


def dump_msgdata(msgdata, *additional_skips):
    """Dump in a more readable way a message metadata dictionary."""
    if len(msgdata) == 0:
        print('*Empty*')
        return
    skips = set(additional_skips)
    # Some stuff we always want to skip, because their values will always be
    # variable data.
    skips.add('received_time')
    longest = max(len(key) for key in msgdata if key not in skips)
    for key in sorted(msgdata):
        if key in skips:
            continue
        print('{0:{2}}: {1}'.format(key, msgdata[key], longest))


def dump_list(list_of_things, key=str):
    """Print items in a string to get rid of stupid u'' prefixes."""
    # List of things may be a generator.
    list_of_things = list(list_of_things)
    if len(list_of_things) == 0:
        print('*Empty*')
    if key is not None:
        list_of_things = sorted(list_of_things, key=key)
    for item in list_of_things:
        print(item)


def call_http(url, data=None, method=None, username=None, password=None):
    """'Call a URL with a given HTTP method and return the resulting object.

    The object will have been JSON decoded.

    :param url: The url to open, read, and print.
    :type url: string
    :param data: Data to use to POST to a URL.
    :type data: dict
    :param method: Alternative HTTP method to use.
    :type method: str
    :param username: The HTTP Basic Auth user name.  None means use the value
        from the configuration.
    :type username: str
    :param password: The HTTP Basic Auth password.  None means use the value
        from the configuration.
    :type username: str
    :return: The decoded JSON data structure.
    :raises HTTPError: when a non-2xx return code is received.
    """
    content, response = call_api(url, data, method, username, password)
    if content is None:
        # We used to use httplib2 here, which included the status code in the
        # response headers in the `status` key.  requests doesn't do this, but
        # the doctests expect it so for backward compatibility, include the
        # status code in the printed response.
        headers = dict(status=response.status_code)
        headers.update({
            field.lower(): response.headers[field]
            for field in response.headers
            })
        for field in sorted(headers):
            print('{}: {}'.format(field, headers[field]))
        return None
    return content


def _print_dict(data, depth=0):
    for item, value in sorted(data.items()):
        if isinstance(value, dict):
            _print_dict(value, depth+1)
        print('    ' * depth + '{}: {}'.format(item, value))


def dump_json(url, data=None, method=None, username=None, password=None):
    """Print the JSON dictionary read from a URL.

    :param url: The url to open, read, and print.
    :type url: string
    :param data: Data to use to POST to a URL.
    :type data: dict
    :param method: Alternative HTTP method to use.
    :type method: str
    :param username: The HTTP Basic Auth user name.  None means use the value
        from the configuration.
    :type username: str
    :param password: The HTTP Basic Auth password.  None means use the value
        from the configuration.
    :type username: str
    """
    results = call_http(url, data, method, username, password)
    if results is None:
        return
    for key in sorted(results):
        value = results[key]
        if key == 'entries':
            for i, entry in enumerate(value):
                # entry is a dictionary.
                print('entry %d:' % i)
                for entry_key in sorted(entry):
                    print('    {}: {}'.format(entry_key, entry[entry_key]))
        elif isinstance(value, list):
            printable_value = COMMASPACE.join(
                "'{}'".format(s) for s in sorted(value))
            print('{}: [{}]'.format(key, printable_value))
        elif isinstance(value, dict):
            print('{}:'.format(key))
            _print_dict(value, 1)
        else:
            print('{}: {}'.format(key, value))


@public
def cli(command_path):
    # Use this to invoke click commands in doctests.  This returns a partial
    # that accepts a sequence of command line options, invokes the click
    # command, and returns the results (unless the keyword argument 'quiet')
    # is True.
    package_path, dot, name = command_path.rpartition('.')
    command = getattr(import_module(package_path), name)
    def inner(command_string, quiet=False, input=None):           # noqa: E306
        args = command_string.split()
        assert args[0] == 'mailman', args
        assert args[1] == command.name, args
        # The first two will be `mailman <command>`.  That's just for
        # documentation purposes, and aren't useful for the test.
        result = CliRunner().invoke(command, args[2:], input=input)
        if not quiet:
            # Print the output, with any trailing newlines stripped, unless
            # the quiet flag is set.  The extra newlines just make the
            # doctests uglier and usually all we care about is the stdout
            # text.
            print(result.output.rstrip('\n'))
    return inner


@public
def run_mailman(args, **overrides):
    exe = os.path.join(os.path.dirname(sys.executable), 'mailman')
    env = os.environ.copy()
    env.update(overrides)
    run_args = [exe]
    run_args.extend(args)
    proc = run(
        run_args, env=env, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
    return proc


@public
def setup(testobj):
    """Test setup."""
    # In general, I don't like adding convenience functions, since I think
    # doctests should do the imports themselves.  It makes for better
    # documentation that way.  However, a few are really useful, or help to
    # hide some icky test implementation details.
    testobj.globs['call_http'] = call_http
    testobj.globs['cli'] = cli
    testobj.globs['config'] = config
    testobj.globs['create_list'] = create_list
    testobj.globs['dump_json'] = dump_json
    testobj.globs['dump_list'] = dump_list
    testobj.globs['dump_msgdata'] = dump_msgdata
    testobj.globs['get_queue_messages'] = get_queue_messages
    testobj.globs['message_from_string'] = specialized_message_from_string
    testobj.globs['run'] = run_mailman
    testobj.globs['smtpd'] = SMTPLayer.smtpd
    testobj.globs['stop'] = stop
    testobj.globs['subscribe'] = subscribe
    testobj.globs['transaction'] = config.db
    # Add this so that cleanups can be automatically added by the doctest.
    testobj.globs['cleanups'] = ExitStack()


@public
def teardown(testobj):
    testobj.globs['cleanups'].close()