Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoveTestPrefix does not check if a similarly named static import exists #569

Open
blipper opened this issue Aug 9, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@blipper
Copy link

blipper commented Aug 9, 2024

What version of OpenRewrite are you using?

Latest

How are you running OpenRewrite?

gradle

What is the smallest, simplest way to reproduce the problem?

import static Bar.foo; 
@Test
class A {
    void testFoo() {
    }
}

What did you expect to see?

@Test
class A {
    void testFoo() {
    }
}

What did you see instead?

@Test
class A {
    void foo() {
    }
}

What is the full stack trace of any errors you encountered?

Compilation will fail

Are you interested in contributing a fix to OpenRewrite?

Yes

@blipper blipper added the bug Something isn't working label Aug 9, 2024
@timtebeek
Copy link
Contributor

Thanks for the clear example and offer to help @blipper! Must say it's appreciated how diligently you're reporting issues you come across. Really helps fix any blind spots.

Your examples would fit right in as a unit test, similar to this earlier one:

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/258")
void removeTestPrefixWithClashingMethod() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static java.util.List.of;
class FooTest {
@Test
void testOf() {
of();
}
}
"""
)
);
}

Once there's a test we will likely want to add a similar exemption as seen here:

// Skip when calling a similarly named method
AtomicBoolean skip = new AtomicBoolean(false);
new JavaIsoVisitor<AtomicBoolean>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
if (method.getName().getSimpleName().equals(newMethodName)) {
skip.set(true);
}
return super.visitMethodInvocation(method, atomicBoolean);
}
}.visitMethodDeclaration(m, skip);
if (skip.get()) {
return m;
}

Your exact example might already work if the name of the static imported method matches the renamed test; but with a few small adjustments I'm sure we can find a way to fail and then fix the issue.

@timtebeek timtebeek changed the title https://github.com/openrewrite/rewrite-testing-frameworks/blob/main/src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java does not check if a similarly named static import exists! RemoveTestPrefix does not check if a similarly named static import exists Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

2 participants