testutils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. import reportlab
  4. reportlab._rl_testing=True
  5. del reportlab
  6. __version__='3.3.0'
  7. __doc__="""Provides support for the test suite.
  8. The test suite as a whole, and individual tests, need to share
  9. certain support functions. We have to put these in here so they
  10. can always be imported, and so that individual tests need to import
  11. nothing more than "reportlab.whatever..."
  12. """
  13. import sys, os, fnmatch, re
  14. try:
  15. from configparser import ConfigParser
  16. except ImportError:
  17. from ConfigParser import ConfigParser
  18. import unittest
  19. from reportlab.lib.utils import isCompactDistro, __rl_loader__, rl_isdir, asUnicode
  20. # Helper functions.
  21. def isWritable(D):
  22. try:
  23. fn = '00DELETE.ME'
  24. f = open(fn, 'w')
  25. f.write('test of writability - can be deleted')
  26. f.close()
  27. if os.path.isfile(fn):
  28. os.remove(fn)
  29. return 1
  30. except:
  31. return 0
  32. _OUTDIR = None
  33. RL_HOME = None
  34. testsFolder = None
  35. def setOutDir(name):
  36. """Is it a writable file system distro being invoked within
  37. test directory? If so, can write test output here. If not,
  38. it had better go in a temp directory. Only do this once per
  39. process"""
  40. global _OUTDIR, RL_HOME, testsFolder
  41. if _OUTDIR: return _OUTDIR
  42. D = [d[9:] for d in sys.argv if d.startswith('--outdir=')]
  43. if not D:
  44. D = os.environ.get('RL_TEST_OUTDIR','')
  45. if D: D=[D]
  46. if D:
  47. _OUTDIR = D[-1]
  48. try:
  49. os.makedirs(_OUTDIR)
  50. except:
  51. pass
  52. for d in D:
  53. if d in sys.argv:
  54. sys.argv.remove(d)
  55. else:
  56. assert name=='__main__',"setOutDir should only be called in the main script"
  57. scriptDir=os.path.dirname(sys.argv[0])
  58. if not scriptDir: scriptDir=os.getcwd()
  59. _OUTDIR = scriptDir
  60. if not isWritable(_OUTDIR):
  61. _OUTDIR = get_rl_tempdir('reportlab_test')
  62. import reportlab
  63. RL_HOME=reportlab.__path__[0]
  64. if not os.path.isabs(RL_HOME): RL_HOME=os.path.normpath(os.path.abspath(RL_HOME))
  65. topDir = os.path.dirname(RL_HOME)
  66. testsFolder = os.path.join(topDir,'tests')
  67. if not os.path.isdir(testsFolder):
  68. testsFolder = os.path.join(os.path.dirname(topDir),'tests')
  69. if not os.path.isdir(testsFolder):
  70. if name=='__main__':
  71. scriptDir=os.path.dirname(sys.argv[0])
  72. if not scriptDir: scriptDir=os.getcwd()
  73. testsFolder = os.path.abspath(scriptDir)
  74. else:
  75. testsFolder = None
  76. if testsFolder:
  77. sys.path.insert(0,os.path.dirname(testsFolder))
  78. return _OUTDIR
  79. _mockumap = (
  80. None if os.environ.get('OFFLINE_MOCK','1')!='1'
  81. else'http://www.reportlab.com/rsrc/encryption.gif',
  82. )
  83. def mockUrlRead(name):
  84. if name in _mockumap:
  85. with open(os.path.join(testsFolder,os.path.basename(name)),'rb') as f:
  86. return f.read()
  87. else:
  88. from urllib.request import urlopen
  89. return urlopen(name).read()
  90. def outputfile(fn):
  91. """This works out where to write test output. If running
  92. code in a locked down file system, this will be a
  93. temp directory; otherwise, the output of 'test_foo.py' will
  94. normally be a file called 'test_foo.pdf', next door.
  95. """
  96. D = setOutDir(__name__)
  97. if fn: D = os.path.join(D,fn)
  98. return D
  99. def printLocation(depth=1):
  100. if sys._getframe(depth).f_locals.get('__name__')=='__main__':
  101. outDir = outputfile('')
  102. if outDir!=_OUTDIR:
  103. print('Logs and output files written to folder "%s"' % outDir)
  104. def makeSuiteForClasses(*classes):
  105. "Return a test suite with tests loaded from provided classes."
  106. suite = unittest.TestSuite()
  107. loader = unittest.TestLoader()
  108. for C in classes:
  109. suite.addTest(loader.loadTestsFromTestCase(C))
  110. return suite
  111. def getCVSEntries(folder, files=1, folders=0):
  112. """Returns a list of filenames as listed in the CVS/Entries file.
  113. 'folder' is the folder that should contain the CVS subfolder.
  114. If there is no such subfolder an empty list is returned.
  115. 'files' is a boolean; 1 and 0 means to return files or not.
  116. 'folders' is a boolean; 1 and 0 means to return folders or not.
  117. """
  118. join = os.path.join
  119. # If CVS subfolder doesn't exist return empty list.
  120. try:
  121. f = open(join(folder, 'CVS', 'Entries'))
  122. except IOError:
  123. return []
  124. # Return names of files and/or folders in CVS/Entries files.
  125. allEntries = []
  126. for line in f.readlines():
  127. if folders and line[0] == 'D' \
  128. or files and line[0] != 'D':
  129. entry = line.split('/')[1]
  130. if entry:
  131. allEntries.append(join(folder, entry))
  132. return allEntries
  133. # Still experimental class extending ConfigParser's behaviour.
  134. class ExtConfigParser(ConfigParser):
  135. "A slightly extended version to return lists of strings."
  136. pat = re.compile(r'\s*\[.*\]\s*')
  137. def getstringlist(self, section, option):
  138. "Coerce option to a list of strings or return unchanged if that fails."
  139. value = ConfigParser.get(self, section, option)
  140. # This seems to allow for newlines inside values
  141. # of the config file, but be careful!!
  142. val = value.replace('\n', '')
  143. if self.pat.match(val):
  144. return eval(val,{__builtins__:None})
  145. else:
  146. return value
  147. # This class as suggested by /F with an additional hook
  148. # to be able to filter filenames.
  149. class GlobDirectoryWalker:
  150. "A forward iterator that traverses files in a directory tree."
  151. def __init__(self, directory, pattern='*'):
  152. self.index = 0
  153. self.pattern = pattern
  154. directory.replace('/',os.sep)
  155. if os.path.isdir(directory):
  156. self.stack = [directory]
  157. self.files = []
  158. else:
  159. if not isCompactDistro() or not __rl_loader__ or not rl_isdir(directory):
  160. raise ValueError('"%s" is not a directory' % directory)
  161. self.directory = directory[len(__rl_loader__.archive)+len(os.sep):]
  162. pfx = self.directory+os.sep
  163. n = len(pfx)
  164. self.files = list(map(lambda x, n=n: x[n:],list(filter(lambda x,pfx=pfx: x.startswith(pfx),list(__rl_loader__._files.keys())))))
  165. self.files.sort()
  166. self.stack = []
  167. def __getitem__(self, index):
  168. while 1:
  169. try:
  170. file = self.files[self.index]
  171. self.index = self.index + 1
  172. except IndexError:
  173. # pop next directory from stack
  174. self.directory = self.stack.pop()
  175. self.files = os.listdir(self.directory)
  176. # now call the hook
  177. self.files = self.filterFiles(self.directory, self.files)
  178. self.index = 0
  179. else:
  180. # got a filename
  181. fullname = os.path.join(self.directory, file)
  182. if os.path.isdir(fullname) and not os.path.islink(fullname):
  183. self.stack.append(fullname)
  184. if fnmatch.fnmatch(file, self.pattern):
  185. return fullname
  186. def filterFiles(self, folder, files):
  187. "Filter hook, overwrite in subclasses as needed."
  188. return files
  189. class RestrictedGlobDirectoryWalker(GlobDirectoryWalker):
  190. "An restricted directory tree iterator."
  191. def __init__(self, directory, pattern='*', ignore=None):
  192. GlobDirectoryWalker.__init__(self, directory, pattern)
  193. if ignore == None:
  194. ignore = []
  195. ip = [].append
  196. if isinstance(ignore,(tuple,list)):
  197. for p in ignore:
  198. ip(p)
  199. elif isinstance(ignore,str):
  200. ip(ignore)
  201. self.ignorePatterns = ([_.replace('/',os.sep) for _ in ip.__self__] if os.sep != '/'
  202. else ip.__self__)
  203. def filterFiles(self, folder, files):
  204. "Filters all items from files matching patterns to ignore."
  205. fnm = fnmatch.fnmatch
  206. indicesToDelete = []
  207. for i,f in enumerate(files):
  208. for p in self.ignorePatterns:
  209. if fnm(f, p) or fnm(os.path.join(folder,f),p):
  210. indicesToDelete.append(i)
  211. indicesToDelete.reverse()
  212. for i in indicesToDelete:
  213. del files[i]
  214. return files
  215. class CVSGlobDirectoryWalker(GlobDirectoryWalker):
  216. "An directory tree iterator that checks for CVS data."
  217. def filterFiles(self, folder, files):
  218. """Filters files not listed in CVS subfolder.
  219. This will look in the CVS subfolder of 'folder' for
  220. a file named 'Entries' and filter all elements from
  221. the 'files' list that are not listed in 'Entries'.
  222. """
  223. join = os.path.join
  224. cvsFiles = getCVSEntries(folder)
  225. if cvsFiles:
  226. indicesToDelete = []
  227. for i in range(len(files)):
  228. f = files[i]
  229. if join(folder, f) not in cvsFiles:
  230. indicesToDelete.append(i)
  231. indicesToDelete.reverse()
  232. for i in indicesToDelete:
  233. del files[i]
  234. return files
  235. # An experimental untested base class with additional 'security'.
  236. class SecureTestCase(unittest.TestCase):
  237. """Secure testing base class with additional pre- and postconditions.
  238. We try to ensure that each test leaves the environment it has
  239. found unchanged after the test is performed, successful or not.
  240. Currently we restore sys.path and the working directory, but more
  241. of this could be added easily, like removing temporary files or
  242. similar things.
  243. Use this as a base class replacing unittest.TestCase and call
  244. these methods in subclassed versions before doing your own
  245. business!
  246. """
  247. def setUp(self):
  248. "Remember sys.path and current working directory."
  249. self._initialPath = sys.path[:]
  250. self._initialWorkDir = os.getcwd()
  251. def tearDown(self):
  252. "Restore previous sys.path and working directory."
  253. sys.path = self._initialPath
  254. os.chdir(self._initialWorkDir)
  255. class NearTestCase(unittest.TestCase):
  256. def assertNear(a,b,accuracy=1e-5):
  257. if isinstance(a,(float,int)):
  258. if abs(a-b)>accuracy:
  259. raise AssertionError("%s not near %s" % (a, b))
  260. else:
  261. for ae,be in zip(a,b):
  262. if abs(ae-be)>accuracy:
  263. raise AssertionError("%s not near %s" % (a, b))
  264. assertNear = staticmethod(assertNear)
  265. class ScriptThatMakesFileTest(unittest.TestCase):
  266. """Runs a Python script at OS level, expecting it to produce a file.
  267. It CDs to the working directory to run the script."""
  268. def __init__(self, scriptDir, scriptName, outFileName, verbose=0):
  269. self.scriptDir = scriptDir
  270. self.scriptName = scriptName
  271. self.outFileName = outFileName
  272. self.verbose = verbose
  273. # normally, each instance is told which method to run)
  274. unittest.TestCase.__init__(self)
  275. def setUp(self):
  276. self.cwd = os.getcwd()
  277. global testsFolder
  278. scriptDir=self.scriptDir
  279. if not os.path.isabs(scriptDir):
  280. scriptDir=os.path.join(testsFolder,scriptDir)
  281. os.chdir(scriptDir)
  282. assert os.path.isfile(self.scriptName), "Script %s not found!" % self.scriptName
  283. if os.path.isfile(self.outFileName):
  284. os.remove(self.outFileName)
  285. def tearDown(self):
  286. os.chdir(self.cwd)
  287. def runTest(self):
  288. fmt = sys.platform=='win32' and '"%s" %s' or '%s %s'
  289. import subprocess
  290. out = subprocess.check_output((sys.executable,self.scriptName))
  291. #p = os.popen(fmt % (sys.executable,self.scriptName),'r')
  292. #out = p.read()
  293. if self.verbose:
  294. print(out)
  295. #status = p.close()
  296. assert os.path.isfile(self.outFileName), "File %s not created!" % self.outFileName
  297. def equalStrings(a,b,enc='utf8'):
  298. return a==b if type(a)==type(b) else asUnicode(a,enc)==asUnicode(b,enc)
  299. def eqCheck(r,x):
  300. if r!=x:
  301. print('Strings unequal\nexp: %s\ngot: %s' % (ascii(x),ascii(r)))