def test_jvm_options(self): init_subsystem( JvmPlatform, options={ "jvm-platform": { "platforms": { "platform-with-jvm-options": { "target": "8", "jvm_options": ["-Dsomething"], }, "platform-without-jvm-options": { "target": "8" }, }, } }, ) instance = JvmPlatform.global_instance() with_options = instance.get_platform_by_name( "platform-with-jvm-options") without_options = instance.get_platform_by_name( "platform-without-jvm-options") assert ("-Dsomething", ) == with_options.jvm_options assert tuple() == without_options.jvm_options
def scala_platform_setup(self): options = { ScalaPlatform.options_scope: { "version": "custom", "suffix_version": "2.10" } } init_subsystem(ScalaPlatform, options) self.make_target( "//:scalastyle", JarLibrary, jars=[JarDependency("org.scalastyle", "scalastyle_2.10", "0.3.2")], ) self.make_target( "//:scala-repl", JarLibrary, jars=[ JarDependency(org="org.scala-lang", name="jline", rev="2.10.5"), JarDependency(org="org.scala-lang", name="scala-compiler", rev="2.10.5"), ], ) self.make_target( "//:scalac", JarLibrary, jars=[JarDependency("org.scala-lang", "scala-compiler", "2.10.5")], )
def is_missing_jvm(version): init_subsystem(DistributionLocator) try: DistributionLocator.cached(minimum_version=version, maximum_version=f"{version}.9999") return False except DistributionLocator.Error: return True
def scala_platform_setup(self): options = { ScalaPlatform.options_scope: { 'version': 'custom', 'suffix_version': '2.10', } } init_subsystem(ScalaPlatform, options) self.make_target('//:scalastyle', JarLibrary, jars=[JarDependency('org.scalastyle', 'scalastyle_2.10', '0.3.2')] ) self.make_target('//:scala-repl', JarLibrary, jars=[ JarDependency(org = 'org.scala-lang', name = 'jline', rev = '2.10.5'), JarDependency(org = 'org.scala-lang', name = 'scala-compiler', rev = '2.10.5')]) self.make_target('//:scalac', JarLibrary, jars=[JarDependency('org.scala-lang', 'scala-compiler', '2.10.5')])
def test_coverage_auto_option_no_explicit_coverage_idiosyncratic_layout(self): # The all target has no coverage attribute and the code under test does not follow the # auto-discover (parallel packages) pattern so we should get no coverage. init_subsystem(Target.Arguments) init_subsystem(SourceRootConfig) self.add_to_build_file('src/python/util', 'python_library()') self.create_file( 'src/python/util/math.py', dedent(""" def one(): # line 1 return 1 # line 2 """).strip()) self.add_to_build_file('test/python/util_tests', 'python_tests(dependencies = ["src/python/util"])') self.create_file( 'test/python/util_tests/test_math.py', dedent(""" import unittest from util import math class MathTest(unittest.TestCase): def test_one(self): self.assertEqual(1, math.one()) """)) test = self.target('test/python/util_tests') covered_path = os.path.join(self.build_root, 'src/python/util/math.py') all_statements, not_run_statements = self.run_coverage_auto(targets=[test], covered_path=covered_path, include_test_sources=True) self.assertEqual([1, 2], all_statements) self.assertEqual([1, 2], not_run_statements)
def setUp(self): super().setUp() init_subsystem(Target.Arguments) self.add_to_build_file( "a", 'java_library(sources=["a1.java", "a2.java"])', ) self.jar_artifact = self.create_artifact(org="org.example", name="foo", rev="1.0.0") with open_zip(self.jar_artifact.pants_path, "w") as jar: jar.writestr("foo/Foo.class", "") self.add_to_build_file( "b", 'jar_library(jars=[jar(org="org.example", name="foo", rev="1.0.0")])', ) self.add_to_build_file( "c", 'java_library(dependencies=["a", "b"])', ) self.target_a = self.target("a") self.target_b = self.target("b") self.target_c = self.target("c")
def setUp(self): init_subsystem(Target.TagAssignments) self.target_macro_factory = TargetMacro.Factory.wrap( lambda ctx: ctx.create_object(self.BlueTarget, type_alias='jill', name=os.path.basename(ctx.rel_path)), self.BlueTarget)
def setUp(self): super().setUp() init_subsystem(Target.Arguments) self.add_to_build_file( 'a', 'java_library(sources=["a1.java", "a2.java"])', ) self.jar_artifact = self.create_artifact(org='org.example', name='foo', rev='1.0.0') with open_zip(self.jar_artifact.pants_path, 'w') as jar: jar.writestr('foo/Foo.class', '') self.add_to_build_file( 'b', 'jar_library(jars=[jar(org="org.example", name="foo", rev="1.0.0")])', ) self.add_to_build_file( 'c', 'java_library(dependencies=["a", "b"])', ) self.target_a = self.target('a') self.target_b = self.target('b') self.target_c = self.target('c')
def test_java_home_extraction_empty(self): init_subsystem(DistributionLocator) result = tuple(self._get_zinc_arguments( JvmPlatformSettings('1.7', '1.7', []) )) self.assertEqual(4, len(result), msg='_get_zinc_arguments did not correctly handle empty args.')
def test_pex_execution(self) -> None: input_files_content = InputFilesContent( ( FileContent(path="main.py", content=b'print("from main")'), FileContent(path="subdir/sub.py", content=b'print("from sub")'), ) ) input_files = self.request_single_product(Digest, input_files_content) pex_output = self.create_pex_and_get_all_data(entry_point="main", input_files=input_files) pex_files = pex_output["files"] self.assertTrue("pex" not in pex_files) self.assertTrue("main.py" in pex_files) self.assertTrue("subdir/sub.py" in pex_files) init_subsystem(PythonSetup) python_setup = PythonSetup.global_instance() env = {"PATH": create_path_env_var(python_setup.interpreter_search_paths)} req = ExecuteProcessRequest( argv=("python", "test.pex"), env=env, input_files=pex_output["pex"].directory_digest, description="Run the pex and make sure it works", ) result = self.request_single_product(ExecuteProcessResult, req) self.assertEqual(result.stdout, b"from main\n")
def test_force_override(self): jars = list(self.a.payload.jars) with temporary_file_path() as ivyxml: init_subsystem(JarDependencyManagement) IvyUtils.generate_ivy([self.a], jars=jars, excludes=[], ivyxml=ivyxml, confs=['default']) doc = ET.parse(ivyxml).getroot() conf = self.find_single(doc, 'configurations/conf') self.assert_attributes(conf, name='default') dependencies = list(doc.findall('dependencies/dependency')) self.assertEqual(2, len(dependencies)) dep1 = dependencies[0] self.assert_attributes(dep1, org='org1', name='name1', rev='rev1') conf = self.find_single(dep1, 'conf') self.assert_attributes(conf, name='default', mapped='default') dep2 = dependencies[1] self.assert_attributes(dep2, org='org2', name='name2', rev='rev2', force='true') conf = self.find_single(dep1, 'conf') self.assert_attributes(conf, name='default', mapped='default') override = self.find_single(doc, 'dependencies/override') self.assert_attributes(override, org='org2', module='name2', rev='rev2')
def setUp(self): super().setUp() init_subsystem(IPython) self.six = self.create_python_requirement_library( "3rdparty/python/six", "six", requirements=["six==1.13.0"]) self.requests = self.create_python_requirement_library( "3rdparty/python/requests", "requests", requirements=["requests==2.6.0"]) self.library = self.create_python_library( "src/python/lib", "lib", { "lib.py": dedent(""" import six def go(): six.print_('go', 'go', 'go!', sep='') """) }, dependencies=["//3rdparty/python/six"], ) self.binary = self.create_python_binary( "src/python/bin", "bin", "lib.go", dependencies=["//src/python/lib"]) self.non_python_target = self.create_non_python_target( "src/python/java", "java")
def setUp(self): super().setUp() init_subsystem(IPython) self.six = self.create_python_requirement_library( '3rdparty/python/six', 'six', requirements=['six==1.13.0']) self.requests = self.create_python_requirement_library( '3rdparty/python/requests', 'requests', requirements=['requests==2.6.0']) self.library = self.create_python_library( 'src/python/lib', 'lib', { 'lib.py': dedent(""" import six def go(): six.print_('go', 'go', 'go!', sep='') """) }, dependencies=['//3rdparty/python/six']) self.binary = self.create_python_binary( 'src/python/bin', 'bin', 'lib.go', dependencies=['//src/python/lib']) self.non_python_target = self.create_non_python_target( 'src/python/java', 'java')
def test_scala_exclude(self): init_subsystem(ScalaPlatform) name = "foo_lib" suffixed_name = ScalaPlatform.global_instance().suffix_version(name) self.assertEqual(suffixed_name, ScalaExclude(org="example.com", name=name).name)
def test_coverage_auto_option_no_explicit_coverage(self): init_subsystem(Target.Arguments) init_subsystem(SourceRootConfig) self.add_to_build_file('src/python/util', 'python_library()') self.create_file( 'src/python/util/math.py', dedent(""" def one(): # line 1 return 1 # line 2 """).strip()) self.add_to_build_file( 'test/python/util', 'python_tests(dependencies = ["src/python/util"])') self.create_file( 'test/python/util/test_math.py', dedent(""" import unittest from util import math class MathTest(unittest.TestCase): def test_one(self): self.assertEqual(1, math.one()) """)) test = self.target('test/python/util') covered_path = os.path.join(self.build_root, 'src/python/util/math.py') all_statements, not_run_statements = self.run_coverage_auto( targets=[test], covered_path=covered_path) self.assertEqual([1, 2], all_statements) self.assertEqual([], not_run_statements)
def test_invalid_binary(self) -> None: init_subsystem(SourceRootConfig) self.create_file( "src/python/invalid_binary/BUILD", textwrap.dedent(""" python_library(name='not_a_binary', sources=[]) python_binary(name='no_entrypoint') python_library( name='invalid_bin1', sources=[], provides=setup_py( name='invalid_bin1', version='1.1.1' ).with_binaries(foo=':not_a_binary') ) python_library( name='invalid_bin2', sources=[], provides=setup_py( name='invalid_bin2', version='1.1.1' ).with_binaries(foo=':no_entrypoint') ) """), ) self.assert_error("src/python/invalid_binary:invalid_bin1", InvalidEntryPoint) self.assert_error("src/python/invalid_binary:invalid_bin2", InvalidEntryPoint)
def setUp(self): """ :API: public """ super().setUp() init_subsystem(JvmResolveSubsystem) self.set_options_for_scope('resolver', resolver='ivy')
def test_force_override(self): jars = list(self.a.payload.jars) with temporary_file_path() as ivyxml: init_subsystem(JarDependencyManagement) IvyUtils.generate_ivy( [self.a], jars=jars, excludes=[], ivyxml=ivyxml, confs=["default"] ) doc = ET.parse(ivyxml).getroot() conf = self.find_single(doc, "configurations/conf") self.assert_attributes(conf, name="default") dependencies = list(doc.findall("dependencies/dependency")) self.assertEqual(2, len(dependencies)) dep1 = dependencies[0] self.assert_attributes(dep1, org="org1", name="name1", rev="rev1") conf = self.find_single(dep1, "conf") self.assert_attributes(conf, name="default", mapped="default") dep2 = dependencies[1] self.assert_attributes(dep2, org="org2", name="name2", rev="rev2", force="true") conf = self.find_single(dep1, "conf") self.assert_attributes(conf, name="default", mapped="default") override = self.find_single(doc, "dependencies/override") self.assert_attributes(override, org="org2", module="name2", rev="rev2")
def setUp(self): """ :API: public """ super().setUp() init_subsystem(JvmResolveSubsystem) self.set_options_for_scope("resolver", resolver="coursier")
def test(self): init_subsystem(SourceRootConfig) self.create_file('src/python/package/module.py') implicit_package_target = self.make_target(spec='src/python/package', target_type=PythonLibrary, sources=['module.py']) pbw = self.pex_builder_wrapper() pbw.add_sources_from(implicit_package_target) with temporary_file_path() as pex: pbw.build(pex) with self.extracted_pex(pex) as chroot_path: # Check the paths we know about: package_path = chroot_path / 'package' self.assert_dir_perms(package_path) user_files = { package_path / f for f in ('__init__.py', 'module.py') } for user_file in user_files: self.assert_file_perms(user_file) # And all other paths pex generates (__main__.py, PEX-INFO, .deps/, etc...): for root, dirs, files in os.walk(chroot_path): for d in dirs: dir_path = pathlib.Path(root) / d if dir_path != package_path: self.assert_dir_perms(dir_path) for f in files: file_path = pathlib.Path(root) / f if file_path not in user_files: self.assert_file_perms(file_path)
def test_subsystem_option_sets(self): init_subsystem(ScoveragePlatform) ScoveragePlatform.global_instance().get_options( ).enable_scoverage = True subsystem = ScoveragePlatform.global_instance() self.assertEqual(True, subsystem.get_options().enable_scoverage)
def setUp(self): super().setUp() # Default scalastyle config (import grouping rule) and no excludes. init_subsystem(ScalaPlatform, {ScalaPlatform.options_scope: { 'version': '2.11' }}) init_subsystem(ScoveragePlatform)
def setUp(self): self.jarjar = '/not/really/jarjar.jar' init_subsystem(DistributionLocator) executor = SubprocessExecutor(DistributionLocator.cached()) self.shader = Shader(jarjar_classpath=[self.jarjar], executor=executor, binary_package_excludes=['javax']) self.output_jar = '/not/really/shaded.jar'
def setUp(self): super().setUp() # This is normally taken care of in RunPrepCommandBase.register_options() when running pants, # but these don't get called in testing unless you call `self.create_task()`. # Some of these unit tests need to create targets before creating the task. PrepCommand.add_allowed_goal("test") PrepCommand.add_allowed_goal("binary") init_subsystem(Target.Arguments)
def marking_tests(self): init_subsystem(Target.Arguments) init_subsystem(SourceRootConfig) with temporary_dir() as marker_dir: self.create_file( 'test/python/passthru/test_passthru.py', dedent(""" import inspect import os import pytest import unittest class PassthruTest(unittest.TestCase): def touch(self, path): with open(path, 'wb') as fp: fp.close() def mark_test_run(self): caller_frame_record = inspect.stack()[1] # For the slot breakdown of a frame record tuple, see: # https://docs.python.org/2/library/inspect.html#the-interpreter-stack _, _, _, caller_func_name, _, _ = caller_frame_record marker_file = os.path.join({marker_dir!r}, caller_func_name) self.touch(marker_file) def test_one(self): self.mark_test_run() @pytest.mark.purple def test_two(self): self.mark_test_run() def test_three(self): self.mark_test_run() @pytest.mark.red def test_four(self): self.mark_test_run() @pytest.mark.green def test_five(self): self.mark_test_run() """.format(marker_dir=marker_dir))) def assert_mark(exists, name): message = f"{('Expected' if exists else 'Did not expect')} {name!r} to be executed." marker_file = os.path.join(marker_dir, name) self.assertEqual(exists, os.path.exists(marker_file), message) self.add_to_build_file('test/python/passthru', 'python_tests()') test = self.target('test/python/passthru') yield test, functools.partial(assert_mark, True), functools.partial( assert_mark, False)
def test_generate_chroot(self) -> None: init_subsystem(SourceRootConfig) self.create_file( "src/python/foo/bar/baz/BUILD", "python_library(provides=setup_py(name='baz', version='1.1.1'))", ) self.create_file("src/python/foo/bar/baz/baz.py", "") self.create_file( "src/python/foo/qux/BUILD", textwrap.dedent(""" python_library() python_binary(name="bin", entry_point="foo.qux.bin") """), ) self.create_file("src/python/foo/qux/__init__.py", "") self.create_file("src/python/foo/qux/qux.py", "") self.create_file("src/python/foo/resources/BUILD", 'resources(sources=["js/code.js"])') self.create_file("src/python/foo/resources/js/code.js", "") self.create_file( "src/python/foo/BUILD", textwrap.dedent(""" python_library(dependencies=['src/python/foo/bar/baz', 'src/python/foo/qux', 'src/python/foo/resources'], provides=setup_py(name='foo', version='1.2.3').with_binaries( foo_main='src/python/foo/qux:bin')) """), ) self.create_file("src/python/foo/__init__.py", _namespace_decl) self.create_file("src/python/foo/foo.py", "") self.assert_chroot( [ "src/foo/qux/__init__.py", "src/foo/qux/qux.py", "src/foo/resources/js/code.js", "src/foo/__init__.py", "src/foo/foo.py", "setup.py", "MANIFEST.in", ], { "name": "foo", "version": "1.2.3", "package_dir": { "": "src" }, "packages": ["foo", "foo.qux"], "namespace_packages": ["foo"], "package_data": { "foo": ["resources/js/code.js"] }, "install_requires": ["baz==1.1.1"], "entry_points": { "console_scripts": ["foo_main=foo.qux.bin"] }, }, "src/python/foo", )
def setUp(self): super().setUp() pants_ivy_settings = pkg_resources.resource_filename( __name__, "../../build-support/ivy/ivysettings.xml") # This ivy settings contains the RBE maven mirror that gets around maven blacklisting. init_subsystem(IvySubsystem, {"ivy": { "bootstrap_ivy_settings": pants_ivy_settings }})
def test_shader_project(self): """Test that the binary target at the ``shading_project`` can be built and run. Explicitly checks that the classes end up with the correct shaded fully qualified classnames. """ shading_project = "testprojects/src/java/org/pantsbuild/testproject/shading" self.assert_success(self.run_pants(["clean-all"])) self.assert_success(self.run_pants(["binary", shading_project])) expected_classes = { # Explicitly excluded by a shading_exclude() rule. "org/pantsbuild/testproject/shadingdep/PleaseDoNotShadeMe.class", # Not matched by any rule, so stays the same. "org/pantsbuild/testproject/shading/Main.class", # Shaded with the target_id prefix, along with the default pants prefix. ("__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/otherpackage/" "ShadeWithTargetId.class"), # Also shaded with the target_id prefix and default pants prefix, but for a different target # (so the target_id is different). ("__shaded_by_pants__/org/pantsbuild/testproject/shading/ShadeSelf.class" ), # All these are shaded by the same shading_relocate_package(), which is recursive by default. "__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/subpackage/Subpackaged.class", "__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/SomeClass.class", "__shaded_by_pants__/org/pantsbuild/testproject/shadingdep/Dependency.class", # Shaded by a shading_relocate() that completely renames the package and class name. "org/pantsbuild/testproject/foo/bar/MyNameIsDifferentNow.class", } path = os.path.join("dist", "shading.jar") init_subsystem(DistributionLocator) execute_java = DistributionLocator.cached( minimum_version="1.6").execute_java self.assertEqual( 0, execute_java(classpath=[path], main="org.pantsbuild.testproject.shading.Main")) self.assertEqual( 0, execute_java( classpath=[path], main="org.pantsbuild.testproject.foo.bar.MyNameIsDifferentNow" ), ) received_classes = set() with temporary_dir() as tempdir: ZIP.extract(path, tempdir, filter_func=lambda f: f.endswith(".class")) for root, dirs, files in os.walk(tempdir): for name in files: received_classes.add( os.path.relpath(os.path.join(root, name), tempdir)) self.assertEqual(expected_classes, received_classes)
def test_validation(self): init_subsystem(JUnit) target = self.make_target("//:mybird", Target) # A plain invocation with no frills test1 = self.make_target( "//:test1", JUnitTests, sources=["Test.java"], dependencies=[target] ) self.assertIsNone(test1.cwd) self.assertIsNone(test1.concurrency) self.assertIsNone(test1.threads) self.assertIsNone(test1.timeout) # cwd parameter testcwd = self.make_target( "//:testcwd1", JUnitTests, sources=["Test.java"], concurrency="SERIAL", cwd="/foo/bar" ) self.assertEqual("/foo/bar", testcwd.cwd) # concurrency parameter tc1 = self.make_target( "//:testconcurrency1", JUnitTests, sources=["Test.java"], concurrency="SERIAL" ) self.assertEqual(JUnitTests.CONCURRENCY_SERIAL, tc1.concurrency) tc2 = self.make_target( "//:testconcurrency2", JUnitTests, sources=["Test.java"], concurrency="PARALLEL_CLASSES" ) self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_CLASSES, tc2.concurrency) tc3 = self.make_target( "//:testconcurrency3", JUnitTests, sources=["Test.java"], concurrency="PARALLEL_METHODS" ) self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_METHODS, tc3.concurrency) tc4 = self.make_target( "//:testconcurrency4", JUnitTests, sources=["Test.java"], concurrency="PARALLEL_CLASSES_AND_METHODS", ) self.assertEqual(JUnitTests.CONCURRENCY_PARALLEL_CLASSES_AND_METHODS, tc4.concurrency) with self.assertRaisesRegex(TargetDefinitionException, r"concurrency"): self.make_target( "//:testconcurrency5", JUnitTests, sources=["Test.java"], concurrency="nonsense" ) # threads parameter tt1 = self.make_target("//:testthreads1", JUnitTests, sources=["Test.java"], threads=99) self.assertEqual(99, tt1.threads) tt2 = self.make_target("//:testthreads2", JUnitTests, sources=["Test.java"], threads="123") self.assertEqual(123, tt2.threads) with self.assertRaisesRegex(TargetDefinitionException, r"threads"): self.make_target("//:testthreads3", JUnitTests, sources=["Test.java"], threads="abc") # timeout parameter timeout = self.make_target( "//:testtimeout1", JUnitTests, sources=["Test.java"], timeout=999 ) self.assertEqual(999, timeout.timeout)
def _generate_strict_dependencies(self): init_subsystem(Target.Arguments) self.lib_aa = self.make_target( "com/foo:AA", target_type=SourcesTarget, sources=["com/foo/AA.scala"], ) self.lib_a = self.make_target( "com/foo:A", target_type=SourcesTarget, sources=["com/foo/A.scala"], ) self.lib_b = self.make_target( "com/foo:B", target_type=SourcesTarget, sources=["com/foo/B.scala"], dependencies=[self.lib_a, self.lib_aa], exports=[":A"], ) self.lib_c = self.make_target( "com/foo:C", target_type=SourcesTarget, sources=["com/foo/C.scala"], dependencies=[self.lib_b], exports=[":B"], ) self.lib_c_alias = self.make_target( "com/foo:C_alias", dependencies=[self.lib_c], ) self.lib_d = self.make_target( "com/foo:D", target_type=SourcesTarget, sources=["com/foo/D.scala"], dependencies=[self.lib_c_alias], exports=[":C_alias"], ) self.lib_f = self.make_target( "com/foo:F", target_type=SourcesTarget, sources=["com/foo/E.scala"], scope=Scopes.RUNTIME, ) self.lib_e = self.make_target( "com/foo:E", target_type=SourcesTarget, sources=["com/foo/E.scala"], dependencies=[self.lib_d, self.lib_f], )