PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/org.modelsphere.sms/lib/jython-2.2.1/Lib/test/test_mhlib.py

https://github.com/DarioGT/OMS-PluginXML
Python | 353 lines | 330 code | 10 blank | 13 comment | 5 complexity | b547144d675c97586e87dba43d307590 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, underlying_system
  9. import os, StringIO
  10. import sys
  11. import mhlib
  12. if (underlying_system.startswith("win") or underlying_system=="riscos" or
  13. underlying_system.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 -- " % underlying_system +
  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. #link counts from os.stat always return 0 in jython, which causes
  153. #lisallfolders and listsubfolders to return empty lists.
  154. if not sys.platform.startswith("java"):
  155. folders = mh.listallfolders()
  156. folders.sort()
  157. tfolders = map(normF, ['deep', 'deep/f1', 'deep/f2', 'deep/f2/f3',
  158. 'inbox', 'wide'])
  159. tfolders.sort()
  160. eq(folders, tfolders)
  161. folders = mh.listsubfolders('deep')
  162. folders.sort()
  163. eq(folders, map(normF, ['deep/f1', 'deep/f2']))
  164. folders = mh.listallsubfolders('deep')
  165. folders.sort()
  166. eq(folders, map(normF, ['deep/f1', 'deep/f2', 'deep/f2/f3']))
  167. eq(mh.listsubfolders(normF('deep/f2')), [normF('deep/f2/f3')])
  168. eq(mh.listsubfolders('inbox'), [])
  169. eq(mh.listallsubfolders('inbox'), [])
  170. def test_sequence(self):
  171. mh = getMH()
  172. eq = self.assertEquals
  173. writeCurMessage('wide', 55)
  174. f = mh.openfolder('wide')
  175. all = f.listmessages()
  176. eq(all, range(5, 101)+range(101, 201, 2))
  177. eq(f.getcurrent(), 55)
  178. f.setcurrent(99)
  179. eq(readFile(os.path.join(_mhpath, 'wide', '.mh_sequences')),
  180. 'cur: 99\n')
  181. def seqeq(seq, val):
  182. eq(f.parsesequence(seq), val)
  183. seqeq('5-55', range(5, 56))
  184. seqeq('90-108', range(90, 101)+range(101, 109, 2))
  185. seqeq('90-108', range(90, 101)+range(101, 109, 2))
  186. seqeq('10:10', range(10, 20))
  187. seqeq('10:+10', range(10, 20))
  188. seqeq('101:10', range(101, 121, 2))
  189. seqeq('cur', [99])
  190. seqeq('.', [99])
  191. seqeq('prev', [98])
  192. seqeq('next', [100])
  193. seqeq('cur:-3', [97, 98, 99])
  194. seqeq('first-cur', range(5, 100))
  195. seqeq('150-last', range(151, 201, 2))
  196. seqeq('prev-next', [98, 99, 100])
  197. lowprimes = [5, 7, 11, 13, 17, 19, 23, 29]
  198. lowcompos = [x for x in range(5, 31) if not x in lowprimes ]
  199. f.putsequences({'cur': [5],
  200. 'lowprime': lowprimes,
  201. 'lowcompos': lowcompos})
  202. seqs = readFile(os.path.join(_mhpath, 'wide', '.mh_sequences'))
  203. seqs = sortLines(seqs)
  204. eq(seqs, ["cur: 5",
  205. "lowcompos: 6 8-10 12 14-16 18 20-22 24-28 30",
  206. "lowprime: 5 7 11 13 17 19 23 29"])
  207. seqeq('lowprime', lowprimes)
  208. seqeq('lowprime:1', [5])
  209. seqeq('lowprime:2', [5, 7])
  210. seqeq('lowprime:-2', [23, 29])
  211. ## Not supported
  212. #seqeq('lowprime:first', [5])
  213. #seqeq('lowprime:last', [29])
  214. #seqeq('lowprime:prev', [29])
  215. #seqeq('lowprime:next', [29])
  216. def test_modify(self):
  217. mh = getMH()
  218. eq = self.assertEquals
  219. mh.makefolder("dummy1")
  220. self.assert_("dummy1" in mh.listfolders())
  221. path = os.path.join(_mhpath, "dummy1")
  222. self.assert_(os.path.exists(path))
  223. f = mh.openfolder('dummy1')
  224. def create(n):
  225. msg = "From: foo\nSubject: %s\n\nDummy Message %s\n" % (n,n)
  226. f.createmessage(n, StringIO.StringIO(msg))
  227. create(7)
  228. create(8)
  229. create(9)
  230. eq(readFile(f.getmessagefilename(9)),
  231. "From: foo\nSubject: 9\n\nDummy Message 9\n")
  232. eq(f.listmessages(), [7, 8, 9])
  233. files = os.listdir(path)
  234. files.sort()
  235. eq(files, ['7', '8', '9'])
  236. f.removemessages(['7', '8'])
  237. files = os.listdir(path)
  238. files.sort()
  239. eq(files, [',7', ',8', '9'])
  240. eq(f.listmessages(), [9])
  241. create(10)
  242. create(11)
  243. create(12)
  244. mh.makefolder("dummy2")
  245. f2 = mh.openfolder("dummy2")
  246. eq(f2.listmessages(), [])
  247. f.movemessage(10, f2, 3)
  248. f.movemessage(11, f2, 5)
  249. eq(f.listmessages(), [9, 12])
  250. eq(f2.listmessages(), [3, 5])
  251. eq(readFile(f2.getmessagefilename(3)),
  252. "From: foo\nSubject: 10\n\nDummy Message 10\n")
  253. f.copymessage(9, f2, 4)
  254. eq(f.listmessages(), [9, 12])
  255. eq(readFile(f2.getmessagefilename(4)),
  256. "From: foo\nSubject: 9\n\nDummy Message 9\n")
  257. f.refilemessages([9, 12], f2)
  258. eq(f.listmessages(), [])
  259. eq(f2.listmessages(), [3, 4, 5, 6, 7])
  260. eq(readFile(f2.getmessagefilename(7)),
  261. "From: foo\nSubject: 12\n\nDummy Message 12\n")
  262. # XXX This should check that _copysequences does the right thing.
  263. mh.deletefolder('dummy1')
  264. mh.deletefolder('dummy2')
  265. self.assert_('dummy1' not in mh.listfolders())
  266. self.assert_(not os.path.exists(path))
  267. def test_read(self):
  268. mh = getMH()
  269. eq = self.assertEquals
  270. f = mh.openfolder('inbox')
  271. msg = f.openmessage(1)
  272. # Check some basic stuff from rfc822
  273. eq(msg.getheader('From'), "Mrs. Premise")
  274. eq(msg.getheader('To'), "Mrs. Conclusion")
  275. # Okay, we have the right message. Let's check the stuff from
  276. # mhlib.
  277. lines = sortLines(msg.getheadertext())
  278. eq(lines, ["Date: 18 July 2001",
  279. "From: Mrs. Premise",
  280. "To: Mrs. Conclusion"])
  281. lines = sortLines(msg.getheadertext(lambda h: len(h)==4))
  282. eq(lines, ["Date: 18 July 2001",
  283. "From: Mrs. Premise"])
  284. eq(msg.getbodytext(), "Hullo, Mrs. Conclusion!\n\n")
  285. eq(msg.getbodytext(0), "Hullo, Mrs. Conclusion!\n\n")
  286. # XXXX there should be a better way to reclaim the file handle
  287. msg.fp.close()
  288. del msg
  289. def test_main():
  290. run_unittest(MhlibTests)
  291. if __name__ == "__main__":
  292. test_main()