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 should not (only) rename methods called from other methods #258

Closed
2 tasks done
timtebeek opened this issue Aug 31, 2022 · 7 comments
Closed
2 tasks done
Labels
bug Something isn't working

Comments

@timtebeek
Copy link
Contributor

timtebeek commented Aug 31, 2022

Quickly noted down here, can be tackled individually.

  • Should not rename test when there's a static method import of the same name (can lead to conflicts)
  • Should not (only) rename test method when called from other methods (which unfortunately happens)
@tkvangorder tkvangorder added the bug Something isn't working label Aug 31, 2022
@tkvangorder tkvangorder moved this to Backlog in OpenRewrite Aug 31, 2022
@nmck257
Copy link
Contributor

nmck257 commented Jan 4, 2024

Note: this issue is a bit more visible now that RemoveTestPrefix is included in JUnit5BestPractices: cb59832

@protocol7
Copy link
Contributor

Just to offer up a test case for the first issue listed above as we ran into this in our codebase:

  @Test
  void testRemoveTestPrefixWithClashingMethod() {
    rewriteRun(
        spec -> spec.recipe(new RemoveTestPrefix()),
        java(
            """
            package com.spotify.helloworld;

            import org.junit.jupiter.api.Test;
            import static java.util.List.of;

            public class FooTest {

                @Test
                void testOf() {
                  of();
                }
            }"""));
  }

I can put this test in a PR if that is preferable. Not sure I'd know how to fix it though.

@timtebeek
Copy link
Contributor Author

Thanks @protocol7 ; this one was easy it seems;

timtebeek added a commit that referenced this issue Jul 3, 2024
* Skip RemoveTestPrefix when calling a similarly named method

#258

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek changed the title Minor issues with RemoveTestPrefix RemoveTestPrefix should not rename methods called from other methods Jul 3, 2024
@timtebeek timtebeek changed the title RemoveTestPrefix should not rename methods called from other methods RemoveTestPrefix should not (only) rename methods called from other methods Jul 3, 2024
@timtebeek
Copy link
Contributor Author

We could maybe fix the remaining issue (within a single class) by switching to ChangeMethodName in a doAfterVisit instead of using the .withXyz() methods in this recipe directly. But we'd need to correctly construct the method pattern to match from the method declaration to do so, and even then it would only work in a single compilation unit, not across.

@protocol7
Copy link
Contributor

protocol7 commented Dec 12, 2024

Not sure if the same issue as discussed here, but here's an issue we're seeing when a test method is called by a different test method:

  @Test
  void removeTestPrefixWhenCalled() {
    rewriteRun(
        spec -> spec.recipe(new RemoveTestPrefix()),
        // language=java
        java(
            """
            package com.helloworld;

            import org.junit.jupiter.api.Test;

            public class FooTest {
              @Test
              void bar() {
                  testFoo();
              }

              @Test
              void testFoo() {}
            }
            """,
            """
            package com.helloworld;

            import org.junit.jupiter.api.Test;

            public class FooTest {
              @Test
              void bar() {
                  foo();
              }

              @Test
              void foo() {}
            }
            """));
  }

Unexpected result in "com/helloworld/FooTest.java":

diff --git a/com/helloworld/FooTest.java b/com/helloworld/FooTest.java
index f0cd70f..222415f 100644
--- a/com/helloworld/FooTest.java
+++ b/com/helloworld/FooTest.java
@@ -5,7 +5,7 @@
 public class FooTest {
   @Test
   void bar() {
-      foo();
+      testFoo();
   }

   @Test

In this case, either testFoo should not be renamed, or the invokation of it also needs to be renamed.

@timtebeek
Copy link
Contributor Author

Thanks for the runnable example @protocol7 ; That's indeed what I had intended with the second not-yet-crossed-off todo item here. I think we'll both agree it's not optimal or expected for test methods to also be invoked directly, but perhaps best to guard against that. This would be relatively straightforward for usages from within the same class; a little more effort for usages from other classes.

For the simplest case I think this still stands:

We could maybe fix the remaining issue (within a single class) by switching to ChangeMethodName in a doAfterVisit instead of using the .withXyz() methods in this recipe directly.

Did you already explore that using the runnable test you have?

@protocol7
Copy link
Contributor

protocol7 commented Dec 13, 2024

I have not tried to fix this one, and probably won't be able to in a near term future.

And, agree on this been a poor practice, but in large code bases, everything will appear at least once :)

@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Dec 13, 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
Archived in project
Development

No branches or pull requests

4 participants