Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
updated bazel targets expander to the new affected/unaffected targets…
Browse files Browse the repository at this point in the history
… input format

Summary: The collect targets script now return two sets of targets: affected and unaffected. We are not yet doing any kind of selective testing, so this diff just takes the two lists and combines them.

Test Plan: unit tests

Reviewers: anupc

Reviewed By: anupc

Subscribers: changesbot, kylec, wwu

Differential Revision: https://tails.corp.dropbox.com/D229757
  • Loading branch information
Naphat Sanguansin committed Sep 20, 2016
1 parent 23d6bce commit 2224953
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
24 changes: 16 additions & 8 deletions changes/expanders/bazel_targets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import absolute_import
from __future__ import absolute_import, division

from typing import Dict, List, Tuple # NOQA

from changes.api.client import api_client
from changes.config import db
from changes.config import db, statsreporter
from changes.expanders.base import Expander
from changes.models.bazeltarget import BazelTarget
from changes.models.command import FutureCommand
Expand All @@ -15,32 +15,40 @@
class BazelTargetsExpander(Expander):
"""
The ``cmd`` value must exist (and contain {target_names}) as well as the
``targets`` attribute which must be a list of strings:
``affected_targets`` and ``unaffected_targets`` attribute which must be a list of strings:
{
"phase": "optional phase name",
"cmd": "bazel test {target_names}",
"path": "",
"targets": [
"affected_targets": [
"foo.bar.test_baz",
"foo.bar.test_bar"
],
"unaffected_targets": [
"foo.other.test_baz",
"foo.other.test_bar"
],
"artifact_search_path": "search path for artifact, required"
}
"""

def validate(self):
assert 'targets' in self.data, 'Missing ``targets`` attribute'
assert 'cmd' in self.data, 'Missing ``cmd`` attribute'
for required in ['affected_targets', 'unaffected_targets', 'cmd', 'artifact_search_path']:
assert required in self.data, 'Missing ``{}`` attribute'.format(required)
assert '{target_names}' in self.data[
'cmd'], 'Missing ``{target_names}`` in command'
assert 'artifact_search_path' in self.data, 'Missing ``artifact_search_path`` attribute'

def expand(self, max_executors, test_stats_from=None):
target_stats, avg_time = self.get_target_stats(
test_stats_from or self.project.slug)

groups = shard(self.data['targets'], max_executors,
affected_targets = self.data['affected_targets']
unaffected_targets = self.data['unaffected_targets']
all_targets = affected_targets + unaffected_targets
statsreporter.stats().set_gauge('{}_bazel_affected_targets_count'.format(self.project.slug), len(affected_targets))
statsreporter.stats().set_gauge('{}_bazel_all_targets_count'.format(self.project.slug), len(all_targets))
groups = shard(all_targets, max_executors,
target_stats, avg_time)

for weight, target_list in groups:
Expand Down
28 changes: 20 additions & 8 deletions tests/changes/expanders/test_bazel_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ def test_validate(self):
self.get_expander({}).validate()

with pytest.raises(AssertionError):
self.get_expander({'targets': []}).validate()
self.get_expander({'affected_targets': []}).validate()

with pytest.raises(AssertionError):
self.get_expander({'cmd': 'echo 1', 'targets': []}).validate()
self.get_expander({'cmd': 'echo 1', 'affected_targets': []}).validate()

with pytest.raises(AssertionError):
self.get_expander(
{'cmd': 'echo {target_names}', 'targets': []}).validate()
{'cmd': 'echo {target_names}', 'affected_targets': []}).validate()

with pytest.raises(AssertionError):
self.get_expander({
'affected_targets': [],
'cmd': 'echo {target_names}',
'artifact_search_path': 'path'
}).validate()

self.get_expander({
'targets': [],
'affected_targets': [],
'unaffected_targets': [],
'cmd': 'echo {target_names}',
'artifact_search_path': 'path'
}).validate()
Expand Down Expand Up @@ -76,9 +84,11 @@ def test_expand(self, mock_get_target_stats):

results = list(self.get_expander({
'cmd': 'bazel test {target_names}',
'targets': [
'affected_targets': [
'//foo/bar:test',
'//foo/baz:target',
],
'unaffected_targets': [
'//foo/bar/test_biz:test',
'//foo/bar/test_buz:test',
],
Expand Down Expand Up @@ -118,16 +128,18 @@ def test_expand_no_duration(self, mock_get_target_stats):

results = list(self.get_expander({
'cmd': 'bazel test {target_names}',
'targets': [
'affected_targets': [
'//foo/bar:test',
'//foo/baz:target',
'//foo/bar/test_biz:test',
'//foo/bar/test_buz:test',

# if a target has no duration, it's not added to the target
# stats dictionary
'//foo/bar/baz:test',
],
'unaffected_targets': [
'//foo/bar/test_biz:test',
'//foo/bar/test_buz:test',
],
'artifact_search_path': 'artifacts/'
}).expand(max_executors=2))

Expand Down

0 comments on commit 2224953

Please sign in to comment.