From bf36edeed2d24ace68c06923b24c85babaa0563b Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Mon, 14 Aug 2023 18:59:23 +0200 Subject: [PATCH 01/11] Migrate `DesignSystemIssueRegistry` into generic `NiaIssueRegistry` --- ...ystemIssueRegistry.kt => NiaIssueRegistry.kt} | 16 ++++++++-------- ...m.android.tools.lint.client.api.IssueRegistry | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) rename lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/{designsystem/DesignSystemIssueRegistry.kt => NiaIssueRegistry.kt} (73%) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemIssueRegistry.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt similarity index 73% rename from lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemIssueRegistry.kt rename to lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt index bb7e971e3..333462770 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemIssueRegistry.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt @@ -1,5 +1,5 @@ /* - * Copyright 2022 The Android Open Source Project + * Copyright 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,18 +14,18 @@ * limitations under the License. */ -package com.google.samples.apps.nowinandroid.lint.designsystem +package com.google.samples.apps.nowinandroid.lint import com.android.tools.lint.client.api.IssueRegistry import com.android.tools.lint.client.api.Vendor import com.android.tools.lint.detector.api.CURRENT_API +import com.google.samples.apps.nowinandroid.lint.designsystem.DesignSystemDetector -/** - * An issue registry that checks for incorrect usages of Compose Material APIs over equivalents in - * the Now in Android design system module. - */ -class DesignSystemIssueRegistry : IssueRegistry() { - override val issues = listOf(DesignSystemDetector.ISSUE) +class NiaIssueRegistry : IssueRegistry() { + + override val issues = listOf( + DesignSystemDetector.ISSUE, + ) override val api: Int = CURRENT_API diff --git a/lint/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry b/lint/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry index 4b8002da2..e673c27ff 100644 --- a/lint/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry +++ b/lint/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry @@ -14,4 +14,4 @@ # limitations under the License. # -com.google.samples.apps.nowinandroid.lint.designsystem.DesignSystemIssueRegistry +com.google.samples.apps.nowinandroid.lint.NiaIssueRegistry From df893558f92e7fd3320dad31cc2096fc7f9777cc Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Mon, 14 Aug 2023 13:29:50 +0200 Subject: [PATCH 02/11] Add missing test dependencies --- gradle/libs.versions.toml | 2 ++ lint/build.gradle.kts | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 60d827c0e..1d1aa90e0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -122,6 +122,8 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" } kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } lint-api = { group = "com.android.tools.lint", name = "lint-api", version.ref = "lint" } +lint-checks = { group = "com.android.tools.lint", name = "lint-checks", version.ref = "lint" } +lint-tests = { group = "com.android.tools.lint", name = "lint-tests", version.ref = "lint" } okhttp-logging = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" } protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" } protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" } diff --git a/lint/build.gradle.kts b/lint/build.gradle.kts index c7032cf40..5f90d1e45 100644 --- a/lint/build.gradle.kts +++ b/lint/build.gradle.kts @@ -29,7 +29,7 @@ java { targetCompatibility = JavaVersion.VERSION_11 } -tasks.withType().configureEach { +tasks.withType { kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } @@ -38,4 +38,7 @@ tasks.withType().configureEach { dependencies { compileOnly(libs.kotlin.stdlib) compileOnly(libs.lint.api) + testImplementation(libs.lint.checks) + testImplementation(libs.lint.tests) + testImplementation(kotlin("test")) } From 66cc677353fd732175d149cc48bd0eb31530c601 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Mon, 14 Aug 2023 15:25:51 +0200 Subject: [PATCH 03/11] Add `TestMethodDetector` with tests --- .../nowinandroid/lint/NiaIssueRegistry.kt | 3 + .../nowinandroid/lint/TestMethodDetector.kt | 160 ++++++++++++++++++ .../lint/TestMethodDetectorTest.kt | 155 +++++++++++++++++ 3 files changed, 318 insertions(+) create mode 100644 lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt create mode 100644 lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt index 333462770..f402b4039 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt @@ -25,6 +25,9 @@ class NiaIssueRegistry : IssueRegistry() { override val issues = listOf( DesignSystemDetector.ISSUE, + TestMethodDetector.UNDERSCORE, + TestMethodDetector.FORMAT, + TestMethodDetector.PREFIX, ) override val api: Int = CURRENT_API diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt new file mode 100644 index 000000000..c7989542e --- /dev/null +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt @@ -0,0 +1,160 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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 + * + * https://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 com.google.samples.apps.nowinandroid.lint + +import com.android.tools.lint.detector.api.AnnotationInfo +import com.android.tools.lint.detector.api.AnnotationUsageInfo +import com.android.tools.lint.detector.api.Category.Companion.TESTING +import com.android.tools.lint.detector.api.Detector +import com.android.tools.lint.detector.api.Implementation +import com.android.tools.lint.detector.api.Issue +import com.android.tools.lint.detector.api.JavaContext +import com.android.tools.lint.detector.api.LintFix +import com.android.tools.lint.detector.api.Scope.JAVA_FILE +import com.android.tools.lint.detector.api.Scope.TEST_SOURCES +import com.android.tools.lint.detector.api.Severity.WARNING +import com.android.tools.lint.detector.api.SourceCodeScanner +import com.android.tools.lint.detector.api.TextFormat.RAW +import com.intellij.psi.PsiMethod +import org.jetbrains.uast.UElement +import java.util.EnumSet +import kotlin.io.path.Path + +/** + * A detector that checks for common patterns in naming the test methods: + * - [detectPrefix] removes unnecessary "test" prefix in all unit test. + * - [detectUnderscore] removes underscores in JVM unit test (and add backticks if necessary). + * - [detectFormat] Checks the `given_when_then` format of Android instrumented tests (backticks are not supported). + */ +class TestMethodDetector : Detector(), SourceCodeScanner { + + override fun applicableAnnotations() = listOf("org.junit.Test") + + override fun visitAnnotationUsage( + context: JavaContext, + element: UElement, + annotationInfo: AnnotationInfo, + usageInfo: AnnotationUsageInfo, + ) { + val method = usageInfo.referenced as? PsiMethod ?: return + + method.detectPrefix(context, usageInfo) + method.detectFormat(context, usageInfo) + method.detectUnderscore(context, usageInfo) + } + + private fun JavaContext.isAndroidTest() = Path("androidTest") in file.toPath() + + private fun PsiMethod.detectPrefix( + context: JavaContext, + usageInfo: AnnotationUsageInfo, + ) { + if (!name.startsWith("test")) return + context.report( + issue = PREFIX, + scope = usageInfo.usage, + location = context.getNameLocation(this), + message = PREFIX.getBriefDescription(RAW), + quickfixData = LintFix.create() + .name("Remove prefix") + .replace().pattern("""test[\s_]*""") + .with("") + .autoFix() + .build(), + ) + } + + private fun PsiMethod.detectFormat( + context: JavaContext, + usageInfo: AnnotationUsageInfo, + ) { + if (!context.isAndroidTest()) return + if ("""^[^\W_]+_[^\W_]+_[^\W_]+$""".toRegex().matches(name)) return + context.report( + issue = FORMAT, + scope = usageInfo.usage, + location = context.getNameLocation(this), + message = FORMAT.getBriefDescription(RAW), + ) + } + + private fun PsiMethod.detectUnderscore( + context: JavaContext, + usageInfo: AnnotationUsageInfo, + ) { + if (context.isAndroidTest()) return + if ("_" !in name) return + context.report( + issue = UNDERSCORE, + scope = usageInfo.usage, + location = context.getNameLocation(this), + message = UNDERSCORE.getBriefDescription(RAW), + quickfixData = LintFix.create() + .name("Replace underscores with spaces") + .replace() + .range(context.getNameLocation(this)) + .with( + name.replace("_", " ") + .removeSurrounding("`") + .let { """`$it`""" }, + ) + .autoFix() + .build(), + ) + } + + companion object { + + private fun issue( + id: String, + briefDescription: String, + explanation: String, + ): Issue = Issue.create( + id = id, + briefDescription = briefDescription, + explanation = explanation, + category = TESTING, + priority = 5, + severity = WARNING, + implementation = Implementation( + TestMethodDetector::class.java, + EnumSet.of(JAVA_FILE, TEST_SOURCES), + ), + ) + + @JvmField + val UNDERSCORE: Issue = issue( + id = "TestMethodUnderscore", + briefDescription = "Test method contains underscores", + explanation = "Test methods should not contains underscores.", + ) + + @JvmField + val PREFIX: Issue = issue( + id = "TestMethodPrefix", + briefDescription = "Test method starts with `test`", + explanation = "Test method should not start with `test`.", + ) + + @JvmField + val FORMAT: Issue = issue( + id = "TestMethodFormat", + briefDescription = "Test method does not follow the `given_when_then` format", + explanation = "Test method should follow the `given_when_then` format.", + ) + } +} diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt new file mode 100644 index 000000000..215efebf8 --- /dev/null +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt @@ -0,0 +1,155 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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 + * + * https://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 com.google.samples.apps.nowinandroid.lint + +import com.android.tools.lint.checks.infrastructure.TestFile +import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin +import com.android.tools.lint.checks.infrastructure.TestLintTask.lint +import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.FORMAT +import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.PREFIX +import org.junit.Test + +class TestMethodDetectorTest { + + @Test + fun `detect prefix`() { + lint().issues(PREFIX) + .files( + JUNIT_TEST_STUB, + kotlin( + """ + import org.junit.Test + class Test { + @Test + fun foo() = Unit + @Test + fun test_foo() = Unit + @Test + fun `test foo`() = Unit + } + """, + ).indented(), + ) + .run() + .expect( + """ + src/Test.kt:6: Warning: Test method starts with test [TestMethodWithTestPrefix] + fun test_foo() = Unit + ~~~~~~~~ + src/Test.kt:8: Warning: Test method starts with test [TestMethodWithTestPrefix] + fun `test foo`() = Unit + ~~~~~~~~~~ + 0 errors, 2 warnings + """.trimIndent(), + ) + .expectFixDiffs( + """ + Fix for src/Test.kt line 6: Remove underscores: + @@ -6 +6 + - fun test_foo() = Unit + + fun foo() = Unit + Fix for src/Test.kt line 8: Remove underscores: + @@ -8 +8 + - fun `test foo`() = Unit + + fun `foo`() = Unit + """.trimIndent(), + ) + } + + @Test + fun `detect format`() { + lint().issues(FORMAT) + .files( + JUNIT_TEST_STUB, + kotlin( + "src/androidTest/com/example/Test.kt", + """ + import org.junit.Test + class Test { + @Test + fun given_when_then() = Unit + @Test + fun given_foo_when_bar_then_baz() = Unit + } + """, + ).indented(), + ) + .run() + .expect( + """ + src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodGivenWhenThenFormatTest] + fun given_foo_when_bar_then_baz() = Unit + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 0 errors, 1 warnings + """.trimIndent(), + ) + } + + @Test + fun `detect underscores`() { + lint().issues(UNDERSCORES) + .files( + JUNIT_TEST_STUB, + kotlin( + """ + import org.junit.Test + class Test { + @Test + fun foo() = Unit + @Test + fun foo_bar_baz() = Unit + @Test + fun `foo_bar_baz`() = Unit + } + """, + ).indented(), + ) + .run() + .expect( + """ + src/Test.kt:6: Warning: Test method contains underscores [TestMethodContainsUnderscore] + fun foo_bar_baz() = Unit + ~~~~~~~~~~~ + src/Test.kt:8: Warning: Test method contains underscores [TestMethodContainsUnderscore] + fun `foo_bar_baz`() = Unit + ~~~~~~~~~~~~~ + 0 errors, 2 warnings + """.trimIndent(), + ) + .expectFixDiffs( + """ + Autofix for src/Test.kt line 6: Replace underscores with spaces: + @@ -6 +6 + - fun foo_bar_baz() = Unit + + fun `foo bar baz`() = Unit + Autofix for src/Test.kt line 8: Replace underscores with spaces: + @@ -8 +8 + - fun `foo_bar_baz`() = Unit + + fun `foo bar baz`() = Unit + """.trimIndent(), + ) + } + + private companion object { + private val JUNIT_TEST_STUB: TestFile = kotlin( + """ + package org.junit + annotation class Test + """, + ).indented() + } +} From f26b497bb21a27a63b849fcfc28f9473e41027d1 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Mon, 14 Aug 2023 16:18:51 +0200 Subject: [PATCH 04/11] Add `:lint:test` task to CI builds --- .github/workflows/Build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Build.yaml b/.github/workflows/Build.yaml index d5d98bf36..4a4320590 100644 --- a/.github/workflows/Build.yaml +++ b/.github/workflows/Build.yaml @@ -47,7 +47,7 @@ jobs: path: '**/build/outputs/apk/**/*.apk' - name: Run local tests - run: ./gradlew testDemoDebug testProdDebug + run: ./gradlew testDemoDebug testProdDebug :lint:test test: runs-on: ubuntu-latest From 8f1652fa00525bf838d8dcbd2097260637297cf8 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 24 Aug 2023 19:25:21 +0200 Subject: [PATCH 05/11] Add missing import --- .../samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt index 215efebf8..1ab2de2d5 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt @@ -21,6 +21,7 @@ import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin import com.android.tools.lint.checks.infrastructure.TestLintTask.lint import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.FORMAT import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.PREFIX +import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.UNDERSCORE import org.junit.Test class TestMethodDetectorTest { @@ -101,7 +102,7 @@ class TestMethodDetectorTest { @Test fun `detect underscores`() { - lint().issues(UNDERSCORES) + lint().issues(UNDERSCORE) .files( JUNIT_TEST_STUB, kotlin( From 437f36ed8b65dc2ffcfc7e215378e5c8aa8aa6d6 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 24 Aug 2023 19:28:31 +0200 Subject: [PATCH 06/11] Fix test --- .../nowinandroid/lint/TestMethodDetectorTest.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt index 1ab2de2d5..ed24b5ccc 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt @@ -48,10 +48,10 @@ class TestMethodDetectorTest { .run() .expect( """ - src/Test.kt:6: Warning: Test method starts with test [TestMethodWithTestPrefix] + src/Test.kt:6: Warning: Test method starts with test [TestMethodPrefix] fun test_foo() = Unit ~~~~~~~~ - src/Test.kt:8: Warning: Test method starts with test [TestMethodWithTestPrefix] + src/Test.kt:8: Warning: Test method starts with test [TestMethodPrefix] fun `test foo`() = Unit ~~~~~~~~~~ 0 errors, 2 warnings @@ -59,11 +59,11 @@ class TestMethodDetectorTest { ) .expectFixDiffs( """ - Fix for src/Test.kt line 6: Remove underscores: + Autofix for src/Test.kt line 6: Remove prefix: @@ -6 +6 - fun test_foo() = Unit + fun foo() = Unit - Fix for src/Test.kt line 8: Remove underscores: + Autofix for src/Test.kt line 8: Remove prefix: @@ -8 +8 - fun `test foo`() = Unit + fun `foo`() = Unit @@ -92,7 +92,7 @@ class TestMethodDetectorTest { .run() .expect( """ - src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodGivenWhenThenFormatTest] + src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodFormat] fun given_foo_when_bar_then_baz() = Unit ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 0 errors, 1 warnings @@ -122,10 +122,10 @@ class TestMethodDetectorTest { .run() .expect( """ - src/Test.kt:6: Warning: Test method contains underscores [TestMethodContainsUnderscore] + src/Test.kt:6: Warning: Test method contains underscores [TestMethodUnderscore] fun foo_bar_baz() = Unit ~~~~~~~~~~~ - src/Test.kt:8: Warning: Test method contains underscores [TestMethodContainsUnderscore] + src/Test.kt:8: Warning: Test method contains underscores [TestMethodUnderscore] fun `foo_bar_baz`() = Unit ~~~~~~~~~~~~~ 0 errors, 2 warnings From 28acb57ca06b6a7345798369b6727f2e992dc456 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 13 Oct 2023 23:05:38 +0200 Subject: [PATCH 07/11] Update regex to match expected format --- .../nowinandroid/lint/TestMethodDetector.kt | 6 ++--- .../lint/TestMethodDetectorTest.kt | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt index c7989542e..bcb75ffd5 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt @@ -83,7 +83,7 @@ class TestMethodDetector : Detector(), SourceCodeScanner { usageInfo: AnnotationUsageInfo, ) { if (!context.isAndroidTest()) return - if ("""^[^\W_]+_[^\W_]+_[^\W_]+$""".toRegex().matches(name)) return + if ("""[^\W_]+(_[^\W_]+){1,2}""".toRegex().matches(name)) return context.report( issue = FORMAT, scope = usageInfo.usage, @@ -153,8 +153,8 @@ class TestMethodDetector : Detector(), SourceCodeScanner { @JvmField val FORMAT: Issue = issue( id = "TestMethodFormat", - briefDescription = "Test method does not follow the `given_when_then` format", - explanation = "Test method should follow the `given_when_then` format.", + briefDescription = "Test method does not follow the `given_when_then` or `when_then` format", + explanation = "Test method should follow the `given_when_then` or `when_then` format.", ) } } diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt index ed24b5ccc..592b4f8fb 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt @@ -81,10 +81,17 @@ class TestMethodDetectorTest { """ import org.junit.Test class Test { + @Test + fun when_then() = Unit @Test fun given_when_then() = Unit + + @Test + fun foo() = Unit + @Test + fun foo_bar_baz_qux() = Unit @Test - fun given_foo_when_bar_then_baz() = Unit + fun `foo bar baz`() = Unit } """, ).indented(), @@ -92,10 +99,16 @@ class TestMethodDetectorTest { .run() .expect( """ - src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodFormat] - fun given_foo_when_bar_then_baz() = Unit - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 0 errors, 1 warnings + src/androidTest/com/example/Test.kt:9: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun foo() = Unit + ~~~ + src/androidTest/com/example/Test.kt:11: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun foo_bar_baz_qux() = Unit + ~~~~~~~~~~~~~~~ + src/androidTest/com/example/Test.kt:13: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun `foo bar baz`() = Unit + ~~~~~~~~~~~~~ + 0 errors, 3 warnings """.trimIndent(), ) } From b4ee73026309e909142dbf3a5747d114950d8b56 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 13 Oct 2023 23:07:21 +0200 Subject: [PATCH 08/11] Rename `TestMethodDetector` to `TestMethodNameDetector` as suggested --- .../samples/apps/nowinandroid/lint/NiaIssueRegistry.kt | 6 +++--- .../{TestMethodDetector.kt => TestMethodNameDetector.kt} | 4 ++-- ...ethodDetectorTest.kt => TestMethodNameDetectorTest.kt} | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) rename lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/{TestMethodDetector.kt => TestMethodNameDetector.kt} (98%) rename lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/{TestMethodDetectorTest.kt => TestMethodNameDetectorTest.kt} (95%) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt index f402b4039..08783dfc3 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt @@ -25,9 +25,9 @@ class NiaIssueRegistry : IssueRegistry() { override val issues = listOf( DesignSystemDetector.ISSUE, - TestMethodDetector.UNDERSCORE, - TestMethodDetector.FORMAT, - TestMethodDetector.PREFIX, + TestMethodNameDetector.UNDERSCORE, + TestMethodNameDetector.FORMAT, + TestMethodNameDetector.PREFIX, ) override val api: Int = CURRENT_API diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt similarity index 98% rename from lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt rename to lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt index bcb75ffd5..01791b2d1 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt @@ -40,7 +40,7 @@ import kotlin.io.path.Path * - [detectUnderscore] removes underscores in JVM unit test (and add backticks if necessary). * - [detectFormat] Checks the `given_when_then` format of Android instrumented tests (backticks are not supported). */ -class TestMethodDetector : Detector(), SourceCodeScanner { +class TestMethodNameDetector : Detector(), SourceCodeScanner { override fun applicableAnnotations() = listOf("org.junit.Test") @@ -131,7 +131,7 @@ class TestMethodDetector : Detector(), SourceCodeScanner { priority = 5, severity = WARNING, implementation = Implementation( - TestMethodDetector::class.java, + TestMethodNameDetector::class.java, EnumSet.of(JAVA_FILE, TEST_SOURCES), ), ) diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt similarity index 95% rename from lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt rename to lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt index 592b4f8fb..bc84b2b4a 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt @@ -19,12 +19,12 @@ package com.google.samples.apps.nowinandroid.lint import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin import com.android.tools.lint.checks.infrastructure.TestLintTask.lint -import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.FORMAT -import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.PREFIX -import com.google.samples.apps.nowinandroid.lint.TestMethodDetector.Companion.UNDERSCORE +import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.FORMAT +import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.PREFIX +import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.UNDERSCORE import org.junit.Test -class TestMethodDetectorTest { +class TestMethodNameDetectorTest { @Test fun `detect prefix`() { From 8c9fe64ac0f77b0024764e0e94e5533eb9d32a0c Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 13 Oct 2023 23:08:22 +0200 Subject: [PATCH 09/11] Restore lazy API call --- lint/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/build.gradle.kts b/lint/build.gradle.kts index 5f90d1e45..acb540c3b 100644 --- a/lint/build.gradle.kts +++ b/lint/build.gradle.kts @@ -29,7 +29,7 @@ java { targetCompatibility = JavaVersion.VERSION_11 } -tasks.withType { +tasks.withType().configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() } From b985f6515862c1ade3308feee8a29ec1cd30762b Mon Sep 17 00:00:00 2001 From: Don Turner Date: Fri, 20 Oct 2023 12:06:59 +0100 Subject: [PATCH 10/11] Remove underscore check Change-Id: Iec1d07787a6e1cd350b9d8d082e729ef62492013 --- .../nowinandroid/lint/NiaIssueRegistry.kt | 1 - .../lint/TestMethodNameDetector.kt | 34 -------------- .../lint/TestMethodNameDetectorTest.kt | 46 ------------------- 3 files changed, 81 deletions(-) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt index 08783dfc3..27ae46ab3 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt @@ -25,7 +25,6 @@ class NiaIssueRegistry : IssueRegistry() { override val issues = listOf( DesignSystemDetector.ISSUE, - TestMethodNameDetector.UNDERSCORE, TestMethodNameDetector.FORMAT, TestMethodNameDetector.PREFIX, ) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt index 01791b2d1..532994d99 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetector.kt @@ -37,7 +37,6 @@ import kotlin.io.path.Path /** * A detector that checks for common patterns in naming the test methods: * - [detectPrefix] removes unnecessary "test" prefix in all unit test. - * - [detectUnderscore] removes underscores in JVM unit test (and add backticks if necessary). * - [detectFormat] Checks the `given_when_then` format of Android instrumented tests (backticks are not supported). */ class TestMethodNameDetector : Detector(), SourceCodeScanner { @@ -54,7 +53,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner { method.detectPrefix(context, usageInfo) method.detectFormat(context, usageInfo) - method.detectUnderscore(context, usageInfo) } private fun JavaContext.isAndroidTest() = Path("androidTest") in file.toPath() @@ -92,31 +90,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner { ) } - private fun PsiMethod.detectUnderscore( - context: JavaContext, - usageInfo: AnnotationUsageInfo, - ) { - if (context.isAndroidTest()) return - if ("_" !in name) return - context.report( - issue = UNDERSCORE, - scope = usageInfo.usage, - location = context.getNameLocation(this), - message = UNDERSCORE.getBriefDescription(RAW), - quickfixData = LintFix.create() - .name("Replace underscores with spaces") - .replace() - .range(context.getNameLocation(this)) - .with( - name.replace("_", " ") - .removeSurrounding("`") - .let { """`$it`""" }, - ) - .autoFix() - .build(), - ) - } - companion object { private fun issue( @@ -136,13 +109,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner { ), ) - @JvmField - val UNDERSCORE: Issue = issue( - id = "TestMethodUnderscore", - briefDescription = "Test method contains underscores", - explanation = "Test methods should not contains underscores.", - ) - @JvmField val PREFIX: Issue = issue( id = "TestMethodPrefix", diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt index bc84b2b4a..8da173285 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodNameDetectorTest.kt @@ -21,7 +21,6 @@ import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin import com.android.tools.lint.checks.infrastructure.TestLintTask.lint import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.FORMAT import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.PREFIX -import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.UNDERSCORE import org.junit.Test class TestMethodNameDetectorTest { @@ -113,51 +112,6 @@ class TestMethodNameDetectorTest { ) } - @Test - fun `detect underscores`() { - lint().issues(UNDERSCORE) - .files( - JUNIT_TEST_STUB, - kotlin( - """ - import org.junit.Test - class Test { - @Test - fun foo() = Unit - @Test - fun foo_bar_baz() = Unit - @Test - fun `foo_bar_baz`() = Unit - } - """, - ).indented(), - ) - .run() - .expect( - """ - src/Test.kt:6: Warning: Test method contains underscores [TestMethodUnderscore] - fun foo_bar_baz() = Unit - ~~~~~~~~~~~ - src/Test.kt:8: Warning: Test method contains underscores [TestMethodUnderscore] - fun `foo_bar_baz`() = Unit - ~~~~~~~~~~~~~ - 0 errors, 2 warnings - """.trimIndent(), - ) - .expectFixDiffs( - """ - Autofix for src/Test.kt line 6: Replace underscores with spaces: - @@ -6 +6 - - fun foo_bar_baz() = Unit - + fun `foo bar baz`() = Unit - Autofix for src/Test.kt line 8: Replace underscores with spaces: - @@ -8 +8 - - fun `foo_bar_baz`() = Unit - + fun `foo bar baz`() = Unit - """.trimIndent(), - ) - } - private companion object { private val JUNIT_TEST_STUB: TestFile = kotlin( """ From adb627bf9472eea10c78e6f3a8825d5a09255023 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 20 Oct 2023 15:25:05 +0200 Subject: [PATCH 11/11] Update lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt --- .../google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt index 27ae46ab3..b806312fd 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/NiaIssueRegistry.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 The Android Open Source Project + * Copyright 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.