def test_loose_files(self): with temporary_dir() as outdir: project_name = 'foobar' self.set_options(project_dir=outdir, project_name=project_name, loose_files=True, merge=False, libraries=False, open=False) with open(os.path.join(get_buildroot(), 'pom.xml'), 'w+') as f: f.write('<?xml version="1.0" encoding="UTF-8"?>' '<project><modules/></project>') task = self.create_task(self.context()) task.execute() ipr = os.path.join(outdir, project_name, 'foobar.ipr') iml = os.path.join(outdir, project_name, 'java.iml') self._assert_tree_contains(ipr, 'project_loose_files.ipr.xml', repo=os.path.basename(get_buildroot())) self._assert_tree_contains(iml, 'project_loose_files.iml.xml', abs_path_to_build_root=os.path.abspath( get_buildroot()))
def test_dependency_managment_finder(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write(DEPENDENCY_MANAGEMENT_POM) dmf = squarepants.pom_handlers.DependencyManagementFinder( rootdir=tmpdir) deps = dmf.find_dependencies('pom.xml') self.assertEquals(2, len(deps)) self.assertEquals( { u'groupId': 'com.amazonaws', u'artifactId': 'aws-java-sdk', u'version': '1.9.5', u'exclusions': [] }, deps[0]) self.assertEquals( { u'groupId': 'io.dropwizard', u'artifactId': 'dropwizard-auth', u'version': '8.7.6', u'exclusions': [{ u'groupId': 'io.dropwizard', u'artifactId': 'dropwizard-core' }] }, deps[1])
def test_protobuf_publishing(self): with temporary_dir() as tmpdir: test_spec = 'service/exemplar-db/src/main/proto' command = [ 'publish.jar', '--no-dryrun', '--no-commit', '--no-prompt', '--no-transitive', '--local={}'.format(tmpdir), '--doc-javadoc-ignore-failure', test_spec, ] run = self.pants(*command) self.assert_success(run) def find_published_jar(): for root, dirs, files in os.walk(tmpdir): for name in files: if name.endswith('SNAPSHOT.jar'): return os.path.join(root, name) jar_path = find_published_jar() self.assertFalse(jar_path is None) with ZipFile(jar_path, 'r') as jar: contents = jar.namelist() self.assertTrue( any(name.endswith('.proto') for name in contents)) self.assertTrue( any(name.endswith('.class') for name in contents)) self.assertFalse( any(name.endswith('.java') for name in contents))
def test_protobuf_publishing(self): with temporary_dir() as tmpdir: test_spec = 'service/exemplar-db/src/main/proto' command = [ 'publish.jar', '--no-dryrun', '--no-commit', '--no-prompt', '--no-transitive', '--local={}'.format(tmpdir), '--doc-javadoc-ignore-failure', test_spec, ] run = self.pants(*command) self.assert_success(run) def find_published_jar(): for root, dirs, files in os.walk(tmpdir): for name in files: if name.endswith('SNAPSHOT.jar'): return os.path.join(root, name) jar_path = find_published_jar() self.assertFalse(jar_path is None) with ZipFile(jar_path, 'r') as jar: contents = jar.namelist() self.assertTrue(any(name.endswith('.proto') for name in contents)) self.assertTrue(any(name.endswith('.class') for name in contents)) self.assertFalse(any(name.endswith('.java') for name in contents))
def _setup_single_module(self, module_name, module_pom_contents, touch_files=None): with temporary_dir() as tempdir: current_dir = os.path.abspath('.') os.chdir(tempdir) self.make_file('pom.xml', dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>{module_name}</module> </modules> </project> '''.format(module_name=module_name)).strip()) self.make_file(os.path.join('parents', 'base', 'pom.xml'), dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project> '''.strip())) self.make_file(os.path.join(module_name, 'pom.xml'), dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../parents/base/pom.xml</relativePath> </parent> <groupId>com.example.project</groupId> <artifactId>{module_name}</artifactId> <version>HEAD-SNAPSHOT</version> {module_pom_contents} </project> ''').format(module_name=module_name, module_pom_contents=module_pom_contents).strip()) for path in (touch_files or ()): touch(path, makedirs=True) yield os.path.join(module_name, 'pom.xml') os.chdir(current_dir)
def test_file_pattern_exists_in_subdir(self): pattern = re.compile(r".*Test.java") with temporary_dir() as tmpdir: self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, "foo.java")) self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, "ExampleTest.java")) self.assertTrue(file_pattern_exists_in_subdir(tmpdir, pattern)) with temporary_dir() as tmpdir: nested_dir = os.path.join(tmpdir, "src", "main", "java", "com", "squareup", "foo") os.makedirs(nested_dir) self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, "bogus.java")) touch(os.path.join(nested_dir, "bogus.java")) touch(os.path.join(nested_dir, "AnotherTest.java")) self.assertTrue(file_pattern_exists_in_subdir(tmpdir, pattern))
def test_type_test_jar(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dep1</artifactId> <type>foo</type> <version>1.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dep2</artifactId> <type>test-jar</type> <version>1.2.3</version> </dependency> </dependencies> </project> ''')) # TODO(Eric Ayers): Right now, our builds expect a file in <rootdir>/parents/base/pom.xml os.makedirs(os.path.join(tmpdir, 'parents', 'base')) with open(os.path.join(tmpdir, 'parents', 'base', 'pom.xml'), 'w') as dep_mgmt_pom: dep_mgmt_pom.write(DEPENDENCY_MANAGEMENT_POM) df = squarepants.pom_handlers.DependencyInfo('pom.xml', rootdir=tmpdir) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'dep1', u'type' : 'foo', u'exclusions' : [], u'version' : '1.0' }, df.dependencies[0]) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'dep2', u'type' : 'test-jar', u'exclusions' : [], u'version' : '1.2.3' }, df.dependencies[1]) self.assertEquals(2, len(df.dependencies)) deps_from_pom = squarepants.pom_handlers.DepsFromPom(PomUtils.pom_provides_target(), rootdir=tmpdir) refs = deps_from_pom.build_pants_refs(df.dependencies) self.assertEquals("sjar(org='com.example', name='dep1', rev='1.0', ext='foo',)", refs[0]) # type test-jar gets transformed into a 'tests' classifier self.assertEquals("sjar(org='com.example', name='dep2', rev='1.2.3', classifier='tests',)", refs[1]) self.assertEquals(2, len(refs))
def test_link_resources_jars_binary(self): binary_spec = '{path}:{name}'.format(path=self._link_resources_jars_testdir, name='bin') with temporary_dir() as dist_dir: self.assert_success(self.pants('--pants-distdir={}'.format(dist_dir), 'binary', binary_spec)) binary_jar = os.path.join(dist_dir, 'link-resources-jars.jar') java_result = self.java('-jar', binary_jar) self.assert_success(java_result) self.assertIn('Everything looks OK.', java_result) self.assertIn('xyzzy.jar', self.jar('-tf', binary_jar))
def test_file_pattern_exists_in_subdir(self): pattern = re.compile(r'.*Test.java') with temporary_dir() as tmpdir: self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, 'foo.java')) self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, 'ExampleTest.java')) self.assertTrue(file_pattern_exists_in_subdir(tmpdir, pattern)) with temporary_dir() as tmpdir: nested_dir = os.path.join(tmpdir, 'src', 'main', 'java', 'com', 'squareup', 'foo') os.makedirs(nested_dir) self.assertFalse(file_pattern_exists_in_subdir(tmpdir, pattern)) touch(os.path.join(tmpdir, 'bogus.java')) touch(os.path.join(nested_dir, 'bogus.java')) touch(os.path.join(nested_dir, 'AnotherTest.java')) self.assertTrue(file_pattern_exists_in_subdir(tmpdir, pattern))
def test_dependency_finder_parent_properties(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <base.prop>FOO</base.prop> <base.overridden>BASE</base.overridden> </properties> </project> ''')) child_path_name = os.path.join(tmpdir, 'child') os.mkdir(child_path_name) child_pom_name = os.path.join(child_path_name, 'pom.xml') with open(child_pom_name, 'w') as child_pomfile: child_pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <properties> <child.prop1>BAR</child.prop1> <child.prop2>base is ${base.prop}</child.prop2> <base.overridden>CHILD</base.overridden> </properties> </project> ''')) df = squarepants.pom_handlers.DependencyInfo('child/pom.xml', rootdir=tmpdir) self.assertEquals('FOO', df.properties['base.prop']) self.assertEquals('BAR', df.properties['child.prop1']) self.assertEquals('base is FOO', df.properties['child.prop2']) self.assertEquals('CHILD', df.properties['base.overridden'])
def test_jax_ws_codegen(self): with temporary_dir() as distdir: binary_run = self.pants( '--pants-distdir={}'.format(distdir), 'binary run', '--binary-jvm-no-use-nailgun', 'squarepants/src/test/java/com/squareup/squarepants/integration/jaxwsgen' ) self.assert_success(binary_run) binary = os.path.join(distdir, 'jaxwsgen.jar') self.assertTrue(os.path.exists(binary))
def test_dependency_finder_parent_properties(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <base.prop>FOO</base.prop> <base.overridden>BASE</base.overridden> </properties> </project> ''')) child_path_name = os.path.join(tmpdir, 'child') os.mkdir(child_path_name) child_pom_name = os.path.join(child_path_name, 'pom.xml') with open(child_pom_name, 'w') as child_pomfile: child_pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <properties> <child.prop1>BAR</child.prop1> <child.prop2>base is ${base.prop}</child.prop2> <base.overridden>CHILD</base.overridden> </properties> </project> ''')) df = squarepants.pom_handlers.DependencyInfo('child/pom.xml', rootdir=tmpdir) self.assertEquals('FOO', df.properties['base.prop']) self.assertEquals('BAR', df.properties['child.prop1']) self.assertEquals('base is FOO', df.properties['child.prop2']) self.assertEquals('CHILD', df.properties['base.overridden'])
def test_debug_port_set(self): with temporary_dir() as outdir: project_name = "debug-port-project" self.set_options(project_dir=outdir, project_name=project_name, merge=False, libraries=False, open=False) self.set_options_for_scope(JVM.options_scope, debug_port=54321) with open(os.path.join(get_buildroot(), "pom.xml"), "w+") as f: f.write('<?xml version="1.0" encoding="UTF-8"?>' "<project><modules/></project>") task = self.create_task(self.context()) task.execute() ipr = os.path.join(outdir, project_name, "debug-port-project.ipr") self._assert_tree_contains(ipr, "project_debug_port.ipr.xml", repo=os.path.basename(get_buildroot()))
def test_jvm_binary_target(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) self.make_file( 'example-app/src/main/java/com/example/ExampleApp.java', '/* java file */') extra_contents = """ <properties> <project.mainclass>com.example.ExampleApp</project.mainclass> </properties> """ self.create_pom_with_modules(tmp_dir, ['example-app'], extra_project_contents=extra_contents) PomToBuild().convert_pom( os.path.join('example-app', 'pom.xml'), rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) expected_contents = """ jvm_binary(name='example-app', main = 'com.example.ExampleApp', basename= 'example-app', dependencies = [ ':lib' ], manifest_entries = square_manifest(), ) # This target's sole purpose is just to invalidate the cache if loose files (eg app-manifest.yaml) # for the jvm_binary change. fingerprint(name='extra-files', sources = [], dependencies = [], ) target(name='lib', dependencies = [ 'example-app/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('example-app/BUILD.gen', expected_contents, ignore_leading_spaces=True)
def test_format_jar_deps_symbols(self): with temporary_dir() as temp_path: parent_pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock-parent</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <foo>1.2.3</foo> </properties> </project> """ mock_parent_pom_filename = os.path.join(temp_path, 'pom.xml') with open(mock_parent_pom_filename, 'w') as f: f.write(parent_pom_contents) mock_path = os.path.join(temp_path, 'mock-project') os.mkdir(mock_path) mock_pom_filename = os.path.join(mock_path, 'pom.xml') pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock</artifactId> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>mock-project</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project> """ with open(mock_pom_filename, 'w') as f: f.write(pom_contents) mock_pom_file = PomFile(mock_pom_filename) formatted_library = JarFilesMixin.format_jar_library( 'jar_files', ["jar(org='square', name='foobar', rev='${foo}')"], pom_file=mock_pom_file) self.assertEquals( dedent(''' jar_library(name='jar_files', jars=[ jar(org='square', name='foobar', rev='1.2.3') ], ) '''), formatted_library)
def test_write_build_gen(self): gen_context = GenerationContext(print_headers=False) is_aux = gen_context.is_aux infer_target_name = gen_context.infer_target_name infer_build_name = gen_context.infer_build_name write_build_file = gen_context.write_build_file with temporary_dir() as build_dir: self.assertFalse(is_aux(build_dir)) self.assertEquals('foo', infer_target_name(build_dir, 'foo')) self.assertEquals(os.path.join(build_dir, 'BUILD.gen'), infer_build_name(build_dir)) write_build_file(build_dir, 'contents of BUILD.gen') self.assertTrue(os.path.exists(os.path.join(build_dir, 'BUILD.gen'))) with open(os.path.join(build_dir, 'BUILD.gen')) as build_file: self.assertEquals('contents of BUILD.gen', build_file.read())
def test_format_jar_deps_symbols(self): with temporary_dir() as temp_path: parent_pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock-parent</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <foo>1.2.3</foo> </properties> </project> """ mock_parent_pom_filename = os.path.join(temp_path, 'pom.xml') with open(mock_parent_pom_filename, 'w') as f: f.write(parent_pom_contents) mock_path = os.path.join(temp_path, 'mock-project') os.mkdir(mock_path) mock_pom_filename = os.path.join(mock_path, 'pom.xml') pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock</artifactId> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>mock-project</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project> """ with open(mock_pom_filename, 'w') as f: f.write(pom_contents) mock_pom_file = PomFile(mock_pom_filename) formatted_library = JarFilesMixin.format_jar_library('jar_files', ["jar(org='square', name='foobar', rev='${foo}')"], pom_file=mock_pom_file) self.assertEquals(dedent(''' jar_library(name='jar_files', jars=[ jar(org='square', name='foobar', rev='1.2.3') ], ) '''), formatted_library)
def test_default_test_target(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) proto_file = 'project1/src/main/proto/foo.proto' java_file = 'project2/src/main/java/Foo.java' self.make_file(proto_file, '/* proto file */') self.make_file(java_file, '/* java file */') projects = ['project1', 'project2'] self.create_pom_with_modules(tmp_dir, projects) for project in projects: PomToBuild().convert_pom( os.path.join(project, 'pom.xml'), rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) triple_quote_string = """ target(name='proto', dependencies = [ 'project1/src/main/proto:proto' ], ) target(name='lib', dependencies = [ ':proto' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('project1/BUILD.gen', triple_quote_string, ignore_leading_spaces=True) triple_quote_string = """ target(name='lib', dependencies = [ 'project2/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('project2/BUILD.gen', triple_quote_string, ignore_leading_spaces=True)
def test_resolve_properties(self): substitute = GenerationUtils.symbol_substitution with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as root_pom_file: root_pom_file.write(self.ROOT_POM) pom_file = PomFile('pom.xml', root_directory=tmpdir) self.assertEquals('FOOBAR', substitute(pom_file.properties, '${prop.foo}${prop.bar}')) self.assertEquals('FOO-BAZ', substitute(pom_file.properties, '${prop.baz}')) deps = [{'key1' : 'key1-${prop.foo}'}, {'key2' : 'key2-${prop.bar}'}] deps = GenerationUtils.symbol_substitution_on_dicts(pom_file.properties, deps) self.assertEquals([{'key1' : 'key1-FOO'}, {'key2' : 'key2-BAR'}], deps)
def test_dependency_finder(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write(DEPENDENCY_POM) df = squarepants.pom_handlers.DependencyInfo('pom.xml', rootdir=tmpdir) self.assertEquals('com.example', df.groupId) self.assertEquals('service', df.artifactId) self.assertEquals('2.3.4', df.properties['jhdf5.version']) self.assertEquals(4, len(df.dependencies)) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'parent', u'relativePath': '../pom.xml', u'version': 'HEAD-SNAPSHOT', }, df.dependencies[0]) self.assertEquals( { u'groupId': 'com.google.guava', u'artifactId': 'guava', u'exclusions': [] }, df.dependencies[1]) # this one has property substitution which doesn't occur until DependencyInfo self.assertEquals( { u'groupId': 'com.squareup.nonmaven.hdfgroup.hdf-java', u'artifactId': 'jhdf5', u'version': '2.3.4', u'exclusions': [] }, df.dependencies[2]) self.assertEquals( { u'groupId': 'io.dropwizard', u'artifactId': 'dropwizard-auth', u'version': '4.5.6', u'classifier': 'shaded', u'exclusions': [{ u'groupId': 'io.dropwizard', u'artifactId': 'dropwizard-core' }] }, df.dependencies[3])
def test_default_junit(self): with temporary_dir() as outdir: self.set_options(project_dir=outdir, merge=False) task = self.create_task(self.context()) ipr = task._generate_project_file( TemplateData( root_dir=get_buildroot(), outdir=task.intellij_output_dir, resource_extensions=[], scala=None, checkstyle_classpath=';'.join([]), debug_port=None, extra_components=[], global_junit_vm_parameters='-one -two -three', )) self._assert_tree_contains(ipr, 'project_default_junit.xml')
def test_write_build_gen(self): gen_context = GenerationContext(print_headers=False) is_aux = gen_context.is_aux infer_target_name = gen_context.infer_target_name infer_build_name = gen_context.infer_build_name write_build_file = gen_context.write_build_file with temporary_dir() as build_dir: self.assertFalse(is_aux(build_dir)) self.assertEquals('foo', infer_target_name(build_dir, 'foo')) self.assertEquals(os.path.join(build_dir, 'BUILD.gen'), infer_build_name(build_dir)) write_build_file(build_dir, 'contents of BUILD.gen') self.assertTrue( os.path.exists(os.path.join(build_dir, 'BUILD.gen'))) with open(os.path.join(build_dir, 'BUILD.gen')) as build_file: self.assertEquals('contents of BUILD.gen', build_file.read())
def test_system_specific_properties(self): dependencies = self._system_specific_properties_dependencies_text profiles = self._system_specific_properties_profiles_text with temporary_dir() as tmp_dir: os.chdir(tmp_dir) self.make_file('project/src/main/java/Foobar.java', '/* nothing to see here */') self.create_pom_with_modules(tmp_dir, ['project'], extra_project_contents=dependencies + profiles) PomToBuild().convert_pom('project/pom.xml', rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) self.assert_file_contents('project/src/main/java/BUILD.gen', self._system_specific_properties_expected_text, ignore_leading_spaces=True, ignore_trailing_spaces=True, ignore_blanklines=True)
def test_jar_manifest(self): with temporary_dir() as distdir: def do_test(): binary_run = self.pants('--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/src/test/java/com/squareup/squarepants/integration/manifest:manifest-test') self.assert_success(binary_run) binary = os.path.join(distdir, 'manifest-test.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('META-INF/jar-manifest.txt') as f: self.assertIn('com.google.guava:guava:', f.read()) do_test() # Make sure it works a second time do_test()
def test_jvm_binary_target(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) self.make_file('example-app/src/main/java/com/example/ExampleApp.java', '/* java file */') extra_contents = """ <properties> <project.mainclass>com.example.ExampleApp</project.mainclass> </properties> """ self.create_pom_with_modules(tmp_dir, ['example-app'], extra_project_contents=extra_contents) PomToBuild().convert_pom(os.path.join('example-app', 'pom.xml'), rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) expected_contents = """ jvm_binary(name='example-app', main = 'com.example.ExampleApp', basename= 'example-app', dependencies = [ ':lib' ], manifest_entries = square_manifest(), ) # This target's sole purpose is just to invalidate the cache if loose files (eg app-manifest.yaml) # for the jvm_binary change. fingerprint(name='extra-files', sources = [], dependencies = [], ) target(name='lib', dependencies = [ 'example-app/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('example-app/BUILD.gen', expected_contents, ignore_leading_spaces=True)
def test_default_junit(self): with temporary_dir() as outdir: self.set_options(project_dir=outdir, merge=False) task = self.create_task(self.context()) ipr = task._generate_project_file( TemplateData( root_dir=get_buildroot(), outdir=task.intellij_output_dir, resource_extensions=[], scala=None, checkstyle_classpath=";".join([]), debug_port=None, extra_components=[], global_junit_vm_parameters="-one -two -three", ) ) self._assert_tree_contains(ipr, "project_default_junit.xml")
def test_default_test_target(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) proto_file = 'project1/src/main/proto/foo.proto' java_file = 'project2/src/main/java/Foo.java' self.make_file(proto_file, '/* proto file */') self.make_file(java_file, '/* java file */') projects = ['project1', 'project2'] self.create_pom_with_modules(tmp_dir, projects) for project in projects: PomToBuild().convert_pom(os.path.join(project, 'pom.xml'), rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) triple_quote_string = """ target(name='proto', dependencies = [ 'project1/src/main/proto:proto' ], ) target(name='lib', dependencies = [ ':proto' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('project1/BUILD.gen', triple_quote_string, ignore_leading_spaces=True) triple_quote_string = """ target(name='lib', dependencies = [ 'project2/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('project2/BUILD.gen', triple_quote_string, ignore_leading_spaces=True)
def XXXtest_simple_pom_file(self): with temporary_dir() as temp_path: parent_pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock-parent</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <foo>1.2.3</foo> </properties> </project> """ mock_parent_pom_filename = os.path.join(temp_path, 'pom.xml') with open(mock_parent_pom_filename, 'w') as f: f.write(parent_pom_contents) mock_path = os.path.join(temp_path, 'mock-project') os.mkdir(mock_path) mock_pom_filename = os.path.join(mock_path, 'pom.xml') pom_contents = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>mock</artifactId> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>mock-project</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project> """ with open(mock_pom_filename, 'w') as f: f.write(pom_contents) mock_pom_file = PomFile(mock_pom_filename) self.assertEquals(mock_path, mock_pom_file.properties['project.basedir']) self.assertEquals('1.2.3', mock_pom_file.properties['foo']) parent_pom_file = mock_pom_file.parent self.assertEquals(temp_path, parent_pom_file.properties['project.basedir'])
def test_jar_manifest(self): with temporary_dir() as distdir: def do_test(): binary_run = self.pants( '--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/src/test/java/com/squareup/squarepants/integration/manifest:manifest-test' ) self.assert_success(binary_run) binary = os.path.join(distdir, 'manifest-test.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('META-INF/jar-manifest.txt') as f: self.assertIn('com.google.guava:guava:', f.read()) do_test() # Make sure it works a second time do_test()
def test_debug_port_set(self): with temporary_dir() as outdir: project_name = 'debug-port-project' self.set_options(project_dir=outdir, project_name=project_name, merge=False, libraries=False, open=False) self.set_options_for_scope(JVM.options_scope, debug_port=54321) with open(os.path.join(get_buildroot(), 'pom.xml'), 'w+') as f: f.write('<?xml version="1.0" encoding="UTF-8"?>' '<project><modules/></project>') task = self.create_task(self.context()) task.execute() ipr = os.path.join(outdir, project_name, 'debug-port-project.ipr') self._assert_tree_contains(ipr, 'project_debug_port.ipr.xml', repo=os.path.basename(get_buildroot()))
def test_app_manifest_bundle(self): """Makes sure the app-manifest.yaml properly makes it into the jar file on a ./pants binary. In particular, makes sure that the app-manifest.yaml is kept up to date and does not get stale, even if it is the only file that has changed since the last time ./pants binary was invoked. """ with temporary_dir() as distdir: source_yaml_file = 'squarepants/pants-aop-test-app/app-manifest.yaml' with self.invariant_file_contents( source_yaml_file) as plain_app_manifest: self.assertNotIn('bogus_flag: "bogus one"', plain_app_manifest) with open(source_yaml_file, 'a') as f: f.write('\nbogus_flag: "bogus one"\n') self.assert_success( self.pants('--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/pants-aop-test-app')) binary = os.path.join(distdir, 'pants-aop-test-app.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('app-manifest.yaml') as f: self.assertIn('bogus_flag: bogus one', f.read()) with open(source_yaml_file, 'w') as f: f.write(plain_app_manifest) self.assert_success( self.pants( '--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/pants-aop-test-app', )) binary = os.path.join(distdir, 'pants-aop-test-app.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('app-manifest.yaml') as f: self.assertNotIn('bogus_flag: bogus one', f.read())
def test_resolve_properties(self): substitute = GenerationUtils.symbol_substitution with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as root_pom_file: root_pom_file.write(self.ROOT_POM) pom_file = PomFile('pom.xml', root_directory=tmpdir) self.assertEquals( 'FOOBAR', substitute(pom_file.properties, '${prop.foo}${prop.bar}')) self.assertEquals('FOO-BAZ', substitute(pom_file.properties, '${prop.baz}')) deps = [{'key1': 'key1-${prop.foo}'}, {'key2': 'key2-${prop.bar}'}] deps = GenerationUtils.symbol_substitution_on_dicts( pom_file.properties, deps) self.assertEquals([{ 'key1': 'key1-FOO' }, { 'key2': 'key2-BAR' }], deps)
def test_system_specific_properties(self): dependencies = self._system_specific_properties_dependencies_text profiles = self._system_specific_properties_profiles_text with temporary_dir() as tmp_dir: os.chdir(tmp_dir) self.make_file('project/src/main/java/Foobar.java', '/* nothing to see here */') self.create_pom_with_modules(tmp_dir, ['project'], extra_project_contents=dependencies + profiles) PomToBuild().convert_pom( 'project/pom.xml', rootdir=tmp_dir, generation_context=GenerationContext(print_headers=False)) self.assert_file_contents( 'project/src/main/java/BUILD.gen', self._system_specific_properties_expected_text, ignore_leading_spaces=True, ignore_trailing_spaces=True, ignore_blanklines=True)
def test_provided_dependency_scope_prefix(self): with temporary_dir() as outdir: project_name = "dep-scope" self.set_options( project_dir=outdir, project_name=project_name, loose_files=True, merge=False, libraries=False, open=False, provided_module_dependencies={"module1": "module2"}, ) with open(os.path.join(get_buildroot(), "pom.xml"), "w+") as f: f.write( '<?xml version="1.0" encoding="UTF-8"?>\n' "<project>\n" " <modules>\n" " <module>module1-aa</module>\n" " <module>module2-bb</module>\n" " <module>module3-cc</module>\n" " </modules>\n" "</project>\n" ) module3 = self.make_target( "module3-cc:module3-cc", dependencies=[], target_type=JavaLibrary, sources=["Module3.java"] ) module2 = self.make_target( "module2-bb:module2-bb", dependencies=[], target_type=JavaLibrary, sources=["Module3.java"] ) module1 = self.make_target( "module1-aa:module1-aa", dependencies=[module2, module3], target_type=JavaLibrary, sources=["Module1.java"], ) task = self.create_task(self.context(target_roots=[module1])) task.execute() iml = os.path.join(outdir, project_name, "module1-aa.iml") self._assert_tree_contains(iml, "project_dep_scope_prefix.iml.xml")
def test_loose_files(self): with temporary_dir() as outdir: project_name = "foobar" self.set_options( project_dir=outdir, project_name=project_name, loose_files=True, merge=False, libraries=False, open=False, ) with open(os.path.join(get_buildroot(), "pom.xml"), "w+") as f: f.write('<?xml version="1.0" encoding="UTF-8"?>' "<project><modules/></project>") task = self.create_task(self.context()) task.execute() ipr = os.path.join(outdir, project_name, "foobar.ipr") iml = os.path.join(outdir, project_name, "java.iml") self._assert_tree_contains(ipr, "project_loose_files.ipr.xml", repo=os.path.basename(get_buildroot())) self._assert_tree_contains( iml, "project_loose_files.iml.xml", abs_path_to_build_root=os.path.abspath(get_buildroot()) )
def test_dependency_managment_finder(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(DEPENDENCY_MANAGEMENT_POM) dmf = squarepants.pom_handlers.DependencyManagementFinder(rootdir=tmpdir) deps = dmf.find_dependencies('pom.xml') self.assertEquals(2, len(deps)) self.assertEquals({u'groupId' : 'com.amazonaws', u'artifactId' : 'aws-java-sdk', u'version' : '1.9.5', u'exclusions' : [] }, deps[0]) self.assertEquals({ u'groupId' : 'io.dropwizard', u'artifactId' : 'dropwizard-auth', u'version' : '8.7.6', u'exclusions' : [ { u'groupId' : 'io.dropwizard', u'artifactId' : 'dropwizard-core' } ] }, deps[1])
def test_dependency_finder(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(DEPENDENCY_POM) df = squarepants.pom_handlers.DependencyInfo('pom.xml', rootdir=tmpdir) self.assertEquals('com.example', df.groupId) self.assertEquals('service', df.artifactId) self.assertEquals('2.3.4', df.properties['jhdf5.version']) self.assertEquals(4, len(df.dependencies)) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'parent', u'relativePath' : '../pom.xml', u'version' : 'HEAD-SNAPSHOT', }, df.dependencies[0]) self.assertEquals({u'groupId' : 'com.google.guava', u'artifactId' : 'guava', u'exclusions' : [] }, df.dependencies[1]) # this one has property substitution which doesn't occur until DependencyInfo self.assertEquals({u'groupId' : 'com.squareup.nonmaven.hdfgroup.hdf-java', u'artifactId' : 'jhdf5', u'version' : '2.3.4', u'exclusions' : [] }, df.dependencies[2]) self.assertEquals({ u'groupId' : 'io.dropwizard', u'artifactId' : 'dropwizard-auth', u'version' : '4.5.6', u'classifier' : 'shaded', u'exclusions' : [ { u'groupId' : 'io.dropwizard', u'artifactId' : 'dropwizard-core' } ] }, df.dependencies[3])
def test_provided_dependency_scope_prefix(self): with temporary_dir() as outdir: project_name = 'dep-scope' self.set_options( project_dir=outdir, project_name=project_name, loose_files=True, merge=False, libraries=False, open=False, provided_module_dependencies={'module1': 'module2'}) with open(os.path.join(get_buildroot(), 'pom.xml'), 'w+') as f: f.write('<?xml version="1.0" encoding="UTF-8"?>\n' '<project>\n' ' <modules>\n' ' <module>module1-aa</module>\n' ' <module>module2-bb</module>\n' ' <module>module3-cc</module>\n' ' </modules>\n' '</project>\n') module3 = self.make_target('module3-cc:module3-cc', dependencies=[], target_type=JavaLibrary, sources=['Module3.java']) module2 = self.make_target('module2-bb:module2-bb', dependencies=[], target_type=JavaLibrary, sources=['Module3.java']) module1 = self.make_target('module1-aa:module1-aa', dependencies=[module2, module3], target_type=JavaLibrary, sources=['Module1.java']) task = self.create_task(self.context(target_roots=[module1])) task.execute() iml = os.path.join(outdir, project_name, 'module1-aa.iml') self._assert_tree_contains(iml, 'project_dep_scope_prefix.iml.xml')
def test_app_manifest_bundle(self): """Makes sure the app-manifest.yaml properly makes it into the jar file on a ./pants binary. In particular, makes sure that the app-manifest.yaml is kept up to date and does not get stale, even if it is the only file that has changed since the last time ./pants binary was invoked. """ with temporary_dir() as distdir: source_yaml_file = 'squarepants/pants-aop-test-app/app-manifest.yaml' with self.invariant_file_contents(source_yaml_file) as plain_app_manifest: self.assertNotIn('bogus_flag: "bogus one"', plain_app_manifest) with open(source_yaml_file, 'a') as f: f.write('\nbogus_flag: "bogus one"\n') self.assert_success(self.pants('--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/pants-aop-test-app')) binary = os.path.join(distdir, 'pants-aop-test-app.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('app-manifest.yaml') as f: self.assertIn('bogus_flag: bogus one', f.read()) with open(source_yaml_file, 'w') as f: f.write(plain_app_manifest) self.assert_success(self.pants('--pants-distdir={}'.format(distdir), 'binary', '--binary-jvm-no-use-nailgun', 'squarepants/pants-aop-test-app', )) binary = os.path.join(distdir, 'pants-aop-test-app.jar') self.assertTrue(os.path.exists(binary)) with ZipFile(binary, 'r') as zf: with zf.open('app-manifest.yaml') as f: self.assertNotIn('bogus_flag: bogus one', f.read())
def _setup_signed_jar_test(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) parent_pom_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>app</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../base/pom.xml</relativePath> </parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <packaging>pom</packaging> <build> <plugins> <plugin> <groupId>com.squareup.maven.plugins</groupId> <artifactId>shade-plugin</artifactId> <version>${shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>shaded</shadedClassifierName> <transformers> <transformer> <manifestEntries> <Class-Path>lib-signed/artifact-one.jar lib-signed/artifact-two.jar</Class-Path> </manifestEntries> </transformer> </transformers> <artifactSet> <excludes> <exclude>org.foobar:artifact-one</exclude> <exclude>org.barfoo:artifact-two</exclude> </excludes> </artifactSet> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib-signed</outputDirectory> <includeArtifactIds>artifact-one,artifact-two</includeArtifactIds> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> ''' parent_pom_text = parent_pom_text.strip() project_pom_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../parents/the-parent/pom.xml</relativePath> </parent> <groupId>com.example.project</groupId> <artifactId>project</artifactId> <version>HEAD-SNAPSHOT</version> <name>gns-server</name> <properties> <project.mainclass>com.example.project.Project</project.mainclass> <deployableBranch>project</deployableBranch> </properties> <dependencies> </dependencies> </project> ''' project_pom_text = project_pom_text.strip() parent_base_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project> ''' parent_base_text = parent_base_text.strip() root_pom_text = """ <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> </modules> </project> """ root_pom_text = root_pom_text.strip() self.make_file( 'project/src/main/java/com/example/project/Project.java', '/* nope */') # NOTE(gm): This test probably could be made to work with fewer pom.xml's, this seems # excessive. self.make_file('project/pom.xml', project_pom_text) self.make_file('parents/the-parent/pom.xml', parent_pom_text) self.make_file('parents/base/pom.xml', parent_base_text) self.make_file('pom.xml', root_pom_text) yield tmp_dir
def test_include_parent_deps(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>parent-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> <groupId>com.example</groupId> <artifactId>child-dep</artifactId> <version>OVERRIDDEN-VERSION</version> <dependency> </dependency> </dependencies> </project> ''')) child_path_name = os.path.join(tmpdir, 'child') os.mkdir(child_path_name) child_pom_name = os.path.join(child_path_name, 'pom.xml') with open(child_pom_name, 'w') as child_pomfile: child_pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> ''')) child2_path_name = os.path.join(tmpdir, 'child2') os.mkdir(child2_path_name) child2_pom_name = os.path.join(child2_path_name, 'pom.xml') with open(child2_pom_name, 'w') as child2_pomfile: child2_pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>child</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../child/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child2-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> ''')) df = squarepants.pom_handlers.DependencyInfo('child2/pom.xml', rootdir=tmpdir) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'child', u'relativePath' : '../child/pom.xml', u'version' : 'HEAD-SNAPSHOT', }, df.dependencies[0]) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'child2-dep', u'version' : 'HEAD-SNAPSHOT', u'exclusions' : [] }, df.dependencies[1]) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'parent', u'relativePath' : '../pom.xml', u'version' : 'HEAD-SNAPSHOT', }, df.dependencies[2]) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'child-dep', u'version' : 'HEAD-SNAPSHOT', u'exclusions' : [] }, df.dependencies[3]) self.assertEquals({u'groupId' : 'com.example', u'artifactId' : 'parent-dep', u'version' : 'HEAD-SNAPSHOT', u'exclusions' : [] }, df.dependencies[4]) self.assertEquals(5, len(df.dependencies))
def setup_two_child_poms(self): with temporary_dir() as tmpdir: os.chdir(tmpdir) with open(os.path.join('pom.xml'), 'w') as pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> <module>child2</module> </modules> </project> """ pomfile.write(smart_dedent(triple_quote_string)) os.makedirs(os.path.join('parents', 'base')) with open(os.path.join('parents', 'base', 'pom.xml'), 'w') as base_pom: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project>""" base_pom.write(smart_dedent(triple_quote_string)) child1_path_name = 'child1' # Make some empty directories to hold BUILD.gen files os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'java')) os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'proto')) os.makedirs( os.path.join(child1_path_name, 'src', 'main', 'resources')) os.makedirs(os.path.join(child1_path_name, 'src', 'test', 'java')) os.makedirs(os.path.join(child1_path_name, 'src', 'test', 'proto')) os.makedirs( os.path.join(child1_path_name, 'src', 'test', 'resources')) child1_pom_name = os.path.join(child1_path_name, 'pom.xml') with open(child1_pom_name, 'w') as child1_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child2</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> """ child1_pomfile.write(smart_dedent(triple_quote_string)) child2_path_name = os.path.join('child2') os.makedirs(os.path.join(child2_path_name, 'src', 'main', 'java')) os.makedirs(os.path.join(child2_path_name, 'src', 'main', 'proto')) os.makedirs( os.path.join(child2_path_name, 'src', 'main', 'resources')) os.makedirs(os.path.join(child2_path_name, 'src', 'test', 'java')) os.makedirs(os.path.join(child2_path_name, 'src', 'test', 'proto')) os.makedirs( os.path.join(child2_path_name, 'src', 'test', 'resources')) child2_pom_name = os.path.join(child2_path_name, 'pom.xml') with open(child2_pom_name, 'w') as child2_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child2</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> </dependencies> </project> """ child2_pomfile.write(smart_dedent(triple_quote_string)) yield tmpdir
def test_find_target_directories(self): with temporary_dir() as outdir: project_name = "foobar" self.set_options( project_dir=outdir, project_name=project_name, loose_files=True, merge=False, libraries=False, open=False, ) with open(os.path.join(get_buildroot(), "pom.xml"), "w+") as f: f.write('<?xml version="1.0" encoding="UTF-8"?>' "<project><modules/></project>") task = self.create_task(self.context()) project = task._create_idea_project(outdir, None) with temporary_dir() as repo: poms = { os.path.join(repo, pom) for pom in ( "pom.xml", "foobar/pom.xml", "foobar/child1/pom.xml", "foobar/child1/sub/sub/pom.xml", "foobar/child1/sub/marine/foo.txt", "foobar/child1/sub/mersible/pom.xml", "foobar/child2/foo.txt", "foobar/child3/pom.xml", "orange/pom.xml", "yellow/foo.txt", ) } for pom in poms: touch(pom, makedirs=True) excludes = project._maven_excludes(os.path.join(repo, "foobar/child1")) expected = { os.path.join(repo, target) for target in ( "target", "foobar/target", "foobar/child1/target", "foobar/child1/sub/sub/target", "foobar/child1/sub/mersible/target", ) } self.assertEqual(expected, set(excludes)) excludes = project._maven_excludes(os.path.join(repo, "foobar")) expected = { os.path.join(repo, target) for target in ( "target", "foobar/target", "foobar/child1/target", "foobar/child1/sub/sub/target", "foobar/child1/sub/mersible/target", "foobar/child3/target", ) } self.assertEqual(expected, set(excludes)) excludes = project._maven_excludes(os.path.join(repo)) expected = { os.path.join(repo, target) for target in ( "target", "foobar/target", "foobar/child1/target", "foobar/child1/sub/sub/target", "foobar/child1/sub/mersible/target", "foobar/child3/target", "orange/target", ) } self.assertEqual(expected, set(excludes))
def setup_two_child_poms(self): with temporary_dir() as tmpdir: os.chdir(tmpdir) with open(os.path.join('pom.xml') , 'w') as pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> <module>child2</module> </modules> </project> """ pomfile.write(smart_dedent(triple_quote_string)) os.makedirs(os.path.join('parents', 'base')) with open(os.path.join('parents', 'base', 'pom.xml'), 'w') as base_pom: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project>""" base_pom.write(smart_dedent(triple_quote_string)) child1_path_name = 'child1' # Make some empty directories to hold BUILD.gen files os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'java')) os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'proto')) os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'resources')) os.makedirs(os.path.join(child1_path_name, 'src', 'test', 'java')) os.makedirs(os.path.join(child1_path_name, 'src', 'test', 'proto')) os.makedirs(os.path.join(child1_path_name, 'src', 'test', 'resources')) child1_pom_name = os.path.join(child1_path_name, 'pom.xml') with open(child1_pom_name, 'w') as child1_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child2</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> """ child1_pomfile.write(smart_dedent(triple_quote_string)) child2_path_name = os.path.join('child2') os.makedirs(os.path.join(child2_path_name, 'src', 'main', 'java')) os.makedirs(os.path.join(child2_path_name, 'src', 'main', 'proto')) os.makedirs(os.path.join(child2_path_name, 'src', 'main', 'resources')) os.makedirs(os.path.join(child2_path_name, 'src', 'test', 'java')) os.makedirs(os.path.join(child2_path_name, 'src', 'test', 'proto')) os.makedirs(os.path.join(child2_path_name, 'src', 'test', 'resources')) child2_pom_name = os.path.join(child2_path_name, 'pom.xml') with open(child2_pom_name, 'w') as child2_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child2</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> </dependencies> </project> """ child2_pomfile.write(smart_dedent(triple_quote_string)) yield tmpdir
def test_external_jar_ref(self): with temporary_dir() as tmpdir: os.chdir(tmpdir) with open(os.path.join('pom.xml') , 'w') as pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> </modules> </project> """ pomfile.write(smart_dedent(triple_quote_string)) os.makedirs(os.path.join('parents', 'base')) with open(os.path.join('parents', 'base', 'pom.xml'), 'w') as base_pom: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project>""" base_pom.write(smart_dedent(triple_quote_string)) child1_path_name = 'child1' # Make some empty directories to hold BUILD.gen files os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'java')) child1_pom_name = os.path.join(child1_path_name, 'pom.xml') with open(child1_pom_name, 'w') as child1_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example.external</groupId> <artifactId>foo</artifactId> <version>1.2.3</version> <classifier>shaded</classifier> <type>tar.gz</type> </dependency> </dependencies> </project> """ child1_pomfile.write(smart_dedent(triple_quote_string)) self.make_file('child1/src/main/java/Foo.java', 'class Foo { }') PomToBuild().convert_pom('child1/pom.xml', rootdir=tmpdir, generation_context=GenerationContext(print_headers=False)) triple_quote_string = """ target(name='lib', dependencies = [ 'child1/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('child1/BUILD.gen', triple_quote_string) triple_quote_string = """ java_library(name='lib', sources = rglobs('*.java'), resources = [], dependencies = [ ':jar_files' ], provides = artifact(org='com.example', name='child1', repo=square,), # see squarepants/plugin/repo/register.py ) jar_library(name='jar_files', jars=[ sjar(org='com.example.external', name='foo', rev='1.2.3', classifier='shaded', ext='tar.gz',) ], ) """ self.assert_file_contents('child1/src/main/java/BUILD.gen', triple_quote_string)
def test_squarepants_binary(self): with temporary_dir() as tempdir: self.assertEquals(os.path.join(tempdir, 'squarepants', 'bin', 'pants_release.sh'), BinaryUtils.squarepants_binary('pants_release.sh', tempdir))
def _setup_single_module(self, module_name, module_pom_contents, touch_files=None): with temporary_dir() as tempdir: current_dir = os.path.abspath('.') os.chdir(tempdir) self.make_file( 'pom.xml', dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>{module_name}</module> </modules> </project> '''.format(module_name=module_name)).strip()) self.make_file( os.path.join('parents', 'base', 'pom.xml'), dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project> '''.strip())) self.make_file( os.path.join(module_name, 'pom.xml'), dedent(''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../parents/base/pom.xml</relativePath> </parent> <groupId>com.example.project</groupId> <artifactId>{module_name}</artifactId> <version>HEAD-SNAPSHOT</version> {module_pom_contents} </project> ''').format(module_name=module_name, module_pom_contents=module_pom_contents).strip()) for path in (touch_files or ()): touch(path, makedirs=True) yield os.path.join(module_name, 'pom.xml') os.chdir(current_dir)
def _setup_signed_jar_test(self): with temporary_dir() as tmp_dir: os.chdir(tmp_dir) parent_pom_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>app</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../base/pom.xml</relativePath> </parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <packaging>pom</packaging> <build> <plugins> <plugin> <groupId>com.squareup.maven.plugins</groupId> <artifactId>shade-plugin</artifactId> <version>${shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>shaded</shadedClassifierName> <transformers> <transformer> <manifestEntries> <Class-Path>lib-signed/artifact-one.jar lib-signed/artifact-two.jar</Class-Path> </manifestEntries> </transformer> </transformers> <artifactSet> <excludes> <exclude>org.foobar:artifact-one</exclude> <exclude>org.barfoo:artifact-two</exclude> </excludes> </artifactSet> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib-signed</outputDirectory> <includeArtifactIds>artifact-one,artifact-two</includeArtifactIds> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> ''' parent_pom_text = parent_pom_text.strip() project_pom_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../parents/the-parent/pom.xml</relativePath> </parent> <groupId>com.example.project</groupId> <artifactId>project</artifactId> <version>HEAD-SNAPSHOT</version> <name>gns-server</name> <properties> <project.mainclass>com.example.project.Project</project.mainclass> <deployableBranch>project</deployableBranch> </properties> <dependencies> </dependencies> </project> ''' project_pom_text = project_pom_text.strip() parent_base_text = ''' <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>the-parent-pom</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project> ''' parent_base_text = parent_base_text.strip() root_pom_text = """ <?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> </modules> </project> """ root_pom_text = root_pom_text.strip() self.make_file('project/src/main/java/com/example/project/Project.java', '/* nope */') # NOTE(gm): This test probably could be made to work with fewer pom.xml's, this seems # excessive. self.make_file('project/pom.xml', project_pom_text) self.make_file('parents/the-parent/pom.xml', parent_pom_text) self.make_file('parents/base/pom.xml', parent_base_text) self.make_file('pom.xml', root_pom_text) yield tmp_dir
def make_sample_repo(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as root_pom: root_pom_data = """<?xml version='1.0' encoding='UTF-8'?> <project xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd' xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <modelVersion>4.0.0</modelVersion> <groupId>com.squareup.foo</groupId> <artifactId>all</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>foo-service</module> </modules> </project> """ root_pom.write(root_pom_data) os.makedirs(os.path.join(tmpdir, 'parents', 'base')) with open(os.path.join(tmpdir, 'parents', 'base', 'pom.xml'), 'w') as base_pom_file: base_pom_data = """<?xml version='1.0' encoding='UTF-8'?> <project xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd' xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <modelVersion>4.0.0</modelVersion> <groupId>com.squareup.foo</groupId> <artifactId>foo-base</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project> """ base_pom_file.write(base_pom_data) os.mkdir(os.path.join(tmpdir, 'foo-service')) with open(os.path.join(tmpdir, 'foo-service', 'pom.xml'), 'w') as pom_file: pom_data = """<?xml version='1.0' encoding='UTF-8'?> <project xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd' xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <modelVersion>4.0.0</modelVersion> <groupId>com.squareup.foo</groupId> <artifactId>foo-service</artifactId> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.squareup.foo</groupId> <artifactId>foo-base</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../parents/base/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.squareup.service</groupId> <artifactId>container</artifactId> <version>1.2.3</version> </dependency> </dependencies> </project> """ pom_file.write(pom_data) yield tmpdir
def test_include_parent_deps(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>parent-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> <groupId>com.example</groupId> <artifactId>child-dep</artifactId> <version>OVERRIDDEN-VERSION</version> <dependency> </dependency> </dependencies> </project> ''')) child_path_name = os.path.join(tmpdir, 'child') os.mkdir(child_path_name) child_pom_name = os.path.join(child_path_name, 'pom.xml') with open(child_pom_name, 'w') as child_pomfile: child_pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> ''')) child2_path_name = os.path.join(tmpdir, 'child2') os.mkdir(child2_path_name) child2_pom_name = os.path.join(child2_path_name, 'pom.xml') with open(child2_pom_name, 'w') as child2_pomfile: child2_pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>child</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../child/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>child2-dep</artifactId> <version>HEAD-SNAPSHOT</version> </dependency> </dependencies> </project> ''')) df = squarepants.pom_handlers.DependencyInfo('child2/pom.xml', rootdir=tmpdir) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'child', u'relativePath': '../child/pom.xml', u'version': 'HEAD-SNAPSHOT', }, df.dependencies[0]) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'child2-dep', u'version': 'HEAD-SNAPSHOT', u'exclusions': [] }, df.dependencies[1]) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'parent', u'relativePath': '../pom.xml', u'version': 'HEAD-SNAPSHOT', }, df.dependencies[2]) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'child-dep', u'version': 'HEAD-SNAPSHOT', u'exclusions': [] }, df.dependencies[3]) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'parent-dep', u'version': 'HEAD-SNAPSHOT', u'exclusions': [] }, df.dependencies[4]) self.assertEquals(5, len(df.dependencies))
def test_wire_info(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>initialize</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.squareup.protos</groupId> <artifactId>all-protos</artifactId> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/../src/main/wire_proto</outputDirectory> <includes>squareup/xp/validation.proto,squareup/xp/oauth/**,squareup/xp/v1/common.proto,squareup/xp/v1/http.proto</includes> </artifactItem> <artifactItem> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/../src/main/wire_proto</outputDirectory> <includes>**/descriptor.proto</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.squareup.wire</groupId> <artifactId>wire-maven-plugin</artifactId> <executions> <execution> <goals> <goal>generate-sources</goal> </goals> <phase>generate-sources</phase> </execution> </executions> <configuration> <noOptions>true</noOptions> <serviceFactory>com.squareup.wire.java.SimpleServiceFactory</serviceFactory> <serviceFactory>com.squareup.wire.java.SimpleServiceFactory</serviceFactory> <protoFiles> <protoFile>squareup/protobuf/rpc/rpc.proto</protoFile> <protoFile>squareup/sake/wire_format.proto</protoFile> </protoFiles> <roots> <root>squareup.franklin.settings.UnlinkSmsRequest</root> <root>squareup.franklin.settings.VerifyEmailRequest</root> </roots> </configuration> </plugin> </plugins> </build> </project> ''')) wf = squarepants.pom_handlers.WireInfo.from_pom('pom.xml', rootdir=tmpdir) self.assertEquals(True, wf.no_options) self.assertEquals(['squareup/protobuf/rpc/rpc.proto', 'squareup/sake/wire_format.proto'], wf.protos) self.assertEquals(['squareup.franklin.settings.UnlinkSmsRequest', 'squareup.franklin.settings.VerifyEmailRequest'], wf.roots) self.assertEquals([], wf.enum_options) self.assertEquals('com.squareup.wire.java.SimpleServiceFactory', wf.service_factory) self.assertEquals(None, wf.registry_class) self.assertEquals(set([('com.squareup.protos', 'all-protos',), ('com.google.protobuf', 'protobuf-java',),]), set(wf.artifacts)) self.assertEquals('**/descriptor.proto', wf.artifacts[('com.google.protobuf', 'protobuf-java',)]['includes'])
def test_type_test_jar(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dep1</artifactId> <type>foo</type> <version>1.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dep2</artifactId> <type>test-jar</type> <version>1.2.3</version> </dependency> </dependencies> </project> ''')) # TODO(Eric Ayers): Right now, our builds expect a file in <rootdir>/parents/base/pom.xml os.makedirs(os.path.join(tmpdir, 'parents', 'base')) with open(os.path.join(tmpdir, 'parents', 'base', 'pom.xml'), 'w') as dep_mgmt_pom: dep_mgmt_pom.write(DEPENDENCY_MANAGEMENT_POM) df = squarepants.pom_handlers.DependencyInfo('pom.xml', rootdir=tmpdir) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'dep1', u'type': 'foo', u'exclusions': [], u'version': '1.0' }, df.dependencies[0]) self.assertEquals( { u'groupId': 'com.example', u'artifactId': 'dep2', u'type': 'test-jar', u'exclusions': [], u'version': '1.2.3' }, df.dependencies[1]) self.assertEquals(2, len(df.dependencies)) deps_from_pom = squarepants.pom_handlers.DepsFromPom( PomUtils.pom_provides_target(), rootdir=tmpdir) refs = deps_from_pom.build_pants_refs(df.dependencies) self.assertEquals( "sjar(org='com.example', name='dep1', rev='1.0', ext='foo',)", refs[0]) # type test-jar gets transformed into a 'tests' classifier self.assertEquals( "sjar(org='com.example', name='dep2', rev='1.2.3', classifier='tests',)", refs[1]) self.assertEquals(2, len(refs))
def test_wire_info(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml'), 'w') as pomfile: pomfile.write( dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>initialize</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.squareup.protos</groupId> <artifactId>all-protos</artifactId> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/../src/main/wire_proto</outputDirectory> <includes>squareup/xp/validation.proto,squareup/xp/oauth/**,squareup/xp/v1/common.proto,squareup/xp/v1/http.proto</includes> </artifactItem> <artifactItem> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/../src/main/wire_proto</outputDirectory> <includes>**/descriptor.proto</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.squareup.wire</groupId> <artifactId>wire-maven-plugin</artifactId> <executions> <execution> <goals> <goal>generate-sources</goal> </goals> <phase>generate-sources</phase> </execution> </executions> <configuration> <noOptions>true</noOptions> <serviceFactory>com.squareup.wire.java.SimpleServiceFactory</serviceFactory> <serviceFactory>com.squareup.wire.java.SimpleServiceFactory</serviceFactory> <protoFiles> <protoFile>squareup/protobuf/rpc/rpc.proto</protoFile> <protoFile>squareup/sake/wire_format.proto</protoFile> </protoFiles> <roots> <root>squareup.franklin.settings.UnlinkSmsRequest</root> <root>squareup.franklin.settings.VerifyEmailRequest</root> </roots> </configuration> </plugin> </plugins> </build> </project> ''')) wf = squarepants.pom_handlers.WireInfo.from_pom('pom.xml', rootdir=tmpdir) self.assertEquals(True, wf.no_options) self.assertEquals([ 'squareup/protobuf/rpc/rpc.proto', 'squareup/sake/wire_format.proto' ], wf.protos) self.assertEquals([ 'squareup.franklin.settings.UnlinkSmsRequest', 'squareup.franklin.settings.VerifyEmailRequest' ], wf.roots) self.assertEquals([], wf.enum_options) self.assertEquals('com.squareup.wire.java.SimpleServiceFactory', wf.service_factory) self.assertEquals(None, wf.registry_class) self.assertEquals( set([ ( 'com.squareup.protos', 'all-protos', ), ( 'com.google.protobuf', 'protobuf-java', ), ]), set(wf.artifacts)) self.assertEquals( '**/descriptor.proto', wf.artifacts[( 'com.google.protobuf', 'protobuf-java', )]['includes'])
def test_pom_properties(self): with temporary_dir() as tmpdir: with open(os.path.join(tmpdir, 'pom.xml') , 'w') as pomfile: pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <properties> <base.prop>FOO</base.prop> <base.overridden>BASE</base.overridden> </properties> </project> ''')) child_path_name = os.path.join(tmpdir, 'child') os.mkdir(child_path_name) child_pom_name = os.path.join(child_path_name, 'pom.xml') with open(child_pom_name, 'w') as child_pomfile: child_pomfile.write(dedent('''<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.example</groupId> <artifactId>child</artifactId> <description>A generic Square module.</description> <version>HEAD-SNAPSHOT</version> <parent> <groupId>com.example</groupId> <artifactId>base</artifactId> <version>HEAD-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <properties> <child.prop1>BAR</child.prop1> <child.prop2>base is ${base.prop}</child.prop2> <base.overridden>CHILD</base.overridden> </properties> </project> ''')) buffer = StringIO.StringIO() PomProperties().write_properties('child/pom.xml', buffer, rootdir=tmpdir) properties=set() for line in buffer.getvalue().split('\n'): properties.add(line) self.assertIn('base_prop="FOO"', properties) self.assertIn('child_prop1="BAR"', properties) self.assertIn('child_prop2="base is FOO"', properties) self.assertIn('base_overridden="CHILD"', properties) self.assertIn('project_artifactId="child"', properties) self.assertIn('project_groupId="com.example"', properties)
def test_external_jar_ref(self): with temporary_dir() as tmpdir: os.chdir(tmpdir) with open(os.path.join('pom.xml'), 'w') as pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>HEAD-SNAPSHOT</version> <modules> <module>child1</module> </modules> </project> """ pomfile.write(smart_dedent(triple_quote_string)) os.makedirs(os.path.join('parents', 'base')) with open(os.path.join('parents', 'base', 'pom.xml'), 'w') as base_pom: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencyManagement> </dependencyManagement> </project>""" base_pom.write(smart_dedent(triple_quote_string)) child1_path_name = 'child1' # Make some empty directories to hold BUILD.gen files os.makedirs(os.path.join(child1_path_name, 'src', 'main', 'java')) child1_pom_name = os.path.join(child1_path_name, 'pom.xml') with open(child1_pom_name, 'w') as child1_pomfile: triple_quote_string = """<?xml version="1.0" encoding="UTF-8"?> <project> <groupId>com.example</groupId> <artifactId>child1</artifactId> <version>HEAD-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.example.external</groupId> <artifactId>foo</artifactId> <version>1.2.3</version> <classifier>shaded</classifier> <type>tar.gz</type> </dependency> </dependencies> </project> """ child1_pomfile.write(smart_dedent(triple_quote_string)) self.make_file('child1/src/main/java/Foo.java', 'class Foo { }') PomToBuild().convert_pom( 'child1/pom.xml', rootdir=tmpdir, generation_context=GenerationContext(print_headers=False)) triple_quote_string = """ target(name='lib', dependencies = [ 'child1/src/main/java:lib' ], ) target(name='test', dependencies = [ ':lib' ], ) """ self.assert_file_contents('child1/BUILD.gen', triple_quote_string) triple_quote_string = """ java_library(name='lib', sources = rglobs('*.java'), resources = [], dependencies = [ ':jar_files' ], provides = artifact(org='com.example', name='child1', repo=square,), # see squarepants/plugin/repo/register.py ) jar_library(name='jar_files', jars=[ sjar(org='com.example.external', name='foo', rev='1.2.3', classifier='shaded', ext='tar.gz',) ], ) """ self.assert_file_contents('child1/src/main/java/BUILD.gen', triple_quote_string)