tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_capture.py (1190B)


      1 import sys
      2 import unittest
      3 
      4 import mozunit
      5 from mozlog import capture, structuredlog
      6 from test_structured import TestHandler
      7 
      8 
      9 class TestCaptureIO(unittest.TestCase):
     10    """Tests expected logging output of CaptureIO"""
     11 
     12    def setUp(self):
     13        self.logger = structuredlog.StructuredLogger("test")
     14        self.handler = TestHandler()
     15        self.logger.add_handler(self.handler)
     16 
     17    def test_captureio_log(self):
     18        """
     19        CaptureIO takes in two arguments. The second argument must
     20        be truthy in order for the code to run. Hence, the string
     21        "capture_stdio" has been used in this test case.
     22        """
     23        with capture.CaptureIO(self.logger, "capture_stdio"):
     24            print("message 1")
     25            sys.stdout.write("message 2")
     26            sys.stderr.write("message 3")
     27            sys.stdout.write("\xff")
     28        log = self.handler.items
     29        messages = [item["message"] for item in log]
     30        self.assertIn("STDOUT: message 1", messages)
     31        self.assertIn("STDOUT: message 2", messages)
     32        self.assertIn("STDERR: message 3", messages)
     33        self.assertIn("STDOUT: \xff", messages)
     34 
     35 
     36 if __name__ == "__main__":
     37    mozunit.main()