-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_misc.py
More file actions
317 lines (252 loc) · 13.9 KB
/
test_misc.py
File metadata and controls
317 lines (252 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# misc tests
import os
import pytest
import re
import shutil
import sys
import time
import unittest
import roundup.anypy.cmp_
from roundup import install_util
from roundup.anypy.strings import StringIO # define StringIO
from roundup.cgi import cgitb
from roundup.cgi.accept_language import parse
from roundup.support import PrioList, Progress, TruthDict
class AcceptLanguageTest(unittest.TestCase):
def testParse(self):
self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"),
['da', 'en_gb', 'en'])
self.assertEqual(parse("da, en-gb;q=0.7, en;q=0.8"),
['da', 'en', 'en_gb'])
self.assertEqual(parse("en;q=0.2, fr;q=1"), ['fr', 'en'])
self.assertEqual(parse("zn; q = 0.2 ,pt-br;q =1"), ['pt_br', 'zn'])
self.assertEqual(parse("pt-br;q =1, zn; q = 0.2"), ['pt_br', 'zn'])
self.assertEqual(parse("pt-br,zn;q= 0.1, en-US;q=0.5"),
['pt_br', 'en_US', 'zn'])
# verify that items with q=1.0 are in same output order as input
self.assertEqual(parse("pt-br,en-US; q=0.5, zn;q= 1.0" ),
['pt_br', 'zn', 'en_US'])
self.assertEqual(parse("zn;q=1.0;q= 1.0,pt-br,en-US; q=0.5" ),
['zn', 'pt_br', 'en_US'])
self.assertEqual(parse("es-AR"), ['es_AR'])
self.assertEqual(parse("es-es-cat"), ['es_es_cat'])
self.assertEqual(parse(""), [])
self.assertEqual(parse(None),[])
self.assertEqual(parse(" "), [])
self.assertEqual(parse("en,"), ['en'])
class CmpTest(unittest.TestCase):
def testCmp(self):
roundup.anypy.cmp_._test()
class InstallUtils(unittest.TestCase):
@classmethod
def setUpClass(cls):
try:
os.mkdir("__install_util")
except FileExistsError as e:
shutil.rmtree('__install_util')
os.mkdir("__install_util") # if this fails exit with exception
@classmethod
def tearDownClass(cls):
shutil.rmtree('__install_util')
def test_install_util(self):
#use install_util.py for the test
testfile = install_util.test.__code__.co_filename
install_util.test(testfile)
def test_copyDigestedFile(self):
#use install_util.py for the test
testfile = install_util.test.__code__.co_filename
install_util.copyDigestedFile(testfile, "__install_util" )
digested_file = '__install_util/install_util.py'
self.assertTrue(os.path.isfile(digested_file))
with open(digested_file) as df:
contents = df.readlines()
last_line = contents[-1]
self.assertIn("#SHA:", last_line)
def test_Check_digest_file_without_digest(self):
testfile = install_util.test.__code__.co_filename
self.assertEqual(install_util.checkDigest(testfile), 0)
class PrioListTest(unittest.TestCase):
def testPL(self):
start_data = [(3, 33), (1, -2), (2, 10)]
pl = PrioList(key=lambda x: x[1])
for i in start_data:
pl.append(i)
l = [x for x in pl]
self.assertEqual(l, [(1, -2), (2, 10), (3, 33)])
pl = PrioList()
for i in start_data:
pl.append(i)
l = [x for x in pl]
self.assertEqual(l, [(1, -2), (2, 10), (3, 33)])
class ProgressTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, capsys):
self._capsys = capsys
def testProgress(self):
for x in Progress("5 Items@2 sec:", [1,2,3,4,5]):
time.sleep(2)
captured = self._capsys.readouterr()
split_capture = captured.out.split('\r')
# lines padded to 75 characters test should be long enough to
# get an ETA printed at 100%, 80% and 60% hopefully this
# doesn't become a flakey test on different hardware.
self.assertIn("5 Items@2 sec: 0%".ljust(75),
split_capture)
self.assertIn("5 Items@2 sec: 60% (ETA 00:00:02)".ljust(75),
split_capture)
self.assertIn("5 Items@2 sec: 100% (ETA 00:00:00)".ljust(75),
split_capture)
print(captured.err)
class TruthDictTest(unittest.TestCase):
def testTD(self):
td = TruthDict([])
# empty TruthDict always returns True.
self.assertTrue(td['a'])
self.assertTrue(td['z'])
self.assertTrue(td[''])
self.assertTrue(td[None])
td = TruthDict(['a', 'b', 'c'])
self.assertTrue(td['a'])
self.assertFalse(td['z'])
class VersionCheck(unittest.TestCase):
def test_Version_Check(self):
# test for valid versions
from roundup.version_check import VERSION_NEEDED
self.assertEqual((3, 7), VERSION_NEEDED)
del(sys.modules['roundup.version_check'])
# fake an invalid version
real_ver = sys.version_info
sys.version_info = (2, 1)
# exit is called on failure, but that breaks testing so
# just return and discard the exit code.
real_exit = sys.exit
sys.exit = lambda code: code
# error case uses print(), capture and check
capturedOutput = StringIO()
sys.stdout = capturedOutput
from roundup.version_check import VERSION_NEEDED
sys.stdout = sys.__stdout__
self.assertIn("Roundup requires Python 3.7", capturedOutput.getvalue())
# reset to valid values for future tests
sys.exit = real_exit
sys.version_info = real_ver
class CgiTbCheck(unittest.TestCase):
def test_NiceDict(self):
d = cgitb.niceDict(" ", { "two": "three", "four": "five" })
expected = (
"<tr><td><strong>four</strong></td><td>'five'</td></tr>\n"
"<tr><td><strong>two</strong></td><td>'three'</td></tr>"
)
self.assertEqual(expected, d)
def test_breaker(self):
b = cgitb.breaker()
expected = ('<body bgcolor="white"><font color="white" size="-5">'
' > </font> </table></table></table></table></table>')
self.assertEqual(expected, b)
def test_pt_html(self):
""" templating error """
try:
f = 5
d = a + 4
except Exception:
p = cgitb.pt_html(context=2)
expected2 = """<h1>Templating Error</h1>
<p><b><type 'exceptions.NameError'></b>: global name 'a' is not defined</p>
<p class="help">Debugging information follows</p>
<ol>
</ol>
<table style="font-size: 80%; color: gray">
<tr><th class="header" align="left">Full traceback:</th></tr>
<tr><td><pre>Traceback (most recent call last):
File "XX/test/test_misc.py", line XX, in test_pt_html
d = a + 4
NameError: global name 'a' is not defined
</pre></td></tr>
</table>
<p> </p>"""
expected3 = """<h1>Templating Error</h1>
<p><b><class 'NameError'></b>: name 'a' is not defined</p>
<p class="help">Debugging information follows</p>
<ol>
</ol>
<table style="font-size: 80%; color: gray">
<tr><th class="header" align="left">Full traceback:</th></tr>
<tr><td><pre>Traceback (most recent call last):
File "XX/test/test_misc.py", line XX, in test_pt_html
d = a + 4
NameError: name 'a' is not defined
</pre></td></tr>
</table>
<p> </p>"""
expected3_11 = """<h1>Templating Error</h1>
<p><b><class 'NameError'></b>: name 'a' is not defined</p>
<p class="help">Debugging information follows</p>
<ol>
</ol>
<table style="font-size: 80%; color: gray">
<tr><th class="header" align="left">Full traceback:</th></tr>
<tr><td><pre>Traceback (most recent call last):
File "XX/test/test_misc.py", line XX, in test_pt_html
d = a + 4
^
NameError: name 'a' is not defined
</pre></td></tr>
</table>
<p> </p>"""
# allow file directory prefix and line number to change
p = re.sub(r'\\',r'/', p) # support windows \ => /
# [A-Z]?:? optional drive spec on windows
p = re.sub(r'(File ")[A-Z]?:?/.*/(test/test_misc.py",)', r'\1XX/\2', p)
p = re.sub(r'(", line )\d*,', r'\1XX,', p)
print(p)
if sys.version_info > (3, 11, 0):
self.assertEqual(expected3_11, p)
elif sys.version_info > (3, 0, 0):
self.assertEqual(expected3, p)
else:
self.assertEqual(expected2, p)
def test_html(self):
""" templating error """
# enabiling this will cause the test to fail as the variable
# is included in the live output but not in expected.
# self.maxDiff = None
try:
f = 5
d = a + 4
except Exception:
h = cgitb.html(context=2)
expected2 = """
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#777777">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><font size=+1><strong>NameError</strong>: global name 'a' is not defined</font></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial">Python XX</font></td></tr></table>
<p>A problem occurred while running a Python script. Here is the sequence of function calls leading up to the error, with the most recent (innermost) call first. The exception attributes are:<br><tt><small> </small> </tt>__class__ = <type 'exceptions.NameError'> <br><tt><small> </small> </tt>__delattr__ = <method-wrapper '__delattr__' of exceptions.NameError object> <br><tt><small> </small> </tt>__dict__ = {} <br><tt><small> </small> </tt>__doc__ = 'Name not found globally.' <br><tt><small> </small> </tt>__format__ = <built-in method __format__ of exceptions.NameError object> <br><tt><small> </small> </tt>__getattribute__ = <method-wrapper '__getattribute__' of exceptions.NameError object> <br><tt><small> </small> </tt>__getitem__ = <method-wrapper '__getitem__' of exceptions.NameError object> <br><tt><small> </small> </tt>__getslice__ = <method-wrapper '__getslice__' of exceptions.NameError object> <br><tt><small> </small> </tt>__hash__ = <method-wrapper '__hash__' of exceptions.NameError object> <br><tt><small> </small> </tt>__init__ = <method-wrapper '__init__' of exceptions.NameError object> <br><tt><small> </small> </tt>__new__ = <built-in method __new__ of type object> <br><tt><small> </small> </tt>__reduce__ = <built-in method __reduce__ of exceptions.NameError object> <br><tt><small> </small> </tt>__reduce_ex__ = <built-in method __reduce_ex__ of exceptions.NameError object> <br><tt><small> </small> </tt>__repr__ = <method-wrapper '__repr__' of exceptions.NameError object> <br><tt><small> </small> </tt>__setattr__ = <method-wrapper '__setattr__' of exceptions.NameError object> <br><tt><small> </small> </tt>__setstate__ = <built-in method __setstate__ of exceptions.NameError object> <br><tt><small> </small> </tt>__sizeof__ = <built-in method __sizeof__ of exceptions.NameError object> <br><tt><small> </small> </tt>__str__ = <method-wrapper '__str__' of exceptions.NameError object> <br><tt><small> </small> </tt>__subclasshook__ = <built-in method __subclasshook__ of type object> <br><tt><small> </small> </tt>__unicode__ = <built-in method __unicode__ of exceptions.NameError object> <br><tt><small> </small> </tt>args = ("global name 'a' is not defined",) <br><tt><small> </small> </tt>message = "global name 'a' is not defined"<p>
<table width="100%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0>
<tr><td><a href="file:XX/test/test_misc.py">XX/test/test_misc.py</a> in <strong>test_html</strong>(self=<test.test_misc.CgiTbCheck testMethod=test_html>)</td></tr></table>
<tt><small><font color="#909090"> XX</font></small> f = 5<br>
</tt>
<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0>
<tr><td><tt><small><font color="#909090"> XX</font></small> d = a + 4<br>
</tt></td></tr></table>
<tt><small> </small> </tt><small><font color="#909090"><strong>d</strong> = <em>undefined</em>, <em>global</em> <strong>a</strong> = <em>undefined</em></font></small><br><p> </p>"""
expected1_3 ="""NameError"""
expected2_3 =""": name \'a\' is not defined"""
expected3_3 ="""built-in method __dir__ of NameError object>"""
# strip file path prefix from href and text
# /home/user/develop/roundup/test/test_misc.py in test_html
h = re.sub(r'(file:)/.*/(test/test_misc.py")', r'\1XX/\2', h)
h = re.sub(r'(/test_misc.py">)/.*/(test/test_misc.py</a>)',
r'\1XX/\2', h)
# replace code line numbers with XX
h = re.sub(r'( )\d*(</font>)', r'\1XX\2', h)
# normalize out python version/path
h = re.sub(r'(Python )[\d.]*<br>[^<]*/python[23]?', r'\1XX', h)
print(h)
if sys.version_info > (3, 0, 0):
self.assertIn(expected1_3, h)
self.assertIn(expected2_3, h)
self.assertIn(expected3_3, h)
else:
self.assertEqual(expected2, h)