aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/util/logger_test.py
blob: 78c4c109a710b695408b55a48c19decc3887c353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
##############################################################################
# Copyright (c) 2017 ZTE Corporation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import pytest

from qtip.util import logger

MODULE = 'test_logger'
ERROR_MSG = 'error level test'
INFO_MSG = 'info level test'
DEBUG_MSG = 'debug level test'


@pytest.fixture()
def env_home(monkeypatch, tmpdir):
    monkeypatch.setenv('HOME', str(tmpdir))
    return tmpdir


@pytest.fixture()
def logger_file(env_home):
    return env_home.mkdir('qtip').mkdir('logs').join('{}.log'.format(MODULE))


def console_expect_debug(content):
    assert DEBUG_MSG in content


def console_expect_nodebug(content):
    assert DEBUG_MSG not in content


@pytest.mark.parametrize('debug, console_expected', [
    ('true', console_expect_debug),
    ('false', console_expect_nodebug)])
def test_logger(monkeypatch, capsys, logger_file, debug, console_expected):
    monkeypatch.setenv('IF_DEBUG', debug)

    log = logger.QtipLogger(MODULE).get
    log.error(ERROR_MSG)
    log.info(INFO_MSG)
    log.debug(DEBUG_MSG)

    file_print = logger_file.read()
    assert ERROR_MSG in file_print
    assert INFO_MSG in file_print
    assert DEBUG_MSG in file_print

    _, console_print = capsys.readouterr()

    console_expected(console_print)