def test_compile_jdk_versions(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'lib', ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "ExampleLib.java": JAVA_LIB_SOURCE, }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")), resolve=make_resolve(rule_runner), ) rule_runner.set_options(["--jvm-jdk=zulu:8.0.312"], env_inherit=PYTHON_BOOTSTRAP_ENV) classpath = rule_runner.request(RenderedClasspath, [request]) assert classpath.content == { ".ExampleLib.java.lib.javac.jar": {"org/pantsbuild/example/lib/ExampleLib.class"} } rule_runner.set_options(["--jvm-jdk=bogusjdk:999"], env_inherit=PYTHON_BOOTSTRAP_ENV) expected_exception_msg = r".*?JVM bogusjdk:999 not found in index.*?" with pytest.raises(ExecutionError, match=expected_exception_msg): rule_runner.request(ClasspathEntry, [request])
def test_compile_jdk_specified_in_build_file(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'lib', jdk = 'adopt:1.12', ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "ExampleLib.java": JAVA_LIB_JDK12_SOURCE, }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")), resolve=make_resolve(rule_runner), ) classpath = rule_runner.request(RenderedClasspath, [request]) assert classpath.content == { ".ExampleLib.java.lib.javac.jar": {"org/pantsbuild/example/lib/ExampleLib.class"} }
def test_compile_of_package_info(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'main', ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "package-info.java": dedent(""" package org.pantsbuild.example; /** * This is a package-info.java file which can have package-level annotations and * documentation comments. It does not generate any output. */ """), }) classpath = rule_runner.request( RenderedClasspath, [ CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) assert classpath.content == {}
def test_compile_no_deps(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": "java_sources(name='lib')", "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "ExampleLib.java": JAVA_LIB_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) classpath = rule_runner.request( RenderedClasspath, [ CompileJavaSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], ) assert classpath.content == { ".ExampleLib.java.lib.javac.jar": {"org/pantsbuild/example/lib/ExampleLib.class"} } # Additionally validate that `check` works. check_results = rule_runner.request( CheckResults, [ JavacCheckRequest([ JavacCheckRequest.field_set_type.create( coarsened_target.representative) ]) ], ).results assert set(check_results) == {CheckResult(0, "", "")}
def test_compile_with_missing_maven_dep_fails(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'main', ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "Example.java": dedent(""" package org.pantsbuild.example; import org.joda.time.DateTime; public class Example { public static void main(String[] args) { DateTime dt = new DateTime(); System.out.println(dt.getYear()); } } """), }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "package org.joda.time does not exist" in fallible_result.stderr
def test_compile_of_package_info(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'main', ) """), "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), "package-info.java": dedent(""" package org.pantsbuild.example; /** * This is a package-info.java file which can have package-level annotations and * documentation comments. It does not generate any output. */ """), }) compiled_classfiles = rule_runner.request( ClasspathEntry, [ CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == {}
def test_compile_with_undeclared_jvm_artifact_target_fails( rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', ) """), "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), "Example.scala": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime object Main { def main(args: Array[String]): Unit = { val dt = new DateTime() println(dt.getYear) } } """), }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "error: object joda is not a member of package org" in fallible_result.stderr
def test_compile_with_missing_dep_fails(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', ) """), "Example.scala": SCALA_LIB_MAIN_SOURCE, "3rdparty/jvm/BUILD": DEFAULT_SCALA_LIBRARY_TARGET, "3rdparty/jvm/default.lock": DEFAULT_LOCKFILE, }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert ( "error: object lib is not a member of package org.pantsbuild.example" in fallible_result.stderr)
def test_compile_jdk_12_file_fails_on_jdk_11( rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'lib', jdk = 'adopt:1.11', ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "ExampleLib.scala": SCALA_LIB_JDK12_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) with pytest.raises(ExecutionError): rule_runner.request( RenderedClasspath, [ CompileScalaSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], )
def test_compile_with_undeclared_jvm_artifact_dependency_fails( rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ kotlin_sources( name = 'main', ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "Example.kt": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime fun main(args: Array<String>): Unit = { val dt = DateTime() println(dt.getYear()) } """), }) request = CompileKotlinSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "unresolved reference: joda" in fallible_result.stderr
def test_compile_jdk_12_file_fails_on_jdk_11(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'lib', jdk = 'adopt:1.11', ) """), "3rdparty/jvm/BUILD": DEFAULT_SCALA_LIBRARY_TARGET, "3rdparty/jvm/default.lock": DEFAULT_LOCKFILE, "ExampleLib.scala": SCALA_LIB_JDK12_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) with pytest.raises(ExecutionError): rule_runner.request( RenderedClasspath, [ CompileScalaSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], )
def test_compile_with_maven_deps(rule_runner: RuleRunner) -> None: resolved_joda_lockfile = CoursierResolvedLockfile( entries=(CoursierLockfileEntry( coord=Coordinate( group="joda-time", artifact="joda-time", version="2.10.10"), file_name="joda-time-2.10.10.jar", direct_dependencies=Coordinates([]), dependencies=Coordinates([]), file_digest=FileDigest( fingerprint= "dd8e7c92185a678d1b7b933f31209b6203c8ffa91e9880475a1be0346b9617e3", serialized_bytes_length=644419, ), ), )) rule_runner.write_files({ "BUILD": dedent("""\ jvm_artifact( name = "joda-time_joda-time", group = "joda-time", artifact = "joda-time", version = "2.10.10", ) scala_sources( name = 'main', dependencies = [":joda-time_joda-time"], ) """), "coursier_resolve.lockfile": resolved_joda_lockfile.to_json().decode("utf-8"), "Example.scala": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime object Main { def main(args: Array[String]): Unit = { val dt = new DateTime() println(dt.getYear) } } """), }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) compiled_classfiles = rule_runner.request(ClasspathEntry, [request]) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == { ".Example.scala.main.jar": { "META-INF/MANIFEST.MF", "org/pantsbuild/example/Main$.class", "org/pantsbuild/example/Main.class", } }
def test_compile_with_maven_deps(rule_runner: RuleRunner) -> None: resolved_joda_lockfile = CoursierResolvedLockfile( entries=(CoursierLockfileEntry( coord=Coordinate( group="joda-time", artifact="joda-time", version="2.10.10"), file_name="joda-time-2.10.10.jar", direct_dependencies=Coordinates([]), dependencies=Coordinates([]), file_digest=FileDigest( fingerprint= "dd8e7c92185a678d1b7b933f31209b6203c8ffa91e9880475a1be0346b9617e3", serialized_bytes_length=644419, ), ), )) rule_runner.write_files({ "BUILD": dedent("""\ jvm_artifact( name = "joda-time_joda-time", group = "joda-time", artifact = "joda-time", version = "2.10.10", ) java_sources( name = 'main', dependencies = [ ':joda-time_joda-time', ] ) """), "coursier_resolve.lockfile": resolved_joda_lockfile.to_json().decode("utf-8"), "Example.java": dedent(""" package org.pantsbuild.example; import org.joda.time.DateTime; public class Example { public static void main(String[] args) { DateTime dt = new DateTime(); System.out.println(dt.getYear()); } } """), }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) compiled_classfiles = rule_runner.request(ClasspathEntry, [request]) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == { ".Example.java.main.jar": {"org/pantsbuild/example/Example.class"} }
def test_compile_with_scalac_plugin(rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "lib/BUILD": dedent("""\ scalac_plugin( name = "acyclic", artifact = "3rdparty/jvm:com.lihaoyi_acyclic_2.13", ) scala_sources( dependencies=["3rdparty/jvm:com.lihaoyi_acyclic_2.13"], ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "lib/A.scala": dedent(""" package lib import acyclic.file class A { val b: B = null } """), "lib/B.scala": dedent(""" package lib class B { val a: A = null } """), }) rule_runner.set_options( args=[ "--scala-version-for-resolve={'jvm-default': '2.13.8'}", "--scalac-plugins-for-resolve={'jvm-default': 'acyclic'}", ], env_inherit=PYTHON_BOOTSTRAP_ENV, ) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="lib", relative_file_path="A.scala")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "error: Unwanted cyclic dependency" in fallible_result.stderr
def test_compile_with_kotlinc_plugin(rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "lib/BUILD": dedent("""\ kotlinc_plugin( name = "allopen", plugin_id = "org.jetbrains.kotlin.allopen", plugin_args = ["annotation=lib.MarkOpen"], artifact = "3rdparty/jvm:org.jetbrains.kotlin_kotlin-allopen", ) kotlin_sources() """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "lib/Grok.kt": dedent(""" package lib annotation class MarkOpen @MarkOpen class A { val value: Boolean = true } class B: A() { override val value = false } """), }) rule_runner.set_options( args=[ "--kotlin-version-for-resolve={'jvm-default': '1.6.20'}", "--kotlinc-plugins-for-resolve={'jvm-default': 'org.jetbrains.kotlin.allopen'}", ], env_inherit=PYTHON_BOOTSTRAP_ENV, ) request = CompileKotlinSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="lib", relative_file_path="Grok.kt")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) print( f"stdout:\n{fallible_result.stdout}\nstderr:\n{fallible_result.stderr}" ) assert fallible_result.result == CompileResult.SUCCEEDED
def test_compile_no_deps(rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ kotlin_sources( name = 'lib', ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "ExampleLib.kt": KOTLIN_LIB_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) classpath = rule_runner.request( RenderedClasspath, [ CompileKotlinSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], ) assert classpath.content == { ".ExampleLib.kt.lib.kotlin.jar": { "META-INF/MANIFEST.MF", "META-INF/main.kotlin_module", "org/pantsbuild/example/lib/C.class", } } # Additionally validate that `check` works. check_results = rule_runner.request( CheckResults, [ KotlincCheckRequest([ KotlincCheckRequest.field_set_type.create( coarsened_target.representative) ]) ], ) assert len(check_results.results) == 1 check_result = check_results.results[0] assert check_result.exit_code == 0
def test_compile_no_deps(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'lib', ) """), "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), "ExampleLib.scala": SCALA_LIB_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) print(coarsened_target) compiled_classfiles = rule_runner.request( ClasspathEntry, [ CompileScalaSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], ) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == { ".ExampleLib.scala.lib.jar": {"META-INF/MANIFEST.MF", "org/pantsbuild/example/lib/C.class"} } # Additionally validate that `check` works. check_results = rule_runner.request( CheckResults, [ ScalacCheckRequest([ ScalacCheckRequest.field_set_type.create( coarsened_target.representative) ]) ], ) assert len(check_results.results) == 1 check_result = check_results.results[0] assert check_result.exit_code == 0
def test_compile_mixed_cycle(rule_runner: RuleRunner) -> None: # Add an extra import to the Java file which will force a cycle between them. rule_runner.write_files( { "BUILD": "scala_sources(name='main')", "3rdparty/jvm/BUILD": DEFAULT_SCALA_LIBRARY_TARGET, "3rdparty/jvm/default.lock": DEFAULT_LOCKFILE, "Example.scala": scala_main_source(), "lib/BUILD": "java_sources()", "lib/C.java": java_lib_source(["org.pantsbuild.example.Main"]), } ) main_address = Address(spec_path="", target_name="main") lib_address = Address(spec_path="lib") assert len(expect_single_expanded_coarsened_target(rule_runner, main_address).members) == 2 rule_runner.request(Classpath, [Addresses([main_address, lib_address])])
def test_compile_jdk_versions(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'lib', ) """), "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), "ExampleLib.java": JAVA_LIB_SOURCE, }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")), resolve=make_resolve(rule_runner), ) rule_runner.set_options( [ "--javac-jdk=zulu:8.0.312", NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION ], env_inherit=PYTHON_BOOTSTRAP_ENV, ) compiled_classfiles = rule_runner.request(ClasspathEntry, [request]) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == { ".ExampleLib.java.lib.jar": {"org/pantsbuild/example/lib/ExampleLib.class"} } rule_runner.set_options( [ "--javac-jdk=bogusjdk:999", NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION ], env_inherit=PYTHON_BOOTSTRAP_ENV, ) expected_exception_msg = r".*?JVM bogusjdk:999 not found in index.*?" with pytest.raises(ExecutionError, match=expected_exception_msg): rule_runner.request(ClasspathEntry, [request])
def test_compile_with_maven_deps(rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', dependencies = ["3rdparty/jvm:joda-time_joda-time"], ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "Example.scala": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime object Main { def main(args: Array[String]): Unit = { val dt = new DateTime() println(dt.getYear) } } """), }) classpath = rule_runner.request( RenderedClasspath, [ CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) assert classpath.content == { ".Example.scala.main.scalac.jar": { "META-INF/MANIFEST.MF", "org/pantsbuild/example/Main$.class", "org/pantsbuild/example/Main.class", } }
def test_compile_no_deps(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'lib', ) """), "3rdparty/jvm/BUILD": DEFAULT_SCALA_LIBRARY_TARGET, "3rdparty/jvm/default.lock": DEFAULT_LOCKFILE, "ExampleLib.scala": SCALA_LIB_SOURCE, }) coarsened_target = expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")) classpath = rule_runner.request( RenderedClasspath, [ CompileScalaSourceRequest(component=coarsened_target, resolve=make_resolve(rule_runner)) ], ) assert classpath.content == { ".ExampleLib.scala.lib.scalac.jar": { "META-INF/MANIFEST.MF", "org/pantsbuild/example/lib/C.class", } } # Additionally validate that `check` works. check_results = rule_runner.request( CheckResults, [ ScalacCheckRequest([ ScalacCheckRequest.field_set_type.create( coarsened_target.representative) ]) ], ) assert len(check_results.results) == 1 check_result = check_results.results[0] assert check_result.exit_code == 0
def test_compile_with_deps(rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', dependencies = [ 'lib:lib', ] ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "Example.scala": SCALA_LIB_MAIN_SOURCE, "lib/BUILD": dedent("""\ scala_sources( name = 'lib', ) """), "lib/ExampleLib.scala": SCALA_LIB_SOURCE, }) classpath = rule_runner.request( RenderedClasspath, [ CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) assert classpath.content == { ".Example.scala.main.scalac.jar": { "META-INF/MANIFEST.MF", "org/pantsbuild/example/Main$.class", "org/pantsbuild/example/Main.class", } }
def test_compile_with_deps(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', dependencies = [ 'lib:lib', ] ) """), "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), "Example.scala": SCALA_LIB_MAIN_SOURCE, "lib/BUILD": dedent("""\ scala_sources( name = 'lib', ) """), "lib/ExampleLib.scala": SCALA_LIB_SOURCE, }) compiled_classfiles = rule_runner.request( ClasspathEntry, [ CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) classpath = rule_runner.request(RenderedClasspath, [compiled_classfiles.digest]) assert classpath.content == { ".Example.scala.main.jar": { "META-INF/MANIFEST.MF", "org/pantsbuild/example/Main$.class", "org/pantsbuild/example/Main.class", } }
def test_compile_with_undeclared_jvm_artifact_dependency_fails( rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ jvm_artifact( name = "joda-time_joda-time", group = "joda-time", artifact = "joda-time", version = "2.10.10", ) scala_sources( name = 'main', dependencies = [], # `joda-time` needs to be here for compile to succeed ) """), "3rdparty/jvm/BUILD": DEFAULT_SCALA_LIBRARY_TARGET, "3rdparty/jvm/default.lock": DEFAULT_LOCKFILE, "Example.scala": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime object Main { def main(args: Array[String]): Unit = { val dt = new DateTime() println(dt.getYear) } } """), }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "error: object joda is not a member of package org" in fallible_result.stderr
def test_compile_with_deps(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'main', dependencies = [ 'lib:lib', ] ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "Example.java": JAVA_LIB_MAIN_SOURCE, "lib/BUILD": dedent("""\ java_sources( name = 'lib', ) """), "lib/ExampleLib.java": JAVA_LIB_SOURCE, }) classpath = rule_runner.request( RenderedClasspath, [ CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) ], ) assert classpath.content == { ".Example.java.main.javac.jar": {"org/pantsbuild/example/Example.class"} }
def test_compile_with_missing_dep_fails(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'main', ) """), "Example.java": JAVA_LIB_MAIN_SOURCE, "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "package org.pantsbuild.example.lib does not exist" in fallible_result.stderr
def test_compile_jdk_12_file_fails_with_jdk_11( rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ java_sources( name = 'lib', jdk = 'adopt:1.11', ) """), "3rdparty/jvm/default.lock": TestCoursierWrapper.new(entries=()).serialize(), "ExampleLib.java": JAVA_LIB_JDK12_SOURCE, }) request = CompileJavaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="lib")), resolve=make_resolve(rule_runner), ) with pytest.raises(ExecutionError): rule_runner.request(RenderedClasspath, [request])
def test_compile_with_missing_dep_fails(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', ) """), "Example.scala": SCALA_LIB_MAIN_SOURCE, "coursier_resolve.lockfile": CoursierResolvedLockfile(entries=()).to_json().decode("utf-8"), }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert ( "error: object lib is not a member of package org.pantsbuild.example" in fallible_result.stderr)
def test_compile_with_missing_dep_fails( rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ kotlin_sources( name = 'main', ) """), "Example.kt": KOTLIN_LIB_MAIN_SOURCE, "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, }) request = CompileKotlinSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "Example.kt:4:31: error: unresolved reference: lib" in fallible_result.stderr
def test_compile_with_undeclared_jvm_artifact_target_fails( rule_runner: RuleRunner, jvm_lockfile: JVMLockfileFixture) -> None: rule_runner.write_files({ "BUILD": dedent("""\ scala_sources( name = 'main', ) """), "3rdparty/jvm/BUILD": jvm_lockfile.requirements_as_jvm_artifact_targets(), "3rdparty/jvm/default.lock": jvm_lockfile.serialized_lockfile, "Example.scala": dedent(""" package org.pantsbuild.example import org.joda.time.DateTime object Main { def main(args: Array[String]): Unit = { val dt = new DateTime() println(dt.getYear) } } """), }) request = CompileScalaSourceRequest( component=expect_single_expanded_coarsened_target( rule_runner, Address(spec_path="", target_name="main")), resolve=make_resolve(rule_runner), ) fallible_result = rule_runner.request(FallibleClasspathEntry, [request]) assert fallible_result.result == CompileResult.FAILED and fallible_result.stderr assert "error: object joda is not a member of package org" in fallible_result.stderr