<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from collections import OrderedDict <EOL> from pants . backend . jvm . jar_dependency_utils import M2Coordinate , ResolvedJar <EOL> from pants . backend . jvm . targets . exclude import Exclude <EOL> from pants . backend . jvm . targets . jvm_target import JvmTarget <EOL> from pants . backend . jvm . tasks . classpath_products import ClasspathEntry , ClasspathProducts <EOL> from pants . backend . jvm . tasks . classpath_util import ClasspathUtil , MissingClasspathEntryError <EOL> from pants . goal . products import UnionProducts <EOL> from pants . util . contextutil import temporary_dir <EOL> from pants . util . dirutil import relativize_paths <EOL> from pants_test . base_test import BaseTest <EOL> from pants_test . testutils . file_test_util import check_file_content , contains_exact_files <EOL> class ClasspathUtilTest ( BaseTest ) : <EOL> def test_path_with_differing_conf_ignored ( self ) : <EOL> a = self . make_target ( '<STR_LIT:a>' , JvmTarget ) <EOL> classpath_product = UnionProducts ( ) <EOL> path = os . path . join ( self . build_root , '<STR_LIT>' ) <EOL> classpath_product . add_for_target ( a , [ ( '<STR_LIT:default>' , path ) ] ) <EOL> classpath = ClasspathUtil . compute_classpath ( [ a ] , <EOL> classpath_product , <EOL> extra_classpath_tuples = [ ] , <EOL> confs = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( [ ] , classpath ) <EOL> def test_path_with_overlapped_conf_added ( self ) : <EOL> a = self . make_target ( '<STR_LIT:a>' , JvmTarget ) <EOL> classpath_product = UnionProducts ( ) <EOL> path = os . path . join ( self . build_root , '<STR_LIT>' ) <EOL> classpath_product . add_for_target ( a , [ ( '<STR_LIT:default>' , path ) ] ) <EOL> classpath = ClasspathUtil . compute_classpath ( [ a ] , <EOL> classpath_product , <EOL> extra_classpath_tuples = [ ] , <EOL> confs = [ '<STR_LIT>' , '<STR_LIT:default>' ] ) <EOL> self . assertEqual ( [ path ] , classpath ) <EOL> def test_extra_path_added ( self ) : <EOL> a = self . make_target ( '<STR_LIT:a>' , JvmTarget ) <EOL> classpath_product = UnionProducts ( ) <EOL> path = os . path . join ( self . build_root , '<STR_LIT>' ) <EOL> classpath_product . add_for_target ( a , [ ( '<STR_LIT:default>' , path ) ] ) <EOL> extra_path = '<STR_LIT>' <EOL> extra_cp_tuples = [ ( '<STR_LIT:default>' , extra_path ) ] <EOL> classpath = ClasspathUtil . compute_classpath ( [ a ] , <EOL> classpath_product , <EOL> extra_classpath_tuples = extra_cp_tuples , <EOL> confs = [ '<STR_LIT:default>' ] ) <EOL> self . assertEqual ( [ path , extra_path ] , classpath ) <EOL> def test_relies_on_product_to_validate_paths_outside_buildroot ( self ) : <EOL> a = self . make_target ( '<STR_LIT:a>' , JvmTarget ) <EOL> classpath_product = UnionProducts ( ) <EOL> classpath_product . add_for_target ( a , [ ( '<STR_LIT:default>' , '<STR_LIT>' ) ] ) <EOL> classpath = ClasspathUtil . compute_classpath ( [ a ] , <EOL> classpath_product , <EOL> extra_classpath_tuples = [ ] , <EOL> confs = [ '<STR_LIT:default>' ] ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , classpath ) <EOL> def test_classpath_by_targets ( self ) : <EOL> b = self . make_target ( '<STR_LIT:b>' , JvmTarget ) <EOL> a = self . make_target ( '<STR_LIT:a>' , JvmTarget , dependencies = [ b ] , <EOL> excludes = [ Exclude ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> path1 = self . _path ( '<STR_LIT>' ) <EOL> path2 = self . _path ( '<STR_LIT>' ) <EOL> path3 = os . path . join ( self . pants_workdir , '<STR_LIT>' ) <EOL> resolved_jar = ResolvedJar ( M2Coordinate ( org = '<STR_LIT>' , name = '<STR_LIT>' , rev = '<STR_LIT:1.0>' ) , <EOL> cache_path = '<STR_LIT>' , <EOL> pants_path = path3 ) <EOL> classpath_products . add_for_target ( a , [ ( '<STR_LIT:default>' , path1 ) ] ) <EOL> classpath_products . add_for_target ( a , [ ( '<STR_LIT>' , path2 ) ] ) <EOL> classpath_products . add_for_target ( b , [ ( '<STR_LIT:default>' , path2 ) ] ) <EOL> classpath_products . add_jars_for_targets ( [ b ] , '<STR_LIT:default>' , [ resolved_jar ] ) <EOL> classpath_products . add_excludes_for_targets ( [ a ] ) <EOL> self . assertEquals ( OrderedDict ( [ ( a , [ ClasspathEntry ( path1 ) ] ) , <EOL> ( b , [ ClasspathEntry ( path2 ) ] ) ] ) , <EOL> ClasspathUtil . classpath_by_targets ( a . closure ( bfs = True ) , <EOL> classpath_products ) ) <EOL> def test_create_canonical_classpath ( self ) : <EOL> a = self . make_target ( '<STR_LIT>' , JvmTarget ) <EOL> jar_path = '<STR_LIT>' <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> resolved_jar = ResolvedJar ( M2Coordinate ( org = '<STR_LIT>' , name = '<STR_LIT:x>' , rev = '<STR_LIT:1.0>' ) , <EOL> cache_path = '<STR_LIT>' , <EOL> pants_path = self . _path ( jar_path ) ) <EOL> classpath_products . add_for_target ( a , [ ( '<STR_LIT:default>' , self . _path ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:default>' , self . _path ( '<STR_LIT>' ) ) ] ) <EOL> classpath_products . add_jars_for_targets ( [ a ] , '<STR_LIT:default>' , [ resolved_jar ] ) <EOL> with temporary_dir ( ) as base_dir : <EOL> self . _test_canonical_classpath_helper ( classpath_products , <EOL> [ a ] , <EOL> base_dir , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' <EOL> . format ( self . pants_workdir , self . pants_workdir , <EOL> self . pants_workdir , jar_path ) <EOL> } , <EOL> excludes = set ( [ Exclude ( org = '<STR_LIT>' , name = '<STR_LIT:y>' ) ] ) ) <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> classpath_products . add_for_target ( a , [ ( '<STR_LIT:default>' , self . _path ( '<STR_LIT>' ) ) ] ) <EOL> self . _test_canonical_classpath_helper ( classpath_products , <EOL> [ a ] , <EOL> base_dir , <EOL> [ <EOL> '<STR_LIT>' , <EOL> ] , <EOL> { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' . format ( self . pants_workdir ) <EOL> } ) <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> classpath_products . add_for_target ( a , [ ( '<STR_LIT:default>' , self . _path ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:default>' , self . _path ( '<STR_LIT>' ) ) ] ) <EOL> self . _test_canonical_classpath_helper ( classpath_products , <EOL> [ a ] , <EOL> base_dir , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' . format ( self . pants_workdir , <EOL> self . pants_workdir ) <EOL> } ) <EOL> def test_create_canonical_classpath_with_broken_classpath ( self ) : <EOL> """<STR_LIT>""" <EOL> a = self . make_target ( '<STR_LIT>' , JvmTarget ) <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> jar_path = '<STR_LIT>' <EOL> resolved_jar = ResolvedJar ( M2Coordinate ( org = '<STR_LIT>' , name = '<STR_LIT:x>' , rev = '<STR_LIT:1.0>' ) , <EOL> cache_path = '<STR_LIT>' , <EOL> pants_path = os . path . join ( self . pants_workdir , jar_path ) ) <EOL> classpath_products . add_jars_for_targets ( [ a ] , '<STR_LIT:default>' , [ resolved_jar ] ) <EOL> with temporary_dir ( ) as base_dir : <EOL> with self . assertRaises ( MissingClasspathEntryError ) : <EOL> self . _test_canonical_classpath_helper ( classpath_products , <EOL> [ a ] , <EOL> base_dir , <EOL> [ ] , <EOL> { } ) <EOL> def test_create_canonical_classpath_no_duplicate_entry ( self ) : <EOL> """<STR_LIT>""" <EOL> jar_path = '<STR_LIT>' <EOL> resolved_jar = ResolvedJar ( M2Coordinate ( org = '<STR_LIT>' , name = '<STR_LIT:x>' , rev = '<STR_LIT:1.0>' ) , <EOL> cache_path = '<STR_LIT>' , <EOL> pants_path = self . _path ( jar_path ) ) <EOL> target_a = self . make_target ( '<STR_LIT:a>' , JvmTarget ) <EOL> target_b = self . make_target ( '<STR_LIT:b>' , JvmTarget ) <EOL> classpath_products = ClasspathProducts ( self . pants_workdir ) <EOL> classpath_products . add_jars_for_targets ( [ target_a ] , '<STR_LIT:default>' , [ resolved_jar ] ) <EOL> classpath_products . add_jars_for_targets ( [ target_b ] , '<STR_LIT:default>' , [ resolved_jar ] ) <EOL> with temporary_dir ( ) as base_dir : <EOL> self . _test_canonical_classpath_helper ( classpath_products , <EOL> [ target_a , target_b ] , <EOL> base_dir , <EOL> [ '<STR_LIT>' ] , <EOL> { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' . format ( self . pants_workdir , jar_path ) , <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' . format ( self . pants_workdir , jar_path ) , <EOL> } ) <EOL> def _test_canonical_classpath_helper ( self , <EOL> classpath_products , <EOL> targets , <EOL> libs_dir , <EOL> expected_canonical_classpath , <EOL> expected_classspath_files , <EOL> excludes = None ) : <EOL> """<STR_LIT>""" <EOL> canonical_classpath = ClasspathUtil . create_canonical_classpath ( classpath_products , <EOL> targets , <EOL> libs_dir , <EOL> save_classpath_file = True , <EOL> internal_classpath_only = False , <EOL> excludes = excludes ) <EOL> self . assertEquals ( expected_canonical_classpath , <EOL> relativize_paths ( canonical_classpath , libs_dir ) ) <EOL> self . assertTrue ( contains_exact_files ( libs_dir , <EOL> expected_canonical_classpath + <EOL> expected_classspath_files . keys ( ) ) ) <EOL> for classpath_file in expected_classspath_files : <EOL> self . assertTrue ( check_file_content ( os . path . join ( libs_dir , classpath_file ) , <EOL> expected_classspath_files [ classpath_file ] ) ) <EOL> def _path ( self , p ) : <EOL> return self . create_workdir_file ( p ) </s>
