test_cgitb.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from test.support.os_helper import temp_dir
  2. from test.support.script_helper import assert_python_failure
  3. from test.support.warnings_helper import import_deprecated
  4. import unittest
  5. import sys
  6. cgitb = import_deprecated("cgitb")
  7. class TestCgitb(unittest.TestCase):
  8. def test_fonts(self):
  9. text = "Hello Robbie!"
  10. self.assertEqual(cgitb.small(text), "<small>{}</small>".format(text))
  11. self.assertEqual(cgitb.strong(text), "<strong>{}</strong>".format(text))
  12. self.assertEqual(cgitb.grey(text),
  13. '<font color="#909090">{}</font>'.format(text))
  14. def test_blanks(self):
  15. self.assertEqual(cgitb.small(""), "")
  16. self.assertEqual(cgitb.strong(""), "")
  17. self.assertEqual(cgitb.grey(""), "")
  18. def test_html(self):
  19. try:
  20. raise ValueError("Hello World")
  21. except ValueError as err:
  22. # If the html was templated we could do a bit more here.
  23. # At least check that we get details on what we just raised.
  24. html = cgitb.html(sys.exc_info())
  25. self.assertIn("ValueError", html)
  26. self.assertIn(str(err), html)
  27. def test_text(self):
  28. try:
  29. raise ValueError("Hello World")
  30. except ValueError:
  31. text = cgitb.text(sys.exc_info())
  32. self.assertIn("ValueError", text)
  33. self.assertIn("Hello World", text)
  34. def test_syshook_no_logdir_default_format(self):
  35. with temp_dir() as tracedir:
  36. rc, out, err = assert_python_failure(
  37. '-c',
  38. ('import cgitb; cgitb.enable(logdir=%s); '
  39. 'raise ValueError("Hello World")') % repr(tracedir),
  40. PYTHONIOENCODING='utf-8')
  41. out = out.decode()
  42. self.assertIn("ValueError", out)
  43. self.assertIn("Hello World", out)
  44. self.assertIn("<strong>&lt;module&gt;</strong>", out)
  45. # By default we emit HTML markup.
  46. self.assertIn('<p>', out)
  47. self.assertIn('</p>', out)
  48. def test_syshook_no_logdir_text_format(self):
  49. # Issue 12890: we were emitting the <p> tag in text mode.
  50. with temp_dir() as tracedir:
  51. rc, out, err = assert_python_failure(
  52. '-c',
  53. ('import cgitb; cgitb.enable(format="text", logdir=%s); '
  54. 'raise ValueError("Hello World")') % repr(tracedir),
  55. PYTHONIOENCODING='utf-8')
  56. out = out.decode()
  57. self.assertIn("ValueError", out)
  58. self.assertIn("Hello World", out)
  59. self.assertNotIn('<p>', out)
  60. self.assertNotIn('</p>', out)
  61. if __name__ == "__main__":
  62. unittest.main()