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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# Copyright (C) 2016-2017 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/>.
"""Tests for mailman.utilities.modules."""
import os
import sys
import unittest
from contextlib import ExitStack, contextmanager
from mailman.interfaces.styles import IStyle
from mailman.utilities.modules import add_components, find_components
from pathlib import Path
from tempfile import TemporaryDirectory
@contextmanager
def hack_syspath(index, path):
old_path = sys.path[:]
try:
sys.path.insert(index, path)
yield
finally:
sys.path = old_path
def clean_mypackage():
# Make a copy since we'll mutate it as we go.
for module in sys.modules.copy():
package, dot, rest = module.partition('.')
if package == 'mypackage':
del sys.modules[module]
class TestModuleImports(unittest.TestCase):
def test_find_modules_with_dotfiles(self):
# Emacs creates lock files when a single file is opened by more than
# one user. These files look like .#<filename>.py because of which
# find_components() tries to import them but fails. All such files
# should be ignored by default.
with ExitStack() as resources:
# Creating a temporary directory and adding it to sys.path.
temp_package = resources.enter_context(TemporaryDirectory())
resources.enter_context(hack_syspath(0, temp_package))
resources.callback(clean_mypackage)
# Create a module inside the above package along with a good, bad
# and __init__.py file so that we can import from it.
module_path = os.path.join(temp_package, 'mypackage')
os.mkdir(module_path)
init_file = os.path.join(module_path, '__init__.py')
Path(init_file).touch()
good_file = os.path.join(module_path, 'goodfile.py')
bad_file = os.path.join(module_path, '.#badfile.py')
with open(good_file, 'w', encoding='utf-8') as fp:
print("""\
from public import public
from mailman.interfaces.styles import IStyle
from zope.interface import implementer
@public
@implementer(IStyle)
class GoodStyle:
name = 'good-style'
def apply(self):
pass
""", file=fp)
with open(bad_file, 'w', encoding='utf-8') as fp:
print("""\
from public import public
from mailman.interfaces.styles import IStyle
from zope.interface import implementer
@public
@implementer(IStyle)
class BadStyle:
name = 'bad-style'
def apply(self):
pass
""", file=fp)
# Find all the IStyle components in the dummy package. This
# should find GoodStyle but not BadStyle.
names = [component.name
for component
in find_components('mypackage', IStyle)]
self.assertEqual(names, ['good-style'])
def test_find_components_no_instantiate(self):
# find_components() now instantiates the class unless it's been
# decorated with the @abstract_component decorator.
with ExitStack() as resources:
# Creating a temporary directory and adding it to sys.path.
temp_package = resources.enter_context(TemporaryDirectory())
resources.enter_context(hack_syspath(0, temp_package))
resources.callback(clean_mypackage)
# Create a module inside the above package along with an
# __init__.py file so that we can import from it.
module_path = os.path.join(temp_package, 'mypackage')
os.mkdir(module_path)
init_file = os.path.join(module_path, '__init__.py')
Path(init_file).touch()
component_file = os.path.join(module_path, 'components.py')
with open(component_file, 'w', encoding='utf-8') as fp:
print("""\
from mailman.interfaces.styles import IStyle
from mailman.utilities.modules import abstract_component
from public import public
from zope.interface import implementer
@public
@implementer(IStyle)
class ConcreteStyle:
name = 'concrete-style'
def apply(self):
pass
@public
@implementer(IStyle)
@abstract_component
class AbstractStyle:
name = 'abstract-style'
def apply(self):
pass
""", file=fp)
names = [component.name
for component
in find_components('mypackage', IStyle)]
self.assertEqual(names, ['concrete-style'])
def test_add_components(self):
with ExitStack() as resources:
# Creating a temporary directory and adding it to sys.path.
temp_package = resources.enter_context(TemporaryDirectory())
resources.enter_context(hack_syspath(0, temp_package))
resources.callback(clean_mypackage)
# Create a module inside the above package along with an
# __init__.py file so that we can import from it.
module_path = os.path.join(temp_package, 'mypackage')
os.mkdir(module_path)
init_file = os.path.join(module_path, '__init__.py')
Path(init_file).touch()
component_file = os.path.join(module_path, 'components.py')
with open(component_file, 'w', encoding='utf-8') as fp:
print("""\
from mailman.interfaces.styles import IStyle
from mailman.utilities.modules import abstract_component
from public import public
from zope.interface import implementer
@public
@implementer(IStyle)
class StyleA:
name = 'styleA'
def apply(self):
pass
@public
@implementer(IStyle)
@abstract_component
class StyleB:
name = 'styleB'
def apply(self):
pass
@public
@implementer(IStyle)
class StyleC:
name = 'styleC'
def apply(self):
pass
""", file=fp)
styles = {}
add_components('mypackage', IStyle, styles)
self.assertEqual(set(styles), {'styleA', 'styleC'})
def test_add_components_duplicates(self):
# A duplicate name exists, so a RuntimeError is raised.
with ExitStack() as resources:
# Creating a temporary directory and adding it to sys.path.
temp_package = resources.enter_context(TemporaryDirectory())
resources.enter_context(hack_syspath(0, temp_package))
resources.callback(clean_mypackage)
# Create a module inside the above package along with an
# __init__.py file so that we can import from it.
module_path = os.path.join(temp_package, 'mypackage')
os.mkdir(module_path)
init_file = os.path.join(module_path, '__init__.py')
Path(init_file).touch()
component_file = os.path.join(module_path, 'components.py')
with open(component_file, 'w', encoding='utf-8') as fp:
print("""\
from mailman.interfaces.styles import IStyle
from mailman.utilities.modules import abstract_component
from public import public
from zope.interface import implementer
@public
@implementer(IStyle)
class StyleA1:
name = 'styleA'
def apply(self):
pass
@public
@implementer(IStyle)
class StyleA2:
name = 'styleA'
def apply(self):
pass
""", file=fp)
with self.assertRaisesRegex(
RuntimeError,
'Duplicate key "styleA" found in '
'<mypackage.components.StyleA2 object at .*>; '
'previously <mypackage.components.StyleA1 object at '
'.*>'):
add_components('mypackage', IStyle, {})
|