blob: 590ffdea1122aca4086f41b2958c71f0160ae168 [file] [log] [blame]
Hai Shia089d212020-07-06 17:15:08 +08001from test.support.os_helper import temp_dir
Berker Peksagce643912015-05-06 06:33:17 +03002from test.support.script_helper import assert_python_failure
Brian Curtin6a4ffd72011-07-05 19:14:16 -05003import unittest
4import sys
Brian Curtin6a4ffd72011-07-05 19:14:16 -05005import cgitb
6
7class TestCgitb(unittest.TestCase):
Brian Curtin692e26b2011-07-05 19:16:37 -05008
Brian Curtin6a4ffd72011-07-05 19:14:16 -05009 def test_fonts(self):
10 text = "Hello Robbie!"
11 self.assertEqual(cgitb.small(text), "<small>{}</small>".format(text))
12 self.assertEqual(cgitb.strong(text), "<strong>{}</strong>".format(text))
13 self.assertEqual(cgitb.grey(text),
14 '<font color="#909090">{}</font>'.format(text))
Brian Curtin692e26b2011-07-05 19:16:37 -050015
Brian Curtin6a4ffd72011-07-05 19:14:16 -050016 def test_blanks(self):
17 self.assertEqual(cgitb.small(""), "")
18 self.assertEqual(cgitb.strong(""), "")
19 self.assertEqual(cgitb.grey(""), "")
Brian Curtin692e26b2011-07-05 19:16:37 -050020
Brian Curtin6a4ffd72011-07-05 19:14:16 -050021 def test_html(self):
22 try:
23 raise ValueError("Hello World")
24 except ValueError as err:
25 # If the html was templated we could do a bit more here.
26 # At least check that we get details on what we just raised.
27 html = cgitb.html(sys.exc_info())
28 self.assertIn("ValueError", html)
29 self.assertIn(str(err), html)
30
31 def test_text(self):
32 try:
33 raise ValueError("Hello World")
Pablo Galindo293dd232019-11-19 21:34:03 +000034 except ValueError:
Brian Curtin6a4ffd72011-07-05 19:14:16 -050035 text = cgitb.text(sys.exc_info())
36 self.assertIn("ValueError", text)
37 self.assertIn("Hello World", text)
Brian Curtin692e26b2011-07-05 19:16:37 -050038
R David Murrayc4b8e052012-10-27 14:55:25 -040039 def test_syshook_no_logdir_default_format(self):
40 with temp_dir() as tracedir:
41 rc, out, err = assert_python_failure(
42 '-c',
R David Murraycc4bacf2012-10-30 20:20:09 -040043 ('import cgitb; cgitb.enable(logdir=%s); '
Serhiy Storchaka700cfa82020-06-25 17:56:31 +030044 'raise ValueError("Hello World")') % repr(tracedir),
45 PYTHONIOENCODING='utf-8')
46 out = out.decode()
Brian Curtin6a4ffd72011-07-05 19:14:16 -050047 self.assertIn("ValueError", out)
48 self.assertIn("Hello World", out)
sblondon7d68bfa2018-04-29 19:48:33 +020049 self.assertIn("<strong>&lt;module&gt;</strong>", out)
R David Murrayc4b8e052012-10-27 14:55:25 -040050 # 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',
R David Murraycc4bacf2012-10-30 20:20:09 -040059 ('import cgitb; cgitb.enable(format="text", logdir=%s); '
Serhiy Storchaka700cfa82020-06-25 17:56:31 +030060 'raise ValueError("Hello World")') % repr(tracedir),
61 PYTHONIOENCODING='utf-8')
62 out = out.decode()
R David Murrayc4b8e052012-10-27 14:55:25 -040063 self.assertIn("ValueError", out)
64 self.assertIn("Hello World", out)
65 self.assertNotIn('<p>', out)
66 self.assertNotIn('</p>', out)
Brian Curtin6a4ffd72011-07-05 19:14:16 -050067
68
Brian Curtin6a4ffd72011-07-05 19:14:16 -050069if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050070 unittest.main()