This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: No unit test for mailcap module
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: anthonyb, ezio.melotti, gnofi, loewis, ncoghlan, petri.lehtinen, python-dev, r.david.murray, vstinner
Priority: normal Keywords:

Created on 2009-07-14 18:39 by gnofi, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
mailcap.txt gnofi, 2010-10-04 01:27 Sample mailcap file used in gnofi, 2010-10-04 01:39 Trunk version of test for mailcap module (Corrected) gnofi, 2010-10-04 01:40 py3k version of test for mailcap module (Corrected)
Messages (17)
msg90516 - (view) Author: Gregory Nofi (gnofi) Date: 2009-07-14 18:39
There is currently no test_mailcap or any other standalone unit test for
the mailcap module. The only existing test is a self-test at the end of
the module.

I would like to be assigned to work on this patch.

(Why am I assigning myself to write tests for a small, older module? I'm
a complete noob to the Python-Dev community and I'm getting my feet wet
with this. Let me know if you have any advice or if I'm doing something
msg90517 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-07-14 19:04

Please read the material at if you haven't
already, especially the dev FAQ.  At the moment the case can't be
assigned to you in the tracker interface, but your claiming it in the
text is sufficient.

Please write the tests and create a diff-patch as outlined in the FAQ. 
Then upload it here for review.  If you need advice, #python-dev on
Freenode is a good place to get it.
msg90519 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-07-14 20:15
Please also identify yourself with your full name.
msg97289 - (view) Author: Gregory Nofi (gnofi) Date: 2010-01-06 01:56
Attached is a new file for testing the mailcap module in Python 2.7.

Writing the test was a little tricky because the existence and contents of the .mailcap file(s) will vary depending on the system or user. Therefore, the test mostly uses its own version of .mailcap, which I will also submit to this issue.

This is the first patch I've ever submitted to Python Core Development. Any feedback is appreciated.
msg97290 - (view) Author: Gregory Nofi (gnofi) Date: 2010-01-06 02:00
This is a sample .mailcap I created for the test. It should also go in the Lib/test directory.

It begins with a period due to a mailcap file naming convention (see RFC 1524). Is it OK that the file will be semi-hidden?
msg103933 - (view) Author: Gregory Nofi (gnofi) Date: 2010-04-22 00:50
Submitting Python 3 version of test. Note that it currently fails due to Issue8496.

It should use the same .mailcap file I submitted earlier.
msg115735 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-06 20:23
Thanks for contributing this; sorry it took so long to get a review.  Overall the tests look good (I didn't work through the logic of each test that looks up data; I'm trusting you on that part :)

Here are some comments:

1) In test_listmailcapfiles, you can use and inside the test set the value of MAILCAPS.  That way you can test both cases (set and not set).

2) I notice that the listmailcapfiles docstring is inaccurate (it actually lists the *possible* locations of mailcap files, and applies only to unix).  You could file a doc bug for that, but it is not an API method so it isn't a huge deal.

3) In test_lookup I think it might be better to hardcode the expected value rather than computing it.  It would make it clearer what the test was expecting, and remove any chance that you are just repeating the algorithm that the code itself is using to compute the value.

4) Your use of EnvironmentVarGuard in GetcapsTest is not technically correct, though it does happen to work.  You should really be doing self.env = to mimic the 'with' protocol.  It is a detail of the implementation that __enter__ returns self.

5) Why conditionalize your test on the existence of a system mailcap file?  You want a controlled environment for testing, so it is better, IMO, to just use your test mailcap file.  This will simplify your tests.
Or you could add a second test method that also does the simple checks if and only if one of the possible system mailcap files does exist, if your goal is to test that part of the code path.  (In that case you should skip the test with an appropriate message if no system mailcap files are found).

6) I don't see any reason why the test file needs to be named ".mailcap", you can specify its filename in any test where you need it.  It would indeed be better not to have a test file with a leading '.'.  Current convention would be to create a directory named 'mailcaptestdata' and put your test data files in there, but since you have only one it would in fact be OK to just put it directly in the test directory if you can come up with a clear name for it.  Alternatively you could group those tests that need it into a single test case and use the new setUpClass to create it from an embedded string and tearDownClass to delete it afterward.

