Skip to content

Commit 245278a

Browse files
authored
Make AddProfile idempotent by comparing existing profile content (#7355) (#7356)
AddProfile unconditionally removed and re-added a profile with the same id on every recipe cycle. Use SemanticallyEqual to compare the existing profile to the desired one and return early when they already match.
1 parent 317e4b9 commit 245278a

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.TreeVisitor;
2525
import org.openrewrite.xml.AddToTagVisitor;
2626
import org.openrewrite.xml.RemoveContentVisitor;
27+
import org.openrewrite.xml.SemanticallyEqual;
2728
import org.openrewrite.xml.tree.Xml;
2829

2930
import java.util.Optional;
@@ -84,29 +85,32 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
8485
profiles = t.getChild("profiles").get();
8586
}
8687

88+
Xml.Tag profileTag = Xml.Tag.build("<profile>\n" +
89+
"<id>" + id + "</id>\n" +
90+
(activation != null ? activation.trim() + "\n" : "") +
91+
(properties != null ? properties.trim() + "\n" : "") +
92+
(build != null ? build.trim() + "\n" : "") +
93+
"</profile>");
94+
8795
Optional<Xml.Tag> maybeProfile = profiles.getChildren().stream()
8896
.filter(profile ->
8997
profile.getChildValue("id").get().equals(id)
9098
)
9199
.findAny();
92100

93101
if (maybeProfile.isPresent()) {
94-
Xml.Tag profile = maybeProfile.get();
95-
96-
t = (Xml.Tag) new RemoveContentVisitor(profile, false, false).visitNonNull(t, ctx, getCursor().getParentOrThrow());
97-
102+
Xml.Tag existing = maybeProfile.get();
103+
if (SemanticallyEqual.areEqual(existing, profileTag)) {
104+
return t;
105+
}
106+
t = (Xml.Tag) new RemoveContentVisitor(existing, false, false).visitNonNull(t, ctx, getCursor().getParentOrThrow());
98107
}
99-
Xml.Tag profileTag = Xml.Tag.build("<profile>\n" +
100-
"<id>" + id + "</id>\n" +
101-
(activation != null ? activation.trim() + "\n" : "") +
102-
(properties != null ? properties.trim() + "\n" : "") +
103-
(build != null ? build.trim() + "\n" : "") +
104-
"</profile>");
105108
t = (Xml.Tag) new AddToTagVisitor<>(profiles, profileTag).visitNonNull(t, ctx, getCursor().getParentOrThrow());
106109

107110
}
108111

109112
return t;
110113
}
114+
111115
}
112116
}

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AddProfileTest implements RewriteTest {
2929
@Test
3030
void addProfileToPom() {
3131
rewriteRun(
32-
spec -> spec.expectedCyclesThatMakeChanges(2).recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
32+
spec -> spec.recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
3333
"<properties><bar>bar</bar></properties>", "<build><param>value</param></build>")),
3434
pomXml(
3535
"""
@@ -69,7 +69,7 @@ void addProfileToPom() {
6969
@Test
7070
void preExistingOtherProfile() {
7171
rewriteRun(
72-
spec -> spec.expectedCyclesThatMakeChanges(2).recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
72+
spec -> spec.recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
7373
"<properties><bar>bar</bar></properties>", "<build><param>value</param></build>")),
7474
pomXml(
7575
"""
@@ -122,7 +122,7 @@ void preExistingOtherProfile() {
122122
@Test
123123
void preExistingMatchingProfile() {
124124
rewriteRun(
125-
spec -> spec.expectedCyclesThatMakeChanges(2).recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
125+
spec -> spec.recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
126126
"<properties><bar>bar</bar></properties>", "<build><param>value</param></build>")),
127127
pomXml(
128128
"""
@@ -169,6 +169,37 @@ void preExistingMatchingProfile() {
169169
}
170170

171171

172+
@Test
173+
void preExistingIdenticalProfile() {
174+
rewriteRun(
175+
spec -> spec.recipe(new AddProfile("myprofile", "<activation><foo>foo</foo></activation>",
176+
"<properties><bar>bar</bar></properties>", "<build><param>value</param></build>")),
177+
pomXml(
178+
"""
179+
<project>
180+
<groupId>group</groupId>
181+
<artifactId>artifact</artifactId>
182+
<version>1</version>
183+
<profiles>
184+
<profile>
185+
<id>myprofile</id>
186+
<activation>
187+
<foo>foo</foo>
188+
</activation>
189+
<properties>
190+
<bar>bar</bar>
191+
</properties>
192+
<build>
193+
<param>value</param>
194+
</build>
195+
</profile>
196+
</profiles>
197+
</project>
198+
"""
199+
)
200+
);
201+
}
202+
172203
@Test
173204
void notAPom() {
174205
rewriteRun(

0 commit comments

Comments
 (0)