From 8f1d1df747571337bc9a1c3b2cd1154b1e61b05e Mon Sep 17 00:00:00 2001 From: Bastien Diederichs Date: Thu, 23 Mar 2023 09:01:18 +0100 Subject: [PATCH] Introduce `BugCheckerRules` Refaster rule collection (#526) --- error-prone-contrib/pom.xml | 2 +- .../refasterrules/BugCheckerRules.java | 53 +++++++++++++++++++ .../bugpatterns/FluxFlatMapUsageTest.java | 1 - .../bugpatterns/IdentityConversionTest.java | 1 - .../StringCaseLocaleUsageTest.java | 1 - .../refasterrules/RefasterRulesTest.java | 1 + .../BugCheckerRulesTestInput.java | 29 ++++++++++ .../BugCheckerRulesTestOutput.java | 27 ++++++++++ 8 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index b16612d0e5..41f6b74e6d 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -37,7 +37,7 @@ ${groupId.error-prone} error_prone_test_helpers - test + provided ${project.groupId} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java new file mode 100644 index 0000000000..a65dff6783 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java @@ -0,0 +1,53 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.errorprone.BugCheckerRefactoringTestHelper; +import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChooser; +import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers; +import com.google.errorprone.refaster.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to {@link com.google.errorprone.bugpatterns.BugChecker} classes. */ +@OnlineDocumentation +final class BugCheckerRules { + private BugCheckerRules() {} + + /** + * Avoid calling {@link BugCheckerRefactoringTestHelper#setFixChooser(FixChooser)} or {@link + * BugCheckerRefactoringTestHelper#setImportOrder(String)} with their respective default values. + */ + static final class BugCheckerRefactoringTestHelperIdentity { + @BeforeTemplate + BugCheckerRefactoringTestHelper before(BugCheckerRefactoringTestHelper helper) { + return Refaster.anyOf( + helper.setFixChooser(FixChoosers.FIRST), helper.setImportOrder("static-first")); + } + + @AfterTemplate + BugCheckerRefactoringTestHelper after(BugCheckerRefactoringTestHelper helper) { + return helper; + } + } + + /** + * Prefer {@link BugCheckerRefactoringTestHelper.ExpectOutput#expectUnchanged()} over repeating + * the input. + */ + // XXX: This rule assumes that the full source code is specified as a single string, e.g. using a + // text block. Support for multi-line source code input would require a `BugChecker` + // implementation instead. + static final class BugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged { + @BeforeTemplate + BugCheckerRefactoringTestHelper before( + BugCheckerRefactoringTestHelper helper, String path, String source) { + return helper.addInputLines(path, source).addOutputLines(path, source); + } + + @AfterTemplate + BugCheckerRefactoringTestHelper after( + BugCheckerRefactoringTestHelper helper, String path, String source) { + return helper.addInputLines(path, source).expectUnchanged(); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsageTest.java index 410993f614..bce223a2b4 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsageTest.java @@ -67,7 +67,6 @@ void identification() { @Test void replacementFirstSuggestedFix() { BugCheckerRefactoringTestHelper.newInstance(FluxFlatMapUsage.class, getClass()) - .setFixChooser(FixChoosers.FIRST) .addInputLines( "A.java", "import reactor.core.publisher.Flux;", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java index 9341c76433..631acdad68 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java @@ -180,7 +180,6 @@ void identification() { @Test void replacementFirstSuggestedFix() { BugCheckerRefactoringTestHelper.newInstance(IdentityConversion.class, getClass()) - .setFixChooser(FixChoosers.FIRST) .addInputLines( "A.java", "import static com.google.errorprone.matchers.Matchers.staticMethod;", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StringCaseLocaleUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StringCaseLocaleUsageTest.java index b1865d6089..a3d087aef4 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StringCaseLocaleUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StringCaseLocaleUsageTest.java @@ -48,7 +48,6 @@ void identification() { @Test void replacementFirstSuggestedFix() { BugCheckerRefactoringTestHelper.newInstance(StringCaseLocaleUsage.class, getClass()) - .setFixChooser(FixChoosers.FIRST) .addInputLines( "A.java", "class A {", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index c60bb8b286..d1b461536c 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -35,6 +35,7 @@ final class RefasterRulesTest { AssertJThrowingCallableRules.class, AssortedRules.class, BigDecimalRules.class, + BugCheckerRules.class, CollectionRules.class, ComparatorRules.class, DoubleStreamRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java new file mode 100644 index 0000000000..49eaa57945 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java @@ -0,0 +1,29 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.BugCheckerRefactoringTestHelper; +import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers; +import com.google.errorprone.bugpatterns.BugChecker; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(FixChoosers.class); + } + + ImmutableSet testBugCheckerRefactoringTestHelperIdentity() { + return ImmutableSet.of( + BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()) + .setFixChooser(FixChoosers.FIRST), + BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()) + .setImportOrder("static-first")); + } + + BugCheckerRefactoringTestHelper + testBugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged() { + return BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()) + .addInputLines("A.java", "class A {}") + .addOutputLines("A.java", "class A {}"); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java new file mode 100644 index 0000000000..39aef89159 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java @@ -0,0 +1,27 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.BugCheckerRefactoringTestHelper; +import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers; +import com.google.errorprone.bugpatterns.BugChecker; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(FixChoosers.class); + } + + ImmutableSet testBugCheckerRefactoringTestHelperIdentity() { + return ImmutableSet.of( + BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()), + BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())); + } + + BugCheckerRefactoringTestHelper + testBugCheckerRefactoringTestHelperAddInputLinesExpectUnchanged() { + return BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()) + .addInputLines("A.java", "class A {}") + .expectUnchanged(); + } +}