summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozlog/tests/test_capture.py
blob: 6e14e155aa03317fb6aee7ab0c80fe08eea36d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
import unittest

import mozunit
from mozlog import capture, structuredlog
from test_structured import TestHandler


class TestCaptureIO(unittest.TestCase):
    """Tests expected logging output of CaptureIO"""

    def setUp(self):
        self.logger = structuredlog.StructuredLogger("test")
        self.handler = TestHandler()
        self.logger.add_handler(self.handler)

    def test_captureio_log(self):
        """
        CaptureIO takes in two arguments. The second argument must
        be truthy in order for the code to run. Hence, the string
        "capture_stdio" has been used in this test case.
        """
        with capture.CaptureIO(self.logger, "capture_stdio"):
            print("message 1")
            sys.stdout.write("message 2")
            sys.stderr.write("message 3")
            sys.stdout.write("\xff")
        log = self.handler.items
        messages = [item["message"] for item in log]
        self.assertIn("STDOUT: message 1", messages)
        self.assertIn("STDOUT: message 2", messages)
        self.assertIn("STDERR: message 3", messages)
        self.assertIn("STDOUT: \xff", messages)


if __name__ == "__main__":
    mozunit.main()