Thanks again for working on this.
msg117937 - (view) Author: Gregory Nofi (gnofi) Date: 2010-10-04 01:27
Replacing .mailcap with mailcap.txt. Same content, but with more conventional file name.
msg117938 - (view) Author: Gregory Nofi (gnofi) Date: 2010-10-04 01:39
r.david.murray: Thanks a lot for your feedback! I've implemented those suggestions and they helped located a bug. (See case 9923.)
msg142667 - (view) Author: Anthony Briggs (anthonyb) Date: 2011-08-22 04:24
Added ncoghlan to the nosy list - we're reviewing/fixing unit test coverage as part of the sprints at PyconAU. Thanks Gnofi!
msg142680 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-08-22 06:06
New changeset 69cb66ab61cc by Nick Coghlan in branch 'default':
Add unit tests for the mailcap module. Patch by Gregory Nofi (closes #6484)
msg142767 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-08-22 22:39
New changeset a1bb07d67a24 by Ezio Melotti in branch 'default':
#6484: refactor a bit the tests.
msg142811 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-08-23 12:16
The buildbots are reporting a test failure on Windows:

FAIL: test_listmailcapfiles (test.test_mailcap.HelperFunctionTest)
Traceback (most recent call last):
  File "D:\cygwin\home\db3l\buildarea\3.x.bolen-windows\build\lib\test\", line 74, in test_listmailcapfiles
    self.assertEqual(env_mailcaps, mcfiles)
AssertionError: Lists differ: ['/testdir1/.mailcap', '/testd... != ['/testdir1/.mailcap;/testdir2...

First differing element 0:

First list contains 1 additional elements.
First extra element 1:

- ['/testdir1/.mailcap', '/testdir2/mailcap']
?                     ^^^^

+ ['/testdir1/.mailcap;/testdir2/mailcap']
?                     ^

There's a discrepancy between the test (uses os.pathsep) and the mailcap module (hardcoded to use ':' on all platforms).

I suspect the test is actually right and the module is wrong, but I don't know enough about mailcap to be sure. Anyone else?
msg142823 - (view) Author: Gregory Nofi (gnofi) Date: 2011-08-23 13:40
There's a bug for that test failure: #9923. I submitted a patch with it. Committing that should fix the failure.
msg142827 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-08-23 13:53
Ah, cheers - closing this one again.

/me wanders off to meta-tracker to ask for a list of "dependency of" and "superseder of" issues in the issue header...
msg168396 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-08-16 18:06
Any reason why this hasn't been backported to 3.2 or 2.7?
msg168397 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-16 18:11
There are divided opinions about the advisability of backporting tests that are not part of a bug fix.  In this case, there is also the fact that it includes a test that fails without a bug fix that was not backported.
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50733
2012-08-16 18:11:36r.david.murraysetmessages: + msg168397
2012-08-16 18:06:48petri.lehtinensetnosy: + petri.lehtinen
messages: + msg168396
2011-08-23 13:53:01ncoghlansetstatus: open -> closed

messages: + msg142827
2011-08-23 13:40:50gnofisetmessages: + msg142823
2011-08-23 12:16:17ncoghlansetstatus: closed -> open

messages: + msg142811
2011-08-22 22:39:33python-devsetmessages: + msg142767
2011-08-22 06:06:01python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg142680

resolution: fixed
stage: patch review -> resolved
2011-08-22 04:24:50anthonybsetmessages: + msg142667
2011-08-22 04:21:27anthonybsetnosy: + ncoghlan, anthonyb

versions: + Python 3.3, - Python 3.2
2011-05-07 15:27:49eric.araujolinkissue9923 dependencies
2010-10-04 01:40:23gnofisetfiles: +
2010-10-04 01:39:23gnofisetfiles: +

messages: + msg117938
2010-10-04 01:30:02gnofisetfiles: -
2010-10-04 01:29:55gnofisetfiles: -
2010-10-04 01:27:13gnofisetfiles: + mailcap.txt

messages: + msg117937
2010-10-04 01:19:32gnofisetfiles: - .mailcap
2010-09-06 20:23:54r.david.murraysetmessages: + msg115735
2010-04-22 12:31:10vstinnersetnosy: + vstinner
2010-04-22 12:28:53pitrousetpriority: low -> normal
nosy: + ezio.melotti
stage: test needed -> patch review

versions: - Python 2.7
2010-04-22 00:53:39gnofisetfiles: -
2010-04-22 00:53:32gnofisetfiles: +
2010-04-22 00:50:35gnofisetfiles: +

messages: + msg103933
2010-01-06 02:00:43gnofisetfiles: + .mailcap

messages: + msg97290
2010-01-06 01:56:12gnofisetfiles: +

messages: + msg97289
2009-07-14 20:15:52loewissetnosy: + loewis
messages: + msg90519
2009-07-14 19:04:41r.david.murraysetpriority: low

nosy: + r.david.murray
messages: + msg90517

stage: test needed
2009-07-14 18:39:06gnoficreate