blob: 51a0c7da8bb6bdfadbcee0a945800977f57dcb6b [file] [log] [blame]
Nick Coghlan029ba2b2011-08-22 16:05:44 +10001import mailcap
2import os
R David Murray347dc952016-09-09 20:04:23 -04003import copy
Nick Coghlan029ba2b2011-08-22 16:05:44 +10004import test.support
Hai Shi0c4f0f32020-06-30 21:46:31 +08005from test.support import os_helper
Nick Coghlan029ba2b2011-08-22 16:05:44 +10006import unittest
7
8# Location of mailcap file
9MAILCAPFILE = test.support.findfile("mailcap.txt")
10
11# Dict to act as mock mailcap entry for this test
12# The keys and values should match the contents of MAILCAPFILE
Ezio Melotti83feff52011-08-23 01:39:22 +030013MAILCAPDICT = {
14 'application/x-movie':
15 [{'compose': 'moviemaker %s',
16 'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"',
17 'description': '"Movie"',
R David Murray347dc952016-09-09 20:04:23 -040018 'view': 'movieplayer %s',
19 'lineno': 4}],
Ezio Melotti83feff52011-08-23 01:39:22 +030020 'application/*':
21 [{'copiousoutput': '',
R David Murray347dc952016-09-09 20:04:23 -040022 'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s',
23 'lineno': 5}],
Ezio Melotti83feff52011-08-23 01:39:22 +030024 'audio/basic':
25 [{'edit': 'audiocompose %s',
26 'compose': 'audiocompose %s',
27 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -040028 'view': 'showaudio %s',
29 'lineno': 6}],
Ezio Melotti83feff52011-08-23 01:39:22 +030030 'video/mpeg':
R David Murray347dc952016-09-09 20:04:23 -040031 [{'view': 'mpeg_play %s', 'lineno': 13}],
Ezio Melotti83feff52011-08-23 01:39:22 +030032 'application/postscript':
R David Murray347dc952016-09-09 20:04:23 -040033 [{'needsterminal': '', 'view': 'ps-to-terminal %s', 'lineno': 1},
34 {'compose': 'idraw %s', 'view': 'ps-to-terminal %s', 'lineno': 2}],
Ezio Melotti83feff52011-08-23 01:39:22 +030035 'application/x-dvi':
R David Murray347dc952016-09-09 20:04:23 -040036 [{'view': 'xdvi %s', 'lineno': 3}],
Ezio Melotti83feff52011-08-23 01:39:22 +030037 'message/external-body':
38 [{'composetyped': 'extcompose %s',
39 'description': '"A reference to data stored in an external location"',
40 'needsterminal': '',
R David Murray347dc952016-09-09 20:04:23 -040041 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}',
42 'lineno': 10}],
Ezio Melotti83feff52011-08-23 01:39:22 +030043 'text/richtext':
44 [{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8',
45 'copiousoutput': '',
R David Murray347dc952016-09-09 20:04:23 -040046 'view': 'shownonascii iso-8859-8 -e richtext -p %s',
47 'lineno': 11}],
Ezio Melotti83feff52011-08-23 01:39:22 +030048 'image/x-xwindowdump':
R David Murray347dc952016-09-09 20:04:23 -040049 [{'view': 'display %s', 'lineno': 9}],
Ezio Melotti83feff52011-08-23 01:39:22 +030050 'audio/*':
R David Murray347dc952016-09-09 20:04:23 -040051 [{'view': '/usr/local/bin/showaudio %t', 'lineno': 7}],
Ezio Melotti83feff52011-08-23 01:39:22 +030052 'video/*':
R David Murray347dc952016-09-09 20:04:23 -040053 [{'view': 'animate %s', 'lineno': 12}],
Ezio Melotti83feff52011-08-23 01:39:22 +030054 'application/frame':
R David Murray347dc952016-09-09 20:04:23 -040055 [{'print': '"cat %s | lp"', 'view': 'showframe %s', 'lineno': 0}],
Ezio Melotti83feff52011-08-23 01:39:22 +030056 'image/rgb':
R David Murray347dc952016-09-09 20:04:23 -040057 [{'view': 'display %s', 'lineno': 8}]
Ezio Melotti83feff52011-08-23 01:39:22 +030058}
Nick Coghlan029ba2b2011-08-22 16:05:44 +100059
R David Murray347dc952016-09-09 20:04:23 -040060# For backwards compatibility, readmailcapfile() and lookup() still support
61# the old version of mailcapdict without line numbers.
62MAILCAPDICT_DEPRECATED = copy.deepcopy(MAILCAPDICT)
63for entry_list in MAILCAPDICT_DEPRECATED.values():
64 for entry in entry_list:
65 entry.pop('lineno')
66
Nick Coghlan029ba2b2011-08-22 16:05:44 +100067
68class HelperFunctionTest(unittest.TestCase):
69
70 def test_listmailcapfiles(self):
71 # The return value for listmailcapfiles() will vary by system.
72 # So verify that listmailcapfiles() returns a list of strings that is of
73 # non-zero length.
74 mcfiles = mailcap.listmailcapfiles()
Ezio Melotti83feff52011-08-23 01:39:22 +030075 self.assertIsInstance(mcfiles, list)
76 for m in mcfiles:
77 self.assertIsInstance(m, str)
Hai Shi0c4f0f32020-06-30 21:46:31 +080078 with os_helper.EnvironmentVarGuard() as env:
Nick Coghlan029ba2b2011-08-22 16:05:44 +100079 # According to RFC 1524, if MAILCAPS env variable exists, use that
80 # and only that.
81 if "MAILCAPS" in env:
82 env_mailcaps = env["MAILCAPS"].split(os.pathsep)
83 else:
84 env_mailcaps = ["/testdir1/.mailcap", "/testdir2/mailcap"]
85 env["MAILCAPS"] = os.pathsep.join(env_mailcaps)
86 mcfiles = mailcap.listmailcapfiles()
87 self.assertEqual(env_mailcaps, mcfiles)
88
89 def test_readmailcapfile(self):
90 # Test readmailcapfile() using test file. It should match MAILCAPDICT.
91 with open(MAILCAPFILE, 'r') as mcf:
R David Murray347dc952016-09-09 20:04:23 -040092 with self.assertWarns(DeprecationWarning):
93 d = mailcap.readmailcapfile(mcf)
94 self.assertDictEqual(d, MAILCAPDICT_DEPRECATED)
Nick Coghlan029ba2b2011-08-22 16:05:44 +100095
96 def test_lookup(self):
97 # Test without key
R David Murray347dc952016-09-09 20:04:23 -040098 expected = [{'view': 'animate %s', 'lineno': 12},
99 {'view': 'mpeg_play %s', 'lineno': 13}]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000100 actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg')
101 self.assertListEqual(expected, actual)
102
103 # Test with key
104 key = 'compose'
105 expected = [{'edit': 'audiocompose %s',
106 'compose': 'audiocompose %s',
107 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -0400108 'view': 'showaudio %s',
109 'lineno': 6}]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000110 actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key)
111 self.assertListEqual(expected, actual)
112
R David Murray347dc952016-09-09 20:04:23 -0400113 # Test on user-defined dicts without line numbers
114 expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}]
115 actual = mailcap.lookup(MAILCAPDICT_DEPRECATED, 'video/mpeg')
116 self.assertListEqual(expected, actual)
117
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000118 def test_subst(self):
119 plist = ['id=1', 'number=2', 'total=3']
120 # test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
121 test_cases = [
Ezio Melotti83feff52011-08-23 01:39:22 +0300122 (["", "audio/*", "foo.txt"], ""),
123 (["echo foo", "audio/*", "foo.txt"], "echo foo"),
124 (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
125 (["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
R David Murray44b548d2016-09-08 13:59:53 -0400126 (["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
Ezio Melotti83feff52011-08-23 01:39:22 +0300127 (["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
128 (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
129 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000130 for tc in test_cases:
131 self.assertEqual(mailcap.subst(*tc[0]), tc[1])
132
133
134class GetcapsTest(unittest.TestCase):
135
136 def test_mock_getcaps(self):
137 # Test mailcap.getcaps() using mock mailcap file in this dir.
138 # Temporarily override any existing system mailcap file by pointing the
139 # MAILCAPS environment variable to our mock file.
Hai Shi0c4f0f32020-06-30 21:46:31 +0800140 with os_helper.EnvironmentVarGuard() as env:
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000141 env["MAILCAPS"] = MAILCAPFILE
142 caps = mailcap.getcaps()
143 self.assertDictEqual(caps, MAILCAPDICT)
144
145 def test_system_mailcap(self):
146 # Test mailcap.getcaps() with mailcap file(s) on system, if any.
147 caps = mailcap.getcaps()
Ezio Melotti83feff52011-08-23 01:39:22 +0300148 self.assertIsInstance(caps, dict)
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000149 mailcapfiles = mailcap.listmailcapfiles()
150 existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)]
151 if existingmcfiles:
152 # At least 1 mailcap file exists, so test that.
153 for (k, v) in caps.items():
Ezio Melotti83feff52011-08-23 01:39:22 +0300154 self.assertIsInstance(k, str)
155 self.assertIsInstance(v, list)
156 for e in v:
157 self.assertIsInstance(e, dict)
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000158 else:
159 # No mailcap files on system. getcaps() should return empty dict.
160 self.assertEqual({}, caps)
161
162
163class FindmatchTest(unittest.TestCase):
164
165 def test_findmatch(self):
166
167 # default findmatch arguments
168 c = MAILCAPDICT
169 fname = "foo.txt"
170 plist = ["access-type=default", "name=john", "site=python.org",
171 "directory=/tmp", "mode=foo", "server=bar"]
Ezio Melotti83feff52011-08-23 01:39:22 +0300172 audio_basic_entry = {
173 'edit': 'audiocompose %s',
174 'compose': 'audiocompose %s',
175 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -0400176 'view': 'showaudio %s',
177 'lineno': 6
Ezio Melotti83feff52011-08-23 01:39:22 +0300178 }
R David Murray347dc952016-09-09 20:04:23 -0400179 audio_entry = {"view": "/usr/local/bin/showaudio %t", 'lineno': 7}
180 video_entry = {'view': 'animate %s', 'lineno': 12}
Ezio Melotti83feff52011-08-23 01:39:22 +0300181 message_entry = {
182 'composetyped': 'extcompose %s',
183 'description': '"A reference to data stored in an external location"', 'needsterminal': '',
R David Murray347dc952016-09-09 20:04:23 -0400184 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}',
185 'lineno': 10,
Ezio Melotti83feff52011-08-23 01:39:22 +0300186 }
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000187
188 # test case: (findmatch args, findmatch keyword args, expected output)
189 # positional args: caps, MIMEtype
190 # keyword args: key="view", filename="/dev/null", plist=[]
191 # output: (command line, mailcap entry)
192 cases = [
193 ([{}, "video/mpeg"], {}, (None, None)),
194 ([c, "foo/bar"], {}, (None, None)),
R David Murray347dc952016-09-09 20:04:23 -0400195 ([c, "video/mpeg"], {}, ('animate /dev/null', video_entry)),
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000196 ([c, "audio/basic", "edit"], {}, ("audiocompose /dev/null", audio_basic_entry)),
197 ([c, "audio/basic", "compose"], {}, ("audiocompose /dev/null", audio_basic_entry)),
198 ([c, "audio/basic", "description"], {}, ('"An audio fragment"', audio_basic_entry)),
199 ([c, "audio/basic", "foobar"], {}, (None, None)),
200 ([c, "video/*"], {"filename": fname}, ("animate %s" % fname, video_entry)),
201 ([c, "audio/basic", "compose"],
202 {"filename": fname},
203 ("audiocompose %s" % fname, audio_basic_entry)),
204 ([c, "audio/basic"],
205 {"key": "description", "filename": fname},
206 ('"An audio fragment"', audio_basic_entry)),
207 ([c, "audio/*"],
208 {"filename": fname},
209 ("/usr/local/bin/showaudio audio/*", audio_entry)),
210 ([c, "message/external-body"],
211 {"plist": plist},
212 ("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
Ezio Melotti83feff52011-08-23 01:39:22 +0300213 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000214 self._run_cases(cases)
215
216 @unittest.skipUnless(os.name == "posix", "Requires 'test' command on system")
217 def test_test(self):
218 # findmatch() will automatically check any "test" conditions and skip
219 # the entry if the check fails.
220 caps = {"test/pass": [{"test": "test 1 -eq 1"}],
221 "test/fail": [{"test": "test 1 -eq 0"}]}
222 # test case: (findmatch args, findmatch keyword args, expected output)
223 # positional args: caps, MIMEtype, key ("test")
224 # keyword args: N/A
225 # output: (command line, mailcap entry)
226 cases = [
227 # findmatch will return the mailcap entry for test/pass because it evaluates to true
228 ([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})),
229 # findmatch will return None because test/fail evaluates to false
230 ([caps, "test/fail", "test"], {}, (None, None))
Ezio Melotti83feff52011-08-23 01:39:22 +0300231 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000232 self._run_cases(cases)
233
234 def _run_cases(self, cases):
235 for c in cases:
236 self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
237
238
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000239if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500240 unittest.main()