Core no longer depends on the standalone `mock` module. (Closes: #146)
[mailman.git] / src / mailman / commands / tests / test_conf.py
blob7412b5decac636539efca6b04fd1a70b70aae885
1 # Copyright (C) 2013-2015 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 <http://www.gnu.org/licenses/>.
18 """Test the conf subcommand."""
20 __all__ = [
21 'TestConf',
25 import os
26 import sys
27 import tempfile
28 import unittest
30 from io import StringIO
31 from mailman.commands.cli_conf import Conf
32 from mailman.testing.layers import ConfigLayer
33 from unittest import mock
37 class FakeArgs:
38 section = None
39 key = None
40 output = None
41 sort = False
44 class FakeParser:
45 def __init__(self):
46 self.message = None
48 def error(self, message):
49 self.message = message
50 sys.exit(1)
54 class TestConf(unittest.TestCase):
55 """Test the conf subcommand."""
57 layer = ConfigLayer
59 def setUp(self):
60 self.command = Conf()
61 self.command.parser = FakeParser()
62 self.args = FakeArgs()
64 def test_cannot_access_nonexistent_section(self):
65 self.args.section = 'thissectiondoesnotexist'
66 self.args.key = None
67 with self.assertRaises(SystemExit):
68 self.command.process(self.args)
69 self.assertEqual(self.command.parser.message,
70 'No such section: thissectiondoesnotexist')
72 def test_cannot_access_nonexistent_key(self):
73 self.args.section = "mailman"
74 self.args.key = 'thiskeydoesnotexist'
75 with self.assertRaises(SystemExit):
76 self.command.process(self.args)
77 self.assertEqual(self.command.parser.message,
78 'Section mailman: No such key: thiskeydoesnotexist')
80 def test_output_to_explicit_stdout(self):
81 self.args.output = '-'
82 self.args.section = 'shell'
83 self.args.key = 'use_ipython'
84 with mock.patch('sys.stdout') as mock_object:
85 self.command.process(self.args)
86 mock_object.write.assert_has_calls(
87 [mock.call('no'), mock.call('\n')])
89 def test_output_to_file(self):
90 self.args.section = 'shell'
91 self.args.key = 'use_ipython'
92 fd, filename = tempfile.mkstemp()
93 try:
94 self.args.output = filename
95 self.command.process(self.args)
96 with open(filename, 'r') as fp:
97 contents = fp.read()
98 finally:
99 os.remove(filename)
100 self.assertEqual(contents, 'no\n')
102 def test_sort_by_section(self):
103 self.args.output = '-'
104 self.args.sort = True
105 output = StringIO()
106 with mock.patch('sys.stdout', output):
107 self.command.process(self.args)
108 last_line = ''
109 for line in output.getvalue().splitlines():
110 if not line.startswith('['):
111 # This is a continuation line. --sort doesn't sort these.
112 continue
113 self.assertTrue(line > last_line,
114 '{} !> {}'.format(line, last_line))
115 last_line = line