blob: c08423c670739ad7c6e4e28d252e1d25d78752bf [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
5import unittest
6
7# Location of mailcap file
8MAILCAPFILE = test.support.findfile("mailcap.txt")
9
10# Dict to act as mock mailcap entry for this test
11# The keys and values should match the contents of MAILCAPFILE
Ezio Melotti83feff52011-08-23 01:39:22 +030012MAILCAPDICT = {
13 'application/x-movie':
14 [{'compose': 'moviemaker %s',
15 'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"',
16 'description': '"Movie"',
R David Murray347dc952016-09-09 20:04:23 -040017 'view': 'movieplayer %s',
18 'lineno': 4}],
Ezio Melotti83feff52011-08-23 01:39:22 +030019 'application/*':
20 [{'copiousoutput': '',
R David Murray347dc952016-09-09 20:04:23 -040021 'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s',
22 'lineno': 5}],
Ezio Melotti83feff52011-08-23 01:39:22 +030023 'audio/basic':
24 [{'edit': 'audiocompose %s',
25 'compose': 'audiocompose %s',
26 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -040027 'view': 'showaudio %s',
28 'lineno': 6}],
Ezio Melotti83feff52011-08-23 01:39:22 +030029 'video/mpeg':
R David Murray347dc952016-09-09 20:04:23 -040030 [{'view': 'mpeg_play %s', 'lineno': 13}],
Ezio Melotti83feff52011-08-23 01:39:22 +030031 'application/postscript':
R David Murray347dc952016-09-09 20:04:23 -040032 [{'needsterminal': '', 'view': 'ps-to-terminal %s', 'lineno': 1},
33 {'compose': 'idraw %s', 'view': 'ps-to-terminal %s', 'lineno': 2}],
Ezio Melotti83feff52011-08-23 01:39:22 +030034 'application/x-dvi':
R David Murray347dc952016-09-09 20:04:23 -040035 [{'view': 'xdvi %s', 'lineno': 3}],
Ezio Melotti83feff52011-08-23 01:39:22 +030036 'message/external-body':
37 [{'composetyped': 'extcompose %s',
38 'description': '"A reference to data stored in an external location"',
39 'needsterminal': '',
R David Murray347dc952016-09-09 20:04:23 -040040 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}',
41 'lineno': 10}],
Ezio Melotti83feff52011-08-23 01:39:22 +030042 'text/richtext':
43 [{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8',
44 'copiousoutput': '',
R David Murray347dc952016-09-09 20:04:23 -040045 'view': 'shownonascii iso-8859-8 -e richtext -p %s',
46 'lineno': 11}],
Ezio Melotti83feff52011-08-23 01:39:22 +030047 'image/x-xwindowdump':
R David Murray347dc952016-09-09 20:04:23 -040048 [{'view': 'display %s', 'lineno': 9}],
Ezio Melotti83feff52011-08-23 01:39:22 +030049 'audio/*':
R David Murray347dc952016-09-09 20:04:23 -040050 [{'view': '/usr/local/bin/showaudio %t', 'lineno': 7}],
Ezio Melotti83feff52011-08-23 01:39:22 +030051 'video/*':
R David Murray347dc952016-09-09 20:04:23 -040052 [{'view': 'animate %s', 'lineno': 12}],
Ezio Melotti83feff52011-08-23 01:39:22 +030053 'application/frame':
R David Murray347dc952016-09-09 20:04:23 -040054 [{'print': '"cat %s | lp"', 'view': 'showframe %s', 'lineno': 0}],
Ezio Melotti83feff52011-08-23 01:39:22 +030055 'image/rgb':
R David Murray347dc952016-09-09 20:04:23 -040056 [{'view': 'display %s', 'lineno': 8}]
Ezio Melotti83feff52011-08-23 01:39:22 +030057}
Nick Coghlan029ba2b2011-08-22 16:05:44 +100058
R David Murray347dc952016-09-09 20:04:23 -040059# For backwards compatibility, readmailcapfile() and lookup() still support
60# the old version of mailcapdict without line numbers.
61MAILCAPDICT_DEPRECATED = copy.deepcopy(MAILCAPDICT)
62for entry_list in MAILCAPDICT_DEPRECATED.values():
63 for entry in entry_list:
64 entry.pop('lineno')
65
Nick Coghlan029ba2b2011-08-22 16:05:44 +100066
67class HelperFunctionTest(unittest.TestCase):
68
69 def test_listmailcapfiles(self):
70 # The return value for listmailcapfiles() will vary by system.
71 # So verify that listmailcapfiles() returns a list of strings that is of
72 # non-zero length.
73 mcfiles = mailcap.listmailcapfiles()
Ezio Melotti83feff52011-08-23 01:39:22 +030074 self.assertIsInstance(mcfiles, list)
75 for m in mcfiles:
76 self.assertIsInstance(m, str)
Nick Coghlan029ba2b2011-08-22 16:05:44 +100077 with test.support.EnvironmentVarGuard() as env:
78 # According to RFC 1524, if MAILCAPS env variable exists, use that
79 # and only that.
80 if "MAILCAPS" in env:
81 env_mailcaps = env["MAILCAPS"].split(os.pathsep)
82 else:
83 env_mailcaps = ["/testdir1/.mailcap", "/testdir2/mailcap"]
84 env["MAILCAPS"] = os.pathsep.join(env_mailcaps)
85 mcfiles = mailcap.listmailcapfiles()
86 self.assertEqual(env_mailcaps, mcfiles)
87
88 def test_readmailcapfile(self):
89 # Test readmailcapfile() using test file. It should match MAILCAPDICT.
90 with open(MAILCAPFILE, 'r') as mcf:
R David Murray347dc952016-09-09 20:04:23 -040091 with self.assertWarns(DeprecationWarning):
92 d = mailcap.readmailcapfile(mcf)
93 self.assertDictEqual(d, MAILCAPDICT_DEPRECATED)
Nick Coghlan029ba2b2011-08-22 16:05:44 +100094
95 def test_lookup(self):
96 # Test without key
R David Murray347dc952016-09-09 20:04:23 -040097 expected = [{'view': 'animate %s', 'lineno': 12},
98 {'view': 'mpeg_play %s', 'lineno': 13}]
Nick Coghlan029ba2b2011-08-22 16:05:44 +100099 actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg')
100 self.assertListEqual(expected, actual)
101
102 # Test with key
103 key = 'compose'
104 expected = [{'edit': 'audiocompose %s',
105 'compose': 'audiocompose %s',
106 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -0400107 'view': 'showaudio %s',
108 'lineno': 6}]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000109 actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key)
110 self.assertListEqual(expected, actual)
111
R David Murray347dc952016-09-09 20:04:23 -0400112 # Test on user-defined dicts without line numbers
113 expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}]
114 actual = mailcap.lookup(MAILCAPDICT_DEPRECATED, 'video/mpeg')
115 self.assertListEqual(expected, actual)
116
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000117 def test_subst(self):
118 plist = ['id=1', 'number=2', 'total=3']
119 # test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
120 test_cases = [
Ezio Melotti83feff52011-08-23 01:39:22 +0300121 (["", "audio/*", "foo.txt"], ""),
122 (["echo foo", "audio/*", "foo.txt"], "echo foo"),
123 (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
124 (["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
R David Murray44b548d2016-09-08 13:59:53 -0400125 (["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
Ezio Melotti83feff52011-08-23 01:39:22 +0300126 (["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
127 (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
128 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000129 for tc in test_cases:
130 self.assertEqual(mailcap.subst(*tc[0]), tc[1])
131
132
133class GetcapsTest(unittest.TestCase):
134
135 def test_mock_getcaps(self):
136 # Test mailcap.getcaps() using mock mailcap file in this dir.
137 # Temporarily override any existing system mailcap file by pointing the
138 # MAILCAPS environment variable to our mock file.
139 with test.support.EnvironmentVarGuard() as env:
140 env["MAILCAPS"] = MAILCAPFILE
141 caps = mailcap.getcaps()
142 self.assertDictEqual(caps, MAILCAPDICT)
143
144 def test_system_mailcap(self):
145 # Test mailcap.getcaps() with mailcap file(s) on system, if any.
146 caps = mailcap.getcaps()
Ezio Melotti83feff52011-08-23 01:39:22 +0300147 self.assertIsInstance(caps, dict)
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000148 mailcapfiles = mailcap.listmailcapfiles()
149 existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)]
150 if existingmcfiles:
151 # At least 1 mailcap file exists, so test that.
152 for (k, v) in caps.items():
Ezio Melotti83feff52011-08-23 01:39:22 +0300153 self.assertIsInstance(k, str)
154 self.assertIsInstance(v, list)
155 for e in v:
156 self.assertIsInstance(e, dict)
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000157 else:
158 # No mailcap files on system. getcaps() should return empty dict.
159 self.assertEqual({}, caps)
160
161
162class FindmatchTest(unittest.TestCase):
163
164 def test_findmatch(self):
165
166 # default findmatch arguments
167 c = MAILCAPDICT
168 fname = "foo.txt"
169 plist = ["access-type=default", "name=john", "site=python.org",
170 "directory=/tmp", "mode=foo", "server=bar"]
Ezio Melotti83feff52011-08-23 01:39:22 +0300171 audio_basic_entry = {
172 'edit': 'audiocompose %s',
173 'compose': 'audiocompose %s',
174 'description': '"An audio fragment"',
R David Murray347dc952016-09-09 20:04:23 -0400175 'view': 'showaudio %s',
176 'lineno': 6
Ezio Melotti83feff52011-08-23 01:39:22 +0300177 }
R David Murray347dc952016-09-09 20:04:23 -0400178 audio_entry = {"view": "/usr/local/bin/showaudio %t", 'lineno': 7}
179 video_entry = {'view': 'animate %s', 'lineno': 12}
Ezio Melotti83feff52011-08-23 01:39:22 +0300180 message_entry = {
181 'composetyped': 'extcompose %s',
182 'description': '"A reference to data stored in an external location"', 'needsterminal': '',
R David Murray347dc952016-09-09 20:04:23 -0400183 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}',
184 'lineno': 10,
Ezio Melotti83feff52011-08-23 01:39:22 +0300185 }
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000186
187 # test case: (findmatch args, findmatch keyword args, expected output)
188 # positional args: caps, MIMEtype
189 # keyword args: key="view", filename="/dev/null", plist=[]
190 # output: (command line, mailcap entry)
191 cases = [
192 ([{}, "video/mpeg"], {}, (None, None)),
193 ([c, "foo/bar"], {}, (None, None)),
R David Murray347dc952016-09-09 20:04:23 -0400194 ([c, "video/mpeg"], {}, ('animate /dev/null', video_entry)),
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000195 ([c, "audio/basic", "edit"], {}, ("audiocompose /dev/null", audio_basic_entry)),
196 ([c, "audio/basic", "compose"], {}, ("audiocompose /dev/null", audio_basic_entry)),
197 ([c, "audio/basic", "description"], {}, ('"An audio fragment"', audio_basic_entry)),
198 ([c, "audio/basic", "foobar"], {}, (None, None)),
199 ([c, "video/*"], {"filename": fname}, ("animate %s" % fname, video_entry)),
200 ([c, "audio/basic", "compose"],
201 {"filename": fname},
202 ("audiocompose %s" % fname, audio_basic_entry)),
203 ([c, "audio/basic"],
204 {"key": "description", "filename": fname},
205 ('"An audio fragment"', audio_basic_entry)),
206 ([c, "audio/*"],
207 {"filename": fname},
208 ("/usr/local/bin/showaudio audio/*", audio_entry)),
209 ([c, "message/external-body"],
210 {"plist": plist},
211 ("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
Ezio Melotti83feff52011-08-23 01:39:22 +0300212 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000213 self._run_cases(cases)
214
215 @unittest.skipUnless(os.name == "posix", "Requires 'test' command on system")
216 def test_test(self):
217 # findmatch() will automatically check any "test" conditions and skip
218 # the entry if the check fails.
219 caps = {"test/pass": [{"test": "test 1 -eq 1"}],
220 "test/fail": [{"test": "test 1 -eq 0"}]}
221 # test case: (findmatch args, findmatch keyword args, expected output)
222 # positional args: caps, MIMEtype, key ("test")
223 # keyword args: N/A
224 # output: (command line, mailcap entry)
225 cases = [
226 # findmatch will return the mailcap entry for test/pass because it evaluates to true
227 ([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})),
228 # findmatch will return None because test/fail evaluates to false
229 ([caps, "test/fail", "test"], {}, (None, None))
Ezio Melotti83feff52011-08-23 01:39:22 +0300230 ]
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000231 self._run_cases(cases)
232
233 def _run_cases(self, cases):
234 for c in cases:
235 self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
236
237
Nick Coghlan029ba2b2011-08-22 16:05:44 +1000238if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500239 unittest.main()