2014-08-12 03:15:55 +02:00
|
|
|
import datetime
|
|
|
|
import os
|
2017-05-16 12:16:30 +02:00
|
|
|
import sys
|
2014-08-12 03:15:55 +02:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2018-05-16 23:55:34 +02:00
|
|
|
|
2014-09-18 17:41:09 +02:00
|
|
|
import freezegun
|
2014-08-12 03:15:55 +02:00
|
|
|
import pretend
|
2017-05-16 12:16:30 +02:00
|
|
|
import pytest
|
2017-06-13 14:17:00 +02:00
|
|
|
from pip._vendor import lockfile
|
|
|
|
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.index import InstallationCandidate
|
|
|
|
from pip._internal.utils import outdated
|
2014-08-12 03:15:55 +02:00
|
|
|
|
|
|
|
|
2017-02-27 05:19:06 +01:00
|
|
|
class MockPackageFinder(object):
|
|
|
|
|
2018-04-13 14:08:51 +02:00
|
|
|
BASE_URL = 'https://pypi.org/simple/pip-{0}.tar.gz'
|
2017-02-27 05:19:06 +01:00
|
|
|
PIP_PROJECT_NAME = 'pip'
|
|
|
|
INSTALLATION_CANDIDATES = [
|
|
|
|
InstallationCandidate(PIP_PROJECT_NAME, '6.9.0',
|
|
|
|
BASE_URL.format('6.9.0')),
|
|
|
|
InstallationCandidate(PIP_PROJECT_NAME, '3.3.1',
|
|
|
|
BASE_URL.format('3.3.1')),
|
|
|
|
InstallationCandidate(PIP_PROJECT_NAME, '1.0',
|
|
|
|
BASE_URL.format('1.0')),
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def find_all_candidates(self, project_name):
|
|
|
|
return self.INSTALLATION_CANDIDATES
|
|
|
|
|
|
|
|
|
|
|
|
def _options():
|
|
|
|
''' Some default options that we pass to outdated.pip_version_check '''
|
|
|
|
return pretend.stub(
|
|
|
|
find_links=False, extra_index_urls=[], index_url='default_url',
|
|
|
|
pre=False, trusted_hosts=False, process_dependency_links=False,
|
2018-05-16 23:55:34 +02:00
|
|
|
cache_dir='',
|
2017-02-27 05:19:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-08-12 03:15:55 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
[
|
2017-02-27 05:19:06 +01:00
|
|
|
'stored_time',
|
|
|
|
'installed_ver',
|
|
|
|
'new_ver',
|
|
|
|
'check_if_upgrade_required',
|
|
|
|
'check_warn_logs',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
# Test we return None when installed version is None
|
|
|
|
('1970-01-01T10:00:00Z', None, '1.0', False, False),
|
|
|
|
# Need an upgrade - upgrade warning should print
|
|
|
|
('1970-01-01T10:00:00Z', '1.0', '6.9.0', True, True),
|
|
|
|
# No upgrade - upgrade warning should not print
|
|
|
|
('1970-01-9T10:00:00Z', '6.9.0', '6.9.0', False, False),
|
2014-08-12 03:15:55 +02:00
|
|
|
]
|
|
|
|
)
|
2017-02-27 05:19:06 +01:00
|
|
|
def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
|
|
|
|
check_if_upgrade_required, check_warn_logs):
|
|
|
|
monkeypatch.setattr(outdated, 'get_installed_version',
|
|
|
|
lambda name: installed_ver)
|
|
|
|
monkeypatch.setattr(outdated, 'PackageFinder', MockPackageFinder)
|
|
|
|
monkeypatch.setattr(outdated.logger, 'warning',
|
|
|
|
pretend.call_recorder(lambda *a, **kw: None))
|
|
|
|
monkeypatch.setattr(outdated.logger, 'debug',
|
|
|
|
pretend.call_recorder(lambda s, exc_info=None: None))
|
2014-08-12 03:15:55 +02:00
|
|
|
|
|
|
|
fake_state = pretend.stub(
|
2017-02-27 05:19:06 +01:00
|
|
|
state={"last_check": stored_time, 'pypi_version': installed_ver},
|
2014-08-12 03:15:55 +02:00
|
|
|
save=pretend.call_recorder(lambda v, t: None),
|
|
|
|
)
|
2014-09-18 13:36:40 +02:00
|
|
|
monkeypatch.setattr(
|
2018-05-17 15:39:26 +02:00
|
|
|
outdated, 'SelfCheckState', lambda **kw: fake_state
|
2014-09-18 13:36:40 +02:00
|
|
|
)
|
2014-08-12 03:15:55 +02:00
|
|
|
|
2014-12-02 00:48:38 +01:00
|
|
|
with freezegun.freeze_time(
|
2018-05-17 04:03:33 +02:00
|
|
|
"1970-01-09 10:00:00",
|
|
|
|
ignore=[
|
|
|
|
"six.moves",
|
|
|
|
"pip._vendor.six.moves",
|
|
|
|
"pip._vendor.requests.packages.urllib3.packages.six.moves",
|
2018-05-17 17:30:10 +02:00
|
|
|
]
|
|
|
|
):
|
2017-02-27 05:19:06 +01:00
|
|
|
latest_pypi_version = outdated.pip_version_check(None, _options())
|
2014-08-12 03:15:55 +02:00
|
|
|
|
2017-02-27 05:19:06 +01:00
|
|
|
# See we return None if not installed_version
|
|
|
|
if not installed_ver:
|
|
|
|
assert not latest_pypi_version
|
|
|
|
# See that we saved the correct version
|
|
|
|
elif check_if_upgrade_required:
|
2014-09-18 17:41:09 +02:00
|
|
|
assert fake_state.save.calls == [
|
2017-02-27 05:19:06 +01:00
|
|
|
pretend.call(new_ver, datetime.datetime(1970, 1, 9, 10, 00, 00)),
|
2014-09-18 17:41:09 +02:00
|
|
|
]
|
2014-08-12 03:15:55 +02:00
|
|
|
else:
|
2017-02-27 05:19:06 +01:00
|
|
|
# Make sure no Exceptions
|
|
|
|
assert not outdated.logger.debug.calls
|
|
|
|
# See that save was not called
|
2014-08-12 03:15:55 +02:00
|
|
|
assert fake_state.save.calls == []
|
|
|
|
|
2017-02-27 05:19:06 +01:00
|
|
|
# Ensure we warn the user or not
|
|
|
|
if check_warn_logs:
|
|
|
|
assert len(outdated.logger.warning.calls) == 1
|
|
|
|
else:
|
|
|
|
assert len(outdated.logger.warning.calls) == 0
|
|
|
|
|
2014-08-12 03:15:55 +02:00
|
|
|
|
2018-05-17 16:36:42 +02:00
|
|
|
def test_self_check_state(monkeypatch, tmpdir):
|
2014-08-12 03:15:55 +02:00
|
|
|
CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z",
|
|
|
|
"pypi_version": "1.0"}}'''
|
|
|
|
fake_file = pretend.stub(
|
|
|
|
read=pretend.call_recorder(lambda: CONTENT),
|
|
|
|
write=pretend.call_recorder(lambda s: None),
|
|
|
|
)
|
|
|
|
|
|
|
|
@pretend.call_recorder
|
|
|
|
@contextmanager
|
|
|
|
def fake_open(filename, mode='r'):
|
|
|
|
yield fake_file
|
|
|
|
|
2014-09-18 13:36:40 +02:00
|
|
|
monkeypatch.setattr(outdated, 'open', fake_open, raising=False)
|
2014-08-12 03:15:55 +02:00
|
|
|
|
|
|
|
@pretend.call_recorder
|
|
|
|
@contextmanager
|
|
|
|
def fake_lock(filename):
|
|
|
|
yield
|
|
|
|
|
2015-01-03 09:24:47 +01:00
|
|
|
monkeypatch.setattr(outdated, "check_path_owner", lambda p: True)
|
2015-01-03 05:24:42 +01:00
|
|
|
|
2014-08-12 03:15:55 +02:00
|
|
|
monkeypatch.setattr(lockfile, 'LockFile', fake_lock)
|
2015-01-06 06:11:00 +01:00
|
|
|
monkeypatch.setattr(os.path, "exists", lambda p: True)
|
2014-08-12 03:15:55 +02:00
|
|
|
|
2017-10-02 19:33:52 +02:00
|
|
|
cache_dir = tmpdir / 'cache_dir'
|
|
|
|
monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix')
|
2014-08-12 03:15:55 +02:00
|
|
|
|
2018-05-17 15:39:26 +02:00
|
|
|
state = outdated.SelfCheckState(cache_dir=cache_dir)
|
2014-08-12 03:15:55 +02:00
|
|
|
state.save('2.0', datetime.datetime.utcnow())
|
|
|
|
|
2017-10-02 19:33:52 +02:00
|
|
|
expected_path = cache_dir / 'selfcheck.json'
|
2014-08-12 03:15:55 +02:00
|
|
|
assert fake_lock.calls == [pretend.call(expected_path)]
|
|
|
|
|
|
|
|
assert fake_open.calls == [
|
|
|
|
pretend.call(expected_path),
|
|
|
|
pretend.call(expected_path),
|
|
|
|
pretend.call(expected_path, 'w'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# json.dumps will call this a number of times
|
|
|
|
assert len(fake_file.write.calls)
|