7
# Location of mailcap file
8
MAILCAPFILE = test.support.findfile("mailcap.txt")
10
# Dict to act as mock mailcap entry for this test
11
# The keys and values should match the contents of MAILCAPFILE
13
'application/x-movie':
14
[{'compose': 'moviemaker %s',
15
'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"',
16
'description': '"Movie"',
17
'view': 'movieplayer %s'}],
19
[{'copiousoutput': '',
20
'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s'}],
22
[{'edit': 'audiocompose %s',
23
'compose': 'audiocompose %s',
24
'description': '"An audio fragment"',
25
'view': 'showaudio %s'}],
27
[{'view': 'mpeg_play %s'}],
28
'application/postscript':
29
[{'needsterminal': '', 'view': 'ps-to-terminal %s'},
30
{'compose': 'idraw %s', 'view': 'ps-to-terminal %s'}],
32
[{'view': 'xdvi %s'}],
33
'message/external-body':
34
[{'composetyped': 'extcompose %s',
35
'description': '"A reference to data stored in an external location"',
37
'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'}],
39
[{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8',
41
'view': 'shownonascii iso-8859-8 -e richtext -p %s'}],
42
'image/x-xwindowdump':
43
[{'view': 'display %s'}],
45
[{'view': '/usr/local/bin/showaudio %t'}],
47
[{'view': 'animate %s'}],
49
[{'print': '"cat %s | lp"', 'view': 'showframe %s'}],
51
[{'view': 'display %s'}]
55
class HelperFunctionTest(unittest.TestCase):
57
def test_listmailcapfiles(self):
58
# The return value for listmailcapfiles() will vary by system.
59
# So verify that listmailcapfiles() returns a list of strings that is of
61
mcfiles = mailcap.listmailcapfiles()
62
self.assertIsInstance(mcfiles, list)
64
self.assertIsInstance(m, str)
65
with test.support.EnvironmentVarGuard() as env:
66
# According to RFC 1524, if MAILCAPS env variable exists, use that
69
env_mailcaps = env["MAILCAPS"].split(os.pathsep)
71
env_mailcaps = ["/testdir1/.mailcap", "/testdir2/mailcap"]
72
env["MAILCAPS"] = os.pathsep.join(env_mailcaps)
73
mcfiles = mailcap.listmailcapfiles()
74
self.assertEqual(env_mailcaps, mcfiles)
76
def test_readmailcapfile(self):
77
# Test readmailcapfile() using test file. It should match MAILCAPDICT.
78
with open(MAILCAPFILE, 'r') as mcf:
79
d = mailcap.readmailcapfile(mcf)
80
self.assertDictEqual(d, MAILCAPDICT)
82
def test_lookup(self):
84
expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}]
85
actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg')
86
self.assertListEqual(expected, actual)
90
expected = [{'edit': 'audiocompose %s',
91
'compose': 'audiocompose %s',
92
'description': '"An audio fragment"',
93
'view': 'showaudio %s'}]
94
actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key)
95
self.assertListEqual(expected, actual)
98
plist = ['id=1', 'number=2', 'total=3']
99
# test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
101
(["", "audio/*", "foo.txt"], ""),
102
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
103
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
104
(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
105
(["echo \%t", "audio/*", "foo.txt"], "echo %t"),
106
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
107
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
109
for tc in test_cases:
110
self.assertEqual(mailcap.subst(*tc[0]), tc[1])
113
class GetcapsTest(unittest.TestCase):
115
def test_mock_getcaps(self):
116
# Test mailcap.getcaps() using mock mailcap file in this dir.
117
# Temporarily override any existing system mailcap file by pointing the
118
# MAILCAPS environment variable to our mock file.
119
with test.support.EnvironmentVarGuard() as env:
120
env["MAILCAPS"] = MAILCAPFILE
121
caps = mailcap.getcaps()
122
self.assertDictEqual(caps, MAILCAPDICT)
124
def test_system_mailcap(self):
125
# Test mailcap.getcaps() with mailcap file(s) on system, if any.
126
caps = mailcap.getcaps()
127
self.assertIsInstance(caps, dict)
128
mailcapfiles = mailcap.listmailcapfiles()
129
existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)]
131
# At least 1 mailcap file exists, so test that.
132
for (k, v) in caps.items():
133
self.assertIsInstance(k, str)
134
self.assertIsInstance(v, list)
136
self.assertIsInstance(e, dict)
138
# No mailcap files on system. getcaps() should return empty dict.
139
self.assertEqual({}, caps)
142
class FindmatchTest(unittest.TestCase):
144
def test_findmatch(self):
146
# default findmatch arguments
149
plist = ["access-type=default", "name=john", "site=python.org",
150
"directory=/tmp", "mode=foo", "server=bar"]
151
audio_basic_entry = {
152
'edit': 'audiocompose %s',
153
'compose': 'audiocompose %s',
154
'description': '"An audio fragment"',
155
'view': 'showaudio %s'
157
audio_entry = {"view": "/usr/local/bin/showaudio %t"}
158
video_entry = {'view': 'animate %s'}
160
'composetyped': 'extcompose %s',
161
'description': '"A reference to data stored in an external location"', 'needsterminal': '',
162
'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'
165
# test case: (findmatch args, findmatch keyword args, expected output)
166
# positional args: caps, MIMEtype
167
# keyword args: key="view", filename="/dev/null", plist=[]
168
# output: (command line, mailcap entry)
170
([{}, "video/mpeg"], {}, (None, None)),
171
([c, "foo/bar"], {}, (None, None)),
172
([c, "video/mpeg"], {}, ('mpeg_play /dev/null', {'view': 'mpeg_play %s'})),
173
([c, "audio/basic", "edit"], {}, ("audiocompose /dev/null", audio_basic_entry)),
174
([c, "audio/basic", "compose"], {}, ("audiocompose /dev/null", audio_basic_entry)),
175
([c, "audio/basic", "description"], {}, ('"An audio fragment"', audio_basic_entry)),
176
([c, "audio/basic", "foobar"], {}, (None, None)),
177
([c, "video/*"], {"filename": fname}, ("animate %s" % fname, video_entry)),
178
([c, "audio/basic", "compose"],
180
("audiocompose %s" % fname, audio_basic_entry)),
182
{"key": "description", "filename": fname},
183
('"An audio fragment"', audio_basic_entry)),
186
("/usr/local/bin/showaudio audio/*", audio_entry)),
187
([c, "message/external-body"],
189
("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
191
self._run_cases(cases)
193
@unittest.skipUnless(os.name == "posix", "Requires 'test' command on system")
195
# findmatch() will automatically check any "test" conditions and skip
196
# the entry if the check fails.
197
caps = {"test/pass": [{"test": "test 1 -eq 1"}],
198
"test/fail": [{"test": "test 1 -eq 0"}]}
199
# test case: (findmatch args, findmatch keyword args, expected output)
200
# positional args: caps, MIMEtype, key ("test")
202
# output: (command line, mailcap entry)
204
# findmatch will return the mailcap entry for test/pass because it evaluates to true
205
([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})),
206
# findmatch will return None because test/fail evaluates to false
207
([caps, "test/fail", "test"], {}, (None, None))
209
self._run_cases(cases)
211
def _run_cases(self, cases):
213
self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
217
test.support.run_unittest(HelperFunctionTest, GetcapsTest, FindmatchTest)
220
if __name__ == '__main__':