11import mailcap
22import os
3+ import copy
34import test .support
45import unittest
56
1314 [{'compose' : 'moviemaker %s' ,
1415 'x11-bitmap' : '"/usr/lib/Zmail/bitmaps/movie.xbm"' ,
1516 'description' : '"Movie"' ,
16- 'view' : 'movieplayer %s' }],
17+ 'view' : 'movieplayer %s' ,
18+ 'lineno' : 4 }],
1719 'application/*' :
1820 [{'copiousoutput' : '' ,
19- 'view' : 'echo "This is \\ "%t\\ " but is 50 \\ % Greek to me" \\ ; cat %s' }],
21+ 'view' : 'echo "This is \\ "%t\\ " but is 50 \\ % Greek to me" \\ ; cat %s' ,
22+ 'lineno' : 5 }],
2023 'audio/basic' :
2124 [{'edit' : 'audiocompose %s' ,
2225 'compose' : 'audiocompose %s' ,
2326 'description' : '"An audio fragment"' ,
24- 'view' : 'showaudio %s' }],
27+ 'view' : 'showaudio %s' ,
28+ 'lineno' : 6 }],
2529 'video/mpeg' :
26- [{'view' : 'mpeg_play %s' }],
30+ [{'view' : 'mpeg_play %s' , 'lineno' : 13 }],
2731 'application/postscript' :
28- [{'needsterminal' : '' , 'view' : 'ps-to-terminal %s' },
29- {'compose' : 'idraw %s' , 'view' : 'ps-to-terminal %s' }],
32+ [{'needsterminal' : '' , 'view' : 'ps-to-terminal %s' , 'lineno' : 1 },
33+ {'compose' : 'idraw %s' , 'view' : 'ps-to-terminal %s' , 'lineno' : 2 }],
3034 'application/x-dvi' :
31- [{'view' : 'xdvi %s' }],
35+ [{'view' : 'xdvi %s' , 'lineno' : 3 }],
3236 'message/external-body' :
3337 [{'composetyped' : 'extcompose %s' ,
3438 'description' : '"A reference to data stored in an external location"' ,
3539 'needsterminal' : '' ,
36- 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' }],
40+ 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' ,
41+ 'lineno' : 10 }],
3742 'text/richtext' :
3843 [{'test' : 'test "`echo %{charset} | tr \' [A-Z]\' \' [a-z]\' `" = iso-8859-8' ,
3944 'copiousoutput' : '' ,
40- 'view' : 'shownonascii iso-8859-8 -e richtext -p %s' }],
45+ 'view' : 'shownonascii iso-8859-8 -e richtext -p %s' ,
46+ 'lineno' : 11 }],
4147 'image/x-xwindowdump' :
42- [{'view' : 'display %s' }],
48+ [{'view' : 'display %s' , 'lineno' : 9 }],
4349 'audio/*' :
44- [{'view' : '/usr/local/bin/showaudio %t' }],
50+ [{'view' : '/usr/local/bin/showaudio %t' , 'lineno' : 7 }],
4551 'video/*' :
46- [{'view' : 'animate %s' }],
52+ [{'view' : 'animate %s' , 'lineno' : 12 }],
4753 'application/frame' :
48- [{'print' : '"cat %s | lp"' , 'view' : 'showframe %s' }],
54+ [{'print' : '"cat %s | lp"' , 'view' : 'showframe %s' , 'lineno' : 0 }],
4955 'image/rgb' :
50- [{'view' : 'display %s' }]
56+ [{'view' : 'display %s' , 'lineno' : 8 }]
5157}
5258
59+ # For backwards compatibility, readmailcapfile() and lookup() still support
60+ # the old version of mailcapdict without line numbers.
61+ MAILCAPDICT_DEPRECATED = copy .deepcopy (MAILCAPDICT )
62+ for entry_list in MAILCAPDICT_DEPRECATED .values ():
63+ for entry in entry_list :
64+ entry .pop ('lineno' )
65+
5366
5467class HelperFunctionTest (unittest .TestCase ):
5568
@@ -75,12 +88,14 @@ def test_listmailcapfiles(self):
7588 def test_readmailcapfile (self ):
7689 # Test readmailcapfile() using test file. It should match MAILCAPDICT.
7790 with open (MAILCAPFILE , 'r' ) as mcf :
78- d = mailcap .readmailcapfile (mcf )
79- self .assertDictEqual (d , MAILCAPDICT )
91+ with self .assertWarns (DeprecationWarning ):
92+ d = mailcap .readmailcapfile (mcf )
93+ self .assertDictEqual (d , MAILCAPDICT_DEPRECATED )
8094
8195 def test_lookup (self ):
8296 # Test without key
83- expected = [{'view' : 'mpeg_play %s' }, {'view' : 'animate %s' }]
97+ expected = [{'view' : 'animate %s' , 'lineno' : 12 },
98+ {'view' : 'mpeg_play %s' , 'lineno' : 13 }]
8499 actual = mailcap .lookup (MAILCAPDICT , 'video/mpeg' )
85100 self .assertListEqual (expected , actual )
86101
@@ -89,10 +104,16 @@ def test_lookup(self):
89104 expected = [{'edit' : 'audiocompose %s' ,
90105 'compose' : 'audiocompose %s' ,
91106 'description' : '"An audio fragment"' ,
92- 'view' : 'showaudio %s' }]
107+ 'view' : 'showaudio %s' ,
108+ 'lineno' : 6 }]
93109 actual = mailcap .lookup (MAILCAPDICT , 'audio/basic' , key )
94110 self .assertListEqual (expected , actual )
95111
112+ # Test on user-defined dicts without line numbers
113+ expected = [{'view' : 'mpeg_play %s' }, {'view' : 'animate %s' }]
114+ actual = mailcap .lookup (MAILCAPDICT_DEPRECATED , 'video/mpeg' )
115+ self .assertListEqual (expected , actual )
116+
96117 def test_subst (self ):
97118 plist = ['id=1' , 'number=2' , 'total=3' ]
98119 # test case: ([field, MIMEtype, filename, plist=[]], <expected string>)
@@ -151,14 +172,16 @@ def test_findmatch(self):
151172 'edit' : 'audiocompose %s' ,
152173 'compose' : 'audiocompose %s' ,
153174 'description' : '"An audio fragment"' ,
154- 'view' : 'showaudio %s'
175+ 'view' : 'showaudio %s' ,
176+ 'lineno' : 6
155177 }
156- audio_entry = {"view" : "/usr/local/bin/showaudio %t" }
157- video_entry = {'view' : 'animate %s' }
178+ audio_entry = {"view" : "/usr/local/bin/showaudio %t" , 'lineno' : 7 }
179+ video_entry = {'view' : 'animate %s' , 'lineno' : 12 }
158180 message_entry = {
159181 'composetyped' : 'extcompose %s' ,
160182 'description' : '"A reference to data stored in an external location"' , 'needsterminal' : '' ,
161- 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}'
183+ 'view' : 'showexternal %s %{access-type} %{name} %{site} %{directory} %{mode} %{server}' ,
184+ 'lineno' : 10 ,
162185 }
163186
164187 # test case: (findmatch args, findmatch keyword args, expected output)
@@ -168,7 +191,7 @@ def test_findmatch(self):
168191 cases = [
169192 ([{}, "video/mpeg" ], {}, (None , None )),
170193 ([c , "foo/bar" ], {}, (None , None )),
171- ([c , "video/mpeg" ], {}, ('mpeg_play /dev/null' , { 'view' : 'mpeg_play %s' } )),
194+ ([c , "video/mpeg" ], {}, ('animate /dev/null' , video_entry )),
172195 ([c , "audio/basic" , "edit" ], {}, ("audiocompose /dev/null" , audio_basic_entry )),
173196 ([c , "audio/basic" , "compose" ], {}, ("audiocompose /dev/null" , audio_basic_entry )),
174197 ([c , "audio/basic" , "description" ], {}, ('"An audio fragment"' , audio_basic_entry )),
0 commit comments