Completed
Push — master ( 454794...2ab37b )
by
unknown
12s
created

info_logger_with_file()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 2
b 0
f 1
1
# -*- coding: utf-8 -*-
2
3
import logging
4
5
import pytest
6
7
from cookiecutter.log import configure_logger
8
9
10
def create_log_records():
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
    return [
27
        'INFO: Welcome to Cookiecutter',
28
        'INFO: Loading user config from home dir',
29
        'ERROR: Aw, snap! Something went wrong',
30
    ]
31
32
33
@pytest.fixture
34
def debug_messages():
35
    return [
36
        'INFO cookiecutter: '
37
        'Welcome to Cookiecutter',
38
39
        'DEBUG cookiecutter: '
40
        'Generating project from pytest-plugin',
41
42
        'INFO cookiecutter.foo: '
43
        'Loading user config from home dir',
44
45
        "DEBUG cookiecutter.foo.bar: "
46
        "I don't know.",
47
48
        'DEBUG cookiecutter.foo.bar: '
49
        'I wanted to save the world.',
50
51
        'ERROR cookiecutter.foo: '
52
        'Aw, snap! Something went wrong',
53
54
        'DEBUG cookiecutter: '
55
        'Successfully generated project',
56
    ]
57
58
59
@pytest.fixture
60
def info_logger():
61
    return configure_logger(stream_level='INFO')
62
63
64
@pytest.fixture
65
def debug_logger():
66
    return configure_logger(stream_level='DEBUG')
67
68
69
@pytest.fixture
70
def debug_file(tmpdir):
71
    return tmpdir / 'pytest-plugin.log'
72
73
74
@pytest.fixture
75
def info_logger_with_file(debug_file):
76
    return configure_logger(
77
        stream_level='INFO',
78
        debug_file=str(debug_file),
79
    )
80
81
82
def test_info_stdout_logging(caplog, info_logger, info_messages):
83
    """Test that stdout logs use info format and level."""
84
    [stream_handler] = info_logger.handlers
85
    assert isinstance(stream_handler, logging.StreamHandler)
86
    assert stream_handler.level == logging.INFO
87
88
    create_log_records()
89
90
    stream_messages = [
91
        stream_handler.format(r)
92
        for r in caplog.records
93
        if r.levelno >= stream_handler.level
94
    ]
95
96
    assert stream_messages == info_messages
97
98
99
def test_debug_stdout_logging(caplog, debug_logger, debug_messages):
100
    """Test that stdout logs use debug format and level."""
101
    [stream_handler] = debug_logger.handlers
102
    assert isinstance(stream_handler, logging.StreamHandler)
103
    assert stream_handler.level == logging.DEBUG
104
105
    create_log_records()
106
107
    stream_messages = [
108
        stream_handler.format(r)
109
        for r in caplog.records
110
        if r.levelno >= stream_handler.level
111
    ]
112
113
    assert stream_messages == debug_messages
114
115
116
def test_debug_file_logging(
117
        caplog, info_logger_with_file, debug_file, debug_messages):
118
    """Test that logging to stdout uses a different format and level than
119
    the the file handler.
120
    """
121
122
    [file_handler, stream_handler] = info_logger_with_file.handlers
123
    assert isinstance(file_handler, logging.FileHandler)
124
    assert isinstance(stream_handler, logging.StreamHandler)
125
    assert stream_handler.level == logging.INFO
126
    assert file_handler.level == logging.DEBUG
127
128
    create_log_records()
129
130
    assert debug_file.exists()
131
132
    # Last line in the log file is an empty line
133
    assert debug_file.readlines(cr=False) == debug_messages + ['']
134