Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Feb 19, 2019
1 parent 14a6aaa commit a229f11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/pip/_internal/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,24 @@
# This list is used to provide some indication of and lower bound for
# CI traffic to PyPI. Thus, it is okay if the list is not comprehensive.
# For more background, see: https://github.com/pypa/pip/issues/5499
CI_ENVIRONMENT_VARIABLES = [
CI_ENVIRONMENT_VARIABLES = (
# Azure Pipelines
'BUILD_BUILDID',
# Jenkins
'BUILD_ID',
# AppVeyor, CircleCI, Codeship, Gitlab CI, Shippable, Travis CI
'CI',
]
)


def looks_like_ci():
# type: () -> bool
"""
Return whether it looks like pip is running under CI.
"""
# We don't use the method of checking for a tty (e.g. using isatty())
# because some CI systems mimic a tty (e.g. Travis CI). Thus that
# method doesn't provide definitive information in either direction.
return any(name in os.environ for name in CI_ENVIRONMENT_VARIABLES)


Expand Down
29 changes: 21 additions & 8 deletions tests/unit/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import pip
from pip._internal.download import (
PipSession, SafeFileCache, path_to_url, unpack_file_url, unpack_http_url,
url_to_path,
CI_ENVIRONMENT_VARIABLES, PipSession, SafeFileCache, path_to_url,
unpack_file_url, unpack_http_url, url_to_path,
)
from pip._internal.exceptions import HashMismatch
from pip._internal.models.link import Link
Expand Down Expand Up @@ -50,26 +50,39 @@ def _fake_session_get(*args, **kwargs):
rmtree(temp_dir)


def get_user_agent():
return PipSession().headers["User-Agent"]


def test_user_agent():
user_agent = PipSession().headers["User-Agent"]
user_agent = get_user_agent()

assert user_agent.startswith("pip/%s" % pip.__version__)


@pytest.mark.parametrize('name, expected_like_ci', [
('BUILD', False),
('BUILD_BUILDID', True),
('BUILD_ID', True),
('CI', True),
# Test a prefix substring of one of the variable names we use.
('BUILD', False),
])
def test_user_agent__ci(monkeypatch, name, expected_like_ci):
# Clear existing names since we can be running under an actual CI.
for ci_name in ('BUILD_BUILDID', 'BUILD_ID', 'CI'):
# Delete the variable names we use to check for CI to prevent the
# detection from always returning True in case the tests are being run
# under actual CI. It is okay to depend on CI_ENVIRONMENT_VARIABLES
# here (part of the code under test) because this setup step can only
# prevent false test failures. It can't cause a false test passage.
for ci_name in CI_ENVIRONMENT_VARIABLES:
monkeypatch.delenv(ci_name, raising=False)

monkeypatch.setenv(name, 'true')
user_agent = PipSession().headers["User-Agent"]
# Confirm the baseline before setting the environment variable.
user_agent = get_user_agent()
assert '"ci":null' in user_agent
assert '"ci":true' not in user_agent

monkeypatch.setenv(name, 'true')
user_agent = get_user_agent()
assert ('"ci":true' in user_agent) == expected_like_ci
assert ('"ci":null' in user_agent) == (not expected_like_ci)

Expand Down

0 comments on commit a229f11

Please sign in to comment.