Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for scalac dependencies #998

Merged
merged 3 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions third_party/dependency_analyzer/src/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ scala_test(
],
)

scala_test(
name = "scalac_dependency_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/ScalacDependencyTest.scala",
],
jvm_flags = common_jvm_flags,
unused_dependency_checker_mode = "off",
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_compiler",
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@scalac_rules_commons_io//jar",
],
)

scala_test(
name = "strict_deps_test",
size = "small",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package third_party.dependency_analyzer.src.test.io.bazel.rulesscala.dependencyanalyzer

import java.nio.file.Files
import java.nio.file.Path
import java.util.UUID
import org.apache.commons.io.FileUtils
import org.scalatest._
import third_party.utils.src.test.io.bazel.rulesscala.utils.JavaCompileUtil
import third_party.utils.src.test.io.bazel.rulesscala.utils.TestUtil

/**
* Test that the scalac compiler behaves how we expect it to around
* dependencies. That is, for given scenarios, we want to make sure
* that scalac requires the given set of dependencies; no more and
* no less.
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
*/
class ScalacDependencyTest extends FunSuite {
private def withSandbox(action: Sandbox => Unit): Unit = {
val tmpDir = Files.createTempDirectory("dependency_analyzer_test_temp")
val file = tmpDir.toFile
try {
action(new Sandbox(tmpDir))
} finally {
FileUtils.deleteDirectory(file)
}
}

private class Sandbox(tmpDir: Path) {
def compile(
code: String
): Unit = {
val errors =
TestUtil.runCompiler(
code = code,
extraClasspath = List(tmpDir.toString),
outputPathOpt = Some(tmpDir)
)
assert(errors.isEmpty)
}

def compileJava(
className: String,
code: String
): Unit = {
JavaCompileUtil.compile(
tmpDir = tmpDir.toString,
className = className,
code = code
)
}

def checkExactDepsNeeded(
code: String,
deps: List[String]
): Unit = {
def doesCompileSucceed(usedDeps: List[String]): Boolean = {
val subdir = tmpDir.resolve(UUID.randomUUID().toString)
Files.createDirectory(subdir)
usedDeps.foreach { dep =>
val name = s"$dep.class"
Files.copy(tmpDir.resolve(name), subdir.resolve(name))
}
val errors =
TestUtil.runCompiler(
code = code,
extraClasspath = List(subdir.toString)
)
errors.isEmpty
}

assert(doesCompileSucceed(deps), s"Failed to compile with all deps")

deps.foreach { toSkip =>
val remaining = deps.filter(_ != toSkip)
// sanity check we removed exactly one item
assert(remaining.size + 1 == deps.size)
assert(
!doesCompileSucceed(remaining),
s"Compile succeeded even though $toSkip was missing")
}
}
}

test("static annotation of superclass not needed") {
withSandbox { sandbox =>
sandbox.compile("class A extends scala.annotation.StaticAnnotation")
sandbox.compile("@A class B")
sandbox.checkExactDepsNeeded(
code = "class C extends B",
deps = List("B")
)
}
}

test("superclass of superclass needed") {
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
withSandbox { sandbox =>
sandbox.compile("class A")
sandbox.compile("class B extends A")
sandbox.checkExactDepsNeeded(
code = "class C extends B",
deps = List("A", "B")
)
}
}
}