def test_blacklist(self): """ When a target is blacklisted, we do not instrument it. For achieving that, we only want `scalac_plugins` to not contain `scoverage`. Thus, the target may still have `scoverage` in `scalac_plugin_args` and in `dependencies` but it will not be instrumented as long as `scalac_plugins` do not contain `scoverage`. :return: """ self.setup_scoverage_platform() ScoveragePlatform.global_instance().get_options( ).enable_scoverage = True ScoveragePlatform.global_instance().get_options().blacklist_targets = [ "blacked" ] self.create_file(relpath='a/scala/pass.scala', contents=dedent(""" import java.util object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } """)) scala_target = self.make_target('a/scala:blacked', ScalaLibrary, sources=['pass.scala']) self.assertNotIn('scoverage', scala_target.scalac_plugins)
def test_subsystem_option_sets(self): init_subsystem(ScoveragePlatform) ScoveragePlatform.global_instance().get_options( ).enable_scoverage = True subsystem = ScoveragePlatform.global_instance() self.assertEqual(True, subsystem.get_options().enable_scoverage)
def compute_dependency_specs(cls, kwargs=None, payload=None): for spec in super().compute_dependency_specs(kwargs, payload): yield spec for spec in ScalaPlatform.global_instance().injectables_specs_for_key( 'scala-library'): yield spec if ScoveragePlatform.global_instance().get_options().enable_scoverage: for spec in ScoveragePlatform.global_instance( ).injectables_specs_for_key('scoverage'): yield spec
def test_library_scoverage_enabled(self): self.setup_scoverage_platform() ScoveragePlatform.global_instance().get_options( ).enable_scoverage = True self.create_file(relpath='a/scala/pass.scala', contents=dedent(""" import java.util object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } """)) scala_target = self.make_target('a/scala:pass', ScalaLibrary, sources=['pass.scala']) self.assertIn('scoverage', scala_target.scalac_plugins) self.assertIn('scoverage', scala_target.scalac_plugin_args) self.assertIn( '//:scoverage', list(map(lambda t: t.address.spec, scala_target.dependencies))) self.assertIn('scoverage', list(scala_target.compiler_option_sets))
def __init__( self, java_sources=None, scalac_plugins=None, scalac_plugin_args=None, compiler_option_sets=None, payload=None, **kwargs, ): """ :param java_sources: Java libraries this library has a *circular* dependency on. If you don't have the particular problem of circular dependencies forced by splitting interdependent java and scala into multiple targets, don't use this at all. Prefer using ``dependencies`` to express non-circular dependencies. :type java_sources: address spec or list of address specs :param resources: An optional list of paths (DEPRECATED) or ``resources`` targets containing resources that belong on this library's classpath. """ payload = payload or Payload() payload.add_fields( { "java_sources": PrimitiveField( self.assert_list(java_sources, key_arg="java_sources") ), } ) scalac_plugins = scalac_plugins or [] scalac_plugin_args = scalac_plugin_args or {} # Allow lists as well as sets in BUILD files: compiler_option_sets = set(compiler_option_sets) if compiler_option_sets else set() # Modify these options in case scoverage is enabled. if ScoveragePlatform.global_instance().get_options().enable_scoverage: if not self.skip_instrumentation(**kwargs): scalac_plugins.append(SCOVERAGE) scalac_plugin_args.update( { "scoverage": [ "writeToClasspath:true", f"dataDir:{Target.compute_target_id(kwargs['address'])}", ] } ) compiler_option_sets.update([SCOVERAGE]) super().__init__( payload=payload, scalac_plugins=scalac_plugins, scalac_plugin_args=scalac_plugin_args, compiler_option_sets=compiler_option_sets, **kwargs, )
def get_coverage_engine(self, task, output_dir, all_targets, execute_java): options = task.get_options() enable_scoverage = ScoveragePlatform.global_instance().get_options( ).enable_scoverage processor = options.coverage_processor if (processor == 'scoverage' and not enable_scoverage): raise self.InvalidCoverageEngine( "Cannot set processor to scoverage without first enabling " "scoverage (by passing --scoverage-enable-scoverage option)") if enable_scoverage: if processor not in (None, 'scoverage'): raise self.InvalidCoverageEngine( f"Scoverage is enabled. " f"Cannot use {processor} as the engine. Set engine to scoverage " f"(--test-junit-coverage-processor=scoverage)") processor = 'scoverage' if options.coverage or processor or options.is_flagged( 'coverage_open'): settings = CodeCoverageSettings.from_task(task, workdir=output_dir) if processor in ('cobertura', None): return Cobertura.Factory.global_instance().create( settings, all_targets, execute_java) elif processor == 'jacoco': return Jacoco.Factory.global_instance().create( settings, all_targets, execute_java) elif processor == 'scoverage': return Scoverage.Factory.global_instance().create( settings, all_targets, execute_java) else: # NB: We should never get here since the `--coverage-processor` is restricted by `choices`, # but for clarity. raise self.InvalidCoverageEngine( 'Unknown and unexpected coverage processor {!r}!'.format( options.coverage_processor)) else: return NoCoverage()
def test_subsystem_defaults(self): init_subsystem(ScoveragePlatform) subsystem = ScoveragePlatform.global_instance() self.assertEqual(False, subsystem.get_options().enable_scoverage)
def skip_instrumentation(**kwargs): return Target.compute_target_id(kwargs["address"]).startswith( ".pants.d.gen") or ScoveragePlatform.global_instance( ).is_blacklisted(kwargs["address"].spec)