Repository navigation
ADFA-7115: Give Kotlin test sources their own modules #2253
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
Changes from all commits
2d7f792
c00ed41
d414add
dcc62d5
65cb745
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package com.itsaky.androidide.lsp.kotlin.compiler | |
|
|
||
| import com.itsaky.androidide.projects.api.AndroidModule | ||
| import com.itsaky.androidide.projects.api.ModuleProject | ||
| import com.itsaky.androidide.projects.api.TestScope | ||
| import com.itsaky.androidide.projects.api.Workspace | ||
| import com.itsaky.androidide.projects.models.bootClassPaths | ||
| import java.nio.file.Path | ||
|
|
@@ -14,8 +15,9 @@ import java.nio.file.Path | |
| * classpaths are sets because the project model does not keep their order. | ||
| * | ||
| * @property bootClassPaths Boot classpath jars of every Android module, existing ones only, in order. | ||
| * @property libraries Every module's compile classpath, existing jars only. | ||
| * @property modules One entry per source module, in workspace order. | ||
| * @property libraries Every module's compile and test compile classpaths, existing jars only. | ||
| * @property modules One entry per source module, in workspace order, each Gradle module's main | ||
| * sources followed by its test sources. | ||
| */ | ||
| internal data class KotlinModuleLayout( | ||
| val bootClassPaths: List<Path>, | ||
|
|
@@ -26,12 +28,15 @@ internal data class KotlinModuleLayout( | |
| /** | ||
| * One source module of a [KotlinModuleLayout]. | ||
| * | ||
| * @property path The Gradle project path, which identifies the module. | ||
| * @property path Identifies the module: the Gradle project path for main sources, with a | ||
| * [TestScope] suffix for test sources (see [testModulePath]). | ||
| * @property sourceRoots The module's source directories. | ||
| * @property classpath The compile classpath without generated sources. | ||
| * @property moduleDependencies Gradle paths of the modules this one compiles against. | ||
| * @property moduleDependencies Paths of the modules this one compiles against. | ||
| * @property apiVersion The Kotlin API version, or null when the module has no Java or Android project. | ||
| * @property jvmTarget The Kotlin JVM target, or null when the module has no Java or Android project. | ||
| * @property friends Paths of the modules whose `internal` declarations this one may use: a test | ||
| * module's main module. | ||
| */ | ||
| internal data class SourceModuleLayout( | ||
| val path: String, | ||
|
|
@@ -41,8 +46,15 @@ internal data class SourceModuleLayout( | |
| val moduleDependencies: List<String>, | ||
| val apiVersion: String?, | ||
| val jvmTarget: String?, | ||
| val friends: List<String> = emptyList(), | ||
| ) | ||
|
|
||
| /** The [SourceModuleLayout.path] of the [scope] test module of the Gradle module at [path]. */ | ||
| internal fun testModulePath( | ||
| path: String, | ||
| scope: TestScope, | ||
| ): String = "$path[${scope.name.lowercase()}Test]" | ||
|
|
||
| /** | ||
| * Returns the [KotlinModuleLayout] of this workspace. Reads the file system, so not on the main thread. | ||
| * | ||
|
|
@@ -65,14 +77,19 @@ internal fun Workspace.kotlinModuleLayout(): KotlinModuleLayout { | |
|
|
||
| val libraries = | ||
| moduleProjects | ||
| .flatMap { it.getCompileClasspaths() } | ||
| .filter { it.exists() } | ||
| .flatMap { module -> | ||
| module.getCompileClasspaths() + TestScope.entries.flatMap { module.getTestCompileClasspaths(it) } | ||
| }.filter { it.exists() } | ||
| .mapTo(LinkedHashSet()) { it.toPath() } | ||
|
|
||
| return KotlinModuleLayout( | ||
| bootClassPaths = bootClassPaths, | ||
| libraries = libraries, | ||
| modules = moduleProjects.map { it.sourceModuleLayout() }, | ||
| modules = | ||
| moduleProjects.flatMap { module -> | ||
| val main = module.sourceModuleLayout() | ||
| listOf(main) + TestScope.entries.mapNotNull { module.testModuleLayout(main, it) } | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -94,3 +111,31 @@ private fun ModuleProject.sourceModuleLayout(): SourceModuleLayout { | |
| jvmTarget = kotlinSettings?.jvmTarget, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * The [scope] test module layered over [main], or null when this module has no such tests. It | ||
| * compiles against main, everything main does and its own test-only modules, and is main's friend so it may use `internal` | ||
| * declarations. Neither test module depends on the other. | ||
| */ | ||
| private fun ModuleProject.testModuleLayout( | ||
| main: SourceModuleLayout, | ||
| scope: TestScope, | ||
| ): SourceModuleLayout? { | ||
| val sourceRoots = getTestSourceDirectories(scope).mapTo(LinkedHashSet()) { it.toPath() } | ||
| if (sourceRoots.isEmpty()) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NITPICK:
Filter with |
||
| return null | ||
| } | ||
|
|
||
| return main.copy( | ||
| path = testModulePath(path, scope), | ||
| name = "$name (${scope.name.lowercase()} test)", | ||
| sourceRoots = sourceRoots, | ||
| classpath = | ||
| getTestCompileClasspaths(scope, excludeSourceGeneratedClassPath = true).mapTo(LinkedHashSet()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep compiled outputs of test-only project dependencies on the test classpath.
The exclusion is only correct for outputs that a source module already covers. Test-only project outputs need a different treatment, for example adding them to the test classpath while keeping main's source-generated outputs excluded. Confirm the design before changing the call. This is a design change, not a one-line fix.
|
||
| it.toPath() | ||
| }, | ||
| // Test-only project dependencies must be source modules: the test classpath leaves out their jars. | ||
| moduleDependencies = (listOf(path) + main.moduleDependencies + getTestCompileModuleProjects(scope).map { it.path }).distinct(), | ||
| friends = listOf(path), | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,14 +318,26 @@ private fun KaSession.scopeOf( | |
|
|
||
| val module = moduleOf(declaration) ?: return fileOnly ?: UsageSearchScope.Modules(sourceModules()) | ||
|
|
||
| // internal is module-wide, and there is no associated test module to widen to: this project model | ||
| // builds one module per Gradle module from the main source set only. A file-confined target with no | ||
| // derivable path lands here too - it cannot be narrowed to one file, but it is still unreferenceable | ||
| // outside its own module, so it must not fall through to the dependents below. | ||
| if (fileConfined || symbol.visibility == KaSymbolVisibility.INTERNAL) { | ||
| // A file-confined target with no derivable path cannot be narrowed to one file, but it is still | ||
| // unreferenceable outside its own module, so it must not fall through to the dependents below. | ||
| if (fileConfined) { | ||
| return UsageSearchScope.Modules(listOf(module)) | ||
| } | ||
|
|
||
| // internal reaches its own module and that module's friends: the test modules layered over a main | ||
| // module. An internal override in a test module answers calls written against main's declaration | ||
| // (R3), so every match-set member's module is a root, as below. | ||
| if (symbol.visibility == KaSymbolVisibility.INTERNAL) { | ||
| val roots = LinkedHashSet<KtModule>() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NITPICK: The match-set-to-roots loop (lines 331-336) is a verbatim copy of the one in the public branch below. A later fix to one copy can miss the other, and the internal and public scopes drift apart. Extract a |
||
| roots.add(module) | ||
| for (member in matchSet) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MINOR: No test covers the INTERNAL branch's match-set union, the case where an Both internal tests target a top-level Add a |
||
| val memberDeclaration = member.sourcePsiSafe<PsiElement>() ?: continue | ||
| moduleOf(memberDeclaration)?.let(roots::add) | ||
| } | ||
| val friends = sourceModules().filter { candidate -> candidate.directFriendDependencies.any { it in roots } } | ||
| return UsageSearchScope.Modules((roots + friends).toList()) | ||
| } | ||
|
|
||
| // Anything more visible can be referenced from any module that depends on this one. Dependents, | ||
| // not all modules: a module that cannot see the declaration cannot reference it. | ||
| // | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MINOR: The doc says an
internaldeclaration in a test module "stays in that module", but the code widens it.For a unitTest
overrideof main'sinternal open fun x(), the roots become {unitTest, main} and main's friends add androidTest, so androidTest call sites are reported. Acceptance criterion 7 (line 190) is wrong for the same case, and QA relying on either line expects results the code does not give.State the override case: an internal override's search spans its main module and every friend of main.