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