Skip to content

Commit

Permalink
fix/integration test for pants_requirement() (pantsbuild#5457)
Browse files Browse the repository at this point in the history
### Problem

It's not possible to use `pants_requirement()` to stand in for `python_requirement_library(requirements=[python_requirement('pantsbuild.pants==1.5.0.dev3')])` in a separate repo. This defeats the purpose of `pants_requirement()`, which expands into an `==` requirement on the pants version denoted in the repo's `pants.ini`. The error is:
```
u"There is no type registered for alias <class 'pants.backend.python.targets.python_requirement_library.PythonRequirementLibrary'>"
```

### Solution

`ParseContext#create_object()` accepts an alias as the first argument, not the class itself. [Changing `PythonRequirementLibrary` to `'python_requirement_library'` in `src/python/pants/backend/python/pants_requirement.py`](https://github.com/cosmicexplorer/pants/blob/1fc4ba0310d3fd20d26384e5e1c356e8f99b9eea/src/python/pants/backend/python/pants_requirement.py#L37) allows `pants_requirement()` to be used as documented. Integration tests were added in `tests/python/pants_test/backend/python{test_pants_requirement_integration,/tasks/test_setup_py_integration}.py`.
  • Loading branch information
cosmicexplorer authored and Stu Hood committed Feb 14, 2018
1 parent 7cdea9a commit f55260a
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/pants_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import os

from pants.backend.python.python_requirement import PythonRequirement
from pants.backend.python.targets.python_requirement_library import PythonRequirementLibrary
from pants.base.build_environment import pants_version


Expand All @@ -34,5 +33,6 @@ def __call__(self, name=None):
"""
name = name or os.path.basename(self._parse_context.rel_path)
requirement = PythonRequirement(requirement='pantsbuild.pants=={}'.format(pants_version()))
self._parse_context.create_object(PythonRequirementLibrary, name=name,
self._parse_context.create_object('python_requirement_library',
name=name,
requirements=[requirement])
1 change: 1 addition & 0 deletions testprojects/pants-plugins/3rdparty/python/pants/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pants_requirement()
15 changes: 15 additions & 0 deletions testprojects/pants-plugins/src/python/test_pants_plugin/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
python_library(
dependencies=[
'testprojects/pants-plugins/3rdparty/python/pants',
'testprojects/pants-plugins/src/python/test_pants_plugin/subsystems',
],
provides=setup_py(
name='test_pants_plugin',
description='A test pants plugin',
version='0.0.1',
namespace_packages=[
'test_pants_plugin',
'test_pants_plugin.subsystems',
],
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# coding=utf-8
# Copyright 2018 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 test_pants_plugin.subsystems.pants_test_infra import PantsTestInfra

from pants.backend.python.targets.python_tests import PythonTests


class PantsInfraTests(object):

def __init__(self, parse_context):
self._parse_context = parse_context
self._pants_test_infra = PantsTestInfra.global_instance()

def __call__(self, dependencies=[], **kwargs):
dependencies.extend(self._pants_test_infra.dependent_target_addrs())
self._parse_context.create_object(
PythonTests.alias(), dependencies=dependencies, **kwargs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# coding=utf-8
# Copyright 2018 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 test_pants_plugin.pants_infra_tests import PantsInfraTests
from test_pants_plugin.subsystems.pants_test_infra import PantsTestInfra

from pants.build_graph.build_file_aliases import BuildFileAliases


def build_file_aliases():
return BuildFileAliases(
context_aware_object_factories={
'pants_infra_tests': PantsInfraTests,
}
)

def global_subsystems():
return (PantsTestInfra,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
python_library(
dependencies=[
'testprojects/pants-plugins/3rdparty/python/pants',
],
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# coding=utf-8
# Copyright 2018 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.backend.python.targets.python_target import PythonTarget
from pants.build_graph.target import Target
from pants.subsystem.subsystem import Subsystem


class PantsTestInfra(Subsystem):
options_scope = 'pants-test-infra'

@classmethod
def register_options(cls, register):
super(PantsTestInfra, cls).register_options(register)
register('--pants-requirement-target', advanced=True, fingerprint=True,
help='Address for a python target providing the pants sdist.',
type=str, default=None)
register('--pants-test-infra-target', advanced=True, fingerprint=True,
help='Address for a python target providing the '
'pants test infra sdist.',
type=str, default=None)

def dependent_target_addrs(self):
return [
self.get_options().pants_requirement_target,
self.get_options().pants_test_infra_target,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pants_infra_tests()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding=utf-8
# Copyright 2018 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.base.build_environment import pants_version
from pants.version import VERSION as _VERSION
from pants_test.base_test import BaseTest


class PantsPluginPantsRequirementTest(BaseTest):
def test_version(self):
self.assertEqual(pants_version(), _VERSION)
10 changes: 10 additions & 0 deletions tests/python/pants_test/backend/python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ python_tests(
],
timeout=1200
)

python_tests(
name='integration',
sources=globs('*_integration.py'),
dependencies=[
'src/python/pants/base:build_environment',
'tests/python/pants_test:int-test',
],
tags={'integration'},
)
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,29 @@ def test_setup_py_with_codegen_unexported_deps(self):
'org/pantsbuild/example/precipitation/__init__.py',
'org/pantsbuild/example/precipitation/constants.py',
'org/pantsbuild/example/precipitation/ttypes.py'])

def test_setup_py_unregistered_pants_plugin(self):
"""setup-py should succeed on a pants plugin target that:
1. uses a pants_requirement() instead of linking directly to targets in the
pants codebase
2. is not on the pythonpath nor registered as a backend package."""

self.maxDiff = None

command = [
'setup-py',
'testprojects/pants-plugins/src/python/test_pants_plugin',
]
pants_run = self.run_pants(command=command)
self.assert_success(pants_run)

self.assert_sdist(pants_run, 'test_pants_plugin', [
'test_pants_plugin/',
'test_pants_plugin/__init__.py',
'test_pants_plugin/pants_infra_tests.py',
'test_pants_plugin/register.py',
'test_pants_plugin/subsystems/',
'test_pants_plugin/subsystems/__init__.py',
'test_pants_plugin/subsystems/pants_test_infra.py',
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# coding=utf-8
# Copyright 2018 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)

import os

from pants.base.build_environment import get_buildroot
from pants_test.pants_run_integration_test import PantsRunIntegrationTest


class PantsRequirementIntegrationTest(PantsRunIntegrationTest):
"""A pants plugin should be able to depend on a pants_requirement() alone to
declare its dependencies on pants modules. This plugin, when added to the
pythonpath and backend_packages, should be able to declare new BUILD file
objects."""

def run_with_testproject_backend_pkgs(self, cmd):
testproject_backend_src_dir = os.path.join(
get_buildroot(), 'testprojects/pants-plugins/src/python')
testproject_backend_pkg_name = 'test_pants_plugin'
pants_req_addr = 'testprojects/pants-plugins/3rdparty/python/pants'
pants_test_infra_addr = 'tests/python/pants_test:test_infra'
pre_cmd_args = [
"--pythonpath=+['{}']".format(testproject_backend_src_dir),
"--backend-packages=+['{}']".format(testproject_backend_pkg_name),
"--pants-test-infra-pants-requirement-target={}".format(pants_req_addr),
"--pants-test-infra-pants-test-infra-target={}".format(pants_test_infra_addr),
]
command = pre_cmd_args + cmd
return self.run_pants(command=command)

def test_pants_requirement(self):
self.maxDiff = None

tests_dir = 'testprojects/pants-plugins/tests/python/test_pants_plugin'
command = [
'test',
tests_dir,
]

tests_dir_absolute = os.path.join(get_buildroot(), tests_dir)
with self.file_renamed(tests_dir_absolute, 'TEST_BUILD', 'BUILD'):
pants_run = self.run_with_testproject_backend_pkgs(command)
self.assert_success(pants_run)
4 changes: 2 additions & 2 deletions tests/python/pants_test/engine/test_isolated_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from pants.engine.fs import PathGlobs, Snapshot, create_fs_rules
from pants.engine.isolated_process import (Binary, ExecuteProcess, ExecuteProcessRequest,
ExecuteProcessResult, SnapshottedProcess,
SnapshottedProcessRequest, create_process_rules)
ExecuteProcessResult, SnapshottedProcess,
SnapshottedProcessRequest, create_process_rules)
from pants.engine.nodes import Return, Throw
from pants.engine.rules import SingletonRule
from pants.engine.selectors import Select
Expand Down

0 comments on commit f55260a

Please sign in to comment.