11import mailcap
22import os
33import shutil
4+ import copy
45import test .support
56import unittest
67
1415 [{'compose' : 'moviemaker %s' ,
1516 'x11-bitmap' : '"/usr/lib/Zmail/bitmaps/movie.xbm"' ,
1617 'description' : '"Movie"' ,
17- 'view' : 'movieplayer %s' }],
18+ 'view' : 'movieplayer %s' ,
19+ 'lineno' : 4 }],
1820 'application/*' :
1921 [{'copiousoutput' : '' ,
20- 'view' : 'echo "This is \\ "%t\\ " but is 50 \\ % Greek to me" \\ ; cat %s' }],
22+ 'view' : 'echo "This is \\ "%t\\ " but is 50 \\ % Greek to me" \\ ; cat %s' ,
23+ 'lineno' : 5 }],
2124 'audio/basic' :
2225 [{'edit' : 'audiocompose %s' ,
2326 'compose' : 'audiocompose %s' ,
2427 'description' : '"An audio fragment"' ,
25- 'view' : 'showaudio %s' }],
28+ 'view' : 'showaudio %s' ,
29+ 'lineno' : 6 }],
2630 'video/mpeg' :
27- [{'view' : 'mpeg_play %s' }],
31+ [{'view' : 'mpeg_play %s' , 'lineno' : 13 }],
2832 'application/postscript' :
29- [{'needsterminal' : '' , 'view' : 'ps-to-terminal %s' },
30- {'compose' : 'idraw %s' , 'view' : 'ps-to-terminal %s' }],
33+ [{'needsterminal' : '' , 'view' : 'ps-to-terminal %s' , 'lineno' : 1 },
34+ {'compose' : 'idraw %s' , 'view' : 'ps-to-terminal %s' , 'lineno' : 2 }],
3135 'application/x-dvi' :
32- [{'view' : 'xdvi %s' }],
36+ [{'view' : 'xdvi %s' , 'lineno' : 3 }],
3337 'message/external-body' :
3438 [{'composetyped' : 'extcompose %s' ,
3539 'description' : '"A reference to data stored in an external location"' ,
3640 'needsterminal' : '' ,
37- 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' }],
41+ 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' ,
42+ 'lineno' : 10 }],
3843 'text/richtext' :
3944 [{'test' : 'test "`echo %{charset} | tr \' [A-Z]\' \' [a-z]\' `" = iso-8859-8' ,
4045 'copiousoutput' : '' ,
41- 'view' : 'shownonascii iso-8859-8 -e richtext -p %s' }],
46+ 'view' : 'shownonascii iso-8859-8 -e richtext -p %s' ,
47+ 'lineno' : 11 }],
4248 'image/x-xwindowdump' :
43- [{'view' : 'display %s' }],
49+ [{'view' : 'display %s' , 'lineno' : 9 }],
4450 'audio/*' :
45- [{'view' : '/usr/local/bin/showaudio %t' }],
51+ [{'view' : '/usr/local/bin/showaudio %t' , 'lineno' : 7 }],
4652 'video/*' :
47- [{'view' : 'animate %s' }],
53+ [{'view' : 'animate %s' , 'lineno' : 12 }],
4854 'application/frame' :
49- [{'print' : '"cat %s | lp"' , 'view' : 'showframe %s' }],
55+ [{'print' : '"cat %s | lp"' , 'view' : 'showframe %s' , 'lineno' : 0 }],
5056 'image/rgb' :
51- [{'view' : 'display %s' }]
57+ [{'view' : 'display %s' , 'lineno' : 8 }]
5258}
5359
60+ # For backwards compatibility, readmailcapfile() and lookup() still support
61+ # the old version of mailcapdict without line numbers.
62+ MAILCAPDICT_DEPRECATED = copy .deepcopy (MAILCAPDICT )
63+ for entry_list in MAILCAPDICT_DEPRECATED .values ():
64+ for entry in entry_list :
65+ entry .pop ('lineno' )
66+
5467
5568class HelperFunctionTest (unittest .TestCase ):
5669
@@ -76,12 +89,14 @@ def test_listmailcapfiles(self):
7689 def test_readmailcapfile (self ):
7790 # Test readmailcapfile() using test file. It should match MAILCAPDICT.
7891 with open (MAILCAPFILE , 'r' ) as mcf :
79- d = mailcap .readmailcapfile (mcf )
80- self .assertDictEqual (d , MAILCAPDICT )
92+ with self .assertWarns (DeprecationWarning ):
93+ d = mailcap .readmailcapfile (mcf )
94+ self .assertDictEqual (d , MAILCAPDICT_DEPRECATED )
8195
8296 def test_lookup (self ):
8397 # Test without key
84- expected = [{'view' : 'mpeg_play %s' }, {'view' : 'animate %s' }]
98+ expected = [{'view' : 'animate %s' , 'lineno' : 12 },
99+ {'view' : 'mpeg_play %s' , 'lineno' : 13 }]
85100 actual = mailcap .lookup (MAILCAPDICT , 'video/mpeg' )
86101 self .assertListEqual (expected , actual )
87102
@@ -90,10 +105,16 @@ def test_lookup(self):
90105 expected = [{'edit' : 'audiocompose %s' ,
91106 'compose' : 'audiocompose %s' ,
92107 'description' : '"An audio fragment"' ,
93- 'view' : 'showaudio %s' }]
108+ 'view' : 'showaudio %s' ,
109+ 'lineno' : 6 }]
94110 actual = mailcap .lookup (MAILCAPDICT , 'audio/basic' , key )
95111 self .assertListEqual (expected , actual )
96112
113+ # Test on user-defined dicts without line numbers
114+ expected = [{'view' : 'mpeg_play %s' }, {'view' : 'animate %s' }]
115+ actual = mailcap .lookup (MAILCAPDICT_DEPRECATED , 'video/mpeg' )
116+ self .assertListEqual (expected , actual )
117+
97118 def test_subst (self ):
98119 plist = ['id=1' , 'number=2' , 'total=3' ]
99120 # test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
@@ -152,14 +173,16 @@ def test_findmatch(self):
152173 'edit' : 'audiocompose %s' ,
153174 'compose' : 'audiocompose %s' ,
154175 'description' : '"An audio fragment"' ,
155- 'view' : 'showaudio %s'
176+ 'view' : 'showaudio %s' ,
177+ 'lineno' : 6
156178 }
157- audio_entry = {"view" : "/usr/local/bin/showaudio %t" }
158- video_entry = {'view' : 'animate %s' }
179+ audio_entry = {"view" : "/usr/local/bin/showaudio %t" , 'lineno' : 7 }
180+ video_entry = {'view' : 'animate %s' , 'lineno' : 12 }
159181 message_entry = {
160182 'composetyped' : 'extcompose %s' ,
161183 'description' : '"A reference to data stored in an external location"' , 'needsterminal' : '' ,
162- 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'
184+ 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' ,
185+ 'lineno' : 10 ,
163186 }
164187
165188 # test case: (findmatch args, findmatch keyword args, expected output)
@@ -169,7 +192,7 @@ def test_findmatch(self):
169192 cases = [
170193 ([{}, "video/mpeg" ], {}, (None , None )),
171194 ([c , "foo/bar" ], {}, (None , None )),
172- ([c , "video/mpeg" ], {}, ('mpeg_play /dev/null' , { 'view' : 'mpeg_play %s' } )),
195+ ([c , "video/mpeg" ], {}, ('animate /dev/null' , video_entry )),
173196 ([c , "audio/basic" , "edit" ], {}, ("audiocompose /dev/null" , audio_basic_entry )),
174197 ([c , "audio/basic" , "compose" ], {}, ("audiocompose /dev/null" , audio_basic_entry )),
175198 ([c , "audio/basic" , "description" ], {}, ('"An audio fragment"' , audio_basic_entry )),
0 commit comments