|
1 | 1 | # misc tests |
2 | 2 |
|
| 3 | +import re |
| 4 | +import sys |
3 | 5 | import unittest |
| 6 | + |
4 | 7 | import roundup.anypy.cmp_ |
5 | | -import sys |
| 8 | + |
6 | 9 | from roundup.anypy.strings import StringIO # define StringIO |
| 10 | +from roundup.cgi import cgitb |
7 | 11 | from roundup.cgi.accept_language import parse |
8 | 12 |
|
| 13 | + |
9 | 14 | class AcceptLanguageTest(unittest.TestCase): |
10 | 15 | def testParse(self): |
11 | 16 | self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"), |
@@ -63,3 +68,138 @@ def test_Version_Check(self): |
63 | 68 | sys.version_info = real_ver |
64 | 69 |
|
65 | 70 |
|
| 71 | +class CgiTbCheck(unittest.TestCase): |
| 72 | + |
| 73 | + def test_NiceDict(self): |
| 74 | + d = cgitb.niceDict(" ", { "two": "three", "four": "five" }) |
| 75 | + |
| 76 | + expected = ( |
| 77 | + "<tr><td><strong>four</strong></td><td>'five'</td></tr>\n" |
| 78 | + "<tr><td><strong>two</strong></td><td>'three'</td></tr>" |
| 79 | + ) |
| 80 | + |
| 81 | + self.assertEqual(expected, d) |
| 82 | + |
| 83 | + def test_breaker(self): |
| 84 | + b = cgitb.breaker() |
| 85 | + |
| 86 | + expected = ('<body bgcolor="white"><font color="white" size="-5">' |
| 87 | + ' > </font> </table></table></table></table></table>') |
| 88 | + |
| 89 | + self.assertEqual(expected, b) |
| 90 | + |
| 91 | + def test_pt_html(self): |
| 92 | + """ templating error """ |
| 93 | + try: |
| 94 | + f = 5 |
| 95 | + d = a + 4 |
| 96 | + except Exception: |
| 97 | + p = cgitb.pt_html(context=2) |
| 98 | + |
| 99 | + expected2 = """<h1>Templating Error</h1> |
| 100 | +<p><b><type 'exceptions.NameError'></b>: global name 'a' is not defined</p> |
| 101 | +<p class="help">Debugging information follows</p> |
| 102 | +<ol> |
| 103 | +
|
| 104 | +</ol> |
| 105 | +<table style="font-size: 80%; color: gray"> |
| 106 | + <tr><th class="header" align="left">Full traceback:</th></tr> |
| 107 | + <tr><td><pre>Traceback (most recent call last): |
| 108 | + File "XX/test/test_misc.py", line XX, in test_pt_html |
| 109 | + d = a + 4 |
| 110 | +NameError: global name 'a' is not defined |
| 111 | +</pre></td></tr> |
| 112 | +</table> |
| 113 | +<p> </p>""" |
| 114 | + |
| 115 | + expected3 = """<h1>Templating Error</h1> |
| 116 | +<p><b><class 'NameError'></b>: name 'a' is not defined</p> |
| 117 | +<p class="help">Debugging information follows</p> |
| 118 | +<ol> |
| 119 | +
|
| 120 | +</ol> |
| 121 | +<table style="font-size: 80%; color: gray"> |
| 122 | + <tr><th class="header" align="left">Full traceback:</th></tr> |
| 123 | + <tr><td><pre>Traceback (most recent call last): |
| 124 | + File "XX/test/test_misc.py", line XX, in test_pt_html |
| 125 | + d = a + 4 |
| 126 | +NameError: name 'a' is not defined |
| 127 | +</pre></td></tr> |
| 128 | +</table> |
| 129 | +<p> </p>""" |
| 130 | + |
| 131 | + # allow file directory prefix and line number to change |
| 132 | + p = re.sub(r'(File ")/.*/(test/test_misc.py",)', r'\1XX/\2', p) |
| 133 | + p = re.sub(r'(", line )\d*,', r'\1XX,', p) |
| 134 | + |
| 135 | + print(p) |
| 136 | + |
| 137 | + if sys.version_info > (3, 0, 0): |
| 138 | + self.assertEqual(expected3, p) |
| 139 | + else: |
| 140 | + self.assertEqual(expected2, p) |
| 141 | + |
| 142 | + def test_html(self): |
| 143 | + """ templating error """ |
| 144 | + # enabiling this will cause the test to fail as the variable |
| 145 | + # is included in the live outpu but not in expected. |
| 146 | + # self.maxDiff = None |
| 147 | + |
| 148 | + try: |
| 149 | + f = 5 |
| 150 | + d = a + 4 |
| 151 | + except Exception: |
| 152 | + h = cgitb.html(context=2) |
| 153 | + |
| 154 | + expected2 = """ |
| 155 | +<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 156 | +<tr bgcolor="#777777"> |
| 157 | +<td valign=bottom> <br> |
| 158 | +<font color="#ffffff" face="helvetica, arial"> <br><font size=+1><strong>NameError</strong>: global name 'a' is not defined</font></font></td |
| 159 | +><td align=right valign=bottom |
| 160 | +><font color="#ffffff" face="helvetica, arial">Python 2.7.17<br>/usr/bin/python2</font></td></tr></table> |
| 161 | + <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> |
| 162 | +<table width="100%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0> |
| 163 | +<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> |
| 164 | +<tt><small><font color="#909090"> XX</font></small> f = 5<br> |
| 165 | +</tt> |
| 166 | +
|
| 167 | +
|
| 168 | +<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0> |
| 169 | +<tr><td><tt><small><font color="#909090"> XX</font></small> d = a + 4<br> |
| 170 | +</tt></td></tr></table> |
| 171 | +<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>""" |
| 172 | + |
| 173 | + expected3 = """ |
| 174 | +<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 175 | +<tr bgcolor="#777777"> |
| 176 | +<td valign=bottom> <br> |
| 177 | +<font color="#ffffff" face="helvetica, arial"> <br><font size=+1><strong>NameError</strong>: name 'a' is not defined</font></font></td |
| 178 | +><td align=right valign=bottom |
| 179 | +><font color="#ffffff" face="helvetica, arial">Python 3.6.9<br>/usr/bin/python3</font></td></tr></table> |
| 180 | + <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>__cause__ = None <br><tt><small> </small> </tt>__class__ = <class 'NameError'> <br><tt><small> </small> </tt>__context__ = None <br><tt><small> </small> </tt>__delattr__ = <method-wrapper '__delattr__' of NameError object> <br><tt><small> </small> </tt>__dict__ = {} <br><tt><small> </small> </tt>__dir__ = <built-in method __dir__ of NameError object> <br><tt><small> </small> </tt>__doc__ = 'Name not found globally.' <br><tt><small> </small> </tt>__eq__ = <method-wrapper '__eq__' of NameError object> <br><tt><small> </small> </tt>__format__ = <built-in method __format__ of NameError object> <br><tt><small> </small> </tt>__ge__ = <method-wrapper '__ge__' of NameError object> <br><tt><small> </small> </tt>__getattribute__ = <method-wrapper '__getattribute__' of NameError object> <br><tt><small> </small> </tt>__gt__ = <method-wrapper '__gt__' of NameError object> <br><tt><small> </small> </tt>__hash__ = <method-wrapper '__hash__' of NameError object> <br><tt><small> </small> </tt>__init__ = <method-wrapper '__init__' of NameError object> <br><tt><small> </small> </tt>__init_subclass__ = <built-in method __init_subclass__ of type object> <br><tt><small> </small> </tt>__le__ = <method-wrapper '__le__' of NameError object> <br><tt><small> </small> </tt>__lt__ = <method-wrapper '__lt__' of NameError object> <br><tt><small> </small> </tt>__ne__ = <method-wrapper '__ne__' of 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 NameError object> <br><tt><small> </small> </tt>__reduce_ex__ = <built-in method __reduce_ex__ of NameError object> <br><tt><small> </small> </tt>__repr__ = <method-wrapper '__repr__' of NameError object> <br><tt><small> </small> </tt>__setattr__ = <method-wrapper '__setattr__' of NameError object> <br><tt><small> </small> </tt>__setstate__ = <built-in method __setstate__ of NameError object> <br><tt><small> </small> </tt>__sizeof__ = <built-in method __sizeof__ of NameError object> <br><tt><small> </small> </tt>__str__ = <method-wrapper '__str__' of NameError object> <br><tt><small> </small> </tt>__subclasshook__ = <built-in method __subclasshook__ of type object> <br><tt><small> </small> </tt>__suppress_context__ = False <br><tt><small> </small> </tt>__traceback__ = <traceback object> <br><tt><small> </small> </tt>args = ("name 'a' is not defined",) <br><tt><small> </small> </tt>with_traceback = <built-in method with_traceback of NameError object><p> |
| 181 | +<table width="100%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0> |
| 182 | +<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> |
| 183 | +<tt><small><font color="#909090"> XX</font></small> <br> |
| 184 | +</tt> |
| 185 | +
|
| 186 | +
|
| 187 | +<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0> |
| 188 | +<tr><td><tt><small><font color="#909090"> XX</font></small> try:<br> |
| 189 | +</tt></td></tr></table> |
| 190 | +<p> </p>""" |
| 191 | + |
| 192 | + # strip file path prefix from href and text |
| 193 | + # /home/user/develop/roundup/test/test_misc.py in test_html |
| 194 | + h = re.sub(r'(file:)/.*/(test/test_misc.py")', r'\1XX/\2', h) |
| 195 | + h = re.sub(r'(/test_misc.py">)/.*/(test/test_misc.py</a>)', |
| 196 | + r'\1XX/\2', h) |
| 197 | + # replace code line numbers with XX |
| 198 | + h = re.sub(r'( )\d*(</font>)', r'\1XX\2', h) |
| 199 | + |
| 200 | + print(h) |
| 201 | + |
| 202 | + if sys.version_info > (3, 0, 0): |
| 203 | + self.assertEqual(expected3, h) |
| 204 | + else: |
| 205 | + self.assertEqual(expected2, h) |
0 commit comments