Skip to content

Commit

Permalink
Add provides=() validation for PythonTarget
Browse files Browse the repository at this point in the history
RB_ID=https://rbcommons.com/s/twitter/r/93/

(sapling split of 9c817e9a999b02a819a200d90ed66aad4018d336)
  • Loading branch information
areitz committed Mar 13, 2014
1 parent 5e25143 commit 85c62b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/python/twitter/pants/targets/python_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from twitter.pants.base.build_manual import manual

from .python_target import PythonTarget


@manual.builddict(tags=["python"])
class PythonArtifact(object):
Expand Down
7 changes: 7 additions & 0 deletions src/python/twitter/pants/targets/python_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .with_dependencies import TargetWithDependencies
from .with_sources import TargetWithSources

from twitter.pants.targets.python_artifact import PythonArtifact

class PythonTarget(TargetWithDependencies, TargetWithSources):
"""Base class for all Python targets."""
Expand All @@ -41,7 +42,13 @@ def __init__(self,

self.add_labels('python')
self.resources = self._resolve_paths(resources) if resources else OrderedSet()

if provides and not isinstance(provides, PythonArtifact):
raise TargetDefinitionException(self,
"Target must provide a valid pants setup_py object. Received a '%s' object instead." %
provides.__class__.__name__)
self.provides = provides

self.compatibility = compatibility or ['']
for req in self.compatibility:
try:
Expand Down
12 changes: 12 additions & 0 deletions tests/python/twitter/pants/targets/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ python_test_suite(
pants(':jar_library'),
pants(':pants_target'),
pants(':python_binary'),
pants(':python_target'),
pants(':target'),
pants(':util'),
]
Expand Down Expand Up @@ -111,6 +112,17 @@ python_tests(
]
)

python_tests(
name = 'python_target',
sources = ['test_python_target.py'],
dependencies = [
pants('tests/python/twitter/pants:base-test'),
pants('src/python/twitter/pants/base:parse_context'),
pants('src/python/twitter/pants/base:target'),
pants('src/python/twitter/pants/targets:python'),
]
)

python_tests(
name = 'util',
sources = ['test_util.py'],
Expand Down
64 changes: 64 additions & 0 deletions tests/python/twitter/pants/targets/test_python_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ==================================================================================================
# Copyright 2014 Twitter, Inc.
# --------------------------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================

import os
import unittest

from textwrap import dedent

from twitter.pants.base.parse_context import ParseContext
from twitter.pants.base.target import TargetDefinitionException
from twitter.pants.base_build_root_test import BaseBuildRootTest
from twitter.pants.targets.artifact import Artifact
from twitter.pants.targets.python_target import PythonTarget
from twitter.pants.targets.python_artifact import PythonArtifact
from twitter.pants.targets.repository import Repository
from twitter.pants.targets.sources import SourceRoot


class PythonTargetTest(BaseBuildRootTest):

@classmethod
def setUpClass(self):
super(PythonTargetTest, self).setUpClass()
SourceRoot.register(os.path.realpath(os.path.join(self.build_root, 'test_python_target')),
PythonTarget)

self.create_target('test_thrift_replacement', dedent('''
python_thrift_library(name='one',
sources=['thrift/keyword.thrift'],
dependencies=None
)
'''))

def test_validation(self):
with ParseContext.temp('PythonTargetTest/test_validation'):

# Adding a JVM Artifact as a provides on a PythonTarget doesn't make a lot of sense. This test
# sets up that very scenario, and verifies that pants throws a TargetDefinitionException.
self.assertRaises(TargetDefinitionException, PythonTarget, name="one", sources=[],
provides=Artifact(org='com.twitter', name='one-jar',
repo=Repository(name='internal', url=None, push_db=None, exclusives=None)))

name = "test-with-PythonArtifact"
pa = PythonArtifact(name='foo', version='1.0', description='foo')

# This test verifies that adding a 'setup_py' provides to a PythonTarget is okay.
self.assertEquals(PythonTarget(name=name, provides=pa, sources=[]).name, name)
name = "test-with-none"

# This test verifies that having no provides is okay.
self.assertEquals(PythonTarget(name=name, provides=None, sources=[]).name, name)

0 comments on commit 85c62b2

Please sign in to comment.