openrewrite / openrewrite/rewrite

RewriteTest - pattern to validate recipes making non-content changes

Open
#3,058 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement test provided
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

consider a case like this:

@SuppressWarnings("DataFlowIssue")
    @ExpectedToFail("fails because there's a reference change but no content diff but that's the point; would need to adjust RewriteTest")
    @Test
    void changeTypeAttributionImplicitUsage() {
        rewriteRun(
          spec -> spec.recipe(new ChangeType("java.util.List", "java.util.ArrayList", false)),
          groovy(
            """
            import java.util.Collections
              
            class Test {
                int zero = Collections.emptyList().size()
            }
            """,
            spec -> spec.afterRecipe(cu -> {
                J.VariableDeclarations varDecl = (J.VariableDeclarations) cu.getClasses().get(0).getBody().getStatements().get(0);
                J.MethodInvocation sizeMi = (J.MethodInvocation) varDecl.getVariables().get(0).getInitializer();
                assertThat(TypeUtils.isOfClassType(sizeMi.getMethodType().getDeclaringType(),
                  "java.util.ArrayList")).isTrue();
                J.MethodInvocation emptyListMi = (J.MethodInvocation) sizeMi.getSelect();
                assertThat(TypeUtils.isOfClassType(emptyListMi.getMethodType().getReturnType(),
                  "java.util.ArrayList")).isTrue();
            })
          )
        );
    }

The recipe makes changes to type attribution in the tree, but, that's not a "visible" change, so the rewriteRun logic auto-fails the test (due to the condition which checks for reference changes without content changes). It would be nice to have some mechanism to run tests like this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in RewriteTest's rewriteRun logic, especially the condition that rejects reference changes without content changes, and compare it with the changeTypeAttributionImplicitUsage example. Define a test mechanism that permits assertions on type attribution when source text is unchanged, then run that test to verify it no longer auto-fails while still checking the attribution assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.