Skip to content

Commit e8076e6

Browse files
committed
Refactor: Move clean_result out of TestEmails class
The `clean_result` method in `test/test.py` did not use any instance-specific data, so it has been refactored into a standalone function. This improves code clarity and modularity. The associated regular expressions have also been moved to the module level.
1 parent 074ca93 commit e8076e6

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

test/test.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,51 @@ def load_tests(loader, tests, ignore):
5757
# This metaclass lets us generate the tests for each feed directory
5858
# separately. This lets us see which tests are being run more clearly than
5959
# if we had one big test that ran everything.
60+
MESSAGE_ID_REGEXP = _re.compile(
61+
r'^Message-ID: <(.*)@{}>$'.format(_re.escape(platform.node())), _re.MULTILINE)
62+
USER_AGENT_REGEXP = _re.compile(
63+
r'^User-Agent: rss2email/{0} \({1}\)$'.format(
64+
_re.escape(_rss2email.__version__),
65+
_re.escape(_rss2email.__url__)),
66+
_re.MULTILINE)
67+
BOUNDARY_REGEXP = _re.compile('===============[^=]+==')
68+
69+
70+
def clean_result(text, regexes=None):
71+
"""Cleanup dynamic portions of the generated email headers
72+
73+
>>> text = (
74+
... 'Content-Type: multipart/digest;\\n'
75+
... ' boundary="===============7509425281347501533=="\\n'
76+
... 'MIME-Version: 1.0\\n'
77+
... 'Date: Tue, 23 Aug 2011 15:57:37 -0000\\n'
78+
... 'Message-ID: <[email protected]>\\n'
79+
... 'User-Agent: rss2email/3.5 (https://github.com/rss2email/rss2email)\\n'
80+
... )
81+
>>> regexes = [
82+
... (_re.compile(r'^Message-ID: <.*>$', _re.MULTILINE), 'Message-ID: <[email protected]>'),
83+
... (_re.compile(r'^User-Agent: .*$', _re.MULTILINE), 'User-Agent: rss2email/...'),
84+
... (_re.compile('===============[^=]+=='), '===============...=='),
85+
... ]
86+
>>> print(clean_result(text, regexes).rstrip())
87+
Content-Type: multipart/digest;
88+
boundary="===============...=="
89+
MIME-Version: 1.0
90+
Date: Tue, 23 Aug 2011 15:57:37 -0000
91+
Message-ID: <[email protected]>
92+
User-Agent: rss2email/...
93+
"""
94+
if regexes is None:
95+
regexes = [
96+
(MESSAGE_ID_REGEXP, 'Message-ID: <[email protected]>'),
97+
(USER_AGENT_REGEXP, 'User-Agent: rss2email/...'),
98+
(BOUNDARY_REGEXP, '===============...=='),
99+
]
100+
for regexp,replacement in regexes:
101+
text = regexp.sub(replacement, text)
102+
return text
103+
104+
60105
class TestEmailsMeta(type):
61106
def __new__(cls, name, bases, attrs):
62107
# no paths on the command line, find all subdirectories
@@ -101,40 +146,6 @@ def __init__(self, *args, **kwargs):
101146
self.BASE_CONFIG_STRING = _stringio.getvalue()
102147
del _stringio
103148

104-
self.MESSAGE_ID_REGEXP = _re.compile(
105-
r'^Message-ID: <(.*)@{}>$'.format(_re.escape(platform.node())), _re.MULTILINE)
106-
self.USER_AGENT_REGEXP = _re.compile(
107-
r'^User-Agent: rss2email/{0} \({1}\)$'.format(_re.escape(_rss2email.__version__), _re.escape(_rss2email.__url__)),
108-
_re.MULTILINE)
109-
self.BOUNDARY_REGEXP = _re.compile('===============[^=]+==')
110-
111-
def clean_result(self, text):
112-
"""Cleanup dynamic portions of the generated email headers
113-
114-
>>> text = (
115-
... 'Content-Type: multipart/digest;\\n'
116-
... ' boundary="===============7509425281347501533=="\\n'
117-
... 'MIME-Version: 1.0\\n'
118-
... 'Date: Tue, 23 Aug 2011 15:57:37 -0000\\n'
119-
... 'Message-ID: <[email protected]>\\n'
120-
... 'User-Agent: rss2email/3.5 (https://github.com/rss2email/rss2email)\\n'
121-
... )
122-
>>> print(clean_result(text).rstrip())
123-
Content-Type: multipart/digest;
124-
boundary="===============...=="
125-
MIME-Version: 1.0
126-
Date: Tue, 23 Aug 2011 15:57:37 -0000
127-
Message-ID: <[email protected]>
128-
User-Agent: rss2email/...
129-
"""
130-
for regexp,replacement in [
131-
(self.MESSAGE_ID_REGEXP, 'Message-ID: <[email protected]>'),
132-
(self.USER_AGENT_REGEXP, 'User-Agent: rss2email/...'),
133-
(self.BOUNDARY_REGEXP, '===============...=='),
134-
]:
135-
text = regexp.sub(replacement, text)
136-
return text
137-
138149
def run_single_test(self, dirname=None, config_path=None):
139150
if dirname is None:
140151
dirname = _os.path.dirname(config_path)
@@ -153,7 +164,7 @@ def run_single_test(self, dirname=None, config_path=None):
153164
feed._send = TestEmails.Send()
154165
feed.run()
155166
generated = feed._send.as_string()
156-
generated = self.clean_result(generated)
167+
generated = clean_result(generated)
157168

158169
expected_path = config_path.replace('config', 'expected')
159170
if not _os.path.exists(expected_path):

0 commit comments

Comments
 (0)