Repository navigation
ADFA-7115: Add per-scope test source and classpath getters #2252
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
498596c
affddf9
f0eec08
1ef995d
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 |
|---|---|---|
|
|
@@ -140,6 +140,78 @@ open class AndroidModule( | |
| return dirs | ||
| } | ||
|
|
||
| override fun getTestSourceDirectories(scope: TestScope): Set<File> { | ||
| val provider = | ||
| when (scope) { | ||
| TestScope.UNIT -> mainSourceSet?.takeIf { it.hasUnitTestSourceProvider() }?.unitTestSourceProvider | ||
| TestScope.ANDROID -> mainSourceSet?.takeIf { it.hasAndroidTestSourceProvider() }?.androidTestSourceProvider | ||
| } ?: return emptySet() | ||
|
|
||
| // build/generated/** for the selected variant's tests: kapt, KSP and the like | ||
| val generated = | ||
| getSelectedVariant() | ||
| ?.let { variant -> | ||
| when (scope) { | ||
| TestScope.UNIT -> variant.unitTestGeneratedSourceFolderPathsList | ||
| TestScope.ANDROID -> variant.androidTestGeneratedSourceFolderPathsList | ||
| } | ||
| }.orEmpty() | ||
| return (provider.javaDirs + provider.kotlinDirs + generated.map(::File)).toSet() | ||
| } | ||
|
|
||
| private fun testArtifact(scope: TestScope): AndroidModels.ArtifactDependencies? = | ||
| when (scope) { | ||
| TestScope.UNIT -> variantDependencies.takeIf { it.hasUnitTestArtifact() }?.unitTestArtifact | ||
| TestScope.ANDROID -> variantDependencies.takeIf { it.hasAndroidTestArtifact() }?.androidTestArtifact | ||
| } | ||
|
|
||
| override fun getTestCompileModuleProjects(scope: TestScope): List<ModuleProject> { | ||
| val graph = testArtifact(scope)?.compileGraph ?: return emptyList() | ||
| val root = IProjectManager.getInstance().workspace ?: return emptyList() | ||
|
|
||
| // Seeded with this module so a test fixture that depends back on it (`:testutils` -> | ||
| // `:app`) is not re-expanded. Not on the recursion path: that back-edge is legal, not a cycle. | ||
| val visited = hashSetOf(path) | ||
| val recursionPath = ArrayDeque<String>() | ||
| val result = mutableListOf<ModuleProject>() | ||
| for (nodeId in graph.rootList) { | ||
| val key = graph.nodeList.getOrNull(nodeId)?.let { graph.keyList.getOrNull(it.keyId) } ?: continue | ||
|
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: A dangling node or key index in the test graph is skipped silently here, unlike in the sibling walkers.
Split the lookup as the siblings do and call |
||
| val lib = variantDependencies.librariesMap[key] ?: continue | ||
| if (lib.type != AndroidModels.LibraryType.Project) { | ||
| continue | ||
| } | ||
| val module = root.findByPath(lib.projectInfo!!.projectPath) as? ModuleProject ?: continue | ||
| if (module.path == path) { | ||
| continue | ||
| } | ||
| result.add(module) | ||
| result.addAll(module.getCompileModuleProjects(visited, recursionPath)) | ||
| } | ||
| return result.filter { it.path != path }.distinctBy { it.path } | ||
| } | ||
|
|
||
| override fun getTestCompileClasspaths( | ||
| scope: TestScope, | ||
| excludeSourceGeneratedClassPath: Boolean, | ||
| ): Set<File> { | ||
| val artifact = testArtifact(scope) ?: return emptySet() | ||
|
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: With a variant that has no androidTest artifact, AGP builds androidTest only for Fall back to |
||
| val project = IProjectManager.getInstance().workspace ?: return emptySet() | ||
|
|
||
| val result = getCompileClasspaths(excludeSourceGeneratedClassPath).toMutableSet() | ||
|
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: Project dependencies that main and the test graph share are expanded twice.
Share one visited set between the two walks. |
||
| val graph = artifact.compileGraph | ||
| collectLibraries( | ||
| root = project, | ||
| graph = graph, | ||
| nodeIds = graph.rootList, | ||
| result = result, | ||
| excludeSourceGeneratedClassPath = excludeSourceGeneratedClassPath, | ||
| visited = HashSet(), | ||
| // A test graph that reaches back to this module must not re-expand it. | ||
| moduleVisited = hashSetOf(path), | ||
| ) | ||
| return result | ||
| } | ||
|
|
||
| override fun getModuleClasspaths(): Set<File> = | ||
| mutableSetOf<File>().apply { | ||
| add(getGeneratedJar()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ class JavaModule( | |
| companion object { | ||
| const val SCOPE_COMPILE = "COMPILE" | ||
| const val SCOPE_RUNTIME = "RUNTIME" | ||
| const val SCOPE_TEST = "TEST" | ||
| } | ||
|
|
||
| init { | ||
|
|
@@ -90,6 +91,37 @@ class JavaModule( | |
| return dirs | ||
| } | ||
|
|
||
| override fun getTestSourceDirectories(scope: TestScope): Set<File> = | ||
| when (scope) { | ||
| TestScope.UNIT -> contentRootList.flatMap { root -> root.testDirectoryList.map { it.directory } }.toSet() | ||
| TestScope.ANDROID -> emptySet() | ||
| } | ||
|
|
||
| // getDependencyClassPaths does not filter by scope, so test dependencies are already on the | ||
| // compile classpath. | ||
| override fun getTestCompileClasspaths( | ||
| scope: TestScope, | ||
| excludeSourceGeneratedClassPath: Boolean, | ||
| ): Set<File> = | ||
| when (scope) { | ||
| TestScope.UNIT -> getCompileClasspaths(excludeSourceGeneratedClassPath) | ||
|
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: For a
Add the test-only modules' classpaths, or narrow the comment and KDoc to external jars. |
||
| TestScope.ANDROID -> emptySet() | ||
| } | ||
|
|
||
| // Unit tests see main's module dependencies plus the TEST-scoped ones (testImplementation). | ||
| override fun getTestCompileModuleProjects(scope: TestScope): List<ModuleProject> { | ||
| if (scope == TestScope.ANDROID) { | ||
| return emptyList() | ||
| } | ||
| val root = IProjectManager.getInstance().workspace ?: return emptyList() | ||
| val testOnly = | ||
| dependencyList | ||
| .filter { it.hasModule() && it.scope == SCOPE_TEST } | ||
|
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: Nothing tests the new Changing Add both cases to |
||
| .mapNotNull { root.findByPath(it.module.projectPath) as? ModuleProject } | ||
| .filter { it.path != path } | ||
| return (getCompileModuleProjects() + testOnly).distinctBy { it.path } | ||
| } | ||
|
|
||
| override fun getModuleClasspaths(): Set<File> = mutableSetOf(getClassesJar()) | ||
|
|
||
| override fun getCompileClasspaths( | ||
|
|
||
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.
NITPICK: The Android override expects AGP's test graph to repeat main's project dependencies, but the KDoc and
JavaModuleadd main'sgetCompileModuleProjects()explicitly.No defect at the tip, because both callers add main's modules themselves, but a main project dependency the test graph omits would be missing here and the KDoc does not say so.
Prepend
getCompileModuleProjects()asJavaModuledoes, or say in the KDoc that the result is the test graph only.