1 # Copyright (C) 2016-2023 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)
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
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."""
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
):
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')
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
)
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')
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
)
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))
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
)
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
):
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
)
98 lines
= self
._stderr
.getvalue()
99 self
.assertEqual(lines
, 'hello\nworld\n\n', lines
)