2963
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2004
|
|
2 |
#see license.txt for license details
|
2966
|
3 |
__version__=''' $Id$ '''
|
2963
|
4 |
"""Tests performed on all Python source files of the ReportLab distribution.
|
|
5 |
"""
|
|
6 |
import os, sys, string, fnmatch, re
|
2966
|
7 |
import unittest
|
|
8 |
from tests.utils import makeSuiteForClasses, SecureTestCase, GlobDirectoryWalker, outputfile, printLocation, RL_HOME
|
2963
|
9 |
from reportlab.lib.utils import open_and_read, open_and_readlines
|
|
10 |
|
|
11 |
# Helper function and class.
|
|
12 |
def unique(seq):
|
|
13 |
"Remove elements from a list that occur more than once."
|
|
14 |
|
|
15 |
# Return input if it has less than 2 elements.
|
|
16 |
if len(seq) < 2:
|
|
17 |
return seq
|
|
18 |
|
|
19 |
# Make a sorted copy of the input sequence.
|
|
20 |
seq2 = seq[:]
|
|
21 |
if type(seq2) == type(''):
|
|
22 |
seq2 = map(None, seq2)
|
|
23 |
seq2.sort()
|
|
24 |
|
|
25 |
# Remove adjacent elements if they are identical.
|
|
26 |
i = 0
|
|
27 |
while i < len(seq2)-1:
|
|
28 |
elem = seq2[i]
|
|
29 |
try:
|
|
30 |
while elem == seq2[i+1]:
|
|
31 |
del seq2[i+1]
|
|
32 |
except IndexError:
|
|
33 |
pass
|
|
34 |
i = i + 1
|
|
35 |
|
|
36 |
# Try to return something of the same type as the input.
|
|
37 |
if type(seq) == type(''):
|
|
38 |
return string.join(seq2, '')
|
|
39 |
else:
|
|
40 |
return seq2
|
|
41 |
|
|
42 |
class SelfTestCase(unittest.TestCase):
|
|
43 |
"Test unique() function."
|
|
44 |
|
|
45 |
def testUnique(self):
|
|
46 |
"Test unique() function."
|
|
47 |
|
|
48 |
cases = [([], []),
|
|
49 |
([0], [0]),
|
|
50 |
([0, 1, 2], [0, 1, 2]),
|
|
51 |
([2, 1, 0], [0, 1, 2]),
|
|
52 |
([0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 2, 3]),
|
|
53 |
('abcabcabc', 'abc')
|
|
54 |
]
|
|
55 |
|
|
56 |
msg = "Failed: unique(%s) returns %s instead of %s."
|
|
57 |
for sequence, expectedOutput in cases:
|
|
58 |
output = unique(sequence)
|
|
59 |
args = (sequence, output, expectedOutput)
|
|
60 |
assert output == expectedOutput, msg % args
|
|
61 |
|
|
62 |
|
|
63 |
class AsciiFileTestCase(unittest.TestCase):
|
|
64 |
"Test if Python files are pure ASCII ones."
|
|
65 |
|
|
66 |
def testAscii(self):
|
|
67 |
"Test if Python files are pure ASCII ones."
|
|
68 |
allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')
|
|
69 |
|
|
70 |
for path in allPyFiles:
|
|
71 |
fileContent = open_and_read(path,'r')
|
|
72 |
nonAscii = filter(lambda c: ord(c)>127, fileContent)
|
|
73 |
nonAscii = unique(nonAscii)
|
|
74 |
|
|
75 |
truncPath = path[string.find(path, 'reportlab'):]
|
|
76 |
args = (truncPath, repr(map(ord, nonAscii)))
|
|
77 |
msg = "File %s contains characters: %s." % args
|
|
78 |
## if nonAscii:
|
|
79 |
## print msg
|
|
80 |
assert nonAscii == '', msg
|
|
81 |
|
|
82 |
|
|
83 |
class FilenameTestCase(unittest.TestCase):
|
|
84 |
"Test if Python files contain trailing digits."
|
|
85 |
|
|
86 |
def testTrailingDigits(self):
|
|
87 |
"Test if Python files contain trailing digits."
|
|
88 |
|
|
89 |
allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')
|
|
90 |
|
|
91 |
for path in allPyFiles:
|
|
92 |
#hack - exclude barcode extensions from this test
|
|
93 |
if string.find(path, 'barcode'):
|
|
94 |
pass
|
|
95 |
else:
|
|
96 |
basename = os.path.splitext(path)[0]
|
|
97 |
truncPath = path[string.find(path, 'reportlab'):]
|
|
98 |
msg = "Filename %s contains trailing digits." % truncPath
|
|
99 |
assert basename[-1] not in string.digits, msg
|
|
100 |
|
|
101 |
## if basename[-1] in string.digits:
|
|
102 |
## print truncPath
|
|
103 |
|
|
104 |
|
|
105 |
class FirstLineTestCase(SecureTestCase):
|
|
106 |
"Testing if objects in the ReportLab package have docstrings."
|
|
107 |
|
|
108 |
def findSuspiciousModules(self, folder, rootName):
|
|
109 |
"Get all modul paths with non-Unix-like first line."
|
|
110 |
|
|
111 |
firstLinePat = re.compile('^#!.*python.*')
|
|
112 |
|
|
113 |
paths = []
|
|
114 |
for file in GlobDirectoryWalker(folder, '*.py'):
|
|
115 |
if os.path.basename(file) == '__init__.py':
|
|
116 |
continue
|
|
117 |
firstLine = open_and_readlines(file)[0]
|
|
118 |
if not firstLinePat.match(firstLine):
|
|
119 |
paths.append(file)
|
|
120 |
|
|
121 |
return paths
|
|
122 |
|
|
123 |
def test1(self):
|
|
124 |
"Test if all Python files have a Unix-like first line."
|
|
125 |
|
|
126 |
path = outputfile("test_firstline.log")
|
|
127 |
file = open(path, 'w')
|
|
128 |
file.write('No Unix-like first line found in the files below.\n\n')
|
|
129 |
|
|
130 |
paths = self.findSuspiciousModules(RL_HOME, 'reportlab')
|
|
131 |
paths.sort()
|
|
132 |
|
|
133 |
for p in paths:
|
|
134 |
file.write("%s\n" % p)
|
|
135 |
|
|
136 |
file.close()
|
|
137 |
|
|
138 |
def makeSuite():
|
|
139 |
suite = makeSuiteForClasses(SelfTestCase, AsciiFileTestCase, FilenameTestCase)
|
|
140 |
if sys.platform[:4] != 'java':
|
|
141 |
loader = unittest.TestLoader()
|
|
142 |
suite.addTest(loader.loadTestsFromTestCase(FirstLineTestCase))
|
|
143 |
return suite
|
|
144 |
|
|
145 |
#noruntests
|
|
146 |
if __name__ == "__main__":
|
|
147 |
unittest.TextTestRunner().run(makeSuite())
|
|
148 |
printLocation()
|