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

Justinass/dep analyzer #1370

Merged
merged 10 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
4 changes: 2 additions & 2 deletions third_party/dependency_analyzer/src/test/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
load(":analyzer_test.bzl", "analyzer_tests")
load(":analyzer_test.bzl", "tests")

analyzer_tests()
tests()
104 changes: 6 additions & 98 deletions third_party/dependency_analyzer/src/test/analyzer_test.bzl
Original file line number Diff line number Diff line change
@@ -1,101 +1,9 @@
load("//scala:scala.bzl", "scala_test")
load(":analyzer_test_scala_2.bzl", "analyzer_tests_scala_2")
load(":analyzer_test_scala_3.bzl", "analyzer_tests_scala_3")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_MAJOR_VERSION")

def analyzer_tests():
def tests():
if SCALA_MAJOR_VERSION.startswith("2"):
common_jvm_flags = [
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)",
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)",
"-Dscala.reflect.location=$(rootpath @io_bazel_rules_scala_scala_reflect)",
]

scala_test(
name = "ast_used_jar_finder_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinderTest.scala",
],
jvm_flags = common_jvm_flags,
deps = [
"//src/java/io/bazel/rulesscala/io_utils",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/dependency_analyzer/src/main:scala_version",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

scala_test(
name = "scala_version_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/ScalaVersionTest.scala",
],
deps = [
"//third_party/dependency_analyzer/src/main:scala_version",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

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 = [
"//src/java/io/bazel/rulesscala/io_utils",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

scala_test(
name = "strict_deps_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/StrictDepsTest.scala",
],
jvm_flags = common_jvm_flags + [
"-Dguava.jar.location=$(rootpath @com_google_guava_guava_21_0_with_file//jar)",
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)",
],
unused_dependency_checker_mode = "off",
deps = [
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@com_google_guava_guava_21_0_with_file//jar",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file",
],
)

scala_test(
name = "unused_dependency_checker_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/UnusedDependencyCheckerTest.scala",
],
jvm_flags = common_jvm_flags + [
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)",
],
unused_dependency_checker_mode = "off",
deps = [
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file",
],
)
analyzer_tests_scala_2()
else:
analyzer_tests_scala_3()
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
load("//scala:scala.bzl", "scala_test")

def analyzer_tests_scala_2():
common_jvm_flags = [
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)",
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)",
"-Dscala.reflect.location=$(rootpath @io_bazel_rules_scala_scala_reflect)",
]

scala_test(
name = "ast_used_jar_finder_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinderTest.scala",
],
jvm_flags = common_jvm_flags,
deps = [
"//src/java/io/bazel/rulesscala/io_utils",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/dependency_analyzer/src/main:scala_version",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

scala_test(
name = "scala_version_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/ScalaVersionTest.scala",
],
deps = [
"//third_party/dependency_analyzer/src/main:scala_version",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

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 = [
"//src/java/io/bazel/rulesscala/io_utils",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
)

scala_test(
name = "strict_deps_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/StrictDepsTest.scala",
],
jvm_flags = common_jvm_flags + [
"-Dguava.jar.location=$(rootpath @com_google_guava_guava_21_0_with_file//jar)",
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)",
],
unused_dependency_checker_mode = "off",
deps = [
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@com_google_guava_guava_21_0_with_file//jar",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file",
],
)

scala_test(
name = "unused_dependency_checker_test",
size = "small",
srcs = [
"io/bazel/rulesscala/dependencyanalyzer/UnusedDependencyCheckerTest.scala",
],
jvm_flags = common_jvm_flags + [
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)",
],
unused_dependency_checker_mode = "off",
deps = [
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")

def analyzer_tests_scala_3():
common_jvm_flags = [
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)",
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)",
"-Dscala.library2.location=$(rootpath @io_bazel_rules_scala_scala_library_2)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment why we need both standard libraries?

Copy link
Contributor Author

