<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> from collections import defaultdict <EOL> from pants . backend . jvm . subsystems . jvm_platform import JvmPlatformSettings <EOL> from pants . backend . jvm . targets . java_library import JavaLibrary <EOL> from pants . backend . jvm . tasks . jvm_compile . zinc . zinc_compile import ZincCompile <EOL> from pants . base . revision import Revision <EOL> from pants . util . memo import memoized_method <EOL> from pants_test . tasks . task_test_base import TaskTestBase <EOL> class JavaCompileSettingsPartitioningTest ( TaskTestBase ) : <EOL> @ classmethod <EOL> def task_type ( cls ) : <EOL> return ZincCompile <EOL> def _java ( self , name , platform = None , deps = None , sources = None ) : <EOL> return self . make_target ( spec = '<STR_LIT>' . format ( name ) , <EOL> target_type = JavaLibrary , <EOL> platform = platform , <EOL> dependencies = deps or [ ] , <EOL> sources = sources ) <EOL> def _platforms ( self , * versions ) : <EOL> return { str ( v ) : { '<STR_LIT:source>' : str ( v ) } for v in versions } <EOL> @ memoized_method <EOL> def _version ( self , version ) : <EOL> return Revision . lenient ( version ) <EOL> def _task_setup ( self , targets , platforms = None , default_platform = None , ** options ) : <EOL> options [ '<STR_LIT:source>' ] = options . get ( '<STR_LIT:source>' , '<STR_LIT>' ) <EOL> options [ '<STR_LIT:target>' ] = options . get ( '<STR_LIT:target>' , '<STR_LIT>' ) <EOL> self . set_options ( ** options ) <EOL> self . set_options_for_scope ( '<STR_LIT>' , platforms = platforms , <EOL> default_platform = default_platform ) <EOL> context = self . context ( target_roots = targets ) <EOL> return self . create_task ( context ) <EOL> def _settings_and_targets ( self , targets , ** options ) : <EOL> self . _task_setup ( targets , ** options ) <EOL> settings_and_targets = defaultdict ( set ) <EOL> for target in targets : <EOL> settings_and_targets [ target . platform ] . add ( target ) <EOL> return settings_and_targets . items ( ) <EOL> def _partition ( self , targets , ** options ) : <EOL> self . _task_setup ( targets , ** options ) <EOL> partition = defaultdict ( set ) <EOL> for target in targets : <EOL> partition [ target . platform . target_level ] . add ( target ) <EOL> return partition <EOL> def _format_partition ( self , partition ) : <EOL> return '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( <EOL> '<STR_LIT>' . format ( key , '<STR_LIT:U+002CU+0020>' . join ( sorted ( t . address . spec for t in value ) ) ) <EOL> for key , value in sorted ( partition . items ( ) ) <EOL> ) ) <EOL> def assert_partitions_equal ( self , expected , received ) : <EOL> expected = { key : set ( val ) for key , val in expected . items ( ) if val } <EOL> received = { key : set ( val ) for key , val in received . items ( ) if val } <EOL> self . assertEqual ( expected , received , '<STR_LIT>' <EOL> . format ( self . _format_partition ( expected ) , <EOL> self . _format_partition ( received ) ) ) <EOL> def test_single_target ( self ) : <EOL> java6 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> partition = self . _partition ( [ java6 ] , platforms = self . _platforms ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( partition ) ) <EOL> self . assertEqual ( { java6 } , set ( partition [ self . _version ( '<STR_LIT>' ) ] ) ) <EOL> def test_independent_targets ( self ) : <EOL> java6 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> java7 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> java8 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> partition = self . _partition ( [ java6 , java7 , java8 ] , <EOL> platforms = self . _platforms ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> expected = { self . _version ( java . payload . platform ) : { java } <EOL> for java in ( java6 , java7 , java8 ) } <EOL> self . assertEqual ( <NUM_LIT:3> , len ( partition ) ) <EOL> self . assert_partitions_equal ( expected , partition ) <EOL> def test_java_version_aliases ( self ) : <EOL> expected = { } <EOL> for version in ( <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) : <EOL> expected [ Revision . lenient ( '<STR_LIT>' . format ( version ) ) ] = { <EOL> self . _java ( '<STR_LIT>' . format ( version ) , '<STR_LIT>' . format ( version ) ) , <EOL> self . _java ( '<STR_LIT>' . format ( version ) , '<STR_LIT:{}>' . format ( version ) ) , <EOL> } <EOL> partition = self . _partition ( list ( reduce ( set . union , expected . values ( ) , set ( ) ) ) , <EOL> platforms = self . _platforms ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( len ( expected ) , len ( partition ) ) <EOL> self . assert_partitions_equal ( expected , partition ) <EOL> def test_valid_dependent_targets ( self ) : <EOL> java6 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> java7 = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> java8 = self . _java ( '<STR_LIT>' , '<STR_LIT>' , deps = [ java6 ] ) <EOL> partition = self . _partition ( [ java6 , java7 , java8 ] , <EOL> platforms = self . _platforms ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assert_partitions_equal ( { <EOL> self . _version ( '<STR_LIT>' ) : { java6 } , <EOL> self . _version ( '<STR_LIT>' ) : { java7 } , <EOL> self . _version ( '<STR_LIT>' ) : { java8 } , <EOL> } , partition ) <EOL> def test_unspecified_default ( self ) : <EOL> java = self . _java ( '<STR_LIT>' , None ) <EOL> java6 = self . _java ( '<STR_LIT>' , '<STR_LIT>' , deps = [ java ] ) <EOL> java7 = self . _java ( '<STR_LIT>' , '<STR_LIT>' , deps = [ java ] ) <EOL> partition = self . _partition ( [ java7 , java , java6 ] , source = '<STR_LIT>' , target = '<STR_LIT>' , <EOL> platforms = self . _platforms ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> default_platform = '<STR_LIT>' ) <EOL> self . assert_partitions_equal ( { <EOL> self . _version ( '<STR_LIT>' ) : { java , java6 } , <EOL> self . _version ( '<STR_LIT>' ) : { java7 } , <EOL> } , partition ) <EOL> def test_invalid_source_target_combination_by_jvm_platform ( self ) : <EOL> java_wrong = self . _java ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( JvmPlatformSettings . IllegalSourceTargetCombination ) : <EOL> self . _settings_and_targets ( [ java_wrong ] , platforms = { <EOL> '<STR_LIT>' : { '<STR_LIT:source>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> } ) <EOL> def test_valid_source_target_combination ( self ) : <EOL> platforms = { <EOL> '<STR_LIT>' : { '<STR_LIT:source>' : <NUM_LIT:6> , '<STR_LIT:target>' : <NUM_LIT:7> } , <EOL> '<STR_LIT>' : { '<STR_LIT:source>' : <NUM_LIT:7> , '<STR_LIT:target>' : <NUM_LIT:8> } , <EOL> '<STR_LIT>' : { '<STR_LIT:source>' : <NUM_LIT:6> , '<STR_LIT:target>' : <NUM_LIT:8> } , <EOL> } <EOL> self . _settings_and_targets ( [ <EOL> self . _java ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . _java ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . _java ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] , platforms = platforms ) <EOL> def test_compile_setting_equivalence ( self ) : <EOL> self . assertEqual ( JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> def test_compile_setting_inequivalence ( self ) : <EOL> self . assertNotEqual ( JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> self . assertNotEqual ( JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) ) <EOL> self . assertNotEqual ( JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> JvmPlatformSettings ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) ) </s>
