Skip to content

Commit

Permalink
Prevent likely static import class with `FailWithMessage{,AndThrowabl…
Browse files Browse the repository at this point in the history
…e}` Refaster rules

This is a workaround for google/error-prone#3584. While there, drop an
unused method from `JUnitToAssertJRules`.
  • Loading branch information
Stephan202 committed Dec 24, 2023
1 parent 0109632 commit 88df557
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down Expand Up @@ -44,21 +43,6 @@
final class JUnitToAssertJRules {
private JUnitToAssertJRules() {}

public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
Assertions.class,
assertDoesNotThrow(() -> null),
assertInstanceOf(null, null),
assertThrows(null, null),
assertThrowsExactly(null, null),
(Runnable) () -> assertFalse(true),
(Runnable) () -> assertNotNull(null),
(Runnable) () -> assertNotSame(null, null),
(Runnable) () -> assertNull(null),
(Runnable) () -> assertSame(null, null),
(Runnable) () -> assertTrue(true));
}

static final class ThrowNewAssertionError {
@BeforeTemplate
void before() {
Expand All @@ -78,8 +62,13 @@ T before(String message) {
return Assertions.fail(message);
}

// XXX: Add `@UseImportPolicy(STATIC_IMPORT_ALWAYS)` once
// https://github.com/google/error-prone/pull/3584 is resolved. Until that time, statically
// importing AssertJ's `fail` is likely to clash with an existing static import of JUnit's
// `fail`. Note that combining Error Prone's `RemoveUnusedImports` and
// `UnnecessarilyFullyQualified` checks and our `StaticImport` check will anyway cause the
// method to be imported statically if possible; just in a less efficient manner.
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
T after(String message) {
return fail(message);
}
Expand All @@ -91,8 +80,13 @@ T before(String message, Throwable throwable) {
return Assertions.fail(message, throwable);
}

// XXX: Add `@UseImportPolicy(STATIC_IMPORT_ALWAYS)` once
// https://github.com/google/error-prone/pull/3584 is resolved. Until that time, statically
// importing AssertJ's `fail` is likely to clash with an existing static import of JUnit's
// `fail`. Note that combining Error Prone's `RemoveUnusedImports` and
// `UnnecessarilyFullyQualified` checks and our `StaticImport` check will anyway cause the
// method to be imported statically if possible; just in a less efficient manner.
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
T after(String message, Throwable throwable) {
return fail(message, throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
Expand Down Expand Up @@ -41,11 +40,11 @@ void testThrowNewAssertionError() {
}

Object testFailWithMessage() {
return fail("foo");
return org.assertj.core.api.Assertions.fail("foo");
}

Object testFailWithMessageAndThrowable() {
return fail("foo", new IllegalStateException());
return org.assertj.core.api.Assertions.fail("foo", new IllegalStateException());
}

void testFailWithThrowable() {
Expand Down

0 comments on commit 88df557

Please sign in to comment.