Skip to content

Commit

Permalink
removing scoverage target from BUILD.tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameer Arora committed Jul 18, 2019
1 parent 081ff68 commit d11fd6c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
6 changes: 0 additions & 6 deletions BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 0 additions & 27 deletions new_blacklist_scoverage

This file was deleted.

1 change: 1 addition & 0 deletions src/python/pants/backend/jvm/subsystems/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
)
Expand Down
58 changes: 44 additions & 14 deletions src/python/pants/backend/jvm/subsystems/scala_coverage_platform.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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.
Expand All @@ -55,27 +84,25 @@ 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

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:
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 <=============
Expand Down Expand Up @@ -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',
Expand All @@ -159,3 +162,4 @@ def main(args: Array[String]) {

self.assertNotIn('scoverage', scala_target.scalac_plugins)


0 comments on commit d11fd6c

Please sign in to comment.