ยปCore Development>Code coverage>Lib/test/test_mailcap.py

Python code coverage for Lib/test/test_mailcap.py

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