mirror of https://github.com/pypa/pip
Use mock to patch for TZ env var
This commit is contained in:
parent
5b1093fc75
commit
cd549eb7f1
|
@ -6,6 +6,7 @@ import re
|
|||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
|
||||
import pytest
|
||||
|
@ -556,3 +557,13 @@ def mock_server():
|
|||
test_server = MockServer(server)
|
||||
with test_server.context:
|
||||
yield test_server
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def utc():
|
||||
# time.tzset() is not implemented on some platforms, e.g. Windows.
|
||||
tzset = getattr(time, 'tzset', lambda: None)
|
||||
with patch.dict(os.environ, {'TZ': 'UTC'}):
|
||||
tzset()
|
||||
yield
|
||||
tzset()
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import logging
|
||||
import os
|
||||
import time
|
||||
|
||||
import pytest
|
||||
from mock import Mock, patch
|
||||
|
@ -12,6 +11,12 @@ from pip._internal.utils.logging import BrokenStdoutLoggingError
|
|||
from pip._internal.utils.temp_dir import TempDirectory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fixed_time(utc):
|
||||
with patch('time.time', lambda: 1547704837.040001):
|
||||
yield
|
||||
|
||||
|
||||
class FakeCommand(Command):
|
||||
|
||||
_name = 'fake'
|
||||
|
@ -91,67 +96,40 @@ def test_handle_pip_version_check_called(mock_handle_version_check):
|
|||
mock_handle_version_check.assert_called_once()
|
||||
|
||||
|
||||
class Test_base_command_logging(object):
|
||||
def test_log_command_success(fixed_time, tmpdir):
|
||||
"""Test the --log option logs when command succeeds."""
|
||||
cmd = FakeCommand()
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake', '--log', log_path])
|
||||
with open(log_path) as f:
|
||||
assert f.read().rstrip() == '2019-01-17T06:00:37,040 fake'
|
||||
|
||||
|
||||
def test_log_command_error(fixed_time, tmpdir):
|
||||
"""Test the --log option logs when command fails."""
|
||||
cmd = FakeCommand(error=True)
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake', '--log', log_path])
|
||||
with open(log_path) as f:
|
||||
assert f.read().startswith('2019-01-17T06:00:37,040 fake')
|
||||
|
||||
|
||||
def test_log_file_command_error(fixed_time, tmpdir):
|
||||
"""Test the --log-file option logs (when there's an error)."""
|
||||
cmd = FakeCommand(error=True)
|
||||
log_file_path = tmpdir.joinpath('log_file')
|
||||
cmd.main(['fake', '--log-file', log_file_path])
|
||||
with open(log_file_path) as f:
|
||||
assert f.read().startswith('2019-01-17T06:00:37,040 fake')
|
||||
|
||||
|
||||
def test_log_unicode_messages(fixed_time, tmpdir):
|
||||
"""Tests that logging bytestrings and unicode objects
|
||||
don't break logging.
|
||||
"""
|
||||
Test `pip.base_command.Command` setting up logging consumers based on
|
||||
options
|
||||
"""
|
||||
|
||||
def setup(self):
|
||||
self.old_time = time.time
|
||||
time.time = lambda: 1547704837.040001
|
||||
self.old_tz = os.environ.get('TZ')
|
||||
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()
|
||||
time.time = self.old_time
|
||||
|
||||
def test_log_command_success(self, tmpdir):
|
||||
"""
|
||||
Test the --log option logs when command succeeds
|
||||
"""
|
||||
cmd = FakeCommand()
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake', '--log', log_path])
|
||||
with open(log_path) as f:
|
||||
assert f.read().rstrip() == '2019-01-17T06:00:37,040 fake'
|
||||
|
||||
def test_log_command_error(self, tmpdir):
|
||||
"""
|
||||
Test the --log option logs when command fails
|
||||
"""
|
||||
cmd = FakeCommand(error=True)
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake', '--log', log_path])
|
||||
with open(log_path) as f:
|
||||
assert f.read().startswith('2019-01-17T06:00:37,040 fake')
|
||||
|
||||
def test_log_file_command_error(self, tmpdir):
|
||||
"""
|
||||
Test the --log-file option logs (when there's an error).
|
||||
"""
|
||||
cmd = FakeCommand(error=True)
|
||||
log_file_path = tmpdir.joinpath('log_file')
|
||||
cmd.main(['fake', '--log-file', log_file_path])
|
||||
with open(log_file_path) as f:
|
||||
assert f.read().startswith('2019-01-17T06:00:37,040 fake')
|
||||
|
||||
def test_unicode_messages(self, tmpdir):
|
||||
"""
|
||||
Tests that logging bytestrings and unicode objects don't break logging
|
||||
"""
|
||||
cmd = FakeCommandWithUnicode()
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake_unicode', '--log', log_path])
|
||||
cmd = FakeCommandWithUnicode()
|
||||
log_path = tmpdir.joinpath('log')
|
||||
cmd.main(['fake_unicode', '--log', log_path])
|
||||
|
||||
|
||||
@pytest.mark.no_auto_tempdir_manager
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import errno
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from threading import Thread
|
||||
|
||||
import pytest
|
||||
|
@ -33,24 +31,7 @@ def _make_broken_pipe_error():
|
|||
|
||||
|
||||
class TestIndentingFormatter(object):
|
||||
"""
|
||||
Test `pip._internal.utils.logging.IndentingFormatter`.
|
||||
"""
|
||||
|
||||
def setup(self):
|
||||
self.old_tz = os.environ.get('TZ')
|
||||
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()
|
||||
"""Test ``pip._internal.utils.logging.IndentingFormatter``."""
|
||||
|
||||
def make_record(self, msg, level_name):
|
||||
level_number = getattr(logging, level_name)
|
||||
|
@ -72,7 +53,7 @@ class TestIndentingFormatter(object):
|
|||
('ERROR', 'ERROR: hello\nworld'),
|
||||
('CRITICAL', 'ERROR: hello\nworld'),
|
||||
])
|
||||
def test_format(self, level_name, expected):
|
||||
def test_format(self, level_name, expected, utc):
|
||||
"""
|
||||
Args:
|
||||
level_name: a logging level name (e.g. "WARNING").
|
||||
|
@ -89,7 +70,7 @@ class TestIndentingFormatter(object):
|
|||
'2019-01-17T06:00:37,040 WARNING: hello\n'
|
||||
'2019-01-17T06:00:37,040 world'),
|
||||
])
|
||||
def test_format_with_timestamp(self, level_name, expected):
|
||||
def test_format_with_timestamp(self, level_name, expected, utc):
|
||||
record = self.make_record('hello\nworld', level_name=level_name)
|
||||
f = IndentingFormatter(fmt="%(message)s", add_timestamp=True)
|
||||
assert f.format(record) == expected
|
||||
|
@ -99,7 +80,7 @@ class TestIndentingFormatter(object):
|
|||
('ERROR', 'DEPRECATION: hello\nworld'),
|
||||
('CRITICAL', 'DEPRECATION: hello\nworld'),
|
||||
])
|
||||
def test_format_deprecated(self, level_name, expected):
|
||||
def test_format_deprecated(self, level_name, expected, utc):
|
||||
"""
|
||||
Test that logged deprecation warnings coming from deprecated()
|
||||
don't get another prefix.
|
||||
|
@ -110,7 +91,7 @@ class TestIndentingFormatter(object):
|
|||
f = IndentingFormatter(fmt="%(message)s")
|
||||
assert f.format(record) == expected
|
||||
|
||||
def test_thread_safety_base(self):
|
||||
def test_thread_safety_base(self, utc):
|
||||
record = self.make_record(
|
||||
'DEPRECATION: hello\nworld', level_name='WARNING',
|
||||
)
|
||||
|
@ -126,7 +107,7 @@ class TestIndentingFormatter(object):
|
|||
thread.join()
|
||||
assert results[0] == results[1]
|
||||
|
||||
def test_thread_safety_indent_log(self):
|
||||
def test_thread_safety_indent_log(self, utc):
|
||||
record = self.make_record(
|
||||
'DEPRECATION: hello\nworld', level_name='WARNING',
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue