Fix test_interact_default_banner for Python 3.10 compatibility.
[mailman.git] / src / mailman / utilities / tests / test_interact.py
blob113504dbd163f9140cacb9d78c444a07eadf0f0e
1 # Copyright (C) 2016-2022 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <https://www.gnu.org/licenses/>.
18 """Test the interact utility."""
21 import sys
22 import unittest
24 from contextlib import ExitStack
25 from io import StringIO
26 from mailman.app.lifecycle import create_list
27 from mailman.testing.helpers import hackenv
28 from mailman.testing.layers import ConfigLayer
29 from mailman.utilities.interact import interact
30 from tempfile import NamedTemporaryFile
31 from unittest.mock import patch
34 class TestInteract(unittest.TestCase):
35 layer = ConfigLayer
37 def setUp(self):
38 resources = ExitStack()
39 self.addCleanup(resources.close)
40 self._enter = resources.enter_context
41 self._enter(patch('builtins.input', side_effect=EOFError))
42 self._stderr = StringIO()
43 self._enter(patch('sys.stderr', self._stderr))
45 def test_interact(self):
46 mlist = create_list('ant@example.com')
47 results = []
48 fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
49 self._enter(hackenv('PYTHONSTARTUP', fp.name))
50 print('results.append(mlist.list_id)', file=fp)
51 fp.flush()
52 interact()
53 self.assertEqual(results, [mlist.list_id])
55 def test_interact_overrides(self):
56 create_list('ant@example.com')
57 bee = create_list('bee@example.com')
58 results = []
59 fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
60 self._enter(hackenv('PYTHONSTARTUP', fp.name))
61 print('results.append(mlist.list_id)', file=fp)
62 fp.flush()
63 interact(overrides=dict(mlist=bee))
64 self.assertEqual(results, [bee.list_id])
66 def test_interact_default_banner(self):
67 self._enter(hackenv('PYTHONSTARTUP', None))
68 interact()
69 stderr = self._stderr.getvalue().splitlines()
70 banner = 'Python {} on {}'.format(sys.version, sys.platform)
71 self.assertEqual(stderr[0], banner.splitlines()[0])
73 def test_interact_custom_banner(self):
74 self._enter(hackenv('PYTHONSTARTUP', None))
75 interact(banner='Welcome')
76 stderr = self._stderr.getvalue().splitlines()
77 self.assertEqual(stderr[0], 'Welcome')
79 def test_interact_no_upframe(self):
80 upframed = False # noqa: F841
81 fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
82 self._enter(hackenv('PYTHONSTARTUP', fp.name))
83 print('print(upframed)', file=fp)
84 fp.flush()
85 interact(upframe=False, banner='')
86 lines = self._stderr.getvalue().splitlines()
87 self.assertIn("NameError: name 'upframed' is not defined", lines)
89 def test_interact_multiline(self):
90 # GL issue #224.
91 fp = self._enter(NamedTemporaryFile('w', encoding='utf-8'))
92 self._enter(hackenv('PYTHONSTARTUP', fp.name))
93 print('import sys', file=fp)
94 print("print('hello', file=sys.stderr)", file=fp)
95 print("print('world', file=sys.stderr)", file=fp)
96 fp.flush()
97 interact(banner='')
98 lines = self._stderr.getvalue()
99 self.assertEqual(lines, 'hello\nworld\n\n', lines)