From c5e03878142f70b2a990222e054e73351ea61dcf Mon Sep 17 00:00:00 2001 From: Andy Reitz Date: Tue, 9 Jun 2015 14:00:12 -0700 Subject: [PATCH] Only run junit when there are junit_test targets in the graph. Reviewed at https://rbcommons.com/s/twitter/r/2291/ --- .../pants/backend/jvm/tasks/junit_run.py | 7 ++++--- .../python/pants_test/backend/jvm/tasks/BUILD | 1 + .../backend/jvm/tasks/test_junit_run.py | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/python/pants/backend/jvm/tasks/junit_run.py b/src/python/pants/backend/jvm/tasks/junit_run.py index 71377eac87c..f32c0bae8ab 100644 --- a/src/python/pants/backend/jvm/tasks/junit_run.py +++ b/src/python/pants/backend/jvm/tasks/junit_run.py @@ -205,8 +205,9 @@ def _collect_test_targets(self, targets): java_tests_targets = list(self._test_target_candidates(targets)) tests_from_targets = dict(list(self._calculate_tests_from_targets(java_tests_targets))) - if self._tests_to_run: - # Find matching targets to any requested test. + if java_tests_targets and self._tests_to_run: + # If there are some junit_test targets in the graph, find ones that match the requested + # test(s). tests_with_targets = {} for test in self._get_tests_to_run(): # A test might contain #specific_method, which is not needed to find a target. @@ -762,7 +763,7 @@ def execute(self): # that missing sources can be detected early. for target in targets: if isinstance(target, junit_tests) and not target.payload.sources.source_paths: - msg = 'JavaTests target {} must include a non-empty set of sources.'.format(target.address.spec) + msg = 'JavaTests target must include a non-empty set of sources.' raise TargetDefinitionException(target, msg) self._runner.execute(targets) diff --git a/tests/python/pants_test/backend/jvm/tasks/BUILD b/tests/python/pants_test/backend/jvm/tasks/BUILD index 964c34e9b89..bfcf679a20a 100644 --- a/tests/python/pants_test/backend/jvm/tasks/BUILD +++ b/tests/python/pants_test/backend/jvm/tasks/BUILD @@ -131,6 +131,7 @@ python_tests( 'src/python/pants/backend/core/targets:common', 'src/python/pants/backend/jvm/targets:java', 'src/python/pants/backend/jvm/tasks:junit_run', + 'src/python/pants/backend/python/tasks:python', 'src/python/pants/base:build_file_aliases', 'src/python/pants/base:exceptions', 'src/python/pants/goal:products', diff --git a/tests/python/pants_test/backend/jvm/tasks/test_junit_run.py b/tests/python/pants_test/backend/jvm/tasks/test_junit_run.py index fa1d423a648..12a20b14117 100644 --- a/tests/python/pants_test/backend/jvm/tasks/test_junit_run.py +++ b/tests/python/pants_test/backend/jvm/tasks/test_junit_run.py @@ -13,6 +13,7 @@ from pants.backend.core.targets.resources import Resources from pants.backend.jvm.targets.java_tests import JavaTests from pants.backend.jvm.tasks.junit_run import JUnitRun +from pants.backend.python.targets.python_tests import PythonTests from pants.base.build_file_aliases import BuildFileAliases from pants.base.exceptions import TargetDefinitionException, TaskError from pants.goal.products import MultipleRootedProducts @@ -43,6 +44,7 @@ def alias_groups(self): return super(JUnitRunnerTest, self).alias_groups.merge(BuildFileAliases.create( targets={ 'java_tests': JavaTests, + 'python_tests': PythonTests, }, )) @@ -137,6 +139,21 @@ def execute_junit_runner(self, content): # Finally execute the task. self.execute(context) + def test_junit_runner_raises_no_error_on_non_junit_target(self): + """Run pants against a `python_tests` target, but set an option for the `test.junit` task. This + should execute without error. + """ + self.add_to_build_file('foo', dedent(''' + python_tests( + name='hello', + sources=['some_file.py'], + ) + ''' + )) + self.set_options(test='#abc') + task = self.create_task(self.context(target_roots=[self.target('foo:hello')])) + task.execute() + def test_empty_sources(self): self.add_to_build_file('foo', dedent(''' java_tests( @@ -147,7 +164,7 @@ def test_empty_sources(self): )) task = self.create_task(self.context(target_roots=[self.target('foo:empty')])) with self.assertRaisesRegexp(TargetDefinitionException, - r':empty must include a non-empty set of sources'): + r'must include a non-empty set of sources'): task.execute()