Issues (31)

tests/test_log.py (4 issues)

1
"""Collection of tests around log handling."""
2
import logging
3
4
import pytest
5
6
from cookiecutter.log import configure_logger
7
8
9
def create_log_records():
10
    """Test function, create log entries in expected stage of test."""
11
    cookiecutter_logger = logging.getLogger('cookiecutter')
12
    foo_logger = logging.getLogger('cookiecutter.foo')
13
    foobar_logger = logging.getLogger('cookiecutter.foo.bar')
14
15
    cookiecutter_logger.info('Welcome to Cookiecutter')
16
    cookiecutter_logger.debug('Generating project from pytest-plugin')
17
    foo_logger.info('Loading user config from home dir')
18
    foobar_logger.debug("I don't know.")
19
    foobar_logger.debug('I wanted to save the world.')
20
    foo_logger.error('Aw, snap! Something went wrong')
21
    cookiecutter_logger.debug('Successfully generated project')
22
23
24
@pytest.fixture
25
def info_messages():
26
    """Fixture. List of test info messages."""
27
    return [
28
        'INFO: Welcome to Cookiecutter',
29
        'INFO: Loading user config from home dir',
30
        'ERROR: Aw, snap! Something went wrong',
31
    ]
32
33
34
@pytest.fixture
35
def debug_messages():
36
    """Fixture. List of test debug messages."""
37
    return [
38
        "INFO cookiecutter: Welcome to Cookiecutter",
39
        "DEBUG cookiecutter: Generating project from pytest-plugin",
40
        "INFO cookiecutter.foo: Loading user config from home dir",
41
        "DEBUG cookiecutter.foo.bar: I don't know.",
42
        "DEBUG cookiecutter.foo.bar: I wanted to save the world.",
43
        "ERROR cookiecutter.foo: Aw, snap! Something went wrong",
44
        "DEBUG cookiecutter: Successfully generated project",
45
    ]
46
47
48
@pytest.fixture
49
def info_logger():
50
    """Fixture. Call cookiecutter logger setup with `info` debug level."""
51
    return configure_logger(stream_level='INFO')
52
53
54
@pytest.fixture
55
def debug_logger():
56
    """Fixture. Call cookiecutter logger setup with `debug` debug level."""
57
    return configure_logger(stream_level='DEBUG')
58
59
60
@pytest.fixture
61
def debug_file(tmp_path):
62
    """Fixture. Generate debug file location for tests."""
63
    return tmp_path.joinpath('pytest-plugin.log')
64
65
66
@pytest.fixture
67
def info_logger_with_file(debug_file):
68
    """Fixture. Call cookiecutter logger setup with `info` debug level + `file`."""
69
    return configure_logger(stream_level='INFO', debug_file=str(debug_file))
70
71
72
def test_info_stdout_logging(caplog, info_logger, info_messages):
73
    """Test that stdout logs use info format and level."""
74
    [stream_handler] = info_logger.handlers
75
    assert isinstance(stream_handler, logging.StreamHandler)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable stream_handler does not seem to be defined.
Loading history...
76
    assert stream_handler.level == logging.INFO
77
78
    create_log_records()
79
80
    stream_messages = [
81
        stream_handler.format(r)
82
        for r in caplog.records
83
        if r.levelno >= stream_handler.level
84
    ]
85
86
    assert stream_messages == info_messages
87
88
89
def test_debug_stdout_logging(caplog, debug_logger, debug_messages):
90
    """Test that stdout logs use debug format and level."""
91
    [stream_handler] = debug_logger.handlers
92
    assert isinstance(stream_handler, logging.StreamHandler)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable stream_handler does not seem to be defined.
Loading history...
93
    assert stream_handler.level == logging.DEBUG
94
95
    create_log_records()
96
97
    stream_messages = [
98
        stream_handler.format(r)
99
        for r in caplog.records
100
        if r.levelno >= stream_handler.level
101
    ]
102
103
    assert stream_messages == debug_messages
104
105
106
def test_debug_file_logging(caplog, info_logger_with_file, debug_file, debug_messages):
107
    """Test that logging to stdout uses a different format and level than \
108
    the the file handler."""
109
    [file_handler, stream_handler] = info_logger_with_file.handlers
110
    assert isinstance(file_handler, logging.FileHandler)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_handler does not seem to be defined.
Loading history...
111
    assert isinstance(stream_handler, logging.StreamHandler)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable stream_handler does not seem to be defined.
Loading history...
112
    assert stream_handler.level == logging.INFO
113
    assert file_handler.level == logging.DEBUG
114
115
    create_log_records()
116
117
    assert debug_file.exists()
118
119
    # Last line in the log file is an empty line
120
    with debug_file.open() as f:
121
        assert f.read().split('\n') == debug_messages + ['']
122