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)
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 <http://www.gnu.org/licenses/>.
18 """Test the conf subcommand."""
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
48 def error(self
, message
):
49 self
.message
= message
54 class TestConf(unittest
.TestCase
):
55 """Test the conf subcommand."""
61 self
.command
.parser
= FakeParser()
62 self
.args
= FakeArgs()
64 def test_cannot_access_nonexistent_section(self
):
65 self
.args
.section
= 'thissectiondoesnotexist'
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()
94 self
.args
.output
= filename
95 self
.command
.process(self
.args
)
96 with
open(filename
, 'r') as fp
:
100 self
.assertEqual(contents
, 'no\n')
102 def test_sort_by_section(self
):
103 self
.args
.output
= '-'
104 self
.args
.sort
= True
106 with mock
.patch('sys.stdout', output
):
107 self
.command
.process(self
.args
)
109 for line
in output
.getvalue().splitlines():
110 if not line
.startswith('['):
111 # This is a continuation line. --sort doesn't sort these.
113 self
.assertTrue(line
> last_line
,
114 '{} !> {}'.format(line
, last_line
))