gecko-dev/testing/mozbase/mozlog/tests/test_capture.py
Nikki S c1eaf010be mozlog: move the capture io class from web-platform/tests to mozlog (bug 1021926) r=jgraham
The ability to capture the parent process' stdio is suggested to be a useful feature
to move from web-platform/tests into mozlog. To do so, I have created a new capture.py
file within mozlog/mozlog. This includes the CaptureIO class and its dependencies,
including the LoggingWrapper and LogThread classes. These have been removed from their
original location, to avoid duplication, and the files depending on them updated
accordingly.

It would be useful to add unittests testing the CaptureIO enter and exit methods, and
the original_stdio, logging_queue and logging_thread properties. I have begun such a
file with test_capture.py in mozlog/tests. This is a work in progress, however I may
need some guidance, please, in regards to creating appropriate mock data to assert.

Differential Revision: https://phabricator.services.mozilla.com/D22166

--HG--
extra : moz-landing-system : lando
2019-03-12 10:21:12 +00:00

39 lines
1.2 KiB
Python

from __future__ import absolute_import, print_function
import sys
import unittest
import mozunit
from test_structured import TestHandler
from mozlog import capture, structuredlog
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(u"STDOUT: \xff", messages)
if __name__ == "__main__":
mozunit.main()