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

Commit

Permalink
Clean up junit xml report file location logic.
Browse files Browse the repository at this point in the history
This introduces a backport of itertools.accumulate and then uses it to
make the logic of scanning from more specific nested class report files
to less specific (containing class) ones a bit more coherent.

Testing Done:
Locally green:
```
./pants test \
    tests/python/pants_test/util:iterators \
    tests/python/pants_test/backend/jvm/tasks:junit_run \
    tests/python/pants_test/backend/jvm/tasks:junit_run_integration \
    tests/python/pants_test/backend/jvm/tasks:junit_tests_concurrency_integration \
    tests/python/pants_test/backend/jvm/tasks:junit_tests_integration
```

CI went green here:
  https://travis-ci.org/pantsbuild/pants/builds/158514392

Bugs closed: 3837, 3844

Reviewed at https://rbcommons.com/s/twitter/r/4211/
  • Loading branch information
jsirois committed Sep 9, 2016
1 parent 19a0dbb commit e834510
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/python/pants/backend/jvm/tasks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ python_library(
'src/python/pants/util:argutil',
'src/python/pants/util:contextutil',
'src/python/pants/util:desktop',
'src/python/pants/util:iterators',
'src/python/pants/util:strutil',
'src/python/pants/util:xml_parser',
],
Expand Down
9 changes: 4 additions & 5 deletions src/python/pants/backend/jvm/tasks/junit_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pants.util import desktop
from pants.util.argutil import ensure_arg, remove_arg
from pants.util.contextutil import environment_as
from pants.util.iterators import accumulate
from pants.util.strutil import pluralize
from pants.util.xml_parser import XmlParser

Expand Down Expand Up @@ -331,15 +332,13 @@ def get_test_filename(test_class_name):
if target is None:
self.context.log.warn('Unknown target for test %{0}'.format(test))

# Look for a TEST-*.xml file that matches the classname or a containing classname
test_class_name = test
for _part in test.split('$'):
# Look for a TEST-*.xml file that matches the classname or a containing classname.
class_names = reversed(list(accumulate(test.split('$'), func=lambda x, y: x + '$' + y)))
for test_class_name in class_names:
filename = get_test_filename(test_class_name)
if os.path.exists(filename):
xml_filenames_to_targets[filename] = target
break
else:
test_class_name = test_class_name.rsplit('$', 1)[0]

failed_targets = defaultdict(set)
for xml_filename, target in xml_filenames_to_targets.items():
Expand Down
6 changes: 5 additions & 1 deletion src/python/pants/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ python_library(
python_library(
name = 'filtering',
sources = ['filtering.py'],
dependencies = [],
)

python_library(
name = 'iterators',
sources = ['iterators.py'],
)

python_library(
Expand Down
31 changes: 31 additions & 0 deletions src/python/pants/util/iterators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# coding=utf-8
# Copyright 2016 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)


try:
from itertools import accumulate # Present in python3 stdlib.
except ImportError:
import operator


def accumulate(items, func=operator.add):
"""Reduce items yielding the initial item and then each reduced value after that.
:param items: A possibly empty iterable.
:param func: A binary operator that can combine items.
:returns: An iterator over the first item if any and subsequent applications of `func` to the
running "total".
"""
iterator = iter(items)
try:
total = next(iterator)
except StopIteration:
return # The items iterable is empty.`
yield total
for item in iterator:
total = func(total, item)
yield total
8 changes: 8 additions & 0 deletions tests/python/pants_test/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ python_tests(
]
)

python_tests(
name = 'iterators',
sources = ['test_iterators.py'],
dependencies = [
'src/python/pants/util:iterators',
]
)

python_tests(
name = 'memo',
sources = ['test_memo.py'],
Expand Down
25 changes: 25 additions & 0 deletions tests/python/pants_test/util/test_iterators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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)

import unittest

from pants.util.iterators import accumulate


class AccumulateTest(unittest.TestCase):
def test_empty(self):
self.assertEqual([], list(accumulate(())))

def test_single(self):
self.assertEqual([42], list(accumulate((42,))))

def test_nominal(self):
self.assertEqual([1, 2, 3], list(accumulate((1, 1, 1))))

def test_heterogeneous(self):
self.assertEqual([1, '11', '111'], list(accumulate((1, 1, 1),
func=lambda x, y: str(x) + str(y))))

0 comments on commit e834510

Please sign in to comment.