Skip to content

Commit

Permalink
Include some more attractive-nuisance runners in TestParametersNotIni…
Browse files Browse the repository at this point in the history
…tialized.

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
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 3, 2023
1 parent c83ba54 commit 09a9aa6
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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<ClassTree, AnnotationTree> JUNIT4_RUNNER =
annotations(
AT_LEAST_ONE,
hasArgumentWithValue(
"value", isJUnit4TestRunnerOfType(ImmutableSet.of("org.junit.runners.JUnit4"))));
private final MultiMatcher<ClassTree, AnnotationTree> 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)) {
Expand Down

0 comments on commit 09a9aa6

Please sign in to comment.