| 1 | """
|
|---|
| 2 | Tests for the mhlib module
|
|---|
| 3 | Nick Mathewson
|
|---|
| 4 | """
|
|---|
| 5 |
|
|---|
| 6 | ### BUG: This suite doesn't currently test the mime functionality of
|
|---|
| 7 | ### mhlib. It should.
|
|---|
| 8 |
|
|---|
| 9 | import unittest
|
|---|
| 10 | from test.test_support import run_unittest, TESTFN, TestSkipped
|
|---|
| 11 | import os, StringIO
|
|---|
| 12 | import sys
|
|---|
| 13 | import mhlib
|
|---|
| 14 |
|
|---|
| 15 | if (sys.platform.startswith("win") or sys.platform=="riscos" or
|
|---|
| 16 | sys.platform.startswith("atheos")):
|
|---|
| 17 | # mhlib.updateline() renames a file to the name of a file that already
|
|---|
| 18 | # exists. That causes a reasonable OS <wink> to complain in test_sequence
|
|---|
| 19 | # here, like the "OSError: [Errno 17] File exists" raised on Windows.
|
|---|
| 20 | # mhlib's listsubfolders() and listallfolders() do something with
|
|---|
| 21 | # link counts, and that causes test_listfolders() here to get back
|
|---|
| 22 | # an empty list from its call of listallfolders().
|
|---|
| 23 | # The other tests here pass on Windows.
|
|---|
| 24 | raise TestSkipped("skipped on %s -- " % sys.platform +
|
|---|
| 25 | "too many Unix assumptions")
|
|---|
| 26 |
|
|---|
| 27 | _mhroot = TESTFN+"_MH"
|
|---|
| 28 | _mhpath = os.path.join(_mhroot, "MH")
|
|---|
| 29 | _mhprofile = os.path.join(_mhroot, ".mh_profile")
|
|---|
| 30 |
|
|---|
| 31 | def normF(f):
|
|---|
| 32 | return os.path.join(*f.split('/'))
|
|---|
| 33 |
|
|---|
| 34 | def writeFile(fname, contents):
|
|---|
| 35 | dir = os.path.split(fname)[0]
|
|---|
| 36 | if dir and not os.path.exists(dir):
|
|---|
| 37 | mkdirs(dir)
|
|---|
| 38 | f = open(fname, 'w')
|
|---|
| 39 | f.write(contents)
|
|---|
| 40 | f.close()
|
|---|
| 41 |
|
|---|
| 42 | def readFile(fname):
|
|---|
| 43 | f = open(fname)
|
|---|
| 44 | r = f.read()
|
|---|
| 45 | f.close()
|
|---|
| 46 | return r
|
|---|
| 47 |
|
|---|
| 48 | def writeProfile(dict):
|
|---|
| 49 | contents = [ "%s: %s\n" % (k, v) for k, v in dict.iteritems() ]
|
|---|
| 50 | writeFile(_mhprofile, "".join(contents))
|
|---|
| 51 |
|
|---|
| 52 | def writeContext(folder):
|
|---|
| 53 | folder = normF(folder)
|
|---|
| 54 | writeFile(os.path.join(_mhpath, "context"),
|
|---|
| 55 | "Current-Folder: %s\n" % folder)
|
|---|
| 56 |
|
|---|
| 57 | def writeCurMessage(folder, cur):
|
|---|
| 58 | folder = normF(folder)
|
|---|
| 59 | writeFile(os.path.join(_mhpath, folder, ".mh_sequences"),
|
|---|
| 60 | "cur: %s\n"%cur)
|
|---|
| 61 |
|
|---|
| 62 | def writeMessage(folder, n, headers, body):
|
|---|
| 63 | folder = normF(folder)
|
|---|
| 64 | headers = "".join([ "%s: %s\n" % (k, v) for k, v in headers.iteritems() ])
|
|---|
| 65 | contents = "%s\n%s\n" % (headers,body)
|
|---|
| 66 | mkdirs(os.path.join(_mhpath, folder))
|
|---|
| 67 | writeFile(os.path.join(_mhpath, folder, str(n)), contents)
|
|---|
| 68 |
|
|---|
| 69 | def getMH():
|
|---|
| 70 | return mhlib.MH(os.path.abspath(_mhpath), _mhprofile)
|
|---|
| 71 |
|
|---|
| 72 | def sortLines(s):
|
|---|
| 73 | lines = s.split("\n")
|
|---|
| 74 | lines = [ line.strip() for line in lines if len(line) >= 2 ]
|
|---|
| 75 | lines.sort()
|
|---|
| 76 | return lines
|
|---|
| 77 |
|
|---|
| 78 | # These next 2 functions are copied from test_glob.py.
|
|---|
| 79 | def mkdirs(fname):
|
|---|
| 80 | if os.path.exists(fname) or fname == '':
|
|---|
| 81 | return
|
|---|
| 82 | base, file = os.path.split(fname)
|
|---|
| 83 | mkdirs(base)
|
|---|
| 84 | os.mkdir(fname)
|
|---|
| 85 |
|
|---|
| 86 | def deltree(fname):
|
|---|
| 87 | if not os.path.exists(fname):
|
|---|
| 88 | return
|
|---|
| 89 | for f in os.listdir(fname):
|
|---|
| 90 | fullname = os.path.join(fname, f)
|
|---|
| 91 | if os.path.isdir(fullname):
|
|---|
| 92 | deltree(fullname)
|
|---|
| 93 | else:
|
|---|
| 94 | try:
|
|---|
| 95 | os.unlink(fullname)
|
|---|
| 96 | except:
|
|---|
| 97 | pass
|
|---|
| 98 | try:
|
|---|
| 99 | os.rmdir(fname)
|
|---|
| 100 | except:
|
|---|
| 101 | pass
|
|---|
| 102 |
|
|---|
| 103 | class MhlibTests(unittest.TestCase):
|
|---|
| 104 | def setUp(self):
|
|---|
| 105 | deltree(_mhroot)
|
|---|
| 106 | mkdirs(_mhpath)
|
|---|
| 107 | writeProfile({'Path' : os.path.abspath(_mhpath),
|
|---|
| 108 | 'Editor': 'emacs',
|
|---|
| 109 | 'ignored-attribute': 'camping holiday'})
|
|---|
| 110 | # Note: These headers aren't really conformant to RFC822, but
|
|---|
| 111 | # mhlib shouldn't care about that.
|
|---|
| 112 |
|
|---|
| 113 | # An inbox with a couple of messages.
|
|---|
| 114 | writeMessage('inbox', 1,
|
|---|
| 115 | {'From': 'Mrs. Premise',
|
|---|
| 116 | 'To': 'Mrs. Conclusion',
|
|---|
| 117 | 'Date': '18 July 2001'}, "Hullo, Mrs. Conclusion!\n")
|
|---|
| 118 | writeMessage('inbox', 2,
|
|---|
| 119 | {'From': 'Mrs. Conclusion',
|
|---|
| 120 | 'To': 'Mrs. Premise',
|
|---|
| 121 | 'Date': '29 July 2001'}, "Hullo, Mrs. Premise!\n")
|
|---|
| 122 |
|
|---|
| 123 | # A folder with many messages
|
|---|
| 124 | for i in range(5, 101)+range(101, 201, 2):
|
|---|
| 125 | writeMessage('wide', i,
|
|---|
| 126 | {'From': 'nowhere', 'Subject': 'message #%s' % i},
|
|---|
| 127 | "This is message number %s\n" % i)
|
|---|
| 128 |
|
|---|
| 129 | # A deeply nested folder
|
|---|
| 130 | def deep(folder, n):
|
|---|
| 131 | writeMessage(folder, n,
|
|---|
| 132 | {'Subject': 'Message %s/%s' % (folder, n) },
|
|---|
| 133 | "This is message number %s in %s\n" % (n, folder) )
|
|---|
| 134 | deep('deep/f1', 1)
|
|---|
| 135 | deep('deep/f1', 2)
|
|---|
| 136 | deep('deep/f1', 3)
|
|---|
| 137 | deep('deep/f2', 4)
|
|---|
| 138 | deep('deep/f2', 6)
|
|---|
| 139 | deep('deep', 3)
|
|---|
| 140 | deep('deep/f2/f3', 1)
|
|---|
| 141 | deep('deep/f2/f3', 2)
|
|---|
| 142 |
|
|---|
| 143 | def tearDown(self):
|
|---|
| 144 | deltree(_mhroot)
|
|---|
| 145 |
|
|---|
| 146 | def test_basic(self):
|
|---|
| 147 | writeContext('inbox')
|
|---|
| 148 | writeCurMessage('inbox', 2)
|
|---|
| 149 | mh = getMH()
|
|---|
| 150 |
|
|---|
| 151 | eq = self.assertEquals
|
|---|
| 152 | eq(mh.getprofile('Editor'), 'emacs')
|
|---|
| 153 | eq(mh.getprofile('not-set'), None)
|
|---|
| 154 | eq(mh.getpath(), os.path.abspath(_mhpath))
|
|---|
| 155 | eq(mh.getcontext(), 'inbox')
|
|---|
| 156 |
|
|---|
|
|---|