forked from turbodog/rss2email
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest.py
executable file
·143 lines (122 loc) · 4.72 KB
/
test.py
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
#!/usr/bin/env python3
"""Test processing logic on known feeds.
"""
import difflib as _difflib
import glob as _glob
import io as _io
import logging as _logging
import os as _os
import re as _re
import rss2email as _rss2email
import rss2email.config as _rss2email_config
import rss2email.feed as _rss2email_feed
# Get a copy of the internal rss2email.CONFIG for copying
_stringio = _io.StringIO()
_rss2email_config.CONFIG.write(_stringio)
BASE_CONFIG_STRING = _stringio.getvalue()
del _stringio
MESSAGE_ID_REGEXP = _re.compile(
'^Message-ID: <[^@]*@dev.null.invalid>$', _re.MULTILINE)
USER_AGENT_REGEXP = _re.compile(
'^User-Agent: rss2email/[0-9.]* \+\S*$', _re.MULTILINE)
BOUNDARY_REGEXP = _re.compile('===============[^=]+==')
class Send (list):
def __call__(self, sender, message):
self.append((sender, message))
def as_string(self):
chunks = [
'SENT BY: {}\n{}\n'.format(sender, message.as_string())
for sender,message in self]
return '\n'.join(chunks)
def clean_result(text):
"""Cleanup dynamic portions of the generated email headers
>>> text = (
... 'Content-Type: multipart/digest;\\n'
... ' boundary="===============7509425281347501533=="\\n'
... 'MIME-Version: 1.0\\n'
... 'Date: Tue, 23 Aug 2011 15:57:37 -0000\\n'
... 'Message-ID: <[email protected]>\\n'
... 'User-Agent: rss2email/3.5 +https://github.com/wking/rss2email\\n'
... )
>>> print(clean_result(text).rstrip())
Content-Type: multipart/digest;
boundary="===============...=="
MIME-Version: 1.0
Date: Tue, 23 Aug 2011 15:57:37 -0000
Message-ID: <[email protected]>
User-Agent: rss2email/...
"""
for regexp,replacement in [
(MESSAGE_ID_REGEXP, 'Message-ID: <[email protected]>'),
(USER_AGENT_REGEXP, 'User-Agent: rss2email/...'),
(BOUNDARY_REGEXP, '===============...=='),
]:
text = regexp.sub(replacement, text)
return text
def test(dirname=None, config_path=None, force=False):
if dirname is None:
dirname = _os.path.dirname(config_path)
if config_path is None:
_rss2email.LOG.info('testing {}'.format(dirname))
for config_path in _glob.glob(_os.path.join(dirname, '*.config')):
test(dirname=dirname, config_path=config_path, force=force)
return
feed_path = _glob.glob(_os.path.join(dirname, 'feed.*'))[0]
_rss2email.LOG.info('testing {}'.format(config_path))
config = _rss2email_config.Config()
config.read_string(BASE_CONFIG_STRING)
read_paths = config.read([config_path])
feed = _rss2email_feed.Feed(name='test', url=feed_path, config=config)
expected_path = config_path.replace('config', 'expected')
with open(expected_path, 'r') as f:
expected = clean_result(f.read())
feed._send = Send()
feed.run()
generated = feed._send.as_string()
if force:
with open(expected_path, 'w') as f:
f.write(generated)
generated = clean_result(generated)
if generated != expected:
diff_lines = _difflib.unified_diff(
expected.splitlines(), generated.splitlines(),
'expected', 'generated', lineterm='')
raise ValueError(
'error processing {}\n{}'.format(
config_path,
'\n'.join(diff_lines)))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--force', action='store_const', const=True,
help=(
"write output files (useful for figuring out what's expected "
'from a new feed).'))
parser.add_argument(
'-V', '--verbose', default=0, action='count',
help='increment verbosity')
parser.add_argument(
'dir', nargs='*',
help='select subdirs to test (tests all subdirs by default)')
args = parser.parse_args()
if args.verbose:
_rss2email.LOG.setLevel(
max(_logging.DEBUG, _logging.ERROR - 10 * args.verbose))
# no paths on the command line, find all subdirectories
this_dir = _os.path.dirname(__file__)
if not args.dir:
for basename in _os.listdir(this_dir):
path = _os.path.join(this_dir, basename)
if _os.path.isdir(path):
args.dir.append(path)
# we need standardized URLs, so change to `this_dir` and adjust paths
orig_dir = _os.getcwd()
_os.chdir(this_dir)
# run tests
for orig_path in args.dir:
this_path = _os.path.relpath(orig_path, start=this_dir)
if _os.path.isdir(this_path):
test(dirname=this_path, force=args.force)
else:
test(config_path=this_path, force=args.force)