Skip to content
Permalink
Browse files

Update TargetFiltering args for applying criteria (pantsbuild#7280)

Update the class to take the criteria in the constructor, and helper methods take the targets against which to apply said criteria.

From suggestion pantsbuild#7275
  • Loading branch information...
codealchemy authored and jsirois committed Feb 25, 2019
1 parent a87a01b commit c095f3b76d3deddcf4bb5cfd775754a2f81dd79f
@@ -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)]
@@ -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.
You can’t perform that action at this time.