-
-
Notifications
You must be signed in to change notification settings - Fork 287
Add test for scalac dependencies #998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...dency_analyzer/src/test/io/bazel/rulesscala/dependencyanalyzer/ScalacDependencyTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| 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. | ||
| * | ||
| * To clarify: these tests do not reflect the end result of strict/unused | ||
| * deps as we are considering alternatives of how to mitigate scalac's | ||
| * limitations. | ||
| */ | ||
| 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") | ||
| ) | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.