From 1f36d4d459798481682cb6337246f24c2285c07d Mon Sep 17 00:00:00 2001 From: Samuel Cox Date: Sat, 2 Nov 2024 09:59:40 -0500 Subject: [PATCH] Tweaks to test and add test showing issue with settings active profiles --- .../openrewrite/maven/MavenParserTest.java | 142 ++++++++++-------- 1 file changed, 78 insertions(+), 64 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java index 37594beb20a..66968f86bc2 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java @@ -24,10 +24,12 @@ import okhttp3.mockwebserver.RecordedRequest; import okhttp3.tls.HandshakeCertificates; import okhttp3.tls.HeldCertificate; +import org.junit.Assert; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.openrewrite.ExecutionContext; import org.openrewrite.HttpSenderExecutionContextView; import org.openrewrite.InMemoryExecutionContext; @@ -41,6 +43,7 @@ import org.openrewrite.test.TypeValidation; import org.openrewrite.test.SourceSpec; import org.openrewrite.tree.ParseError; +import org.opentest4j.AssertionFailedError; import java.io.IOException; import java.net.InetAddress; @@ -53,8 +56,8 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.openrewrite.java.Assertions.mavenProject; import static org.openrewrite.maven.Assertions.pomXml; @@ -1271,11 +1274,8 @@ void activeByDefaultWithoutPomLocalActiveProfile() { final String activeProfile = "foobar"; rewriteRun(recipeSpec -> { final ExecutionContext context = new InMemoryExecutionContext(); - // I don't think this is having the effect I want:( - recipeSpec.executionContext( - MavenExecutionContextView.view(context) - .setActiveProfiles(Collections.singletonList(activeProfile)) - ); + recipeSpec.executionContext(MavenExecutionContextView.view(context)); + recipeSpec.parser(MavenParser.builder().activeProfiles(activeProfile)); }, mavenProject("c", pomXml( @@ -1325,9 +1325,7 @@ void activeByDefaultWithoutPomLocalActiveProfile() { - """, spec -> { - ((MavenParser.Builder) spec.getParser()).activeProfiles(activeProfile); - } + """ ) ), mavenProject("a", @@ -1354,7 +1352,6 @@ void activeByDefaultWithoutPomLocalActiveProfile() { """, spec -> { - ((MavenParser.Builder) spec.getParser()).activeProfiles(activeProfile); spec.afterRecipe(pomXml -> { final Map> deps = pomXml.getMarkers() @@ -1407,31 +1404,28 @@ void activeByDefaultWithoutPomLocalActiveProfile() { @Issue("https://github.com/openrewrite/rewrite/issues/4269") @DisplayName("activeByDefault=true profiles from a POM should not be active" + - " if deactivated from the command-line") + " if there is another active profile _from the same POM file_") @Test - @Disabled - void activeByDefaultWithProfileDisabled() { - expectMavenDownloadingException("!active-profile-1"); - expectMavenDownloadingException("-active-profile-1"); + void activeByDefaultWithPomLocalActiveProfile() { + expectMavenDownloadingException("active-profile-1"); } - @Issue("https://github.com/openrewrite/rewrite/issues/4269") @DisplayName("activeByDefault=true profiles from a POM should not be active" + - " if there is another active profile _from the same POM file_") + " if deactivated from the command-line") @Test - void activeByDefaultWithPomLocalActiveProfile() { - expectMavenDownloadingException("active-profile-1"); + @Disabled + void activeByDefaultWithProfileDisabled() { + expectMavenDownloadingException("!active-profile-1"); + expectMavenDownloadingException("-active-profile-1"); } private void expectMavenDownloadingException(final String activeProfile) { - rewriteRun(recipeSpec -> { + Executable fn = () -> rewriteRun(recipeSpec -> { final ExecutionContext context = new InMemoryExecutionContext(); // I don't think this is having the effect I want:( - recipeSpec.executionContext( - MavenExecutionContextView.view(context) - .setActiveProfiles(Collections.singletonList(activeProfile)) - ); + recipeSpec.executionContext(MavenExecutionContextView.view(context)); + recipeSpec.parser(MavenParser.builder().activeProfiles(activeProfile)); }, mavenProject("c", pomXml( @@ -1481,9 +1475,7 @@ private void expectMavenDownloadingException(final String activeProfile) { - """, spec -> { - ((MavenParser.Builder) spec.getParser()).activeProfiles(activeProfile); - } + """ ) ), mavenProject("a", @@ -1509,42 +1501,7 @@ private void expectMavenDownloadingException(final String activeProfile) { - """, - spec -> { - ((MavenParser.Builder) spec.getParser()).activeProfiles(activeProfile); - spec.afterRecipe(pomXml -> { - final ParseExceptionResult parseExceptionResult = - pomXml.getMarkers() - .findFirst(ParseExceptionResult.class) - .orElseThrow(); - - assertThat(parseExceptionResult.getExceptionType()) - .isEqualTo("MavenDownloadingExceptions"); - - assertThat(parseExceptionResult.getMessage().trim()).isEqualToIgnoringWhitespace(""" - - - org.openrewrite.maven - parent - 0.1.0-SNAPSHOT - - - org.openrewrite.maven - a - - - org.openrewrite.maven - d - - - org.openrewrite.maven - e - - - - """); - }); - } + """ ) ), mavenProject("d", @@ -1572,6 +1529,63 @@ private void expectMavenDownloadingException(final String activeProfile) { ) ) ); + + final AssertionFailedError err = assertThrows(AssertionFailedError.class, fn); + assertThat(err.getMessage()).contains("Problem parsing a/pom.xml"); // brittle:(, but class above is broad + } + + @Issue("TODO: create issue") + @Test + @Disabled + void settingsActiveProfiles() throws IOException { + var mavenCtx = MavenExecutionContextView.view(new InMemoryExecutionContext(t -> { + throw new RuntimeException(t); + })); + var settings = MavenSettings.parse(Parser.Input.fromString(Paths.get("settings.xml"), + """ + + + foo + + + """ + ), mavenCtx); + + mavenCtx.setMavenSettings(settings); + + rewriteRun(recipeSpec -> recipeSpec.executionContext(mavenCtx), + pomXml(""" + + some.group + some.artifact + 1-SNAPSHOT + + + commons-io + commons-io + + + + + foo + + 2.11.0 + + + + + commons-io + commons-io + ${commons-io.version} + + + + + + + """ + ) + ); } }