def handle_pip_version_check(self, options: Values) -> None: """ Do the pip version check if not disabled. This overrides the default behavior of not doing the check. """ # Make sure the index_group options are present. assert hasattr(options, "no_index") if options.disable_pip_version_check or options.no_index: return # Otherwise, check if we're using the latest version of pip available. session = self._build_session( options, retries=0, timeout=min(5, options.timeout), # This is set to ensure the function does not fail when truststore is # specified in use-feature but cannot be loaded. This usually raises a # CommandError and shows a nice user-facing error, but this function is not # called in that try-except block. fallback_to_certifi=True, ) with session: pip_self_version_check(session, options)
def handle_pip_version_check(self, options): # type: (Values) -> None """ Do the pip version check if not disabled. This overrides the default behavior of not doing the check. """ # Make sure the index_group options are present. assert hasattr(options, 'no_index') if options.disable_pip_version_check or options.no_index: return # Otherwise, check if we're using the latest version of pip available. session = self._build_session(options, retries=0, timeout=min(5, options.timeout)) with session: pip_self_version_check(session, options)
def test_pip_self_version_check( monkeypatch: pytest.MonkeyPatch, stored_time: str, installed_ver: Optional[str], new_ver: str, installer: str, check_if_upgrade_required: bool, check_warn_logs: bool, ) -> None: monkeypatch.setattr( self_outdated_check, "get_default_environment", functools.partial(MockEnvironment, installer, installed_ver), ) monkeypatch.setattr( self_outdated_check, "PackageFinder", MockPackageFinder, ) monkeypatch.setattr(logger, "warning", mock.Mock()) monkeypatch.setattr(logger, "debug", mock.Mock()) fake_state = mock.Mock( state={ "last_check": stored_time, "pypi_version": installed_ver }, save=mock.Mock(), ) monkeypatch.setattr(self_outdated_check, "SelfCheckState", lambda **kw: fake_state) with freezegun.freeze_time( "1970-01-09 10:00:00", ignore=[ "six.moves", "pip._vendor.six.moves", "pip._vendor.requests.packages.urllib3.packages.six.moves", ], ): pip_self_version_check(PipSession(), _options()) # See that we saved the correct version if check_if_upgrade_required: assert fake_state.save.call_args_list == [ mock.call(new_ver, datetime.datetime(1970, 1, 9, 10, 00, 00)), ] elif installed_ver: # Make sure no Exceptions assert not cast(mock.Mock, logger.debug).call_args_list # See that save was not called assert fake_state.save.call_args_list == [] # Ensure we warn the user or not if check_warn_logs: assert cast(mock.Mock, logger.warning).call_count == 1 else: assert cast(mock.Mock, logger.warning).call_count == 0
def test_pip_self_version_check_calls_underlying_implementation( mocked_state: Mock, mocked_function: Mock, tmpdir: Path) -> None: # GIVEN mock_session = Mock() fake_options = Values(dict(cache_dir=str(tmpdir))) # WHEN self_outdated_check.pip_self_version_check(mock_session, fake_options) # THEN mocked_state.assert_called_once_with(cache_dir=str(tmpdir)) mocked_function.assert_called_once_with( state=mocked_state(cache_dir=str(tmpdir)), current_time=datetime.datetime(1970, 1, 2, 11, 0, 0), local_version=ANY, get_remote_version=ANY, )
def test_pip_self_version_check(monkeypatch, stored_time, installed_ver, new_ver, installer, check_if_upgrade_required, check_warn_logs): monkeypatch.setattr(self_outdated_check, 'get_installed_version', lambda name: installed_ver) monkeypatch.setattr(self_outdated_check, 'PackageFinder', MockPackageFinder) monkeypatch.setattr(logger, 'warning', pretend.call_recorder(lambda *a, **kw: None)) monkeypatch.setattr(logger, 'debug', pretend.call_recorder(lambda s, exc_info=None: None)) monkeypatch.setattr(self_outdated_check, 'get_distribution', lambda name: MockDistribution(installer)) fake_state = pretend.stub( state={ "last_check": stored_time, 'pypi_version': installed_ver }, save=pretend.call_recorder(lambda v, t: None), ) monkeypatch.setattr(self_outdated_check, 'SelfCheckState', lambda **kw: fake_state) with freezegun.freeze_time( "1970-01-09 10:00:00", ignore=[ "six.moves", "pip._vendor.six.moves", "pip._vendor.requests.packages.urllib3.packages.six.moves", ]): latest_pypi_version = pip_self_version_check(None, _options()) # 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: assert fake_state.save.calls == [ pretend.call(new_ver, datetime.datetime(1970, 1, 9, 10, 00, 00)), ] else: # Make sure no Exceptions assert not logger.debug.calls # See that save was not called assert fake_state.save.calls == [] # Ensure we warn the user or not if check_warn_logs: assert len(logger.warning.calls) == 1 else: assert len(logger.warning.calls) == 0