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