Skip to content

Commit

Permalink
Don't forget that snapshot tests must start with 'test' (#2352)
Browse files Browse the repository at this point in the history
* Don't forget that snapshot tests must start with 'test'

* Fix a test that was missing the required prefix
  • Loading branch information
squarejesse authored Oct 1, 2024
1 parent 7db5420 commit f8afbea
Show file tree
Hide file tree
Showing 34 changed files with 138 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build-support-ksp-processor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
kotlin("jvm")
}

dependencies {
implementation(libs.kotlin.kspApi)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.buildsupportksp

import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated

class RedwoodSymbolProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
SnapshotTestProcessor(environment).process(resolver)

return listOf() // No more rounds of annotation processing.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.buildsupportksp

import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider

class RedwoodSymbolProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment) =
RedwoodSymbolProcessor(environment)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.buildsupportksp

import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSFunctionDeclaration

/** Confirm all snapshot `@Test` functions also have names starting with `test`. */
internal class SnapshotTestProcessor(
private val environment: SymbolProcessorEnvironment,
) {
fun process(resolver: Resolver) {
// Only run on the first round.
if (!resolver.getNewFiles().iterator().hasNext()) return

checkAllTests(resolver)
}

private fun checkAllTests(resolver: Resolver) {
for (symbol in resolver.getSymbolsWithAnnotation("org.junit.Test")) {
when {
symbol !is KSFunctionDeclaration -> {
environment.logger.info("Unexpected @Test", symbol)
}

!symbol.simpleName.asString().startsWith("test") -> {
environment.logger.error("Expected @Test to start with 'test'", symbol)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.cash.redwood.buildsupportksp.RedwoodSymbolProcessorProvider
1 change: 1 addition & 0 deletions build-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ apply plugin: 'com.android.lint'
dependencies {
compileOnly gradleApi()
implementation libs.kotlin.gradlePlugin
implementation libs.kotlin.kspGradlePlugin
implementation libs.gradleMavenPublishPlugin
implementation libs.dokkaPlugin
implementation libs.spotlessPlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ interface RedwoodBuildExtension {
fun TaskContainer.generateComposeHelpers(packageName: String): TaskProvider<CopyPastaTask>

fun TaskContainer.generateFlexboxHelpers(packageName: String): TaskProvider<CopyPastaTask>

/** Confirm all snapshot `@Test` functions also have names starting with `test`. */
fun sharedSnapshotTests()
}

enum class TargetGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ private class RedwoodBuildExtensionImpl(private val project: Project) : RedwoodB
it.packageName.set(packageName)
}
}

override fun sharedSnapshotTests() {
project.plugins.apply("com.google.devtools.ksp")
project.dependencies.add("kspJvm", project.project(":build-support-ksp-processor"))
}
}

private val ziplineAttribute = Attribute.of("zipline", String::class.java)
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ kotlinx-serialization = "1.7.3"
androidx-activity = "1.9.2"
androidx-compose-ui = "1.7.2"
jbCompose = "1.6.11"
ksp = "2.0.20-1.0.25"
lint = "31.6.1"
paparazzi = "1.3.2"
zipline = "1.17.0"
Expand All @@ -16,6 +17,8 @@ kotlin-compiler = { module = "org.jetbrains.kotlin:kotlin-compiler", version.ref
kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }
kotlin-composePlugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-kspApi = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
kotlin-kspGradlePlugin = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" }
kotlin-serializationPlugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Expand Down
1 change: 1 addition & 0 deletions redwood-layout-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import static app.cash.redwood.buildsupport.TargetGroup.ToolkitAllWithoutAndroid

redwoodBuild {
targets(ToolkitAllWithoutAndroid)
sharedSnapshotTests()
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter(container.value).snapshot()
}

@Test fun columnWithUpdatedCrossAxisAlignment() {
@Test fun testColumnWithUpdatedCrossAxisAlignment() {
val container = flexContainer(FlexDirection.Column)
val snapshotter = snapshotter(container.value)
container.width(Constraint.Fill)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions redwood-lazylayout-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import static app.cash.redwood.buildsupport.TargetGroup.ToolkitAllWithoutAndroid

redwoodBuild {
targets(ToolkitAllWithoutAndroid)
sharedSnapshotTests()
}

kotlin {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions redwood-widget-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import static app.cash.redwood.buildsupport.TargetGroup.ToolkitAllWithoutAndroid

redwoodBuild {
targets(ToolkitAllWithoutAndroid)
sharedSnapshotTests()
}

kotlin {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enableFeaturePreview('TYPESAFE_PROJECT_ACCESSORS')

rootProject.name = 'redwood'

include ':build-support-ksp-processor'
include ':redwood-bom'
include ':redwood-compose'
include ':redwood-composeui'
Expand Down

0 comments on commit f8afbea

Please sign in to comment.