summaryrefslogtreecommitdiff
path: root/src/mailman/commands/tests/test_shell.py
blob: 12e78c1ab238620214cec38ca42cf5d3d0d6857c (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
# 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 withlist/shell command."""

import os
import unittest

from mailman.commands.cli_withlist import Withlist
from mailman.config import config
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import configuration
from mailman.testing.layers import ConfigLayer
from unittest.mock import patch

try:
    import readline                                 # noqa
    has_readline = True
except ImportError:
    has_readline = False


class FakeArgs:
    interactive = None
    run = None
    details = False
    listname = None


class TestShell(unittest.TestCase):
    layer = ConfigLayer

    def setUp(self):
        self._shell = Withlist()

    def test_namespace(self):
        args = FakeArgs()
        args.interactive = True
        with patch.object(self._shell, '_start_python') as mock:
            self._shell.process(args)
        self.assertEqual(mock.call_count, 1)
        # Don't test that all names are available, just a few choice ones.
        positional, keywords = mock.call_args
        namespace = positional[0]
        self.assertIn('getUtility', namespace)
        self.assertIn('IArchiver', namespace)
        self.assertEqual(namespace['IUserManager'], IUserManager)

    @configuration('shell', banner='my banner')
    def test_banner(self):
        args = FakeArgs()
        args.interactive = True
        with patch('mailman.commands.cli_withlist.interact') as mock:
            self._shell.process(args)
        self.assertEqual(mock.call_count, 1)
        positional, keywords = mock.call_args
        self.assertEqual(keywords['banner'], 'my banner\n')

    @unittest.skipUnless(has_readline, 'readline module is not available')
    @configuration('shell', history_file='$var_dir/history.py')
    def test_history_file(self):
        args = FakeArgs()
        args.interactive = True
        with patch('mailman.commands.cli_withlist.interact'):
            self._shell.process(args)
        history_file = os.path.join(config.VAR_DIR, 'history.py')
        self.assertTrue(os.path.exists(history_file))