khmarbaise / maven-it-extension

Experimental JUnit Jupiter Extension for writing integration tests for Maven plugins/Maven extensions/Maven Core
https://khmarbaise.github.io/maven-it-extension/
Apache License 2.0
90 stars 30 forks source link

Helper methods on MavenProjectResult to get Model from sub-modules in multi-model project #298

Open JWT007 opened 1 year ago

JWT007 commented 1 year ago

I don't know if this is even possible but I thought I would throw it out there. :) Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is.

Currently it is possible to get the maven Model from the execution result.

result.getMavenProjectResult().getModel()

In a multi-module project, you can currently only get the path to a nested POM and use the MavenXpp3Reader to read it to get the nested Model.

assertThat(result)
        .isSuccessful()
        .satisfies(r -> {
          assertThat(new File(r.getMavenProjectResult().getTargetProjectDirectory(), "test-jar1/pom.xml"))
            .as("Jar1 POM file")
            .exists()
            .satisfies(f -> {
              assertThat(readPomModelFromFile(f))
                .as("Jar1 POM Model")
                .isNotNull()
                .extracting(Model::getGroupId, Model::getArtifactId, Model::getVersion, Model::getPackaging)
                .as("GAVP")
                .containsExactly("me.jwt007.maven.plugins.foobar.its", "foobar-jar1", "1.2.3", "jar");
            });
        });

  private Model readPomModelFromFile(final File pomFile) {
    Model model = null;
    try (FileInputStream fis = new FileInputStream(pomFile)) {
      model = (new MavenXpp3Reader()).read(fis);
    } catch (Exception ex) {
      fail("Unable to parse flattened POM model from: " + pomFile, ex);
    }
    assertNotNull(model);
    return model;
  }

Describe the solution you'd like A clear and concise description of what you want to happen.

If possible it would be great if there was a convenience function to get the Model of a sub-module in a multi-module project.

result.getMavenProjectResult().getModule("myjars").getModule("jar1").getModel()

.. or something along those lines. :).

It would reduce the test size by a few lines and eliminate the need for a helper function (at least in my code :P)

  assertThat(result)
        .isSuccessful()
        .satisfies(r -> {
          assertThat(r.getMavenProjectResult().getModule("test-jar1").getModel())
            .as("Jar1 POM Model")
            .isNotNull()
            .extracting(Model::getGroupId, Model::getArtifactId, Model::getVersion, Model::getPackaging)
            .as("GAVP")
            .containsExactly("me.jwt007.maven.plugins.foobar.its", "foobar-jar1", "1.2.3", "jar");
          });
JWT007 commented 1 year ago

Maybe this request makes no sense... sorry I am no assertj expert and am sort of learning by doing now.

I guess to get the Model you need to do the satisfies/assertThat to get at the "actual" where the Model lives. Since I am assuming you don't want to write AssertJ extensions for the whole Maven model. :)


But if I am not totally off :) (not the subject of this issue)...

It should be relatively easy to add the following to MavenProjectResultAssert since you are saving the Optional<MavenProjectResultAssert> parent.