1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/unit/test_logging.py
Ami Fischman 2a90808387 Prefix user_log (--log) entries with timestamp (#6142)
Why? Eases post-facto analysis of time spent in different phases of pip operation.

Historical note:
767d11e49c (diff-b670e3b192038c9ffe810c1a12c0c51fL219)
made it so that pip invocations emit zero timestamp information to the log
file. Prior to that each pip invocation's start time was written out (search
that commit's diff for [strftime]).

Result: https://gist.github.com/fischman/f570886219de5c64a3b695300195c70a

Resolves https://github.com/pypa/pip/issues/6141
2019-01-20 03:02:22 -08:00

46 lines
1.3 KiB
Python

import logging
import os
import time
from pip._internal.utils.logging import IndentingFormatter
class TestIndentingFormatter(object):
"""
Test `pip._internal.utils.logging.IndentingFormatter`.
"""
def setup(self):
# Robustify the tests below to the ambient timezone by setting it
# explicitly here.
self.old_tz = getattr(os.environ, 'TZ', None)
os.environ['TZ'] = 'UTC'
# time.tzset() is not implemented on some platforms (notably, Windows).
if hasattr(time, 'tzset'):
time.tzset()
def teardown(self):
if self.old_tz:
os.environ['TZ'] = self.old_tz
else:
del os.environ['TZ']
if 'tzset' in dir(time):
time.tzset()
def test_format(self, tmpdir):
record = logging.makeLogRecord(dict(
created=1547704837.4,
msg='hello\nworld',
))
f = IndentingFormatter(fmt="%(message)s")
assert f.format(record) == 'hello\nworld'
def test_format_with_timestamp(self, tmpdir):
record = logging.makeLogRecord(dict(
created=1547704837.4,
msg='hello\nworld',
))
f = IndentingFormatter(fmt="%(message)s", add_timestamp=True)
expected = '2019-01-17T06:00:37 hello\n2019-01-17T06:00:37 world'
assert f.format(record) == expected