Mercurial > p > roundup > code
diff test/test_misc.py @ 6983:3129d73e8535
flake8 plus tests for cgitb.py
flake8 fixes for cgitb.py.
Also set up tests for functions in unit.
Need tests before making some other changes in cgitb.py.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 14 Sep 2022 22:35:23 -0400 |
| parents | 5986ddd0d2e7 |
| children | 83720f0aee06 |
line wrap: on
line diff
--- a/test/test_misc.py Wed Sep 14 20:36:34 2022 -0400 +++ b/test/test_misc.py Wed Sep 14 22:35:23 2022 -0400 @@ -1,11 +1,16 @@ # misc tests -import unittest -import roundup.anypy.cmp_ +import re import sys +import unittest + +import roundup.anypy.cmp_ + from roundup.anypy.strings import StringIO # define StringIO +from roundup.cgi import cgitb from roundup.cgi.accept_language import parse + class AcceptLanguageTest(unittest.TestCase): def testParse(self): self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"), @@ -63,3 +68,138 @@ 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>""" + + # allow file directory prefix and line number to change + p = re.sub(r'(File ")/.*/(test/test_misc.py",)', r'\1XX/\2', p) + p = re.sub(r'(", line )\d*,', r'\1XX,', p) + + print(p) + + if 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 outpu 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 2.7.17<br>/usr/bin/python2</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>""" + + expected3 = """ +<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>: name 'a' is not defined</font></font></td +><td align=right valign=bottom +><font color="#ffffff" face="helvetica, arial">Python 3.6.9<br>/usr/bin/python3</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>__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> +<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> <br> +</tt> + + +<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0> +<tr><td><tt><small><font color="#909090"> XX</font></small> try:<br> +</tt></td></tr></table> +<p> </p>""" + + # 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) + + print(h) + + if sys.version_info > (3, 0, 0): + self.assertEqual(expected3, h) + else: + self.assertEqual(expected2, h)
