forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize finding target types for an alias.
Both `ReverseDepmap` and `Filter` tasks were doing this slightly differently, but now they share an implementation via a mixin. Add tests for the mixin and move the test_dependees.py and test_filter.py tests to their proper home along side it. Also cleans up docs and whitespace along the way. Testing Done: CI went green here: https://travis-ci.org/pantsbuild/pants/builds/79766329 Bugs closed: 2169 Reviewed at https://rbcommons.com/s/twitter/r/2796/
- Loading branch information
Showing
11 changed files
with
157 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/python/pants/backend/core/tasks/target_filter_task_mixin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# coding=utf-8 | ||
# Copyright 2015 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.core.tasks.task import Task | ||
from pants.base.exceptions import TaskError | ||
|
||
|
||
class TargetFilterTaskMixin(Task): | ||
"""A Task mixin that provides methods to help with filtering by target type.""" | ||
|
||
class InvalidTargetType(TaskError): | ||
"""Indicates a target type name that is not registered or does not point to a `Target` type.""" | ||
|
||
def target_types_for_alias(self, alias): | ||
"""Returns all the target types that might be produced by the given alias. | ||
Normally there is 1 target type per alias, but macros can expand a single alias to several | ||
target types. | ||
:param string alias: The alias to look up associated target types for. | ||
:returns: The set of target types that can be produced by the given alias. | ||
:raises :class:`TargetFilterTaskMixin.InvalidTargetType`: when no target types correspond to | ||
the given `alias`. | ||
""" | ||
registered_aliases = self.context.build_file_parser.registered_aliases() | ||
target_types = registered_aliases.target_types_by_alias.get(alias, None) | ||
if not target_types: | ||
raise self.InvalidTargetType('Not a target type: {}'.format(alias)) | ||
return target_types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions
57
tests/python/pants_test/backend/core/tasks/test_target_filter_task_mixin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# coding=utf-8 | ||
# Copyright 2015 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.core.tasks.target_filter_task_mixin import TargetFilterTaskMixin | ||
from pants.base.build_file_aliases import BuildFileAliases, TargetMacro | ||
from pants.base.target import Target | ||
from pants_test.tasks.task_test_base import TaskTestBase | ||
|
||
|
||
class TargetFilterTaskMixinTest(TaskTestBase): | ||
|
||
@classmethod | ||
def task_type(cls): | ||
class TestTargetFilteringTask(TargetFilterTaskMixin): | ||
def execute(self): | ||
raise NotImplementedError() | ||
return TestTargetFilteringTask | ||
|
||
class RedTarget(Target): | ||
pass | ||
|
||
class BlueTarget(Target): | ||
pass | ||
|
||
class GreenTarget(Target): | ||
pass | ||
|
||
@property | ||
def alias_groups(self): | ||
purple_macro = TargetMacro.Factory.wrap(lambda ctx: None, self.RedTarget, self.BlueTarget) | ||
return BuildFileAliases(targets={'green': self.GreenTarget, 'purple': purple_macro}, | ||
objects={'red': object()}, | ||
context_aware_object_factories={'blue': lambda ctx: None}) | ||
|
||
def setUp(self): | ||
super(TargetFilterTaskMixinTest, self).setUp() | ||
self.task = self.create_task(self.context()) | ||
|
||
def test_simple_alias(self): | ||
green_targets = self.task.target_types_for_alias('green') | ||
self.assertEqual({self.GreenTarget}, green_targets) | ||
|
||
def test_macro_alias(self): | ||
purple_targets = self.task.target_types_for_alias('purple') | ||
self.assertEqual({self.RedTarget, self.BlueTarget}, purple_targets) | ||
|
||
def test_alias_miss(self): | ||
with self.assertRaises(self.task.InvalidTargetType): | ||
self.task.target_types_for_alias('red') # Not a target - an object. | ||
with self.assertRaises(self.task.InvalidTargetType): | ||
self.task.target_types_for_alias('blue') # Not a target - a context aware object factory. | ||
with self.assertRaises(self.task.InvalidTargetType): | ||
self.task.target_types_for_alias('yellow') # Not a registered alias. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters