Skip to content

Commit 485764e

Browse files
nmck257timtebeek
andauthored
fix an edge case in RemoveUnusedProperties when there is no parent in filtering (#6687)
* rewrite-maven/RemoveUnusedProperties: fixing an edge case which would throw if a singular/root pom (no parent in the in-memory relative path) included resource filtering * Minimize test --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent ae70bc3 commit 485764e

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/RemoveUnusedProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
238238
directory != null) {
239239
Path path = getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath();
240240
try {
241-
acc.filteredResourcePathsToDeclaringPoms.put(path.getParent().resolve(directory), getResolutionResult());
241+
acc.filteredResourcePathsToDeclaringPoms.put(path.resolveSibling(directory), getResolutionResult());
242242
} catch (InvalidPathException ignored) {
243243
} // fail quietly
244244
}

rewrite-maven/src/test/java/org/openrewrite/maven/RemoveUnusedPropertiesTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.params.ParameterizedTest;
2020
import org.junit.jupiter.params.provider.ValueSource;
2121
import org.openrewrite.DocumentExample;
22+
import org.openrewrite.Issue;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
2425

@@ -842,4 +843,55 @@ void removesIrrelevantPropertyDeclarationForFilteredResourceUsage() {
842843
)
843844
);
844845
}
846+
847+
@Issue("https://github.com/openrewrite/rewrite/pull/6687")
848+
@Test
849+
void removesUnusedPropertiesInSinglePomWithResourceFilteringEnabled() {
850+
rewriteRun(
851+
pomXml(
852+
"""
853+
<project>
854+
<modelVersion>4.0.0</modelVersion>
855+
<groupId>com.sample</groupId>
856+
<artifactId>test.application</artifactId>
857+
<version>1.0.0</version>
858+
<name>test-application</name>
859+
<packaging>jar</packaging>
860+
861+
<properties>
862+
<junk>junk</junk>
863+
</properties>
864+
865+
<build>
866+
<resources>
867+
<resource>
868+
<directory>src/main/resources</directory>
869+
<filtering>true</filtering>
870+
</resource>
871+
</resources>
872+
</build>
873+
</project>
874+
""",
875+
"""
876+
<project>
877+
<modelVersion>4.0.0</modelVersion>
878+
<groupId>com.sample</groupId>
879+
<artifactId>test.application</artifactId>
880+
<version>1.0.0</version>
881+
<name>test-application</name>
882+
<packaging>jar</packaging>
883+
884+
<build>
885+
<resources>
886+
<resource>
887+
<directory>src/main/resources</directory>
888+
<filtering>true</filtering>
889+
</resource>
890+
</resources>
891+
</build>
892+
</project>
893+
"""
894+
)
895+
);
896+
}
845897
}

0 commit comments

Comments
 (0)