Repository navigation
ADFA-7115: Carry test source sets and test compile graphs in the Android model #2251
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
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 |
|---|---|---|
|
|
@@ -79,11 +79,16 @@ fun createAndroidProjectProtoModel( | |
| variantDependencies = variantDependencies.asProtoModel(), | ||
| ) | ||
|
|
||
| // The single-artifact test getters are deprecated but present from AGP 7.3 (our minimum) onwards; | ||
| // newer AGP implements them over the host/device test maps. | ||
| @Suppress("DEPRECATION") | ||
| fun VariantDependencies.asProtoModel() = | ||
| VariantDependencies( | ||
| name = this.name, | ||
| mainArtifact = this.mainArtifact.asProtoModel(), | ||
| librariesMap = this.libraries.mapValues { entry -> entry.value.asProtoModel() }, | ||
| unitTestArtifact = this.unitTestArtifact?.asProtoModel(), | ||
|
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: Each test artifact runs its own
If the size matters, share one key table across the three artifacts of a |
||
| androidTestArtifact = this.androidTestArtifact?.asProtoModel(), | ||
| ) | ||
|
|
||
| fun Library.asProtoModel() = | ||
|
|
@@ -206,7 +211,14 @@ fun UnresolvedDependency.asProtoModel() = | |
| cause = this.cause, | ||
| ) | ||
|
|
||
| fun SourceSetContainer.asProtoModel() = SourceSetContainer(sourceProvider = this.sourceProvider?.asProtoModel()) | ||
| // See VariantDependencies.asProtoModel for why the deprecated test getters are used. | ||
| @Suppress("DEPRECATION") | ||
| fun SourceSetContainer.asProtoModel() = | ||
| SourceSetContainer( | ||
| sourceProvider = this.sourceProvider?.asProtoModel(), | ||
| unitTestSourceProvider = this.unitTestSourceProvider?.asProtoModel(), | ||
| androidTestSourceProvider = this.androidTestSourceProvider?.asProtoModel(), | ||
| ) | ||
|
|
||
| fun SourceProvider.asProtoModel() = | ||
| SourceProvider( | ||
|
|
@@ -226,6 +238,8 @@ fun ViewBindingOptions.asProtoModel() = | |
| isEnabled = this.isEnabled, | ||
| ) | ||
|
|
||
| // See VariantDependencies.asProtoModel for why the deprecated test getters are used. | ||
| @Suppress("DEPRECATION") | ||
|
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: If AGP later deprecates another member these functions read ( Scope the suppression to each test-getter expression or a small helper. |
||
| fun Variant.asProtoModel( | ||
| basicAndroidProject: BasicAndroidProject, | ||
| androidProject: AndroidProject, | ||
|
|
@@ -244,6 +258,16 @@ fun Variant.asProtoModel( | |
| ), | ||
| variantSourceProvidersList = | ||
| basicAndroidProject.variantSourceProviders(this.name).map { provider -> provider.asProtoModel() }, | ||
| unitTestGeneratedSourceFolderPathsList = | ||
|
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 Each future per-artifact field will need another copy. Extract an |
||
| this.unitTestArtifact | ||
| ?.generatedSourceFolders | ||
| ?.map { it.absolutePath } | ||
| .orEmpty(), | ||
| androidTestGeneratedSourceFolderPathsList = | ||
| this.androidTestArtifact | ||
| ?.generatedSourceFolders | ||
| ?.map { it.absolutePath } | ||
| .orEmpty(), | ||
| ) | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package com.itsaky.androidide.tooling.impl.serial | ||
|
|
||
| import com.android.builder.model.v2.ide.AbstractArtifact | ||
| import com.android.builder.model.v2.ide.ArtifactDependencies | ||
| import com.android.builder.model.v2.ide.GraphItem | ||
| import com.android.builder.model.v2.ide.SourceProvider | ||
| import com.android.builder.model.v2.ide.SourceSetContainer | ||
| import com.android.builder.model.v2.ide.Variant | ||
| import com.android.builder.model.v2.models.VariantDependencies | ||
| import com.android.builder.model.v2.models.Versions | ||
| import com.google.common.truth.Truth.assertThat | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * The unit test and android test source providers and compile graphs reach the proto model, so | ||
| * the IDE can give test sources their own scopes (ADFA-7115). | ||
| */ | ||
| @Suppress("DEPRECATION") | ||
| @RunWith(JUnit4::class) | ||
| class TestArtifactsSerializationTest { | ||
| @Test | ||
| fun `serializes unit test and android test source providers`() { | ||
| val container: SourceSetContainer = | ||
| mockk { | ||
| every { sourceProvider } returns provider("/p/src/main/java") | ||
| every { unitTestSourceProvider } returns provider("/p/src/test/java") | ||
| every { androidTestSourceProvider } returns provider("/p/src/androidTest/java") | ||
| } | ||
|
|
||
| val proto = container.asProtoModel() | ||
|
|
||
| assertThat(proto.sourceProvider.javaDirsList).containsExactly("/p/src/main/java") | ||
| assertThat(proto.unitTestSourceProvider.javaDirsList).containsExactly("/p/src/test/java") | ||
| assertThat(proto.androidTestSourceProvider.javaDirsList).containsExactly("/p/src/androidTest/java") | ||
| } | ||
|
|
||
| @Test | ||
| fun `a module without test source sets leaves the test providers unset`() { | ||
| val container: SourceSetContainer = | ||
| mockk { | ||
| every { sourceProvider } returns provider("/p/src/main/java") | ||
| every { unitTestSourceProvider } returns null | ||
| every { androidTestSourceProvider } returns null | ||
| } | ||
|
|
||
| val proto = container.asProtoModel() | ||
|
|
||
| assertThat(proto.hasUnitTestSourceProvider()).isFalse() | ||
| assertThat(proto.hasAndroidTestSourceProvider()).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `serializes unit test and android test compile graphs`() { | ||
| val dependencies: VariantDependencies = | ||
| mockk { | ||
| every { name } returns "debug" | ||
| every { mainArtifact } returns artifact("main-lib") | ||
| every { libraries } returns emptyMap() | ||
| every { unitTestArtifact } returns artifact("junit") | ||
| every { androidTestArtifact } returns artifact("espresso") | ||
| } | ||
|
|
||
| val proto = dependencies.asProtoModel() | ||
|
|
||
| assertThat(proto.mainArtifact.compileGraph.keyList).containsExactly("main-lib") | ||
| assertThat(proto.unitTestArtifact.compileGraph.keyList).containsExactly("junit") | ||
| assertThat(proto.androidTestArtifact.compileGraph.keyList).containsExactly("espresso") | ||
| } | ||
|
|
||
| @Test | ||
| fun `a variant without test artifacts leaves the test graphs unset`() { | ||
| val dependencies: VariantDependencies = | ||
| mockk { | ||
| every { name } returns "debug" | ||
| every { mainArtifact } returns artifact("main-lib") | ||
| every { libraries } returns emptyMap() | ||
| every { unitTestArtifact } returns null | ||
| every { androidTestArtifact } returns null | ||
| } | ||
|
|
||
| val proto = dependencies.asProtoModel() | ||
|
|
||
| assertThat(proto.hasUnitTestArtifact()).isFalse() | ||
| assertThat(proto.hasAndroidTestArtifact()).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `serializes each test artifact's generated source folders`() { | ||
|
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 a That is the path a release-variant sync takes, since androidTest exists only for Add a case with both artifacts null that asserts both generated-folder lists are empty. |
||
| val variant: Variant = | ||
| mockk(relaxed = true) { | ||
| every { name } returns "debug" | ||
| every { unitTestArtifact } returns generating("/p/build/generated/ksp/debugUnitTest") | ||
| every { androidTestArtifact } returns generating("/p/build/generated/ksp/debugAndroidTest") | ||
| } | ||
| val versions: Versions = mockk { every { agp } returns "8.8.2" } | ||
|
|
||
| val proto = variant.asProtoModel(mockk(relaxed = true), mockk(relaxed = true), mockk(relaxed = true), versions) | ||
|
|
||
| assertThat(proto.unitTestGeneratedSourceFolderPathsList).containsExactly("/p/build/generated/ksp/debugUnitTest") | ||
| assertThat(proto.androidTestGeneratedSourceFolderPathsList).containsExactly("/p/build/generated/ksp/debugAndroidTest") | ||
| } | ||
|
|
||
| /** A test artifact whose only generated source folder is [dir]. */ | ||
| private inline fun <reified T : AbstractArtifact> generating(dir: String): T = | ||
| mockk(relaxed = true) { every { generatedSourceFolders } returns listOf(File(dir)) } | ||
|
|
||
| private fun provider(javaDir: String): SourceProvider = | ||
| mockk { | ||
| every { javaDirectories } returns listOf(File(javaDir)) | ||
| every { kotlinDirectories } returns emptyList() | ||
| every { resDirectories } returns emptyList() | ||
| } | ||
|
|
||
| private fun artifact(key: String): ArtifactDependencies { | ||
| val item: GraphItem = | ||
| mockk { | ||
| every { this@mockk.key } returns key | ||
| every { requestedCoordinates } returns null | ||
| every { dependencies } returns emptyList() | ||
| } | ||
| return mockk { | ||
| every { compileDependencies } returns listOf(item) | ||
| every { unresolvedDependencies } returns emptyList() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,7 @@ object ProjectSyncHelper { | |
| * differs from this one discards the sync files and forces a full sync. The models are | ||
| * produced and consumed only by the IDE and hold no user data, so discarding them is safe. | ||
| */ | ||
| const val SYNC_META_VERSION = "2" | ||
| const val SYNC_META_VERSION = "3" | ||
|
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: The The next developer who adds optional fields follows the KDoc and skips the bump. Cached projects then read the new fields as unset until a manual re-sync, which is the stale-cache bug this bump exists to prevent. Reword it: bump whenever a persisted proto schema changes, including added fields that consumers rely on. |
||
|
|
||
| private val logger = LoggerFactory.getLogger(ProjectSyncHelper::class.java) | ||
|
|
||
|
|
||
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 proto copies AGP's deprecated singular test shape rather than the
hostTestArtifacts/deviceTestArtifactsmaps.Screenshot tests and AGP 9 test suites cannot be represented, so supporting them needs another schema change and another
SYNC_META_VERSIONbump.Consider
map<string, ArtifactDependencies>keyed by artifact name, or list it under Limitations.