Fix mailman lists command to not output trailing blanks and handle long list ids.
[mailman.git] / src / mailman / commands / tests / test_cli_lists.py
blob0c1b123817b935bd0361692c83edf524967033d4
1 # Copyright (C) 2015-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)
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 """Additional tests for the `lists` command line subcommand."""
20 import unittest
22 from click.testing import CliRunner
23 from mailman.app.lifecycle import create_list
24 from mailman.commands.cli_lists import lists
25 from mailman.interfaces.domain import IDomainManager
26 from mailman.testing.layers import ConfigLayer
27 from zope.component import getUtility
30 class TestLists(unittest.TestCase):
31 layer = ConfigLayer
33 def setUp(self):
34 self._command = CliRunner()
36 def test_lists_with_domain_option(self):
37 # LP: #1166911 - non-matching lists were returned.
38 getUtility(IDomainManager).add(
39 'example.net', 'An example domain.')
40 create_list('test1@example.com')
41 create_list('test2@example.com')
42 # Only this one should show up.
43 create_list('test3@example.net')
44 create_list('test4@example.com')
45 result = self._command.invoke(lists, ('--domain', 'example.net'))
46 self.assertEqual(result.exit_code, 0)
47 self.assertEqual(
48 result.output,
49 '1 matching mailing lists found:\ntest3@example.net\n')
51 def test_lists_with_description(self):
52 getUtility(IDomainManager).add(
53 'example.net', 'An example domain.')
54 mlist1 = create_list('test1@example.com')
55 mlist1.description = ('Test list number 1 plus a whole lot more stuff '
56 'and even longer')
57 create_list('test2@example.com')
58 mlist3 = create_list('test3@example.net')
59 mlist3.description = 'Test list number 3'
60 create_list('test4@example.com')
61 result = self._command.invoke(lists, ('-d'))
62 self.assertEqual(result.exit_code, 0)
63 self.assertEqual(
64 result.output,
65 '4 matching mailing lists found:\n'
66 'test1@example.com - Test list number 1 plus a whole lot more '
67 'stuff and even longer\n'
68 'test2@example.com -\n'
69 'test3@example.net - Test list number 3\n'
70 'test4@example.com -\n')
72 def test_lists_without_description(self):
73 getUtility(IDomainManager).add(
74 'example.net', 'An example domain.')
75 mlist1 = create_list('test1@example.com')
76 mlist1.description = ('Test list number 1 plus a whole lot more stuff '
77 'and even longer')
78 create_list('test2@example.com')
79 mlist3 = create_list('test3@example.net')
80 mlist3.description = 'Test list number 3'
81 create_list('test4@example.com')
82 result = self._command.invoke(lists)
83 self.assertEqual(result.exit_code, 0)
84 self.assertEqual(
85 result.output,
86 '4 matching mailing lists found:\n'
87 'test1@example.com\n'
88 'test2@example.com\n'
89 'test3@example.net\n'
90 'test4@example.com\n')
92 def test_lists_with_description_and_long_name(self):
93 mlist = create_list('a_very_long_list_name_with_more_than_seventy'
94 '_characters_xxxxxxx@example.com')
95 mlist.description = ('Test list number 1 plus a whole lot more stuff '
96 'and even longer')
97 result = self._command.invoke(lists, '-d')
98 self.assertEqual(result.exit_code, 0)
99 self.assertEqual(
100 result.output,
101 '1 matching mailing lists found:\n'
102 'a_very_long_list_name_with_more_than_seventy'
103 '_characters_xxxxxxx@example.com - Test list number 1 '
104 'plus a whole lot more stuff and even longer\n')