summaryrefslogtreecommitdiff
path: root/src/mailman/utilities/tests/test_interact.py
blob: d3803ab2838b0c70787c658504579bc1bc64045f (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
# Copyright (C) 2016 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/>.

"""Test the interact utility."""


import sys
import unittest

from contextlib import ExitStack
from io import StringIO
from mailman.app.lifecycle import create_list
from mailman.testing.helpers import hackenv
from mailman.testing.layers import ConfigLayer
from mailman.utilities.interact import interact
from tempfile import NamedTemporaryFile
from unittest.mock import patch


class TestInteract(unittest.TestCase):
    layer = ConfigLayer

    def setUp(self):
        resources = ExitStack()
        self.addCleanup(resources.close)
        self._enter = resources.enter_context
        self._enter(patch('builtins.input', side_effect=EOFError))
        self._stderr = StringIO()
        self._enter(patch('sys.stderr', self._stderr))

    def test_interact(self):
        mlist = create_list('ant@example.com')
        results = []
        fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
        self._enter(hackenv('PYTHONSTARTUP', fp.name))
        print('results.append(mlist.list_id)', file=fp)
        fp.flush()
        interact()
        self.assertEqual(results, [mlist.list_id])

    def test_interact_overrides(self):
        create_list('ant@example.com')
        bee = create_list('bee@example.com')
        results = []
        fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
        self._enter(hackenv('PYTHONSTARTUP', fp.name))
        print('results.append(mlist.list_id)', file=fp)
        fp.flush()
        interact(overrides=dict(mlist=bee))
        self.assertEqual(results, [bee.list_id])

    def test_interact_default_banner(self):
        self._enter(hackenv('PYTHONSTARTUP', None))
        interact()
        stderr = self._stderr.getvalue().splitlines()
        banner = 'Python {} on {} '.format(sys.version, sys.platform)
        self.assertEqual(stderr[0], banner.splitlines()[0])

    def test_interact_custom_banner(self):
        self._enter(hackenv('PYTHONSTARTUP', None))
        interact(banner='Welcome')
        stderr = self._stderr.getvalue().splitlines()
        self.assertEqual(stderr[0], 'Welcome')

    def test_interact_no_upframe(self):
        upframed = False                            # noqa
        fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
        self._enter(hackenv('PYTHONSTARTUP', fp.name))
        print('print(upframed)', file=fp)
        fp.flush()
        interact(upframe=False, banner='')
        lines = self._stderr.getvalue().splitlines()
        self.assertIn("NameError: name 'upframed' is not defined", lines)

    def test_interact_multiline(self):
        # GL issue #224.
        fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
        self._enter(hackenv('PYTHONSTARTUP', fp.name))
        print('import sys', file=fp)
        print("print('hello', file=sys.stderr)", file=fp)
        print("print('world', file=sys.stderr)", file=fp)
        fp.flush()
        interact(banner='')
        lines = self._stderr.getvalue()
        self.assertEqual(lines, 'hello\nworld\n\n', lines)