Beispiel #1
0
def test_colored_others_methods(caplog, uniq_raw_logger):
    # Given the logger is properly set,
    uniq_raw_logger.setLevel(logging.DEBUG)
    uniq_logger = ReportLogger(uniq_raw_logger,
                               formatter=ColoredReportFormatter())
    # When conventional methods are called on logger,
    name = uniqstr()
    uniq_logger.debug(name)
    # Then the message should be surrounded by ansi codes
    out = caplog.text
    assert ansi_regex(name).search(out)
Beispiel #2
0
def test_colored_others_methods(caplog, uniq_raw_logger):
    # Given the logger is properly set,
    uniq_raw_logger.setLevel(logging.DEBUG)
    uniq_logger = ReportLogger(uniq_raw_logger,
                               formatter=ColoredReportFormatter())
    # When conventional methods are called on logger,
    name = uniqstr()
    uniq_logger.debug(name)
    # Then the message should be surrounded by ansi codes
    out = caplog.messages[-1]
    assert ansi_regex(name).search(out)
Beispiel #3
0
def test_colored_report(tmpfolder, caplog, uniq_raw_logger):
    # Given the logger is properly set,
    uniq_raw_logger.setLevel(logging.INFO)
    uniq_logger = ReportLogger(uniq_raw_logger,
                               formatter=ColoredReportFormatter())
    # When the report method is called,
    name = uniqstr()
    uniq_logger.report('make', str(tmpfolder.join(name)))
    # Then the message should contain activity surrounded by ansi codes,
    out = caplog.messages[-1]
    assert re.search(ansi_pattern('make') + '.+' + name, out)
    # And relative paths should be used
    assert lp('/tmp') not in out
Beispiel #4
0
def test_colored_report(tmpfolder, caplog, uniq_raw_logger):
    # Given the logger is properly set,
    uniq_raw_logger.setLevel(logging.INFO)
    uniq_logger = ReportLogger(uniq_raw_logger,
                               formatter=ColoredReportFormatter())
    # When the report method is called,
    name = uniqstr()
    uniq_logger.report('make', str(tmpfolder.join(name)))
    # Then the message should contain activity surrounded by ansi codes,
    out = caplog.text
    assert re.search(ansi_pattern('make') + '.+' + name, out)
    # And relative paths should be used
    assert '/tmp' not in out
Beispiel #5
0
def reset_logger():
    yield
    raw_logger = logging.getLogger('pyscaffold.log')
    raw_logger.setLevel(logging.NOTSET)

    for h in raw_logger.handlers:
        raw_logger.removeHandler(h)
    raw_logger.handlers = []

    from pyscaffold.log import ReportLogger, logger
    new_logger = ReportLogger()

    logger.handler = new_logger.handler
    logger.formatter = new_logger.formatter

    assert len(raw_logger.handlers) == 1
    assert raw_logger.handlers[0] == logger.handler
Beispiel #6
0
def test_pass_formatter(uniq_raw_logger):
    # When the report logger is created with a formatter
    # Then that formatter should be registered.
    formatter = logging.Formatter('%(levelname)s')
    new_logger = ReportLogger(uniq_raw_logger, formatter=formatter)
    assert new_logger.formatter == formatter
Beispiel #7
0
def test_pass_handler(uniq_raw_logger):
    # When the report logger is created with a handler
    new_logger = ReportLogger(uniq_raw_logger, handler=logging.NullHandler())
    assert isinstance(new_logger.handler, logging.NullHandler)
Beispiel #8
0
def test_pass_formatter(reset_logger):
    # When the report logger is created with a handler
    formatter = logging.Formatter('%(levelname)s')
    new_logger = ReportLogger(formatter=formatter)
    assert new_logger.formatter == formatter
Beispiel #9
0
def test_reconfigure(monkeypatch, caplog, uniq_raw_logger):
    # Given an environment that supports color, and a restrictive logger
    caplog.set_level(logging.NOTSET)
    monkeypatch.setattr("pyscaffold.termui.supports_color", lambda *_: True)
    new_logger = ReportLogger(uniq_raw_logger, formatter=ReportFormatter())
    new_logger.level = logging.INFO
    # when the logger is reconfigured
    new_logger.reconfigure()
    name = uniqstr()
    # then the messages should be displayed and use colors
    new_logger.report("some1", name)
    out = caplog.messages[-1]
    assert re.search(ansi_pattern("some1") + ".+" + name, out)

    # when the logger is reconfigured with a higher level
    new_logger.reconfigure(log_level=logging.CRITICAL)
    # then the messages should not be displayed
    name = uniqstr()
    new_logger.report("some2", name)
    assert not re.search(ansi_pattern("some2") + ".+" + name, caplog.text)