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