From d11fd6cb8878905a20805ad0cbcf2adecd84d212 Mon Sep 17 00:00:00 2001 From: Sameer Arora Date: Wed, 17 Jul 2019 23:15:25 -0700 Subject: [PATCH] removing scoverage target from BUILD.tools --- BUILD.tools | 6 -- new_blacklist_scoverage | 27 --------- src/python/pants/backend/jvm/subsystems/BUILD | 1 + .../jvm/subsystems/scala_coverage_platform.py | 58 ++++++++++++++----- .../test_scala_coverage_platform.py | 16 +++-- 5 files changed, 55 insertions(+), 53 deletions(-) delete mode 100644 new_blacklist_scoverage diff --git a/BUILD.tools b/BUILD.tools index 5eb84cebae93..1e0acbce5de4 100644 --- a/BUILD.tools +++ b/BUILD.tools @@ -27,12 +27,6 @@ jar_library(name = 'scrooge-linter', jar(org='com.twitter', name='scrooge-linter_2.11', rev=SCROOGE_REV) ]) -jar_library(name = 'scoverage', - jars = [ - scala_jar(org = 'com.twitter.scoverage', name = 'scalac-scoverage-plugin', rev = '1.0.1-twitter' ), - scala_jar(org = 'com.twitter.scoverage', name = 'scalac-scoverage-runtime', rev = '1.0.1-twitter' ), - ]) - # Google doesn't publish Kythe jars (yet?). So we publish them to a custom repo # (https://github.com/toolchainlabs/binhost) for now. See build-support/ivy/ivysettings.xml diff --git a/new_blacklist_scoverage b/new_blacklist_scoverage deleted file mode 100644 index 251aefee5c25..000000000000 --- a/new_blacklist_scoverage +++ /dev/null @@ -1,27 +0,0 @@ -src/scala/com/twitter/botmaker/knowndevices:knowndevices -bizinsights/src/main/scala/com/twitter/bizinsights/jobs/events_entrypoints:events-entrypoints -src/scala/com/twitter/tormenta_internal/util:util -src/java/com/twitter/ml/vw/main:main -src/java/com/twitter/ml/api:api-base -src/java/com/twitter/ads/internal/feature_switch:feature_switch -twml/runtime/src/main/scala/com/twitter/deepbird/runtime/prediction_engine:prediction_engine -src/scala/com/twitter/pluck/source:remote-sources -src/scala/com/twitter/pluck/source/cassowary:cassowary -3rdparty/src/jvm/com/twitter/storehaus:core -src/scala/com/twitter/ml/api/decision_forest:decision_forest -birdherd/src/main/scala:scala -src/scala/com/twitter/ads/dataservice/scalding:scalding -src/java/com/twitter/search/common/file:file -birdherd/integration/options/src/main/scala:scala -src/scala/com/twitter/frigate/data_pipeline/features_aggregated/extractor:extractor -src/scala/com/twitter/frigate/data_pipeline/scalding:push-ddg-metrics-job -ads-data-infrastructure/ec/ecnext/src/main/scala/com/twitter/ads/ecnext/migration/readers:readers -geoduck/data/ipindex/src/main/scala/com/twitter/geoduck/data/ipindex/api/common:common -tsar/core/src/main/scala:scala -ads-data-infrastructure/ec/ecnext/src/main/scala/com/twitter/ads/ecnext/migration/readers:readers -src/java/com/twitter/ads/common/config:configmap -emoji/scala/src/main/scala/com/twitter/emoji:emoji -finatra-internal/authorization/src/test/scala:test-deps -kafka/provisioning/client/src/main/scala:scala -appsec/sanitization-lib/src/main/scala:scala -a/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blackeda/scala:blacked \ No newline at end of file diff --git a/src/python/pants/backend/jvm/subsystems/BUILD b/src/python/pants/backend/jvm/subsystems/BUILD index 10252a62b631..07316ec9d56d 100644 --- a/src/python/pants/backend/jvm/subsystems/BUILD +++ b/src/python/pants/backend/jvm/subsystems/BUILD @@ -117,6 +117,7 @@ python_library( dependencies = [ 'src/python/pants/option', 'src/python/pants/subsystem', + 'src/python/pants/java/jar', 'src/python/pants/backend/jvm/targets:jvm', ], ) diff --git a/src/python/pants/backend/jvm/subsystems/scala_coverage_platform.py b/src/python/pants/backend/jvm/subsystems/scala_coverage_platform.py index e5d38064f86b..a7734e55c86a 100644 --- a/src/python/pants/backend/jvm/subsystems/scala_coverage_platform.py +++ b/src/python/pants/backend/jvm/subsystems/scala_coverage_platform.py @@ -1,12 +1,13 @@ -# 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, print_function, unicode_literals - +import os from pants.build_graph.injectables_mixin import InjectablesMixin from pants.subsystem.subsystem import Subsystem - +from pants.java.jar.jar_dependency import JarDependency +from pants.backend.jvm.targets.jar_library import JarLibrary +from pants.build_graph.address import Address +from typing import Any, Dict, List, Optional, Union SCOVERAGE = "scoverage" blacklist_file = 'new_blacklist_scoverage' @@ -26,22 +27,50 @@ def register_options(cls, register): help='Specifies whether to generate scoverage reports for scala test targets.' 'Default value is False') + register('--blacklist-file', + default=blacklist_file, + type=str, + help='Path to files containing targets not to be instrumented.') + register('--scoverage-target-path', default='//:scoverage', type=str, help='Path to the scoverage dependency.') + def scoverage_jar(self): + return [JarDependency(org='com.twitter.scoverage', name='scalac-scoverage-plugin_2.12', rev='1.0.1-twitter'), + JarDependency(org='com.twitter.scoverage', name='scalac-scoverage-runtime_2.12', rev='1.0.1-twitter')] + + + def injectables(self,build_graph): + specs_to_create = [ + ('scoverage',self.scoverage_jar), + ] + + for spec_key, create_jardep_func in specs_to_create: + spec = self.injectables_spec_for_key(spec_key) + target_address = Address.parse(spec) + if not build_graph.contains_address(target_address): + target_jars = create_jardep_func() + jars = target_jars if isinstance(target_jars, list) else [target_jars] + build_graph.inject_synthetic_target(target_address, + JarLibrary, + jars=jars, + scope='forced') + elif not build_graph.get_target(target_address).is_synthetic: + raise build_graph.ManualSyntheticTargetError(target_address) + + @property def injectables_spec_mapping(self): return { - 'scoverage': ['{}'.format(self.get_options().scoverage_target_path)], + 'scoverage': [f"{self.get_options().scoverage_target_path}"], } - def get_scalac_plugins(self, target): + def get_scalac_plugins(self, target) -> List[str]: """ Adds 'scoverage' to scalac_plugins in case scoverage is enabled for that [target]. :return: modified scalac_plugins - :rtype: list of strings """ # Prevent instrumenting generated targets and targets in blacklist. @@ -55,19 +84,18 @@ def get_scalac_plugins(self, target): scalac_plugins = [SCOVERAGE] return scalac_plugins - def get_scalac_plugin_args(self, target): + def get_scalac_plugin_args(self, target) -> Dict[str, List[str]]: """ Adds 'scoverage' to scalac_plugins_args in case scoverage is enabled for that [target]. :return: modified scalac_plugins_args - :rtype: map from string to list of strings. """ scalac_plugin_args = target.payload.scalac_plugin_args if scalac_plugin_args: scalac_plugin_args.update( - {"scoverage": ["writeToClasspath:true", "dataDir:{}".format(target.identifier)]}) + {"scoverage": ["writeToClasspath:true", f"dataDir:{target.identifier}"]}) else: scalac_plugin_args = { - "scoverage": ["writeToClasspath:true", "dataDir:{}".format(target.identifier)] + "scoverage": ["writeToClasspath:true", f"dataDir:{target.identifier}"] } return scalac_plugin_args @@ -75,7 +103,6 @@ def get_compiler_option_sets(self, target): """ Adds 'scoverage' to compiler_options_sets in case scoverage is enabled for that [target]. :return: modified compiler_option_sets - :rtype: see constructor """ compiler_option_sets = target.payload.compiler_option_sets if compiler_option_sets: @@ -84,11 +111,14 @@ def get_compiler_option_sets(self, target): compiler_option_sets = [SCOVERAGE] return tuple(compiler_option_sets) - def is_blacklisted(self, target): + def is_blacklisted(self, target) -> bool: """ Checks if the [target] is blacklisted or not. """ - if target.address.spec in open(blacklist_file).read(): + if not os.path.exists(self.get_options().blacklist_file): + return False + + if target.address.spec in open(self.get_options().blacklist_file).read(): return True else: return False diff --git a/tests/python/pants_test/backend/jvm/subsystems/test_scala_coverage_platform.py b/tests/python/pants_test/backend/jvm/subsystems/test_scala_coverage_platform.py index 86e7acdf4935..02f5247e82bc 100644 --- a/tests/python/pants_test/backend/jvm/subsystems/test_scala_coverage_platform.py +++ b/tests/python/pants_test/backend/jvm/subsystems/test_scala_coverage_platform.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) +import os from pants_test.test_base import TestBase from pants_test.subsystem.subsystem_util import init_subsystem from pants.backend.jvm.subsystems.scala_coverage_platform import ScalaCoveragePlatform @@ -15,6 +16,7 @@ class ScalaCoveragePlatformTest(TestBase): scoverage_path = '//:scoverage' + blacklist_file_path = 'my/file/new_blacklist_scoverage_test' def setup_scala_coverage_platform(self): options = { @@ -56,10 +58,6 @@ def setup_scala_coverage_platform(self): JarLibrary, jars=[JarDependency('org.scala-lang', 'scala-library', '2.10.5')]) - self.make_target('//:scoverage', - JarLibrary, - jars=[JarDependency('com.twitter.scoverage', 'scalac-scoverage-plugin', '1.0.1-twitter'), - JarDependency('com.twitter.scoverage', 'scalac-scoverage-runtime', '1.0.1-twitter')]) # ==========> TESTS <============= @@ -141,8 +139,13 @@ def test_blacklist(self): self.setup_scala_coverage_platform() ScalaCoveragePlatform.global_instance().get_options().enable_scoverage = True - blacklist_file = open("new_blacklist_scoverage", "a+") - blacklist_file.write("a/scala:blacked") + tmp = self.create_file( + relpath=self.blacklist_file_path, + contents=dedent(""" + a/scala:blacked + """) + ) + ScalaCoveragePlatform.global_instance().get_options().blacklist_file = tmp self.create_file( relpath='a/scala/pass.scala', @@ -159,3 +162,4 @@ def main(args: Array[String]) { self.assertNotIn('scoverage', scala_target.scalac_plugins) +