示例#1
0
    def test_csh_format_no_color_support(self):
        colorizer = GenericColorizer(color_map={
            'bracket': ('[', ']'),
        })
        highlighter = GenericColorizer(color_map={
            'bracket': ('<', '>'),
        })
        formatter = ColorizingFormatter(fmt='%(message)s')
        no_color_stream = MagicMock()
        no_color_stream.isatty = lambda: False
        handler = ColorizingStreamHandler(
            stream=no_color_stream,
            colorizer=colorizer,
            highlighter=highlighter,
        )
        handler.setFormatter(formatter)

        record = LogRecord(
            name='my_record',
            level=DEBUG,
            pathname='my_path',
            lineno=42,
            msg='%s + %s gives %s',
            args=(
                4,
                5,
                Mark(4 + 5, color_tag='bracket'),
            ),
            exc_info=None,
        )

        self.assertEqual('4 + 5 gives <9>', handler.format(record))

        # Make sure that the colorizer attribute was removed after processing.
        self.assertFalse(hasattr(record, 'colorizer'))
示例#2
0
    def test_stream_display_py2_with_color(self, AnsiToWin32):
        stream = MagicMock()
        del stream.buffer
        colorizer = GenericColorizer(color_map={
            'error': ('<', '>'),
            'warning': ('(', ')'),
        })
        display = StreamDisplay(stream=stream, colorizer=colorizer)
        display.set_context(commands=["my command"])

        self.assertEqual(AnsiToWin32().stream, display.stream)
        self.assertEqual(stream, display.binary_stream)

        with display.command(42, "my command") as result:
            data = "DATA".encode('utf-8')
            display.command_output(42, data)
            result.returncode = 17

        self.assertEqual(
            [
                call("(43)) my command"),
                call("\t[<failed>]\n"),
                call("(43)) <Command exited with> <17>\n"),
            ],
            AnsiToWin32().stream.write.mock_calls,
        )
        self.assertEqual(
            [
                call(data),
            ],
            stream.write.mock_calls,
        )
示例#3
0
    def test_csh_format_with_context(self):
        colorizer = GenericColorizer(color_map={
            'bracket': ('[', ']'),
            'context': ('{', '}'),
        })
        highlighter = GenericColorizer(color_map={
            'bracket': ('<', '>'),
            'context': ('(', ')'),
        })
        formatter = ColorizingFormatter(fmt='%(levelname)s %(message)s')
        color_stream = MagicMock()
        color_stream.isatty = lambda: True
        handler = ColorizingStreamHandler(
            stream=color_stream,
            colorizer=colorizer,
            highlighter=highlighter,
            attributes_map={
                'message': 'context',
                'levelname': 'bracket',
            },
        )
        handler.setFormatter(formatter)

        record = LogRecord(
            name='my_record',
            level=DEBUG,
            pathname='my_path',
            lineno=42,
            msg='%s + %s gives %s',
            args=(
                4,
                5,
                Mark(4 + 5, color_tag='bracket'),
            ),
            exc_info=None,
        )

        self.assertEqual(
            '[DEBUG] {4 + 5 gives }{[9]}{}',
            handler.format(record),
        )

        # Make sure that the colorizer attribute was removed after processing.
        self.assertFalse(hasattr(record, 'colorizer'))