Skip to content

Commit

Permalink
Make internal snapshots generation based on experimental snapshot pro…
Browse files Browse the repository at this point in the history
…perty (#92)

* Make internal snapshots generation based on experimental snapshot property

* Fix to pass extension values

* Fix test

* Lint

* Add tests for arg

* Detekt
  • Loading branch information
rbro112 authored Oct 20, 2023
1 parent 44cc186 commit 70da713
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class EmergePlugin : Plugin<Project> {
emergeExtension: EmergePluginExtension,
) {
appProject.afterEvaluate {
configureAppProjectSnapshots(appProject)
configureAppProjectSnapshots(
appProject = appProject,
emergeExtension = emergeExtension
)
}

appProject.pluginManager.withPlugin(ANDROID_APPLICATION_PLUGIN_ID) { _ ->
Expand Down Expand Up @@ -365,8 +368,15 @@ class EmergePlugin : Plugin<Project> {
}
}

private fun configureAppProjectSnapshots(appProject: Project) {
applyMainSourceSetConfig(appProject, appProject)
private fun configureAppProjectSnapshots(
appProject: Project,
emergeExtension: EmergePluginExtension,
) {
applyMainSourceSetConfig(
project = appProject,
appProject = appProject,
emergeExtension = emergeExtension
)

// Configure all dependent modules to apply the same configuration as the appProject so
// generated snapshots don't run into compilation issues.
Expand All @@ -377,13 +387,20 @@ class EmergePlugin : Plugin<Project> {
.distinct()
.filter { !it.state.executed }
.forEach { subproject ->
subproject.afterEvaluate { applyMainSourceSetConfig(it, appProject) }
subproject.afterEvaluate {
applyMainSourceSetConfig(
project = it,
appProject = appProject,
emergeExtension = emergeExtension
)
}
}
}

private fun applyMainSourceSetConfig(
project: Project,
appProject: Project,
emergeExtension: EmergePluginExtension,
) {
val errorMessages = mutableListOf<String>()
if (!project.plugins.hasPlugin(KSP_PLUGIN_ID)) {
Expand Down Expand Up @@ -411,11 +428,16 @@ class EmergePlugin : Plugin<Project> {
// TODO: Ryan: Explore using variants API for finding proper ourput dir.
val emergeSrcDir = "${project.buildDir}/$BUILD_OUTPUT_DIR_NAME/ksp/debugAndroidTest/kotlin"

val internalSnapshotsEnabled =
emergeExtension.snapshotOptions.experimentalInternalSnapshotsEnabled.getOrElse(false)
val internalEnabledArg = if (internalSnapshotsEnabled) "true" else "false"

appProject.logger.info(
"Configuring ${project.name} for Emerge snapshot testing, outputting to $emergeSrcDir"
)
project.extensions.getByType(KspExtension::class.java).apply {
arg(OUTPUT_SRC_DIR_OPTION_NAME, emergeSrcDir)
arg(INTERNAL_ENABLED_OPTION_NAME, internalEnabledArg)
}

appProject.extensions.getByType(KotlinAndroidProjectExtension::class.java).apply {
Expand Down Expand Up @@ -535,6 +557,7 @@ class EmergePlugin : Plugin<Project> {
const val EMERGE_JUNIT_RUNNER = "com.emergetools.test.EmergeJUnitRunner"

private const val OUTPUT_SRC_DIR_OPTION_NAME = "emerge.outputDir"
private const val INTERNAL_ENABLED_OPTION_NAME = "emerge.experimentalInternalEnabled"

private const val GENERATE_PERF_PROJECT_TASK_NAME = "emergeGeneratePerformanceProject"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ abstract class PerfOptions : ProductOptions() {
abstract class SnapshotOptions : ProductOptions() {

abstract val snapshotsStorageDirectory: DirectoryProperty

abstract val experimentalInternalSnapshotsEnabled: Property<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,34 @@ class PreviewProcessorTest {
}

@Test
fun `standalone preview function with internal preview produces one snapshot test`() {
fun `standalone preview function with internal preview produces no snapshot test without arg`() {
compileInputsAndVerifyOutputs()
}

@Test
fun `standalone preview function with internal preview produces one snapshot test with arg`() {
compileInputsAndVerifyOutputs(
mutableMapOf("emerge.experimentalInternalEnabled" to "true")
)
}

@Test
fun `multipreview function with two previews produces two snapshots`() {
compileInputsAndVerifyOutputs()
}

@Test
fun `multipreview internal function with two previews produces two snapshots`() {
fun `multipreview internal function with two previews produces no snapshots without arg`() {
compileInputsAndVerifyOutputs()
}

@Test
fun `multipreview internal function with two previews produces two snapshots with arg`() {
compileInputsAndVerifyOutputs(
mutableMapOf("emerge.experimentalInternalEnabled" to "true")
)
}

@Test
fun `multipreview function with two previews and additional preview produces three snapshots`() {
compileInputsAndVerifyOutputs()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_no_snapshots_without_arg.input

import androidx.compose.ui.tooling.preview.Preview

@Preview(
name = "Small font",
fontScale = 0.5f,
)
@Preview(
name = "Large font",
fontScale = 1.5f,
)
annotation class FontScalePreviews
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_no_snapshots_without_arg.input

import androidx.compose.runtime.Composable

@FontScalePreviews
@Composable
internal fun MultiPreviewComposable() {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots.input
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input

import androidx.compose.ui.tooling.preview.Preview

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input

import androidx.compose.runtime.Composable

@FontScalePreviews
@Composable
internal fun MultiPreviewComposable() {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.emergetools.android.standalone_preview_function_compiles_ok
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input

import PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input.MultiPreviewComposable
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.emergetools.android.standalone_preview_function_compiles_ok.MultiPreviewComposable
import com.emergetools.snapshots.EmergeSnapshots
import com.emergetools.snapshots.compose.SnapshotVariantProvider
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
Expand All @@ -13,12 +13,13 @@ import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
public class MultiPreviewComposable_1597336221_GenSnapshot {
public class `MultiPreviewComposable_-1707151673_GenSnapshot` {
@get:Rule
public val composeRule: ComposeContentTestRule = createComposeRule()

public val previewConfig: ComposePreviewSnapshotConfig = ComposePreviewSnapshotConfig(originalFqn
= "com.emergetools.android.standalone_preview_function_compiles_ok.MultiPreviewComposable",
=
"PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input.MultiPreviewComposable",
name = "Large font",
fontScale = 1.5f)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.emergetools.android.standalone_preview_function_compiles_ok
package PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input

import PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input.MultiPreviewComposable
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.emergetools.android.standalone_preview_function_compiles_ok.MultiPreviewComposable
import com.emergetools.snapshots.EmergeSnapshots
import com.emergetools.snapshots.compose.SnapshotVariantProvider
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
Expand All @@ -13,12 +13,13 @@ import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
public class `MultiPreviewComposable_-849881519_GenSnapshot` {
public class MultiPreviewComposable_140597883_GenSnapshot {
@get:Rule
public val composeRule: ComposeContentTestRule = createComposeRule()

public val previewConfig: ComposePreviewSnapshotConfig = ComposePreviewSnapshotConfig(originalFqn
= "com.emergetools.android.standalone_preview_function_compiles_ok.MultiPreviewComposable",
=
"PreviewProcessorTest.multipreview_internal_function_with_two_previews_produces_two_snapshots_with_arg.input.MultiPreviewComposable",
name = "Small font",
fontScale = 0.5f)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package PreviewProcessorTest.standalone_preview_function_with_internal_preview_produces_no_snapshot_test_without_arg.input

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.runtime.Composable

@Preview
@Composable
internal fun TestComposable() {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test
package PreviewProcessorTest.standalone_preview_function_with_internal_preview_produces_one_snapshot_test_with_arg.input

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.runtime.Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test
package PreviewProcessorTest.standalone_preview_function_with_internal_preview_produces_one_snapshot_test_with_arg.input

import PreviewProcessorTest.standalone_preview_function_with_internal_preview_produces_one_snapshot_test_with_arg.input.TestComposable
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test.TestComposable
import com.emergetools.snapshots.EmergeSnapshots
import com.emergetools.snapshots.compose.SnapshotVariantProvider
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
Expand All @@ -19,7 +19,7 @@ public class TestComposable_GenSnapshot {

public val previewConfig: ComposePreviewSnapshotConfig = ComposePreviewSnapshotConfig(originalFqn
=
"com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test.TestComposable")
"PreviewProcessorTest.standalone_preview_function_with_internal_preview_produces_one_snapshot_test_with_arg.input.TestComposable")

@get:Rule
public val snapshotRule: EmergeSnapshots = EmergeSnapshots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.emergetools.snapshots.processor.utils.putOrAppend
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.isAnnotationPresent
import com.google.devtools.ksp.isInternal
import com.google.devtools.ksp.isPrivate
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
Expand Down Expand Up @@ -79,6 +80,11 @@ class PreviewProcessor(
return@flatMap emptyList()
}

if (function.isInternal() && environment.options[INTERNAL_ENABLED_OPTION_NAME] != "true") {
logger.info("Skipping ${function.simpleName.asString()} as it is internal")
return@flatMap emptyList()
}

if (function.isAnnotationPresent(IgnoreEmergeSnapshot::class)) {
logger.info(
"Skipping ${function.simpleName.asString()} as it's annotated with @IgnoreEmergeSnapshot"
Expand Down Expand Up @@ -212,6 +218,7 @@ class PreviewProcessor(

companion object {
private const val OUTPUT_SRC_DIR_OPTION_NAME = "emerge.outputDir"
private const val INTERNAL_ENABLED_OPTION_NAME = "emerge.experimentalInternalEnabled"

private val ANDROID_JUNIT_RUNNER_CLASSNAME =
ClassName("androidx.test.ext.junit.runners", "AndroidJUnit4")
Expand Down

0 comments on commit 70da713

Please sign in to comment.