Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the pytest-related runtime requirement specs into a subsystem. #4071

Merged
merged 2 commits into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/python/pants/backend/python/subsystems/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_library(
dependencies = [
'src/python/pants/subsystem',
],
)
Empty file.
40 changes: 40 additions & 0 deletions src/python/pants/backend/python/subsystems/pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# coding=utf-8
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.subsystem.subsystem import Subsystem


class PyTest(Subsystem):
options_scope = 'pytest'

@classmethod
def register_options(cls, register):
super(PyTest, cls).register_options(register)
register('--requirements', advanced=True, default='pytest>=2.6,<2.7',
help='Requirements string for the pytest library.')
# NB, pytest-timeout 1.0.0 introduces a conflicting pytest>=2.8.0 requirement, see:
# https://github.com/pantsbuild/pants/issues/2566
register('--timeout-requirements', advanced=True, default='pytest-timeout<1.0.0',
help='Requirements string for the pytest-timeout library.')
register('--cov-requirements', advanced=True, default='pytest-cov>=1.8,<1.9',
help='Requirements string for the pytest-cov library.')
register('--unittest2-requirements', advanced=True, default='unittest2>=0.6.0,<=1.9.0',
help='Requirements string for the unittest2 library, which some python versions '
'may need.')

def get_requirement_strings(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A doc-comment would be great here.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"""Returns a tuple of requirements-style strings for pytest and related libraries.

Make sure the requirements are satisfied in any environment used for running tests.
"""
opts = self.get_options()
return (
opts.requirements,
opts.timeout_requirements,
opts.cov_requirements,
opts.unittest2_requirements,
)
2 changes: 2 additions & 0 deletions src/python/pants/backend/python/targets/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ python_library(
'3rdparty/python/twitter/commons:twitter.common.collections',
'src/python/pants/backend/python:python_artifact',
'src/python/pants/backend/python:python_requirement',
'src/python/pants/backend/python/subsystems',
'src/python/pants/base:exceptions',
'src/python/pants/base:payload',
'src/python/pants/base:payload_field',
'src/python/pants/build_graph',
'src/python/pants/subsystem',
'src/python/pants/util:memo',
],
)
Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/backend/python/tasks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ python_library(
'3rdparty/python/twitter/commons:twitter.common.dirutil',
'3rdparty/python:coverage',
'3rdparty/python:pex',
'3rdparty/python:pytest',
'3rdparty/python:pytest-cov',
'3rdparty/python:setuptools',
'3rdparty/python:six',
'src/python/pants/backend/codegen/targets:python',
Expand All @@ -20,6 +18,7 @@ python_library(
'src/python/pants/backend/python:python_requirement',
'src/python/pants/backend/python:python_setup',
'src/python/pants/backend/python:thrift_builder',
'src/python/pants/backend/python/subsystems',
'src/python/pants/base:build_environment',
'src/python/pants/base:exceptions',
'src/python/pants/base:generator',
Expand All @@ -40,4 +39,4 @@ python_library(
'src/python/pants/util:xml_parser',
'src/python/pants/util:timeout',
]
)
)
19 changes: 6 additions & 13 deletions src/python/pants/backend/python/tasks/pytest_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from pants.backend.python.python_requirement import PythonRequirement
from pants.backend.python.python_setup import PythonRepos, PythonSetup
from pants.backend.python.subsystems.pytest import PyTest
from pants.backend.python.targets.python_tests import PythonTests
from pants.backend.python.tasks.python_task import PythonTask
from pants.base.build_environment import get_buildroot
Expand Down Expand Up @@ -75,21 +76,10 @@ class PytestRun(TestRunnerTaskMixin, PythonTask):
"""
:API: public
"""
_TESTING_TARGETS = [
# Note: the requirement restrictions on pytest and pytest-cov match those in requirements.txt,
# to avoid confusion when debugging pants tests.
# TODO: make these an option, so any pants install base can pick their pytest version.
PythonRequirement('pytest>=2.6,<2.7'),
# NB, pytest-timeout 1.0.0 introduces a conflicting pytest>=2.8.0 requirement, see:
# https://github.com/pantsbuild/pants/issues/2566
PythonRequirement('pytest-timeout<1.0.0'),
PythonRequirement('pytest-cov>=1.8,<1.9'),
PythonRequirement('unittest2>=0.6.0,<=1.9.0'),
]

@classmethod
def subsystem_dependencies(cls):
return super(PytestRun, cls).subsystem_dependencies() + (PythonSetup, PythonRepos)
return super(PytestRun, cls).subsystem_dependencies() + (PyTest, PythonSetup, PythonRepos)

@classmethod
def register_options(cls, register):
Expand Down Expand Up @@ -416,11 +406,14 @@ def _test_runner(self, targets, workunit):
pex_info = PexInfo.default()
pex_info.entry_point = 'pytest'

testing_reqs = [PythonRequirement(s)
for s in PyTest.global_instance().get_requirement_strings()]

chroot = self.cached_chroot(interpreter=interpreter,
pex_info=pex_info,
targets=targets,
platforms=('current',),
extra_requirements=self._TESTING_TARGETS)
extra_requirements=testing_reqs)
pex = chroot.pex()
with self._maybe_shard() as shard_args:
with self._maybe_emit_junit_xml(targets) as junit_args:
Expand Down