|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +def test_setup_logging(monkeypatch, tmpdir, caplog): |
| 6 | + from pynvim import setup_logging |
| 7 | + |
| 8 | + major_version = sys.version_info[0] |
| 9 | + |
| 10 | + setup_logging('name1') |
| 11 | + assert caplog.messages == [] |
| 12 | + |
| 13 | + def get_expected_logfile(prefix, name): |
| 14 | + return '{}_py{}_{}'.format(prefix, major_version, name) |
| 15 | + |
| 16 | + prefix = tmpdir.join('testlog1') |
| 17 | + monkeypatch.setenv('NVIM_PYTHON_LOG_FILE', str(prefix)) |
| 18 | + setup_logging('name2') |
| 19 | + assert caplog.messages == [] |
| 20 | + logfile = get_expected_logfile(prefix, 'name2') |
| 21 | + assert os.path.exists(logfile) |
| 22 | + assert open(logfile, 'r').read() == '' |
| 23 | + |
| 24 | + monkeypatch.setenv('NVIM_PYTHON_LOG_LEVEL', 'invalid') |
| 25 | + setup_logging('name3') |
| 26 | + assert caplog.record_tuples == [ |
| 27 | + ('pynvim', 30, "Invalid NVIM_PYTHON_LOG_LEVEL: 'invalid', using INFO."), |
| 28 | + ] |
| 29 | + logfile = get_expected_logfile(prefix, 'name2') |
| 30 | + assert os.path.exists(logfile) |
| 31 | + with open(logfile, 'r') as f: |
| 32 | + lines = f.readlines() |
| 33 | + assert len(lines) == 1 |
| 34 | + assert lines[0].endswith( |
| 35 | + "- Invalid NVIM_PYTHON_LOG_LEVEL: 'invalid', using INFO.\n" |
| 36 | + ) |
0 commit comments