Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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> {

Copy link
Copy Markdown
Collaborator Author

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 JavaModule add main's getCompileModuleProjects() 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() as JavaModule does, or say in the KDoc that the result is the test graph only.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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.

collectLibraries and getCompileModuleProjects call logLostGraphEntry in the same case, so a damaged cache logs "Re-sync the project". Here a truncated test graph drops the test-only modules with no log line, and the user sees unresolved symbols in tests with no hint why.

Split the lookup as the siblings do and call logLostGraphEntry(nodeId, node?.keyId) before continue.

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()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

MINOR: With a variant that has no androidTest artifact, getTestCompileClasspaths(ANDROID) returns an empty set while getTestSourceDirectories(ANDROID) is not empty.

AGP builds androidTest only for testBuildType, so selecting release leaves androidTestArtifact unset, but the source provider is module-wide. At the tip, SourceFileManager.configureClasspaths then gives androidTest Java files no classpath at all, not even main's, so every androidx import is unresolved; before this stack they at least used main's. Graded PLAUSIBLE: traced in code, not reproduced on a device.

Fall back to getCompileClasspaths(...) when the artifact is absent, or return no test source dirs for that scope.

val project = IProjectManager.getInstance().workspace ?: return emptySet()

val result = getCompileClasspaths(excludeSourceGeneratedClassPath).toMutableSet()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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.

getCompileClasspaths(...) walks with one visited set and the test walk starts again from hashSetOf(path), so every shared module's graph is walked again. KotlinModuleLayout calls this per scope per module at the tip. The result is correct.

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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class JavaModule(
companion object {
const val SCOPE_COMPILE = "COMPILE"
const val SCOPE_RUNTIME = "RUNTIME"
const val SCOPE_TEST = "TEST"
}

init {
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

MINOR: For a TEST-scoped module dependency, getTestCompileClasspaths(UNIT) leaves out that module's classpath, which the KDoc promises.

getCompileClasspaths recurses only into COMPILE module dependencies, and getDependencyClassPaths gets nothing for a module dependency (jarFilePath is set only for single-entry libraries, JavaProjectExts.kt:65). So with testImplementation(project(":testutils")) its classes are missing, while the Android side includes them. Unreachable as a user-visible bug today because #2253 and #2254 add that module's sources through getTestCompileModuleProjects, but the comment above misleads the next caller.

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 }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

MINOR: Nothing tests the new JavaModule test-scope paths, getTestCompileModuleProjects with a TEST module dependency and getTestCompileClasspaths.

Changing SCOPE_TEST to SCOPE_COMPILE here, or dropping .filter { it.path != path }, leaves all 8 TestScopeSourcesTest tests green. The Android back-edge case (a test-only module depending back on :app, see the IMPORTANT above) is untested too.

Add both cases to TestScopeSourcesTest.

.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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ import java.io.File
import java.nio.file.Path
import kotlin.io.path.pathString

/** A test source set kind. Each one is its own scope: neither sees the other. */
enum class TestScope {
/** Host (JVM) tests, `src/test`. */
UNIT,

/** Device tests, `src/androidTest`. Android modules only. */
ANDROID,
}

/**
* A module project. Base class for [AndroidModule] and [JavaModule].
*
Expand Down Expand Up @@ -87,6 +96,33 @@ abstract class ModuleProject(
*/
abstract fun getCompileSourceDirectories(): Set<File>

/**
* Get the source directories of this module's [scope] tests (non-transitive). These are kept out
* of [getSourceDirectories] and [getCompileSourceDirectories], so neither main code nor dependent
* modules see test code.
*
* @return The test source directories, or an empty set if the module has no such tests.
*/
abstract fun getTestSourceDirectories(scope: TestScope): Set<File>

/**
* Get the classpaths that [scope] test sources compile against: everything in
* [getCompileClasspaths] plus the test-only dependencies of that scope.
*
* @param excludeSourceGeneratedClassPath See [getCompileClasspaths].
* @return The test compile classpaths, or an empty set if the module has no such tests.
*/
abstract fun getTestCompileClasspaths(
scope: TestScope,
excludeSourceGeneratedClassPath: Boolean = false,
): Set<File>

/**
* Get the module projects [scope] tests compile against, transitively: main's, plus those only
* the tests depend on, such as `testImplementation(project(":lib"))`. Never this module.
*/
abstract fun getTestCompileModuleProjects(scope: TestScope): List<ModuleProject>

/**
* Get the classpaths for this module project. The returned list always included the
* `classes.jar`.
Expand Down
Loading