def test_create_canonical_classpath(self): a = self.make_target('a/b', JvmTarget) jar_path = 'ivy/jars/org.x/lib/x-1.0.jar' jar_path_excluded = 'ivy/jars/org.y/lib/y-1.0.jar' classpath_products = ClasspathProducts(self.pants_workdir) resolved_jar = ResolvedJar(M2Coordinate(org='org', name='x', rev='1.0'), cache_path='somewhere', pants_path=self._path(jar_path)) # org.y should be excluded from result canonical path resolved_jar_to_exclude = ResolvedJar( M2Coordinate(org='org', name='y', rev='1.0'), cache_path='somewhere', pants_path=self._path(jar_path_excluded)) classpath_products.add_for_target( a, [('default', self._path('a.jar')), ('default', self._path('resources'))]) classpath_products.add_jars_for_targets([a], 'default', [resolved_jar]) with temporary_dir() as base_dir: self._test_canonical_classpath_helper( classpath_products, [a], base_dir, True, [ 'a.b.b-0.jar', 'a.b.b-1', 'a.b.b-2.jar', ], { 'a.b.b-classpath.txt': '{}/a.jar:{}/resources:{}/{}\n'.format( self.pants_workdir, self.pants_workdir, self.pants_workdir, jar_path) }, excludes=set([Exclude(org='org', name='y')])) # incrementally delete the resource dendendency classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar'))]) self._test_canonical_classpath_helper( classpath_products, [a], base_dir, True, [ 'a.b.b-0.jar', ], {'a.b.b-classpath.txt': '{}/a.jar\n'.format(self.pants_workdir)}) # incrementally add another jar dependency classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar')), ('default', self._path('b.jar'))]) self._test_canonical_classpath_helper( classpath_products, [a], base_dir, True, ['a.b.b-0.jar', 'a.b.b-1.jar'], { 'a.b.b-classpath.txt': '{}/a.jar:{}/b.jar\n'.format(self.pants_workdir, self.pants_workdir) })
def test_create_canonical_classpath(self): a = self.make_target('a/b', JvmTarget) classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar')), ('default', self._path('resources'))]) with temporary_dir() as base_dir: self._test_canonical_classpath_helper(classpath_products, [a], base_dir, True, [ 'a.b.b-0.jar', 'a.b.b-1' ], { 'a.b.b-classpath.txt': '{}/a.jar:{}/resources\n'.format(self.pants_workdir, self.pants_workdir) }) # incrementally delete the resource dendendency classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar'))]) self._test_canonical_classpath_helper(classpath_products, [a], base_dir, True, [ 'a.b.b-0.jar', ], { 'a.b.b-classpath.txt': '{}/a.jar\n'.format(self.pants_workdir) }) # incrementally add another jar dependency classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar')), ('default', self._path('b.jar'))]) self._test_canonical_classpath_helper(classpath_products, [a], base_dir, True, [ 'a.b.b-0.jar', 'a.b.b-1.jar' ], { 'a.b.b-classpath.txt': '{}/a.jar:{}/b.jar\n'.format(self.pants_workdir, self.pants_workdir) })
def test_classpath_by_targets(self): b = self.make_target("b", JvmTarget) a = self.make_target("a", JvmTarget, dependencies=[b], excludes=[Exclude("com.example", "lib")]) classpath_products = ClasspathProducts(self.pants_workdir) path1 = self._path("jar/path1") path2 = self._path("jar/path2") path3 = os.path.join(self.pants_workdir, "jar/path3") resolved_jar = ResolvedJar( M2Coordinate(org="com.example", name="lib", rev="1.0"), cache_path="somewhere", pants_path=path3, ) classpath_products.add_for_target(a, [("default", path1)]) classpath_products.add_for_target(a, [("non-default", path2)]) classpath_products.add_for_target(b, [("default", path2)]) classpath_products.add_jars_for_targets([b], "default", [resolved_jar]) classpath_products.add_excludes_for_targets([a]) # (a, path2) filtered because of conf # (b, path3) filtered because of excludes self.assertEqual( OrderedDict([(a, [ClasspathEntry(path1)]), (b, [ClasspathEntry(path2)])]), ClasspathUtil.classpath_by_targets(a.closure(bfs=True), classpath_products), )
def test_create_canonical_classpath_no_duplicate_entry(self): """Test no more than one symlink are created for the same classpath entry.""" jar_path = "ivy/jars/org.x/lib/x-1.0.jar" resolved_jar = ResolvedJar( M2Coordinate(org="org", name="x", rev="1.0"), cache_path="somewhere", pants_path=self._create_file(jar_path), ) target_a = self.make_target("a", JvmTarget) target_b = self.make_target("b", JvmTarget) classpath_products = ClasspathProducts(self.pants_workdir) # Both target a and target b depend on the same jar library classpath_products.add_jars_for_targets([target_a], "default", [resolved_jar]) classpath_products.add_jars_for_targets([target_b], "default", [resolved_jar]) with temporary_dir() as base_dir: # Only target a generates symlink to jar library, target b skips creating the # symlink for the same jar library. Both targets' classpath.txt files should # still contain the jar library. self._test_canonical_classpath_helper( classpath_products, [target_a, target_b], base_dir, ["a.a-0.jar"], { "a.a-classpath.txt": f"{self.pants_workdir}/{jar_path}\n", "b.b-classpath.txt": f"{self.pants_workdir}/{jar_path}\n", }, )
def test_get_artifact_classpath_entries_for_targets(self): b = self.make_target("b", JvmTarget, excludes=[Exclude("com.example", "lib")]) a = self.make_target("a", JvmTarget, dependencies=[b]) classpath_product = ClasspathProducts(self.pants_workdir) example_jar_path = self._example_jar_path() resolved_jar = self.add_jar_classpath_element_for_path( classpath_product, a, example_jar_path) # These non-artifact classpath entries should be ignored. classpath_product.add_for_target( b, [("default", self.path("b/loose/classes/dir"))]) classpath_product.add_for_target( a, [ ("default", self.path("a/loose/classes/dir")), ("default", self.path("an/internally/generated.jar")), ], ) classpath = classpath_product.get_artifact_classpath_entries_for_targets( [a]) self.assertEqual( [( "default", ArtifactClasspathEntry(example_jar_path, resolved_jar.coordinate, resolved_jar.cache_path), )], classpath, )
def resolve_jars(self, targets): # TODO: Why is this computed directly here instead of taking from the actual product # computed by the {Ivy,Coursier}Resolve task? executor = SubprocessExecutor(DistributionLocator.cached()) confs = [] if self.get_options().libraries: confs.append("default") if self.get_options().libraries_sources: confs.append("sources") if self.get_options().libraries_javadocs: confs.append("javadoc") compile_classpath = None if confs: compile_classpath = ClasspathProducts( self.get_options().pants_workdir) CoursierMixin.resolve( self, targets, compile_classpath, sources=self.get_options().libraries_sources, javadoc=self.get_options().libraries_javadocs, executor=executor, ) return compile_classpath
def test_target_with_multiple_path_entries(self): """ :API: public """ options = attrdict(coverage=True, coverage_jvm_options=[]) syscalls = MockSystemCalls() settings = self.get_settings(options, self.pants_workdir, fake_log(), syscalls) classpath_products = ClasspathProducts(self.pants_workdir) self._add_for_target(classpath_products, self.java_target, '/java/target/first.jar') self._add_for_target(classpath_products, self.java_target, '/java/target/second.jar') self._add_for_target(classpath_products, self.java_target, '/java/target/third.jar') Cobertura.initialize_instrument_classpath(settings, [self.java_target], classpath_products) self.assertEquals( len(syscalls.copy2_calls), 3, 'Should be 3 call for the single java_library target.') self._assert_target_copy(syscalls, '/java/target/first.jar', '/coverage/classes/foo.foo-java/0') self._assert_target_copy(syscalls, '/java/target/second.jar', '/coverage/classes/foo.foo-java/1') self._assert_target_copy(syscalls, '/java/target/third.jar', '/coverage/classes/foo.foo-java/2') self.assertEquals( len(syscalls.copytree_calls), 0, 'Should be no copytree calls when targets are not coverage targets.' )
def resolve_jars(self, targets): # TODO: Why is this computed directly here instead of taking from the actual product # computed by the {Ivy,Coursier}Resolve task? executor = SubprocessExecutor(DistributionLocator.cached()) confs = [] if self.get_options().libraries: confs.append('default') if self.get_options().libraries_sources: confs.append('sources') if self.get_options().libraries_javadocs: confs.append('javadoc') compile_classpath = None if confs: compile_classpath = ClasspathProducts( self.get_options().pants_workdir) if JvmResolveSubsystem.global_instance().get_options( ).resolver == 'ivy': IvyTaskMixin.resolve(self, executor=executor, targets=targets, classpath_products=compile_classpath, confs=confs) else: CoursierMixin.resolve( self, targets, compile_classpath, sources=self.get_options().libraries_sources, javadoc=self.get_options().libraries_javadocs, executor=executor) return compile_classpath
def test_skips_non_coverage_targets(self): """ :API: public """ options = attrdict(coverage=True, coverage_jvm_options=[]) syscalls = MockSystemCalls() settings = self.get_settings(options, self.pants_workdir, fake_log(), syscalls) classpath_products = ClasspathProducts(self.pants_workdir) self._add_for_target(classpath_products, self.jar_lib, '/jar/lib/classpath') self._add_for_target(classpath_products, self.binary_target, '/binary/target/classpath') self._add_for_target(classpath_products, self.app_target, '/app/target/classpath') self._add_for_target(classpath_products, self.java_target, '/java/target/classpath.jar') Cobertura.initialize_instrument_classpath(settings, [ self.jar_lib, self.binary_target, self.app_target, self.java_target ], classpath_products) self.assertEquals( len(syscalls.copy2_calls), 1, 'Should only be 1 call for the single java_library target.') self._assert_target_copy(syscalls, '/java/target/classpath.jar', '/coverage/classes/foo.foo-java/0') self.assertEquals( len(syscalls.copytree_calls), 0, 'Should be no copytree calls when targets are not coverage targets.' )
def resolve_jars(self, targets): executor = SubprocessExecutor(DistributionLocator.cached()) confs = [] if self.get_options().libraries: confs.append('default') if self.get_options().libraries_sources: confs.append('sources') if self.get_options().libraries_javadocs: confs.append('javadoc') # TODO(gmalmquist): This is a terrible hack for backwards-compatibility with the pants-plugin. # Kill it ASAP, and update test_export_integration#test_export_jar_path_with_excludes_soft to # use the flag actually scoped for this task. export_options = self.get_options() try: ivy_options = self.context.options.for_scope('resolve.ivy') except OptionsError: # No resolve.ivy task installed, so continue silently. ivy_options = [] for name in set.intersection(set(export_options), set(ivy_options)): if not ivy_options.is_default(name): setattr(export_options, name, RankedValue(RankedValue.FLAG, ivy_options[name])) confs = confs or export_options.confs compile_classpath = None if confs: compile_classpath = ClasspathProducts(self.get_options().pants_workdir) self.resolve(executor=executor, targets=targets, classpath_products=compile_classpath, confs=confs) return compile_classpath
def _unpack(self, unpacked_archives): """Extracts files from the downloaded jar files and places them in a work directory. :param UnpackedArchives unpacked_archives: target referencing jar_libraries to unpack. """ self.context.log.info('Unpacking {}'.format( unpacked_archives.address.spec)) unpack_dir = unpacked_archives.destination safe_mkdir(unpack_dir, clean=True) unpack_filter = self.get_unpack_filter(unpacked_archives) classpath_products = ClasspathProducts( self.get_options().pants_workdir) resolve_hashes = self.resolve(None, unpacked_archives.dependencies, classpath_products) ivy_cache_dir = os.path.expanduser( IvySubsystem.global_instance().get_options().cache_dir) def to_m2(jar): return M2Coordinate(org=jar.org, name=jar.name, rev=jar.rev, classifier=jar.classifier, ext=jar.ext) libraries = self.context.build_graph.transitive_subgraph_of_addresses( [unpacked_archives.address]) libraries = [t for t in libraries if isinstance(t, JarLibrary)] coords = set() for library in libraries: coords.update(to_m2(jar) for jar in library.payload.jars) for resolve_hash in resolve_hashes: path = IvyUtils.xml_report_path(ivy_cache_dir, resolve_hash, 'default') info = IvyUtils.parse_xml_report('default', path) refs_for_libraries = set() for ref in info.modules_by_ref.keys(): if to_m2(ref) in coords: refs_for_libraries.add(ref) memo = {} for ref in tuple(refs_for_libraries): info.traverse_dependency_graph(ref, refs_for_libraries.add, memo) for ref in sorted(refs_for_libraries): module = info.modules_by_ref[ref] artifact_path = module.artifact self.context.log.debug('Extracting {} to {}.'.format( to_m2(ref), unpack_dir)) if artifact_path.endswith('.zip') or artifact_path.endswith( '.jar'): ZIP.extract(artifact_path, unpack_dir, filter_func=unpack_filter) else: self._extract_tar(artifact_path, unpack_dir, filter_func=unpack_filter)
def test_create_canonical_classpath_with_common_prefix(self): """ A special case when two targets' canonical classpath share a common prefix. Until we use `target.id` for canonical classpath, today's implementation is error-prone. This is such a regression test case added for a bug discovered in https://github.com/pantsbuild/pants/pull/2664 TODO(peiyu) Remove once we fully migrate to use `target.id`. """ # a and c' canonical classpath share a common prefix: a/b/b a = self.make_target('a/b', JvmTarget) c = self.make_target('a/b/b/c', JvmTarget) classpath_products = ClasspathProducts(self.pants_workdir) classpath_products.add_for_target(a, [('default', self._path('a.jar'))]) classpath_products.add_for_target(c, [('default', self._path('c.jar'))]) # target c first to verify its first created canonical classpath is preserved self._test_canonical_classpath_helper( classpath_products, [c, a], [ 'a/b/b/c/c/0-c.jar', 'a/b/b/0-a.jar', ], { 'a/b/b/classpath.txt': '{}/a.jar\n'.format(self.pants_workdir), 'a/b/b/c/c/classpath.txt': '{}/c.jar\n'.format( self.pants_workdir), }, False)
def resolve_jars(self, targets): executor = SubprocessExecutor(DistributionLocator.cached()) confs = [] if self.get_options().libraries: confs.append('default') if self.get_options().libraries_sources: confs.append('sources') if self.get_options().libraries_javadocs: confs.append('javadoc') compile_classpath = None if confs: compile_classpath = ClasspathProducts( self.get_options().pants_workdir) if JvmResolveSubsystem.global_instance().get_options( ).resolver == 'ivy': IvyTaskMixin.resolve(self, executor=executor, targets=targets, classpath_products=compile_classpath, confs=confs) else: CoursierMixin.resolve( self, targets, compile_classpath, sources=self.get_options().libraries_sources, javadoc=self.get_options().libraries_javadocs) return compile_classpath
def test_create_canonical_classpath_no_duplicate_entry(self): """Test no more than one symlink are created for the same classpath entry.""" jar_path = 'ivy/jars/org.x/lib/x-1.0.jar' resolved_jar = ResolvedJar(M2Coordinate(org='org', name='x', rev='1.0'), cache_path='somewhere', pants_path=self._create_file(jar_path)) target_a = self.make_target('a', JvmTarget) target_b = self.make_target('b', JvmTarget) classpath_products = ClasspathProducts(self.pants_workdir) # Both target a and target b depend on the same jar library classpath_products.add_jars_for_targets([target_a], 'default', [resolved_jar]) classpath_products.add_jars_for_targets([target_b], 'default', [resolved_jar]) with temporary_dir() as base_dir: # Only target a generates symlink to jar library, target b skips creating the # symlink for the same jar library. Both targets' classpath.txt files should # still contain the jar library. self._test_canonical_classpath_helper( classpath_products, [target_a, target_b], base_dir, ['a.a-0.jar'], { 'a.a-classpath.txt': '{}/{}\n'.format(self.pants_workdir, jar_path), 'b.b-classpath.txt': '{}/{}\n'.format(self.pants_workdir, jar_path), })
def test_classpath_custom_product(self): self.assertEqual( [], self.task.classpath([self.t1], classpath_product=ClasspathProducts( self.pants_workdir)), )
def execute(self): jar_import_products = self.context.products.get_data( JarImportProducts, init_func=JarImportProducts) # Gather all targets that are both capable of importing jars and actually declare some imports. targets = self.context.targets(predicate=self.has_imports) if not targets: return # Create a list of all of these targets plus the list of JarDependencies they depend on. all_targets = set(targets) for target in targets: all_targets.update(target.imported_jar_libraries) imports_classpath = ClasspathProducts(self.get_options().pants_workdir) self.resolve(executor=self.create_java_executor(), targets=all_targets, classpath_products=imports_classpath, invalidate_dependents=True) for target in targets: cp_entries = imports_classpath.get_classpath_entries_for_targets( target.closure(bfs=True)) for conf, cp_entry in cp_entries: if isinstance(cp_entry, ArtifactClasspathEntry): jar_import_products.imported(target, cp_entry.coordinate, cp_entry.path)
def test_get_internal_classpath_entries_for_targets(self): b = self.make_target('b', JvmTarget) a = self.make_target('a', JvmTarget, dependencies=[b]) classpath_product = ClasspathProducts(self.pants_workdir) # This artifact classpath entry should be ignored. example_jar_path = self._example_jar_path() self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path) classpath_product.add_for_target( b, [('default', self.path('b/loose/classes/dir'))]) classpath_product.add_for_target( a, [('default', self.path('a/loose/classes/dir')), ('default', self.path('an/internally/generated.jar'))]) classpath = classpath_product.get_internal_classpath_entries_for_targets( a.closure(bfs=True)) self.assertEqual( [('default', ClasspathEntry(self.path('a/loose/classes/dir'))), ('default', ClasspathEntry(self.path('an/internally/generated.jar'))), ('default', ClasspathEntry(self.path('b/loose/classes/dir')))], classpath)
def test_copy(self): b = self.make_target('b', JvmTarget, excludes=[Exclude('com.example', 'lib')]) a = self.make_target('a', JvmTarget, dependencies=[b]) classpath_product = ClasspathProducts(self.pants_workdir) resolved_jar = self.add_jar_classpath_element_for_path(classpath_product, a, self._example_jar_path()) classpath_product.add_for_target(a, [('default', self.path('a/path'))]) copied = classpath_product.copy() a_closure = a.closure(bfs=True) self.assertEqual([('default', resolved_jar.pants_path), ('default', self.path('a/path'))], classpath_product.get_for_targets(a_closure)) self.assertEqual([('default', resolved_jar.pants_path), ('default', self.path('a/path'))], copied.get_for_targets(a_closure)) self.add_excludes_for_targets(copied, b, a) self.assertEqual([('default', resolved_jar.pants_path), ('default', self.path('a/path'))], classpath_product.get_for_targets(a_closure)) self.assertEqual([('default', self.path('a/path'))], copied.get_for_targets(a_closure)) copied.add_for_target(b, [('default', self.path('b/path'))]) self.assertEqual([('default', resolved_jar.pants_path), ('default', self.path('a/path'))], classpath_product.get_for_targets(a_closure)) self.assertEqual([('default', self.path('a/path')), ('default', self.path('b/path'))], copied.get_for_targets(a_closure))
def test_classpath_by_targets(self): b = self.make_target('b', JvmTarget) a = self.make_target('a', JvmTarget, dependencies=[b], excludes=[Exclude('com.example', 'lib')]) classpath_products = ClasspathProducts(self.pants_workdir) path1 = self._path('jar/path1') path2 = self._path('jar/path2') path3 = os.path.join(self.pants_workdir, 'jar/path3') resolved_jar = ResolvedJar(M2Coordinate(org='com.example', name='lib', rev='1.0'), cache_path='somewhere', pants_path=path3) classpath_products.add_for_target(a, [('default', path1)]) classpath_products.add_for_target(a, [('non-default', path2)]) classpath_products.add_for_target(b, [('default', path2)]) classpath_products.add_jars_for_targets([b], 'default', [resolved_jar]) classpath_products.add_excludes_for_targets([a]) # (a, path2) filtered because of conf # (b, path3) filtered because of excludes self.assertEqual( OrderedDict([(a, [ClasspathEntry(path1)]), (b, [ClasspathEntry(path2)])]), ClasspathUtil.classpath_by_targets(a.closure(bfs=True), classpath_products))
def test_get_product_target_mappings_for_targets_intransitive(self): b = self.make_target('b', JvmTarget, excludes=[Exclude('com.example', 'lib')]) a = self.make_target('a', JvmTarget, dependencies=[b]) classpath_product = ClasspathProducts(self.pants_workdir) example_jar_path = self._example_jar_path() resolved_jar = self.add_jar_classpath_element_for_path( classpath_product, a, example_jar_path) classpath_product.add_for_target( b, [('default', self.path('b/loose/classes/dir'))]) classpath_product.add_for_target( a, [('default', self.path('a/loose/classes/dir')), ('default', self.path('an/internally/generated.jar'))]) classpath_target_tuples = classpath_product.get_product_target_mappings_for_targets( [a]) self.assertEqual([ (('default', ArtifactClasspathEntry(example_jar_path, resolved_jar.coordinate, resolved_jar.cache_path)), a), (('default', ClasspathEntry(self.path('a/loose/classes/dir'))), a), (('default', ClasspathEntry(self.path('an/internally/generated.jar'))), a) ], classpath_target_tuples)
def test_target_with_multiple_path_entries(self): """ :API: public """ options = attrdict(coverage=True, coverage_jvm_options=[]) settings = CoverageTaskSettings(options, None, self.pants_workdir, None, None, fake_log()) coverage = CoverageEngineForTesting(settings) classpath_products = ClasspathProducts(self.pants_workdir) self._add_for_target(classpath_products, self.java_target, '/java/target/first.jar') self._add_for_target(classpath_products, self.java_target, '/java/target/second.jar') self._add_for_target(classpath_products, self.java_target, '/java/target/third.jar') coverage.initialize_instrument_classpath([self.java_target], classpath_products) self.assertEquals( len(coverage.copy2_calls), 3, 'Should be 3 call for the single java_library target.') self._assert_target_copy(coverage, '/java/target/first.jar', '/coverage/classes/foo.foo-java/0') self._assert_target_copy(coverage, '/java/target/second.jar', '/coverage/classes/foo.foo-java/1') self._assert_target_copy(coverage, '/java/target/third.jar', '/coverage/classes/foo.foo-java/2') self.assertEquals( len(coverage.copytree_calls), 0, 'Should be no copytree calls when targets are not coverage targets.' )
def test_skips_non_coverage_targets(self): """ :API: public """ options = attrdict(coverage=True, coverage_jvm_options=[]) settings = CoverageTaskSettings(options, None, self.pants_workdir, None, None, fake_log()) coverage = CoverageEngineForTesting(settings) classpath_products = ClasspathProducts(self.pants_workdir) self._add_for_target(classpath_products, self.jar_lib, '/jar/lib/classpath') self._add_for_target(classpath_products, self.binary_target, '/binary/target/classpath') self._add_for_target(classpath_products, self.app_target, '/app/target/classpath') self._add_for_target(classpath_products, self.java_target, '/java/target/classpath.jar') coverage.initialize_instrument_classpath([ self.jar_lib, self.binary_target, self.app_target, self.java_target ], classpath_products) self.assertEquals( len(coverage.copy2_calls), 1, 'Should only be 1 call for the single java_library target.') self._assert_target_copy(coverage, '/java/target/classpath.jar', '/coverage/classes/foo.foo-java/0') self.assertEquals( len(coverage.copytree_calls), 0, 'Should be no copytree calls when targets are not coverage targets.' )
def test_single_classpath_element_no_excludes(self): a = self.make_target('a', JvmTarget) classpath_product = ClasspathProducts(self.pants_workdir) path = self.path('jar/path') self.add_jar_classpath_element_for_path(classpath_product, a, path) self.assertEqual([('default', path)], classpath_product.get_for_target(a))
def test_single_classpath_element_no_excludes(self): a = self.make_target('a', JvmTarget) classpath_product = ClasspathProducts() path = os.path.join(self.build_root, 'jar/path') self.add_jar_classpath_element_for_path(classpath_product, a, path) self.assertEqual([('default', path)], classpath_product.get_for_target(a))
def create_runtime_classpath_for_targets(self, target): def path_to_zjar_with_workdir(address: Address): return os.path.join(self.pants_workdir, address.path_safe_spec, "z.jar") runtime_classpath = ClasspathProducts(self.pants_workdir) for dep in target.dependencies: runtime_classpath.add_for_target(dep, [('default', path_to_zjar_with_workdir(dep.address))]) return runtime_classpath
def test_fails_if_paths_outside_buildroot(self): a = self.make_target('a', JvmTarget) classpath_product = ClasspathProducts(self.pants_workdir) with self.assertRaises(TaskError) as cm: classpath_product.add_for_target(a, [('default', '/dev/null')]) self.assertEqual( 'Classpath entry /dev/null for target a:a is located outside the working directory "{}".' .format(self.pants_workdir), str(cm.exception))
def test_fails_if_jar_paths_outside_buildroot(self): a = self.make_target('a', JvmTarget) classpath_product = ClasspathProducts() with self.assertRaises(TaskError) as cm: classpath_product.add_jars_for_targets([a], 'default', [(resolved_example_jar_at('/dev/null'))]) self.assertEqual( 'Classpath entry /dev/null for target a:a is located outside the buildroot.', str(cm.exception))
def test_transitive_dependencies_excluded_classpath_element(self): b = self.make_target('b', JvmTarget, excludes=[Exclude('com.example', 'lib')]) a = self.make_target('a', JvmTarget, dependencies=[b]) classpath_product = ClasspathProducts(self.pants_workdir) self.add_jar_classpath_element_for_path(classpath_product, a, self._example_jar_path()) self.add_excludes_for_targets(classpath_product, b, a) classpath = classpath_product.get_for_target(a) self.assertEqual([], classpath)
def _bootstrap_classpath(self, jvm_tool, targets): self._check_underspecified_tools(jvm_tool, targets) self.context.log.debug(f"Bootstrapping {jvm_tool.key}") classpath_holder = ClasspathProducts(self.get_options().pants_workdir) CoursierMixin.resolve( self, targets, classpath_holder, sources=False, javadoc=False, executor=None ) return [ cp_entry for _, cp_entry in classpath_holder.get_classpath_entries_for_targets(targets) ]
def test_excluded_classpath_element(self): a = self.make_target('a', JvmTarget, excludes=[Exclude('com.example', 'lib')]) classpath_product = ClasspathProducts(self.pants_workdir) example_jar_path = self._example_jar_path() self.add_jar_classpath_element_for_path(classpath_product, a, example_jar_path) self.add_excludes_for_targets(classpath_product, a) classpath = classpath_product.get_for_target(a) self.assertEqual([], classpath)