Skip to content

Commit

Permalink
Update TargetFiltering args for applying criteria (#7280)
Browse files Browse the repository at this point in the history
Update the class to take the criteria in the constructor, and helper methods take the targets against which to apply said criteria.

From suggestion https://github.com/pantsbuild/pants/pull/7275\#discussion_r259554586
  • Loading branch information
codealchemy authored and jsirois committed Feb 25, 2019
1 parent a87a01b commit c095f3b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/python/pants/build_graph/target_filter_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ def register_options(cls, register):

def apply(self, targets):
exclude_tags = set(self.get_options().exclude_tags)
return TargetFiltering(targets, exclude_tags).apply_tag_blacklist()
return TargetFiltering(exclude_tags).apply_tag_blacklist(targets)


class TargetFiltering(object):
"""Apply filtering logic against targets."""

def __init__(self, targets, exclude_tags):
self.targets = targets
def __init__(self, exclude_tags):
self.exclude_tags = exclude_tags

def apply_tag_blacklist(self):
return [t for t in self.targets if not self.exclude_tags.intersection(t.tags)]
def apply_tag_blacklist(self, targets):
return [t for t in targets if not self.exclude_tags.intersection(t.tags)]
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ def test_filtering_single_tag(self):
b = self.make_target('b', tags=['skip-me'])
c = self.make_target('c', tags=['tag1', 'skip-me'])

filtered_targets = TargetFiltering([a, b, c], {'skip-me'}).apply_tag_blacklist()
filtered_targets = TargetFiltering({'skip-me'}).apply_tag_blacklist([a, b, c])
self.assertEqual([a], filtered_targets)

def test_filtering_multiple_tags(self):
a = self.make_target('a', tags=['tag1', 'skip-me'])
b = self.make_target('b', tags=['tag1', 'tag2', 'skip-me'])
c = self.make_target('c', tags=['tag2'])

filtered_targets = TargetFiltering([a, b, c], {'skip-me', 'tag2'}).apply_tag_blacklist()
filtered_targets = TargetFiltering({'skip-me', 'tag2'}).apply_tag_blacklist([a, b, c])
self.assertEqual([], filtered_targets)

def test_filtering_no_tags(self):
a = self.make_target('a', tags=['tag1'])
b = self.make_target('b', tags=['tag1', 'tag2'])
c = self.make_target('c', tags=['tag2'])

filtered_targets = TargetFiltering([a, b, c], set()).apply_tag_blacklist()
filtered_targets = TargetFiltering(set()).apply_tag_blacklist([a, b, c])
self.assertEqual([a, b, c], filtered_targets)

0 comments on commit c095f3b

Please sign in to comment.