PageRenderTime 38ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/SiRFLive/PythonStdLib/test/test_mhlib.py

https://bitbucket.org/x893/sirflive
Python | 348 lines | 327 code | 8 blank | 13 comment | 5 complexity | fd43c9dfefc8c69284e70d3604f01bbb MD5 | raw file
  1. """
  2. Tests for the mhlib module
  3. Nick Mathewson
  4. """
  5. ### BUG: This suite doesn't currently test the mime functionality of
  6. ### mhlib. It should.
  7. import unittest
  8. from test.test_support import run_unittest, TESTFN, TestSkipped
  9. import os, StringIO
  10. import sys
  11. import mhlib
  12. if (sys.platform.startswith("win") or sys.platform=="riscos" or
  13. sys.platform.startswith("atheos")):
  14. # mhlib.updateline() renames a file to the name of a file that already
  15. # exists. That causes a reasonable OS <wink> to complain in test_sequence
  16. # here, like the "OSError: [Errno 17] File exists" raised on Windows.
  17. # mhlib's listsubfolders() and listallfolders() do something with
  18. # link counts, and that causes test_listfolders() here to get back
  19. # an empty list from its call of listallfolders().
  20. # The other tests here pass on Windows.
  21. raise TestSkipped("skipped on %s -- " % sys.platform +
  22. "too many Unix assumptions")
  23. _mhroot = TESTFN+"_MH"
  24. _mhpath = os.path.join(_mhroot, "MH")
  25. _mhprofile = os.path.join(_mhroot, ".mh_profile")
  26. def normF(f):
  27. return os.path.join(*f.split('/'))
  28. def writeFile(fname, contents):
  29. dir = os.path.split(fname)[0]
  30. if dir and not os.path.exists(dir):
  31. mkdirs(dir)
  32. f = open(fname, 'w')
  33. f.write(contents)
  34. f.close()
  35. def readFile(fname):
  36. f = open(fname)
  37. r = f.read()
  38. f.close()
  39. return r
  40. def writeProfile(dict):
  41. contents = [ "%s: %s\n" % (k, v) for k, v in dict.iteritems() ]
  42. writeFile(_mhprofile, "".join(contents))
  43. def writeContext(folder):
  44. folder = normF(folder)
  45. writeFile(os.path.join(_mhpath, "context"),
  46. "Current-Folder: %s\n" % folder)
  47. def writeCurMessage(folder, cur):
  48. folder = normF(folder)
  49. writeFile(os.path.join(_mhpath, folder, ".mh_sequences"),
  50. "cur: %s\n"%cur)
  51. def writeMessage(folder, n, headers, body):
  52. folder = normF(folder)
  53. headers = "".join([ "%s: %s\n" % (k, v) for k, v in headers.iteritems() ])
  54. contents = "%s\n%s\n" % (headers,body)
  55. mkdirs(os.path.join(_mhpath, folder))
  56. writeFile(os.path.join(_mhpath, folder, str(n)), contents)
  57. def getMH():
  58. return mhlib.MH(os.path.abspath(_mhpath), _mhprofile)
  59. def sortLines(s):
  60. lines = s.split("\n")
  61. lines = [ line.strip() for line in lines if len(line) >= 2 ]
  62. lines.sort()
  63. return lines
  64. # These next 2 functions are copied from test_glob.py.
  65. def mkdirs(fname):
  66. if os.path.exists(fname) or fname == '':
  67. return
  68. base, file = os.path.split(fname)
  69. mkdirs(base)
  70. os.mkdir(fname)
  71. def deltree(fname):
  72. if not os.path.exists(fname):
  73. return
  74. for f in os.listdir(fname):
  75. fullname = os.path.join(fname, f)
  76. if os.path.isdir(fullname):
  77. deltree(fullname)
  78. else:
  79. try:
  80. os.unlink(fullname)
  81. except:
  82. pass
  83. try:
  84. os.rmdir(fname)
  85. except:
  86. pass
  87. class MhlibTests(unittest.TestCase):
  88. def setUp(self):
  89. deltree(_mhroot)
  90. mkdirs(_mhpath)
  91. writeProfile({'Path' : os.path.abspath(_mhpath),
  92. 'Editor': 'emacs',
  93. 'ignored-attribute': 'camping holiday'})
  94. # Note: These headers aren't really conformant to RFC822, but
  95. # mhlib shouldn't care about that.
  96. # An inbox with a couple of messages.
  97. writeMessage('inbox', 1,
  98. {'From': 'Mrs. Premise',
  99. 'To': 'Mrs. Conclusion',
  100. 'Date': '18 July 2001'}, "Hullo, Mrs. Conclusion!\n")
  101. writeMessage('inbox', 2,
  102. {'From': 'Mrs. Conclusion',
  103. 'To': 'Mrs. Premise',
  104. 'Date': '29 July 2001'}, "Hullo, Mrs. Premise!\n")
  105. # A folder with many messages
  106. for i in range(5, 101)+range(101, 201, 2):
  107. writeMessage('wide', i,
  108. {'From': 'nowhere', 'Subject': 'message #%s' % i},
  109. "This is message number %s\n" % i)
  110. # A deeply nested folder
  111. def deep(folder, n):
  112. writeMessage(folder, n,
  113. {'Subject': 'Message %s/%s' % (folder, n) },
  114. "This is message number %s in %s\n" % (n, folder) )
  115. deep('deep/f1', 1)
  116. deep('deep/f1', 2)
  117. deep('deep/f1', 3)
  118. deep('deep/f2', 4)
  119. deep('deep/f2', 6)
  120. deep('deep', 3)
  121. deep('deep/f2/f3', 1)
  122. deep('deep/f2/f3', 2)
  123. def tearDown(self):
  124. deltree(_mhroot)
  125. def test_basic(self):
  126. writeContext('inbox')
  127. writeCurMessage('inbox', 2)
  128. mh = getMH()
  129. eq = self.assertEquals
  130. eq(mh.getprofile('Editor'), 'emacs')
  131. eq(mh.getprofile('not-set'), None)
  132. eq(mh.getpath(), os.path.abspath(_mhpath))
  133. eq(mh.getcontext(), 'inbox')
  134. mh.setcontext('wide')
  135. eq(mh.getcontext(), 'wide')
  136. eq(readFile(os.path.join(_mhpath, 'context')),
  137. "Current-Folder: wide\n")
  138. mh.setcontext('inbox')
  139. inbox = mh.openfolder('inbox')
  140. eq(inbox.getfullname(),
  141. os.path.join(os.path.abspath(_mhpath), 'inbox'))
  142. eq(inbox.getsequencesfilename(),
  143. os.path.join(os.path.abspath(_mhpath), 'inbox', '.mh_sequences'))
  144. eq(inbox.getmessagefilename(1),
  145. os.path.join(os.path.abspath(_mhpath), 'inbox', '1'))
  146. def test_listfolders(self):
  147. mh = getMH()
  148. eq = self.assertEquals
  149. folders = mh.listfolders()
  150. folders.sort()
  151. eq(folders, ['deep', 'inbox', 'wide'])
  152. folders = mh.listallfolders()
  153. folders.sort()
  154. tfolders = map(normF, ['deep', 'deep/f1', 'deep/f2', 'deep/f2/f3',
  155. 'inbox', 'wide'])
  156. tfolders.sort()
  157. eq(folders, tfolders)
  158. folders = mh.listsubfolders('deep')
  159. folders.sort()
  160. eq(folders, map(normF, ['deep/f1', 'deep/f2']))
  161. folders = mh.listallsubfolders('deep')
  162. folders.sort()
  163. eq(folders, map(normF, ['deep/f1', 'deep/f2', 'deep/f2/f3']))
  164. eq(mh.listsubfolders(normF('deep/f2')), [normF('deep/f2/f3')])
  165. eq(mh.listsubfolders('inbox'), [])
  166. eq(mh.listallsubfolders('inbox'), [])
  167. def test_sequence(self):
  168. mh = getMH()
  169. eq = self.assertEquals
  170. writeCurMessage('wide', 55)
  171. f = mh.openfolder('wide')
  172. all = f.listmessages()
  173. eq(all, range(5, 101)+range(101, 201, 2))
  174. eq(f.getcurrent(), 55)
  175. f.setcurrent(99)
  176. eq(readFile(os.path.join(_mhpath, 'wide', '.mh_sequences')),
  177. 'cur: 99\n')
  178. def seqeq(seq, val):
  179. eq(f.parsesequence(seq), val)
  180. seqeq('5-55', range(5, 56))
  181. seqeq('90-108', range(90, 101)+range(101, 109, 2))
  182. seqeq('90-108', range(90, 101)+range(101, 109, 2))
  183. seqeq('10:10', range(10, 20))
  184. seqeq('10:+10', range(10, 20))
  185. seqeq('101:10', range(101, 121, 2))
  186. seqeq('cur', [99])
  187. seqeq('.', [99])
  188. seqeq('prev', [98])
  189. seqeq('next', [100])
  190. seqeq('cur:-3', [97, 98, 99])
  191. seqeq('first-cur', range(5, 100))
  192. seqeq('150-last', range(151, 201, 2))
  193. seqeq('prev-next', [98, 99, 100])
  194. lowprimes = [5, 7, 11, 13, 17, 19, 23, 29]
  195. lowcompos = [x for x in range(5, 31) if not x in lowprimes ]
  196. f.putsequences({'cur': [5],
  197. 'lowprime': lowprimes,
  198. 'lowcompos': lowcompos})
  199. seqs = readFile(os.path.join(_mhpath, 'wide', '.mh_sequences'))
  200. seqs = sortLines(seqs)
  201. eq(seqs, ["cur: 5",
  202. "lowcompos: 6 8-10 12 14-16 18 20-22 24-28 30",
  203. "lowprime: 5 7 11 13 17 19 23 29"])
  204. seqeq('lowprime', lowprimes)
  205. seqeq('lowprime:1', [5])
  206. seqeq('lowprime:2', [5, 7])
  207. seqeq('lowprime:-2', [23, 29])
  208. ## Not supported
  209. #seqeq('lowprime:first', [5])
  210. #seqeq('lowprime:last', [29])
  211. #seqeq('lowprime:prev', [29])
  212. #seqeq('lowprime:next', [29])
  213. def test_modify(self):
  214. mh = getMH()
  215. eq = self.assertEquals
  216. mh.makefolder("dummy1")
  217. self.assert_("dummy1" in mh.listfolders())
  218. path = os.path.join(_mhpath, "dummy1")
  219. self.assert_(os.path.exists(path))
  220. f = mh.openfolder('dummy1')
  221. def create(n):
  222. msg = "From: foo\nSubject: %s\n\nDummy Message %s\n" % (n,n)
  223. f.createmessage(n, StringIO.StringIO(msg))
  224. create(7)
  225. create(8)
  226. create(9)
  227. eq(readFile(f.getmessagefilename(9)),
  228. "From: foo\nSubject: 9\n\nDummy Message 9\n")
  229. eq(f.listmessages(), [7, 8, 9])
  230. files = os.listdir(path)
  231. files.sort()
  232. eq(files, ['7', '8', '9'])
  233. f.removemessages(['7', '8'])
  234. files = os.listdir(path)
  235. files.sort()
  236. eq(files, [',7', ',8', '9'])
  237. eq(f.listmessages(), [9])
  238. create(10)
  239. create(11)
  240. create(12)
  241. mh.makefolder("dummy2")
  242. f2 = mh.openfolder("dummy2")
  243. eq(f2.listmessages(), [])
  244. f.movemessage(10, f2, 3)
  245. f.movemessage(11, f2, 5)
  246. eq(f.listmessages(), [9, 12])
  247. eq(f2.listmessages(), [3, 5])
  248. eq(readFile(f2.getmessagefilename(3)),
  249. "From: foo\nSubject: 10\n\nDummy Message 10\n")
  250. f.copymessage(9, f2, 4)
  251. eq(f.listmessages(), [9, 12])
  252. eq(readFile(f2.getmessagefilename(4)),
  253. "From: foo\nSubject: 9\n\nDummy Message 9\n")
  254. f.refilemessages([9, 12], f2)
  255. eq(f.listmessages(), [])
  256. eq(f2.listmessages(), [3, 4, 5, 6, 7])
  257. eq(readFile(f2.getmessagefilename(7)),
  258. "From: foo\nSubject: 12\n\nDummy Message 12\n")
  259. # XXX This should check that _copysequences does the right thing.
  260. mh.deletefolder('dummy1')
  261. mh.deletefolder('dummy2')
  262. self.assert_('dummy1' not in mh.listfolders())
  263. self.assert_(not os.path.exists(path))
  264. def test_read(self):
  265. mh = getMH()
  266. eq = self.assertEquals
  267. f = mh.openfolder('inbox')
  268. msg = f.openmessage(1)
  269. # Check some basic stuff from rfc822
  270. eq(msg.getheader('From'), "Mrs. Premise")
  271. eq(msg.getheader('To'), "Mrs. Conclusion")
  272. # Okay, we have the right message. Let's check the stuff from
  273. # mhlib.
  274. lines = sortLines(msg.getheadertext())
  275. eq(lines, ["Date: 18 July 2001",
  276. "From: Mrs. Premise",
  277. "To: Mrs. Conclusion"])
  278. lines = sortLines(msg.getheadertext(lambda h: len(h)==4))
  279. eq(lines, ["Date: 18 July 2001",
  280. "From: Mrs. Premise"])
  281. eq(msg.getbodytext(), "Hullo, Mrs. Conclusion!\n\n")
  282. eq(msg.getbodytext(0), "Hullo, Mrs. Conclusion!\n\n")
  283. # XXXX there should be a better way to reclaim the file handle
  284. msg.fp.close()
  285. del msg
  286. def test_main():
  287. run_unittest(MhlibTests)
  288. if __name__ == "__main__":
  289. test_main()