Skip to content

Commit cbb4cbe

Browse files
authored
Add tests for imports (#1016)
* Add tests for imports * update
1 parent 206d912 commit cbb4cbe

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

third_party/dependency_analyzer/src/test/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinderTest.scala

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class AstUsedJarFinderTest extends FunSuite {
5050
)
5151
}
5252

53+
private def verifyAndConvertDepToClass(dep: String): String = {
54+
val classPath = tmpDir.resolve(s"$dep.class")
55+
// Make sure the dep refers to a real file
56+
assert(classPath.toFile.isFile)
57+
classPath.toString
58+
}
59+
5360
def checkStrictDepsErrorsReported(
5461
code: String,
5562
expectedStrictDeps: List[String]
@@ -61,7 +68,7 @@ class AstUsedJarFinderTest extends FunSuite {
6168
dependencyAnalyzerParamsOpt =
6269
Some(
6370
DependencyAnalyzerTestParams(
64-
indirectJars = expectedStrictDeps.map(name => tmpDir.resolve(s"$name.class").toString),
71+
indirectJars = expectedStrictDeps.map(verifyAndConvertDepToClass),
6572
indirectTargets = expectedStrictDeps,
6673
strictDeps = true,
6774
dependencyTrackingMethod = DependencyTrackingMethod.Ast
@@ -94,7 +101,7 @@ class AstUsedJarFinderTest extends FunSuite {
94101
dependencyAnalyzerParamsOpt =
95102
Some(
96103
DependencyAnalyzerTestParams(
97-
directJars = expectedUnusedDeps.map(name => tmpDir.resolve(s"$name.class").toString),
104+
directJars = expectedUnusedDeps.map(verifyAndConvertDepToClass),
98105
directTargets = expectedUnusedDeps,
99106
unusedDeps = true,
100107
dependencyTrackingMethod = DependencyTrackingMethod.Ast
@@ -421,6 +428,70 @@ class AstUsedJarFinderTest extends FunSuite {
421428
)
422429
}
423430

431+
test("imports are complicated") {
432+
// This test documents the behavior of imports as is currently.
433+
// Ideally all imports would be direct dependencies. However there
434+
// are complications. The main one being that the scala AST treats
435+
// imports as (expr, selectors) where in e.g. `import a.b.{c, d}`
436+
// expr=`a.b` and selectors=[c, d]. (Note selectors are always formed
437+
// from the last part of the import).
438+
// And only the expr part has type information attached. In order
439+
// to gather type information from the selector, we would need to
440+
// do some resolution of types, which is possible but probably complex.
441+
// Note also that fixing this is probably less of a priority, as
442+
// people who want to check unused deps generally also want to check
443+
// unused imports, so they wouldn't run into these problems in the
444+
// first place.
445+
446+
def testImport(importString: String, isDirect: Boolean): Unit = {
447+
withSandbox { sandbox =>
448+
sandbox.compileWithoutAnalyzer(
449+
s"""
450+
|package foo.bar
451+
|
452+
|object A { val i: Int = 0 }
453+
|""".stripMargin
454+
)
455+
456+
val bCode =
457+
s"""
458+
|import $importString
459+
|
460+
|class B
461+
|""".stripMargin
462+
val dep = "foo/bar/A"
463+
464+
if (isDirect) {
465+
sandbox.checkStrictDepsErrorsReported(
466+
code = bCode,
467+
expectedStrictDeps = List(dep)
468+
)
469+
} else {
470+
sandbox.checkUnusedDepsErrorReported(
471+
code = bCode,
472+
expectedUnusedDeps = List(dep)
473+
)
474+
}
475+
}
476+
}
477+
478+
// In this case, expr=foo.bar.A and selectors=[i], so looking at expr does
479+
// give us a type.
480+
testImport("foo.bar.A.i", isDirect = true)
481+
482+
// In this case expr=foo.bar and selectors=[A], so expr does not have
483+
// a type which corresponds with A.
484+
testImport("foo.bar.A", isDirect = false)
485+
486+
// In this case expr=foo and selectors=[bar], so expr does not have
487+
// a type which corresponds with A.
488+
testImport("foo.bar", isDirect = false)
489+
490+
// In this case expr=foo.bar and selectors=[_], so expr does not have
491+
// a type which corresponds with A.
492+
testImport("foo.bar._", isDirect = false)
493+
}
494+
424495
test("java interface method argument is direct") {
425496
withSandbox { sandbox =>
426497
sandbox.compileJava(

0 commit comments

Comments
 (0)