| 1 | n/a | import mailcap |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import copy |
|---|
| 4 | n/a | import test.support |
|---|
| 5 | n/a | import unittest |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | # Location of mailcap file |
|---|
| 8 | n/a | MAILCAPFILE = test.support.findfile("mailcap.txt") |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | # Dict to act as mock mailcap entry for this test |
|---|
| 11 | n/a | # The keys and values should match the contents of MAILCAPFILE |
|---|
| 12 | n/a | MAILCAPDICT = { |
|---|
| 13 | n/a | 'application/x-movie': |
|---|
| 14 | n/a | [{'compose': 'moviemaker %s', |
|---|
| 15 | n/a | 'x11-bitmap': '"/usr/lib/Zmail/bitmaps/movie.xbm"', |
|---|
| 16 | n/a | 'description': '"Movie"', |
|---|
| 17 | n/a | 'view': 'movieplayer %s', |
|---|
| 18 | n/a | 'lineno': 4}], |
|---|
| 19 | n/a | 'application/*': |
|---|
| 20 | n/a | [{'copiousoutput': '', |
|---|
| 21 | n/a | 'view': 'echo "This is \\"%t\\" but is 50 \\% Greek to me" \\; cat %s', |
|---|
| 22 | n/a | 'lineno': 5}], |
|---|
| 23 | n/a | 'audio/basic': |
|---|
| 24 | n/a | [{'edit': 'audiocompose %s', |
|---|
| 25 | n/a | 'compose': 'audiocompose %s', |
|---|
| 26 | n/a | 'description': '"An audio fragment"', |
|---|
| 27 | n/a | 'view': 'showaudio %s', |
|---|
| 28 | n/a | 'lineno': 6}], |
|---|
| 29 | n/a | 'video/mpeg': |
|---|
| 30 | n/a | [{'view': 'mpeg_play %s', 'lineno': 13}], |
|---|
| 31 | n/a | 'application/postscript': |
|---|
| 32 | n/a | [{'needsterminal': '', 'view': 'ps-to-terminal %s', 'lineno': 1}, |
|---|
| 33 | n/a | {'compose': 'idraw %s', 'view': 'ps-to-terminal %s', 'lineno': 2}], |
|---|
| 34 | n/a | 'application/x-dvi': |
|---|
| 35 | n/a | [{'view': 'xdvi %s', 'lineno': 3}], |
|---|
| 36 | n/a | 'message/external-body': |
|---|
| 37 | n/a | [{'composetyped': 'extcompose %s', |
|---|
| 38 | n/a | 'description': '"A reference to data stored in an external location"', |
|---|
| 39 | n/a | 'needsterminal': '', |
|---|
| 40 | n/a | 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}', |
|---|
| 41 | n/a | 'lineno': 10}], |
|---|
| 42 | n/a | 'text/richtext': |
|---|
| 43 | n/a | [{'test': 'test "`echo %{charset} | tr \'[A-Z]\' \'[a-z]\'`" = iso-8859-8', |
|---|
| 44 | n/a | 'copiousoutput': '', |
|---|
| 45 | n/a | 'view': 'shownonascii iso-8859-8 -e richtext -p %s', |
|---|
| 46 | n/a | 'lineno': 11}], |
|---|
| 47 | n/a | 'image/x-xwindowdump': |
|---|
| 48 | n/a | [{'view': 'display %s', 'lineno': 9}], |
|---|
| 49 | n/a | 'audio/*': |
|---|
| 50 | n/a | [{'view': '/usr/local/bin/showaudio %t', 'lineno': 7}], |
|---|
| 51 | n/a | 'video/*': |
|---|
| 52 | n/a | [{'view': 'animate %s', 'lineno': 12}], |
|---|
| 53 | n/a | 'application/frame': |
|---|
| 54 | n/a | [{'print': '"cat %s | lp"', 'view': 'showframe %s', 'lineno': 0}], |
|---|
| 55 | n/a | 'image/rgb': |
|---|
| 56 | n/a | [{'view': 'display %s', 'lineno': 8}] |
|---|
| 57 | n/a | } |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | # For backwards compatibility, readmailcapfile() and lookup() still support |
|---|
| 60 | n/a | # the old version of mailcapdict without line numbers. |
|---|
| 61 | n/a | MAILCAPDICT_DEPRECATED = copy.deepcopy(MAILCAPDICT) |
|---|
| 62 | n/a | for entry_list in MAILCAPDICT_DEPRECATED.values(): |
|---|
| 63 | n/a | for entry in entry_list: |
|---|
| 64 | n/a | entry.pop('lineno') |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | class HelperFunctionTest(unittest.TestCase): |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def test_listmailcapfiles(self): |
|---|
| 70 | n/a | # The return value for listmailcapfiles() will vary by system. |
|---|
| 71 | n/a | # So verify that listmailcapfiles() returns a list of strings that is of |
|---|
| 72 | n/a | # non-zero length. |
|---|
| 73 | n/a | mcfiles = mailcap.listmailcapfiles() |
|---|
| 74 | n/a | self.assertIsInstance(mcfiles, list) |
|---|
| 75 | n/a | for m in mcfiles: |
|---|
| 76 | n/a | self.assertIsInstance(m, str) |
|---|
| 77 | n/a | with test.support.EnvironmentVarGuard() as env: |
|---|
| 78 | n/a | # According to RFC 1524, if MAILCAPS env variable exists, use that |
|---|
| 79 | n/a | # and only that. |
|---|
| 80 | n/a | if "MAILCAPS" in env: |
|---|
| 81 | n/a | env_mailcaps = env["MAILCAPS"].split(os.pathsep) |
|---|
| 82 | n/a | else: |
|---|
| 83 | n/a | env_mailcaps = ["/testdir1/.mailcap", "/testdir2/mailcap"] |
|---|
| 84 | n/a | env["MAILCAPS"] = os.pathsep.join(env_mailcaps) |
|---|
| 85 | n/a | mcfiles = mailcap.listmailcapfiles() |
|---|
| 86 | n/a | self.assertEqual(env_mailcaps, mcfiles) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | def test_readmailcapfile(self): |
|---|
| 89 | n/a | # Test readmailcapfile() using test file. It should match MAILCAPDICT. |
|---|
| 90 | n/a | with open(MAILCAPFILE, 'r') as mcf: |
|---|
| 91 | n/a | with self.assertWarns(DeprecationWarning): |
|---|
| 92 | n/a | d = mailcap.readmailcapfile(mcf) |
|---|
| 93 | n/a | self.assertDictEqual(d, MAILCAPDICT_DEPRECATED) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_lookup(self): |
|---|
| 96 | n/a | # Test without key |
|---|
| 97 | n/a | expected = [{'view': 'animate %s', 'lineno': 12}, |
|---|
| 98 | n/a | {'view': 'mpeg_play %s', 'lineno': 13}] |
|---|
| 99 | n/a | actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg') |
|---|
| 100 | n/a | self.assertListEqual(expected, actual) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | # Test with key |
|---|
| 103 | n/a | key = 'compose' |
|---|
| 104 | n/a | expected = [{'edit': 'audiocompose %s', |
|---|
| 105 | n/a | 'compose': 'audiocompose %s', |
|---|
| 106 | n/a | 'description': '"An audio fragment"', |
|---|
| 107 | n/a | 'view': 'showaudio %s', |
|---|
| 108 | n/a | 'lineno': 6}] |
|---|
| 109 | n/a | actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key) |
|---|
| 110 | n/a | self.assertListEqual(expected, actual) |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # Test on user-defined dicts without line numbers |
|---|
| 113 | n/a | expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}] |
|---|
| 114 | n/a | actual = mailcap.lookup(MAILCAPDICT_DEPRECATED, 'video/mpeg') |
|---|
| 115 | n/a | self.assertListEqual(expected, actual) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | def test_subst(self): |
|---|
| 118 | n/a | plist = ['id=1', 'number=2', 'total=3'] |
|---|
| 119 | n/a | # test case: ([field, MIMEtype, filename, plist=[]], <expected string>) |
|---|
| 120 | n/a | test_cases = [ |
|---|
| 121 | n/a | (["", "audio/*", "foo.txt"], ""), |
|---|
| 122 | n/a | (["echo foo", "audio/*", "foo.txt"], "echo foo"), |
|---|
| 123 | n/a | (["echo %s", "audio/*", "foo.txt"], "echo foo.txt"), |
|---|
| 124 | n/a | (["echo %t", "audio/*", "foo.txt"], "echo audio/*"), |
|---|
| 125 | n/a | (["echo \\%t", "audio/*", "foo.txt"], "echo %t"), |
|---|
| 126 | n/a | (["echo foo", "audio/*", "foo.txt", plist], "echo foo"), |
|---|
| 127 | n/a | (["echo %{total}", "audio/*", "foo.txt", plist], "echo 3") |
|---|
| 128 | n/a | ] |
|---|
| 129 | n/a | for tc in test_cases: |
|---|
| 130 | n/a | self.assertEqual(mailcap.subst(*tc[0]), tc[1]) |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | class GetcapsTest(unittest.TestCase): |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def test_mock_getcaps(self): |
|---|
| 136 | n/a | # Test mailcap.getcaps() using mock mailcap file in this dir. |
|---|
| 137 | n/a | # Temporarily override any existing system mailcap file by pointing the |
|---|
| 138 | n/a | # MAILCAPS environment variable to our mock file. |
|---|
| 139 | n/a | with test.support.EnvironmentVarGuard() as env: |
|---|
| 140 | n/a | env["MAILCAPS"] = MAILCAPFILE |
|---|
| 141 | n/a | caps = mailcap.getcaps() |
|---|
| 142 | n/a | self.assertDictEqual(caps, MAILCAPDICT) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | def test_system_mailcap(self): |
|---|
| 145 | n/a | # Test mailcap.getcaps() with mailcap file(s) on system, if any. |
|---|
| 146 | n/a | caps = mailcap.getcaps() |
|---|
| 147 | n/a | self.assertIsInstance(caps, dict) |
|---|
| 148 | n/a | mailcapfiles = mailcap.listmailcapfiles() |
|---|
| 149 | n/a | existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)] |
|---|
| 150 | n/a | if existingmcfiles: |
|---|
| 151 | n/a | # At least 1 mailcap file exists, so test that. |
|---|
| 152 | n/a | for (k, v) in caps.items(): |
|---|
| 153 | n/a | self.assertIsInstance(k, str) |
|---|
| 154 | n/a | self.assertIsInstance(v, list) |
|---|
| 155 | n/a | for e in v: |
|---|
| 156 | n/a | self.assertIsInstance(e, dict) |
|---|
| 157 | n/a | else: |
|---|
| 158 | n/a | # No mailcap files on system. getcaps() should return empty dict. |
|---|
| 159 | n/a | self.assertEqual({}, caps) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | class FindmatchTest(unittest.TestCase): |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | def test_findmatch(self): |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | # default findmatch arguments |
|---|
| 167 | n/a | c = MAILCAPDICT |
|---|
| 168 | n/a | fname = "foo.txt" |
|---|
| 169 | n/a | plist = ["access-type=default", "name=john", "site=python.org", |
|---|
| 170 | n/a | "directory=/tmp", "mode=foo", "server=bar"] |
|---|
| 171 | n/a | audio_basic_entry = { |
|---|
| 172 | n/a | 'edit': 'audiocompose %s', |
|---|
| 173 | n/a | 'compose': 'audiocompose %s', |
|---|
| 174 | n/a | 'description': '"An audio fragment"', |
|---|
| 175 | n/a | 'view': 'showaudio %s', |
|---|
| 176 | n/a | 'lineno': 6 |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | audio_entry = {"view": "/usr/local/bin/showaudio %t", 'lineno': 7} |
|---|
| 179 | n/a | video_entry = {'view': 'animate %s', 'lineno': 12} |
|---|
| 180 | n/a | message_entry = { |
|---|
| 181 | n/a | 'composetyped': 'extcompose %s', |
|---|
| 182 | n/a | 'description': '"A reference to data stored in an external location"', 'needsterminal': '', |
|---|
| 183 | n/a | 'view': 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}', |
|---|
| 184 | n/a | 'lineno': 10, |
|---|
| 185 | n/a | } |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | # test case: (findmatch args, findmatch keyword args, expected output) |
|---|
| 188 | n/a | # positional args: caps, MIMEtype |
|---|
| 189 | n/a | # keyword args: key="view", filename="/dev/null", plist=[] |
|---|
| 190 | n/a | # output: (command line, mailcap entry) |
|---|
| 191 | n/a | cases = [ |
|---|
| 192 | n/a | ([{}, "video/mpeg"], {}, (None, None)), |
|---|
| 193 | n/a | ([c, "foo/bar"], {}, (None, None)), |
|---|
| 194 | n/a | ([c, "video/mpeg"], {}, ('animate /dev/null', video_entry)), |
|---|
| 195 | n/a | ([c, "audio/basic", "edit"], {}, ("audiocompose /dev/null", audio_basic_entry)), |
|---|
| 196 | n/a | ([c, "audio/basic", "compose"], {}, ("audiocompose /dev/null", audio_basic_entry)), |
|---|
| 197 | n/a | ([c, "audio/basic", "description"], {}, ('"An audio fragment"', audio_basic_entry)), |
|---|
| 198 | n/a | ([c, "audio/basic", "foobar"], {}, (None, None)), |
|---|
| 199 | n/a | ([c, "video/*"], {"filename": fname}, ("animate %s" % fname, video_entry)), |
|---|
| 200 | n/a | ([c, "audio/basic", "compose"], |
|---|
| 201 | n/a | {"filename": fname}, |
|---|
| 202 | n/a | ("audiocompose %s" % fname, audio_basic_entry)), |
|---|
| 203 | n/a | ([c, "audio/basic"], |
|---|
| 204 | n/a | {"key": "description", "filename": fname}, |
|---|
| 205 | n/a | ('"An audio fragment"', audio_basic_entry)), |
|---|
| 206 | n/a | ([c, "audio/*"], |
|---|
| 207 | n/a | {"filename": fname}, |
|---|
| 208 | n/a | ("/usr/local/bin/showaudio audio/*", audio_entry)), |
|---|
| 209 | n/a | ([c, "message/external-body"], |
|---|
| 210 | n/a | {"plist": plist}, |
|---|
| 211 | n/a | ("showexternal /dev/null default john python.org /tmp foo bar", message_entry)) |
|---|
| 212 | n/a | ] |
|---|
| 213 | n/a | self._run_cases(cases) |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | @unittest.skipUnless(os.name == "posix", "Requires 'test' command on system") |
|---|
| 216 | n/a | def test_test(self): |
|---|
| 217 | n/a | # findmatch() will automatically check any "test" conditions and skip |
|---|
| 218 | n/a | # the entry if the check fails. |
|---|
| 219 | n/a | caps = {"test/pass": [{"test": "test 1 -eq 1"}], |
|---|
| 220 | n/a | "test/fail": [{"test": "test 1 -eq 0"}]} |
|---|
| 221 | n/a | # test case: (findmatch args, findmatch keyword args, expected output) |
|---|
| 222 | n/a | # positional args: caps, MIMEtype, key ("test") |
|---|
| 223 | n/a | # keyword args: N/A |
|---|
| 224 | n/a | # output: (command line, mailcap entry) |
|---|
| 225 | n/a | cases = [ |
|---|
| 226 | n/a | # findmatch will return the mailcap entry for test/pass because it evaluates to true |
|---|
| 227 | n/a | ([caps, "test/pass", "test"], {}, ("test 1 -eq 1", {"test": "test 1 -eq 1"})), |
|---|
| 228 | n/a | # findmatch will return None because test/fail evaluates to false |
|---|
| 229 | n/a | ([caps, "test/fail", "test"], {}, (None, None)) |
|---|
| 230 | n/a | ] |
|---|
| 231 | n/a | self._run_cases(cases) |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | def _run_cases(self, cases): |
|---|
| 234 | n/a | for c in cases: |
|---|
| 235 | n/a | self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2]) |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | if __name__ == '__main__': |
|---|
| 239 | n/a | unittest.main() |
|---|