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