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