@justinass-wix justinass-wix Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know why Scala 2 library is required, I tried various things until I got the code to compile... One hint was this line .

Without it compilation fails:
class dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to class dotty.tools.dotc.core.Symbols$ClassSymbol (dotty.tools.dotc.core.Symbols$NoSymbol$ and dotty.tools.dotc.core.Symbols$ClassSymbol are in unnamed module of loader 'app')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment.

]

scala_test(
name = "test_that_tests_run",
size = "small",
jvm_flags = common_jvm_flags,
srcs = [
"io/bazel/rulesscala/dependencyanalyzer3/CompileTest.scala",
],
deps = [
"//scala/private/toolchain_deps:scala_compile_classpath",
"//third_party/dependency_analyzer/src/main:dependency_analyzer",
"//third_party/utils/src/test:test_util",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_library_2",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.bazel.rulesscala.dependencyanalyzer

import org.scalatest.funsuite.AnyFunSuite
import io.bazel.rulesscala.utils.Scala3CompilerUtils

class CompileTest extends AnyFunSuite {

test("Util compiles valid code") {
val scalaCode = "class Foo { }"
val messages = Scala3CompilerUtils.runCompiler(scalaCode)

assert(messages.isEmpty, "No messages must be reported when valid code is compiled")
}

test("Util returns errors on invalid code") {
val scalaCode = "!@#"
val messages = Scala3CompilerUtils.runCompiler(scalaCode)

assert(
messages.exists(_.message.contains("Illegal start of toplevel definition")),
"Error must be reported when valid code is compiled"
)
}
}
7 changes: 5 additions & 2 deletions third_party/utils/src/test/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
load("//scala:scala.bzl", "scala_library")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_MAJOR_VERSION")

licenses(["notice"]) # 3-clause BSD

scala_library(
name = "test_util",
srcs = [
srcs = ([
"io/bazel/rulesscala/utils/JavaCompileUtil.scala",
"io/bazel/rulesscala/utils/TestUtil.scala",
],
] if SCALA_MAJOR_VERSION.startswith("2") else [
"io/bazel/rulesscala/utils/Scala3CompilerUtils.scala",
]),
visibility = ["//visibility:public"],
deps = [
"//scala/private/toolchain_deps:scala_compile_classpath",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.bazel.rulesscala.utils

import dotty.tools.dotc.Compiler
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.reporting.Diagnostic
import dotty.tools.dotc.util.SourceFile

import java.nio.file.Paths

object Scala3CompilerUtils {
def runCompiler(
code: String,
extraClasspath: List[String] = List.empty,
): List[Diagnostic] = {
val reporter = new TestReporter()

implicit val context: FreshContext = (new ContextBase).initialCtx.fresh.setReporter(reporter)

val fullClassPath =
justinass-wix marked this conversation as resolved.
Show resolved Hide resolved
builtinClasspaths.filterNot(_.isEmpty) ++
extraClasspath :+
context.settings.classpath.value

context.setSetting(context.settings.classpath, fullClassPath.mkString(":"))

val compiler = new Compiler()
val run = compiler.newRun
run.compileSources(List(SourceFile.virtual("code.scala", code, maybeIncomplete = false)))
justinass-wix marked this conversation as resolved.
Show resolved Hide resolved

reporter.storedInfos
}

private lazy val builtinClasspaths: Vector[String] =
Vector(
pathOf("scala.library.location"),
pathOf("scala.library2.location")
)

private def pathOf(jvmFlag: String) = {
val jar = System.getProperty(jvmFlag)
val libPath = Paths.get(baseDir, jar).toAbsolutePath
libPath.toString
}

private lazy val baseDir = System.getProperty("user.dir")
}

import dotty.tools.dotc.reporting.StoreReporter
justinass-wix marked this conversation as resolved.
Show resolved Hide resolved

class TestReporter extends StoreReporter(null) {
def storedInfos: List[Diagnostic] = if (infos != null) infos.toList else List()
}