From 09a9aa6bb3a5d21143f55a6b4ecf61eeab1b5d75 Mon Sep 17 00:00:00 2001 From: ghm Date: Fri, 3 Nov 2023 04:44:42 -0700 Subject: [PATCH] Include some more attractive-nuisance runners in TestParametersNotInitialized. We can't only include TestParameterInjector, because other frameworks are adopting its annotation for injecting parameters. But these are other parameterising frameworks you _might_ guess would work with @TestParameter, but don't. PiperOrigin-RevId: 579143309 --- .../TestParametersNotInitialized.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TestParametersNotInitialized.java b/core/src/main/java/com/google/errorprone/bugpatterns/TestParametersNotInitialized.java index 85e172cda5e..64a7ae24241 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TestParametersNotInitialized.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TestParametersNotInitialized.java @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; +import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; import com.google.errorprone.fixes.SuggestedFix; @@ -36,6 +37,7 @@ import com.sun.source.tree.AnnotationTree; import com.sun.source.tree.ClassTree; import com.sun.source.tree.Tree; +import javax.inject.Inject; /** Flags uses of parameters in non-parameterized tests. */ @BugPattern( @@ -59,15 +61,37 @@ public final class TestParametersNotInitialized extends BugChecker implements Cl AT_LEAST_ONE, hasArgumentWithValue("value", isJUnit4TestRunnerOfType(ImmutableSet.of(RUNNER)))); - private static final MultiMatcher JUNIT4_RUNNER = - annotations( - AT_LEAST_ONE, - hasArgumentWithValue( - "value", isJUnit4TestRunnerOfType(ImmutableSet.of("org.junit.runners.JUnit4")))); + private final MultiMatcher nonParameterizedRunner; + + @Inject + TestParametersNotInitialized(ErrorProneFlags flags) { + var moreRunners = flags.getBoolean("TestParametersNotInitialized:MoreRunners").orElse(true); + this.nonParameterizedRunner = + moreRunners + ? annotations( + AT_LEAST_ONE, + hasArgumentWithValue( + "value", + isJUnit4TestRunnerOfType( + ImmutableSet.of( + // keep-sorted start + "androidx.test.ext.junit.runners.AndroidJUnit4", + "junitparams.JUnitParamsRunner", + "org.junit.experimental.theories.Theories", + "org.junit.runners.JUnit4", + "org.junit.runners.Parameterized" + // keep-sorted end + )))) + : annotations( + AT_LEAST_ONE, + hasArgumentWithValue( + "value", + isJUnit4TestRunnerOfType(ImmutableSet.of("org.junit.runners.JUnit4")))); + } @Override public Description matchClass(ClassTree tree, VisitorState state) { - if (!JUNIT4_RUNNER.matches(tree, state)) { + if (!nonParameterizedRunner.matches(tree, state)) { return NO_MATCH; } if (TEST_PARAMETER_INJECTOR.matches(tree, state)) {