Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 1 | from test.support import run_unittest |
R David Murray | c4b8e05 | 2012-10-27 14:55:25 -0400 | [diff] [blame] | 2 | from test.script_helper import assert_python_failure, temp_dir |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 3 | import unittest |
| 4 | import sys |
| 5 | import subprocess |
R David Murray | c4b8e05 | 2012-10-27 14:55:25 -0400 | [diff] [blame] | 6 | import tempfile |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 7 | import cgitb |
| 8 | |
| 9 | class TestCgitb(unittest.TestCase): |
Brian Curtin | 692e26b | 2011-07-05 19:16:37 -0500 | [diff] [blame] | 10 | |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 11 | def test_fonts(self): |
| 12 | text = "Hello Robbie!" |
| 13 | self.assertEqual(cgitb.small(text), "<small>{}</small>".format(text)) |
| 14 | self.assertEqual(cgitb.strong(text), "<strong>{}</strong>".format(text)) |
| 15 | self.assertEqual(cgitb.grey(text), |
| 16 | '<font color="#909090">{}</font>'.format(text)) |
Brian Curtin | 692e26b | 2011-07-05 19:16:37 -0500 | [diff] [blame] | 17 | |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 18 | def test_blanks(self): |
| 19 | self.assertEqual(cgitb.small(""), "") |
| 20 | self.assertEqual(cgitb.strong(""), "") |
| 21 | self.assertEqual(cgitb.grey(""), "") |
Brian Curtin | 692e26b | 2011-07-05 19:16:37 -0500 | [diff] [blame] | 22 | |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 23 | def test_html(self): |
| 24 | try: |
| 25 | raise ValueError("Hello World") |
| 26 | except ValueError as err: |
| 27 | # If the html was templated we could do a bit more here. |
| 28 | # At least check that we get details on what we just raised. |
| 29 | html = cgitb.html(sys.exc_info()) |
| 30 | self.assertIn("ValueError", html) |
| 31 | self.assertIn(str(err), html) |
| 32 | |
| 33 | def test_text(self): |
| 34 | try: |
| 35 | raise ValueError("Hello World") |
| 36 | except ValueError as err: |
| 37 | text = cgitb.text(sys.exc_info()) |
| 38 | self.assertIn("ValueError", text) |
| 39 | self.assertIn("Hello World", text) |
Brian Curtin | 692e26b | 2011-07-05 19:16:37 -0500 | [diff] [blame] | 40 | |
R David Murray | c4b8e05 | 2012-10-27 14:55:25 -0400 | [diff] [blame] | 41 | def test_syshook_no_logdir_default_format(self): |
| 42 | with temp_dir() as tracedir: |
| 43 | rc, out, err = assert_python_failure( |
| 44 | '-c', |
| 45 | ('import cgitb; cgitb.enable(logdir="%s"); ' |
| 46 | 'raise ValueError("Hello World")') % tracedir) |
| 47 | out = out.decode(sys.getfilesystemencoding()) |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 48 | self.assertIn("ValueError", out) |
| 49 | self.assertIn("Hello World", out) |
R David Murray | c4b8e05 | 2012-10-27 14:55:25 -0400 | [diff] [blame] | 50 | # By default we emit HTML markup. |
| 51 | self.assertIn('<p>', out) |
| 52 | self.assertIn('</p>', out) |
| 53 | |
| 54 | def test_syshook_no_logdir_text_format(self): |
| 55 | # Issue 12890: we were emitting the <p> tag in text mode. |
| 56 | with temp_dir() as tracedir: |
| 57 | rc, out, err = assert_python_failure( |
| 58 | '-c', |
| 59 | ('import cgitb; cgitb.enable(format="text", logdir="%s"); ' |
| 60 | 'raise ValueError("Hello World")') % tracedir) |
| 61 | out = out.decode(sys.getfilesystemencoding()) |
| 62 | self.assertIn("ValueError", out) |
| 63 | self.assertIn("Hello World", out) |
| 64 | self.assertNotIn('<p>', out) |
| 65 | self.assertNotIn('</p>', out) |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def test_main(): |
| 69 | run_unittest(TestCgitb) |
Brian Curtin | 692e26b | 2011-07-05 19:16:37 -0500 | [diff] [blame] | 70 | |
Brian Curtin | 6a4ffd7 | 2011-07-05 19:14:16 -0500 | [diff] [blame] | 71 | if __name__ == "__main__": |
| 72 | test_main() |