<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from textwrap import dedent <EOL> from pants . backend . jvm . targets . jvm_app import Bundle , DirectoryReMapper , JvmApp <EOL> from pants . backend . jvm . targets . jvm_binary import JvmBinary <EOL> from pants . base . exceptions import TargetDefinitionException <EOL> from pants . base . parse_context import ParseContext <EOL> from pants . build_graph . address import Address <EOL> from pants . source . wrapped_globs import Globs <EOL> from pants_test . base_test import BaseTest <EOL> def _bundle ( rel_path ) : <EOL> pc = ParseContext ( rel_path = rel_path , type_aliases = { } ) <EOL> return Bundle ( pc ) <EOL> def _globs ( rel_path ) : <EOL> pc = ParseContext ( rel_path = rel_path , type_aliases = { } ) <EOL> return Globs ( pc ) <EOL> class JvmAppTest ( BaseTest ) : <EOL> def test_simple ( self ) : <EOL> binary_target = self . make_target ( '<STR_LIT>' , JvmBinary , main = '<STR_LIT>' ) <EOL> app_target = self . make_target ( '<STR_LIT>' , JvmApp , basename = '<STR_LIT>' , binary = '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , app_target . payload . basename ) <EOL> self . assertEquals ( '<STR_LIT>' , app_target . basename ) <EOL> self . assertEquals ( binary_target , app_target . binary ) <EOL> self . assertEquals ( [ '<STR_LIT>' ] , list ( app_target . traversable_dependency_specs ) ) <EOL> def test_bad_basename ( self ) : <EOL> with self . assertRaisesRegexp ( TargetDefinitionException , <EOL> r'<STR_LIT>' ) : <EOL> self . make_target ( '<STR_LIT>' , JvmApp , basename = '<STR_LIT:foo>' ) <EOL> def create_app ( self , rel_path , name = None , ** kwargs ) : <EOL> self . create_file ( os . path . join ( rel_path , '<STR_LIT>' ) ) <EOL> return self . make_target ( Address ( rel_path , name or '<STR_LIT>' ) . spec , <EOL> JvmApp , <EOL> bundles = [ _bundle ( rel_path ) ( fileset = '<STR_LIT>' ) ] , <EOL> ** kwargs ) <EOL> def test_binary_via_binary ( self ) : <EOL> bin = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> app = self . create_app ( '<STR_LIT>' , binary = '<STR_LIT>' ) <EOL> self . assertEquals ( app . binary , bin ) <EOL> def test_binary_via_dependencies ( self ) : <EOL> bin = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> app = self . create_app ( '<STR_LIT>' , dependencies = [ bin ] ) <EOL> self . assertEquals ( app . binary , bin ) <EOL> def test_degenerate_binaries ( self ) : <EOL> bin = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> app = self . create_app ( '<STR_LIT>' , binary = '<STR_LIT>' , dependencies = [ bin ] ) <EOL> self . assertEquals ( app . binary , bin ) <EOL> def test_no_binary ( self ) : <EOL> app = self . create_app ( '<STR_LIT>' ) <EOL> with self . assertRaisesRegexp ( TargetDefinitionException , <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) : <EOL> app . binary <EOL> def test_too_many_binaries_mixed ( self ) : <EOL> self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> bin2 = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> app = self . create_app ( '<STR_LIT>' , binary = '<STR_LIT>' , dependencies = [ bin2 ] ) <EOL> with self . assertRaisesRegexp ( TargetDefinitionException , <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) : <EOL> app . binary <EOL> def test_too_many_binaries_via_deps ( self ) : <EOL> bin = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> bin2 = self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> app = self . create_app ( '<STR_LIT>' , dependencies = [ bin , bin2 ] ) <EOL> with self . assertRaisesRegexp ( TargetDefinitionException , <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) : <EOL> app . binary <EOL> def test_not_a_binary ( self ) : <EOL> self . make_target ( '<STR_LIT>' , JvmBinary ) <EOL> self . create_app ( '<STR_LIT>' , name = '<STR_LIT>' , binary = '<STR_LIT>' ) <EOL> app = self . create_app ( '<STR_LIT>' , name = '<STR_LIT>' , binary = '<STR_LIT>' ) <EOL> with self . assertRaisesRegexp ( TargetDefinitionException , <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) : <EOL> app . binary <EOL> class BundleTest ( BaseTest ) : <EOL> def test_bundle_filemap_dest_bypath ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> densities = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> app = self . make_target ( spec_path , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( fileset = '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app . bundles ) ) <EOL> self . assertEqual ( { densities : '<STR_LIT>' } , app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_bundle_filemap_dest_byglobs ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> one = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> two = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> globs = _globs ( spec_path ) <EOL> app = self . make_target ( spec_path , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( fileset = globs ( '<STR_LIT>' ) ) ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app . bundles ) ) <EOL> self . assertEqual ( { one : '<STR_LIT>' , two : '<STR_LIT>' } , app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_bundle_filemap_dest_relative ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> five = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> app = self . make_target ( spec_path , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( relative_to = '<STR_LIT>' , <EOL> fileset = '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app . bundles ) ) <EOL> self . assertEqual ( { five : '<STR_LIT>' } , app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_bundle_filemap_dest_remap ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> one = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> mapper = DirectoryReMapper ( os . path . join ( spec_path , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> app = self . make_target ( spec_path , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( mapper = mapper , fileset = '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app . bundles ) ) <EOL> self . assertEqual ( { one : '<STR_LIT>' } , app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_bundle_filemap_remap_base_not_exists ( self ) : <EOL> with self . assertRaises ( DirectoryReMapper . NonexistentBaseError ) : <EOL> DirectoryReMapper ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_bundle_add ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> stone_dense = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> metal_dense = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> bundle = _bundle ( spec_path ) ( relative_to = '<STR_LIT>' , <EOL> fileset = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> app = self . make_target ( spec_path , JvmApp , dependencies = [ unused ] , bundles = [ bundle ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app . bundles ) ) <EOL> self . assertEqual ( { stone_dense : '<STR_LIT>' , metal_dense : '<STR_LIT>' } , <EOL> app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_multiple_bundles ( self ) : <EOL> spec_path = '<STR_LIT>' <EOL> stone_dense = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> metal_dense = self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> unused = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , JvmBinary ) <EOL> self . add_to_build_file ( '<STR_LIT>' , dedent ( """<STR_LIT>""" ) ) <EOL> app1 = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( relative_to = '<STR_LIT>' , <EOL> fileset = '<STR_LIT>' ) ] ) <EOL> app2 = self . make_target ( Address ( spec_path , '<STR_LIT>' ) . spec , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ _bundle ( spec_path ) ( fileset = '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app1 . bundles ) ) <EOL> self . assertEqual ( { stone_dense : '<STR_LIT>' } , app1 . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( app2 . bundles ) ) <EOL> self . assertEqual ( { metal_dense : '<STR_LIT>' } , app2 . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> def test_globs_relative_to_build_root ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> unused = self . make_target ( spec_path , JvmBinary ) <EOL> globs = _globs ( spec_path ) <EOL> app = self . make_target ( '<STR_LIT>' , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ <EOL> _bundle ( spec_path ) ( fileset = globs ( "<STR_LIT>" ) ) , <EOL> _bundle ( spec_path ) ( fileset = [ '<STR_LIT>' ] ) <EOL> ] ) <EOL> self . assertEquals ( [ '<STR_LIT>' , '<STR_LIT>' ] , sorted ( app . globs_relative_to_buildroot ( ) [ '<STR_LIT>' ] ) ) <EOL> def test_list_of_globs_fails ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> globs = _globs ( spec_path ) <EOL> with self . assertRaises ( ValueError ) : <EOL> _bundle ( spec_path ) ( fileset = [ globs ( "<STR_LIT>" ) ] ) <EOL> def test_jvmapp_fingerprinting ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> globs = _globs ( spec_path ) <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> def calc_fingerprint ( ) : <EOL> self . reset_build_graph ( ) <EOL> app = self . make_target ( '<STR_LIT>' , <EOL> JvmApp , <EOL> dependencies = [ ] , <EOL> bundles = [ <EOL> _bundle ( spec_path ) ( fileset = globs ( "<STR_LIT:*>" ) ) <EOL> ] ) <EOL> return app . payload . fingerprint ( ) <EOL> fingerprint_before = calc_fingerprint ( ) <EOL> os . mkdir ( os . path . join ( self . build_root , spec_path , '<STR_LIT>' ) ) <EOL> self . assertEqual ( fingerprint_before , calc_fingerprint ( ) ) <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> self . assertNotEqual ( fingerprint_before , calc_fingerprint ( ) ) <EOL> def test_jvmapp_fingerprinting_with_non_existing_files ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> def calc_fingerprint ( ) : <EOL> self . reset_build_graph ( ) <EOL> return self . make_target ( '<STR_LIT>' , <EOL> JvmApp , <EOL> dependencies = [ ] , <EOL> bundles = [ <EOL> _bundle ( spec_path ) ( fileset = [ '<STR_LIT>' ] ) <EOL> ] ) . payload . fingerprint ( ) <EOL> fingerprint_non_existing_file = calc_fingerprint ( ) <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) ) <EOL> fingerprint_empty_file = calc_fingerprint ( ) <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT>' ) , contents = '<STR_LIT>' ) <EOL> fingerprint_file_with_content = calc_fingerprint ( ) <EOL> self . assertNotEqual ( fingerprint_empty_file , fingerprint_non_existing_file ) <EOL> self . assertNotEqual ( fingerprint_empty_file , fingerprint_file_with_content ) <EOL> self . assertNotEqual ( fingerprint_file_with_content , fingerprint_empty_file ) <EOL> def test_rel_path_with_glob_fails ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> self . create_file ( os . path . join ( spec_path , '<STR_LIT:z>' , '<STR_LIT>' ) ) <EOL> globs = _globs ( spec_path ) <EOL> with self . assertRaises ( ValueError ) : <EOL> _bundle ( spec_path ) ( rel_path = "<STR_LIT>" , fileset = globs ( '<STR_LIT>' ) ) <EOL> def test_rel_path_overrides_context_rel_path_for_explicit_path ( self ) : <EOL> spec_path = '<STR_LIT:y>' <EOL> unused = self . make_target ( spec_path , JvmBinary ) <EOL> app = self . make_target ( '<STR_LIT>' , <EOL> JvmApp , <EOL> dependencies = [ unused ] , <EOL> bundles = [ <EOL> _bundle ( spec_path ) ( rel_path = "<STR_LIT>" , fileset = [ '<STR_LIT>' ] ) <EOL> ] ) <EOL> self . assertEqual ( { os . path . join ( self . build_root , '<STR_LIT>' ) : '<STR_LIT>' } , app . bundles [ <NUM_LIT:0> ] . filemap ) <EOL> self . assertEquals ( [ '<STR_LIT>' ] , sorted ( app . globs_relative_to_buildroot ( ) [ '<STR_LIT>' ] ) ) </s>
