def test_console_direct():
    extension = ConsoleDirectEventHandler()

    with patch('sys.stdout') as stdout:
        event = StdoutLine(b'bytes line')
        extension((event, None))
        assert stdout.buffer.write.call_count == 1
        event = StdoutLine('string line')
        extension((event, None))
        assert stdout.write.call_count == 1

        stdout.buffer.write.reset_mock()
        stdout.write.reset_mock()
        extension(('unknown', None))
        assert stdout.buffer.write.call_count == 0
        assert stdout.write.call_count == 0

    with patch('sys.stderr') as stderr:
        event = StderrLine(b'bytes line')
        extension((event, None))
        assert stderr.buffer.write.call_count == 1
        event = StderrLine('string line')
        extension((event, None))
        assert stderr.write.call_count == 1

        stderr.buffer.write.reset_mock()
        stderr.write.reset_mock()
        extension(('unknown', None))
        assert stderr.buffer.write.call_count == 0
        assert stderr.write.call_count == 0
Пример #2
0
    def print(self, msg, *, file=None):  # noqa: A003
        """
        Post a message event into the event queue.

        :param msg: The message
        :param file: The sink to write the message to. An argument of `None` or
          `sys.stdout` posts a `StdoutLine` event, `sys.stderr` posts a
          `StderrLine` event.
        """
        if file is None or file == sys.stdout:
            data = StdoutLine(msg + '\n')
        elif file == sys.stderr:
            data = StderrLine(msg + '\n')
        else:
            assert False, 'Unknown file object: ' + str(file)
        self.context.put_event_into_queue(data)
Пример #3
0
 def stdout_callback(line):
     context.put_event_into_queue(StdoutLine(line))