From 9747255e33d461896da1a909f41c2bc5801cf724 Mon Sep 17 00:00:00 2001 From: Don Turner Date: Fri, 20 Oct 2023 12:06:59 +0100 Subject: [PATCH] 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( """