openrewrite / rewrite-testing-frameworks

OpenRewrite recipes that perform common Java testing migration tasks.
Apache License 2.0
68 stars 57 forks source link

Is there a way to simulate a real Project in the rewrite-testing framework? #535

Closed SilasSchaprian closed 1 week ago

SilasSchaprian commented 1 week ago

Hey sorry for opening a bug but i was not able to find a better place to ask my question.

I am authoring a recipe which does some "pre-refactoring checks" because i have to check a yaml key. Depending on the yaml key i am starting a recipe meant for another (xml)-file than the yaml SourceSpec. Is there a way to simulate a real Project in the testing Framework or preparing multiple files for the same run of the Recipe (as it behaves in the maven plugin)?

MBoegers commented 1 week ago

Hi @SilasSchaprian, a good way to get direct support is the Open Rewrite Community Slack, but we provide help here as well :)

You can write a test which works on multiple files (like java and xml) like in ParameterizedRunnerToParameterizedTest see https://docs.openrewrite.org/authoring-recipes/recipe-testing for more information. Or you could publish the artifact localy and run it against one of your projects.

MBoegers commented 1 week ago

Maybe you can add some context and describe your goal a little bit more. Than we can provide a more tailored answer

SilasSchaprian commented 1 week ago

Hey @MBoegers thanks for the answer :)

My Problem is that i want to author a recipe that reads a certain key from a config.yml in the project and depending on the value, i have to execute another Recipe with different parameters. So i dont think the ParameterizedRunner will help (if i understood it correctly)

Having a test case for the problem would be cool because the debugger can be helpful while authoring a recipe.

MBoegers commented 1 week ago

Okay in this case could define the yaml next to the Java expectation in the rewriteRun method call.

SilasSchaprian commented 1 week ago

Thank you! I am going to give it a try :)

MBoegers commented 1 week ago

It should look similar to this, sorry I have no IDE at hand. I think you can find everything in this repository and in the rewrite-yaml project.

If you encounter any further problems feel free to ask 😊

class MyTest implements RewriteTest {
  @Test
  void test(){
    rewriteRun(
      yml("""
       yml: here
       """),
      java("""
       //original Java
       """, """
       //expected Java
       """)
    )
  }
}
SilasSchaprian commented 1 week ago

Hey @MBoegers i know that this is kind of another question but since i already opened this ticket I thougt it would be okay to aks here..

For my Recipe to work i Need to run a recipe over the whole project before my others are allowed to.

@Override
    public List<Recipe> getRecipeList() {
        RecipeA recipeA = new REcipeA();
        return List.of(
                recipeA,
                new RecipeB(recipeA),
                new RecipeC()
        );
    }

in recipeA I have a property I need to access in B.

Is there a way to ensure recipeA was run for the whole Project instead of being run first for the current LST?

I saw the preconditions in the rewrite.yml but did not found anything in the Recipe class.

I hope my question is understandable and if this is the wrong place i can ask it somewhere else :)

MBoegers commented 1 week ago

You can define preconditions in Java recipes as well. To do so just wrap your Visitor instance in the getVisitor method. See AddManagedDependency Line 159 for example.

knutwannheden commented 1 week ago

It sounds like you may want to have a ScanningRecipe which processes all sources in two phases, so that you in the first phase can scan for a certain occurrence and then in the second phase act upon it.

SilasSchaprian commented 1 week ago

@knutwannheden @MBoegers awesome i will look into those options. Thank you!!

SilasSchaprian commented 1 week ago

@knutwannheden that worked for me.. Thank you!