Skip to content

Commit fbc70c6

Browse files
expectedWithUrls helper for Gradle tests
1 parent 1f30b54 commit fbc70c6

4 files changed

Lines changed: 166 additions & 76 deletions

File tree

rewrite-gradle/src/test/java/org/openrewrite/gradle/search/EffectiveGradlePluginRepositoriesTest.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import org.openrewrite.test.RecipeSpec;
2121
import org.openrewrite.test.RewriteTest;
2222

23+
import java.util.List;
24+
2325
import static org.assertj.core.api.Assertions.assertThat;
2426
import static org.openrewrite.gradle.Assertions.*;
27+
import static org.openrewrite.gradle.search.EffectiveRepositoryAssertions.expectedWithUrls;
2528
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
2629
import static org.openrewrite.maven.search.EffectiveMavenRepositoriesTable.Row;
2730

@@ -41,21 +44,21 @@ void pluginRepositories() {
4144
"""
4245
rootProject.name = 'my-project'
4346
""",
44-
"""
45-
/*~~(https://plugins.gradle.org/m2)~~>*/rootProject.name = 'my-project'
46-
"""
47+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://plugins.gradle.org/m2"), """
48+
rootProject.name = 'my-project'
49+
"""))
4750
),
4851
buildGradle(
4952
"""
5053
plugins {
5154
id 'java'
5255
}
5356
""",
54-
"""
55-
/*~~(https://plugins.gradle.org/m2)~~>*/plugins {
57+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://plugins.gradle.org/m2"), """
58+
plugins {
5659
id 'java'
5760
}
58-
"""
61+
"""))
5962
)
6063
);
6164
}
@@ -72,28 +75,28 @@ void multiplePluginRepositoriesInSettingsGradle() {
7275
}
7376
}
7477
""",
75-
"""
76-
/*~~(https://repo.spring.io/milestone
77-
https://plugins.gradle.org/m2)~~>*/pluginManagement {
78+
spec -> spec.after(actual -> expectedWithUrls(actual,
79+
List.of("https://repo.spring.io/milestone", "https://plugins.gradle.org/m2"), """
80+
pluginManagement {
7881
repositories {
7982
maven { url 'https://repo.spring.io/milestone' }
8083
gradlePluginPortal()
8184
}
8285
}
83-
"""
86+
"""))
8487
),
8588
buildGradle(
8689
"""
8790
plugins {
8891
id 'java'
8992
}
9093
""",
91-
"""
92-
/*~~(https://repo.spring.io/milestone
93-
https://plugins.gradle.org/m2)~~>*/plugins {
94+
spec -> spec.after(actual -> expectedWithUrls(actual,
95+
List.of("https://repo.spring.io/milestone", "https://plugins.gradle.org/m2"), """
96+
plugins {
9497
id 'java'
9598
}
96-
"""
99+
"""))
97100
)
98101
);
99102
}
@@ -109,25 +112,25 @@ void pluginRepositoryInSettingsGradleKts() {
109112
}
110113
}
111114
""",
112-
"""
113-
/*~~(https://repo.spring.io/milestone)~~>*/pluginManagement {
115+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
116+
pluginManagement {
114117
repositories {
115118
maven { url = uri("https://repo.spring.io/milestone") }
116119
}
117120
}
118-
"""
121+
"""))
119122
),
120123
buildGradleKts(
121124
"""
122125
plugins {
123126
id("java")
124127
}
125128
""",
126-
"""
127-
/*~~(https://repo.spring.io/milestone)~~>*/plugins {
129+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
130+
plugins {
128131
id("java")
129132
}
130-
"""
133+
"""))
131134
)
132135
);
133136
}
@@ -137,7 +140,7 @@ void producesDataTable() {
137140
rewriteRun(
138141
spec -> spec
139142
.recipe(new EffectiveGradlePluginRepositories(false))
140-
.dataTable(Row.class, rows -> assertThat(rows).containsExactlyInAnyOrder(
143+
.dataTable(Row.class, rows -> assertThat(rows).contains(
141144
new Row("settings.gradle", "https://repo.spring.io/milestone"),
142145
new Row("settings.gradle", "https://plugins.gradle.org/m2"),
143146
new Row("build.gradle", "https://repo.spring.io/milestone"),
@@ -208,9 +211,9 @@ void buildscriptRepositoriesInBuildGradle() {
208211
id 'java'
209212
}
210213
""",
211-
"""
212-
/*~~(https://repo.spring.io/milestone
213-
https://plugins.gradle.org/m2)~~>*/buildscript {
214+
spec -> spec.after(actual -> expectedWithUrls(actual,
215+
List.of("https://repo.spring.io/milestone", "https://plugins.gradle.org/m2"), """
216+
buildscript {
214217
repositories {
215218
maven { url 'https://repo.spring.io/milestone' }
216219
}
@@ -219,7 +222,7 @@ void buildscriptRepositoriesInBuildGradle() {
219222
plugins {
220223
id 'java'
221224
}
222-
"""
225+
"""))
223226
)
224227
);
225228
}
@@ -239,9 +242,9 @@ void buildscriptRepositoriesInBuildGradleKts() {
239242
id("java")
240243
}
241244
""",
242-
"""
243-
/*~~(https://repo.spring.io/milestone
244-
https://plugins.gradle.org/m2)~~>*/buildscript {
245+
spec -> spec.after(actual -> expectedWithUrls(actual,
246+
List.of("https://repo.spring.io/milestone", "https://plugins.gradle.org/m2"), """
247+
buildscript {
245248
repositories {
246249
maven { url = uri("https://repo.spring.io/milestone") }
247250
}
@@ -250,7 +253,7 @@ void buildscriptRepositoriesInBuildGradleKts() {
250253
plugins {
251254
id("java")
252255
}
253-
"""
256+
"""))
254257
)
255258
);
256259
}
@@ -268,15 +271,15 @@ void doesNotAffectProjectRepositoriesInBuildGradle() {
268271
maven { url 'https://repo.spring.io/milestone' }
269272
}
270273
""",
271-
"""
272-
/*~~(https://plugins.gradle.org/m2)~~>*/plugins {
274+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://plugins.gradle.org/m2"), """
275+
plugins {
273276
id 'java'
274277
}
275278
276279
repositories {
277280
maven { url 'https://repo.spring.io/milestone' }
278281
}
279-
"""
282+
"""))
280283
)
281284
);
282285
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/search/EffectiveGradleRepositoriesTest.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
package org.openrewrite.gradle.search;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
1920
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
2223

24+
import java.util.List;
25+
2326
import static org.assertj.core.api.Assertions.assertThat;
2427
import static org.openrewrite.gradle.Assertions.*;
28+
import static org.openrewrite.gradle.search.EffectiveRepositoryAssertions.expectedWithUrls;
2529
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
2630
import static org.openrewrite.maven.search.EffectiveMavenRepositoriesTable.Row;
2731

@@ -47,20 +51,22 @@ void repositoryInBuildGradle() {
4751
maven { url 'https://repo.spring.io/milestone' }
4852
}
4953
""",
50-
"""
51-
/*~~(https://repo.spring.io/milestone)~~>*/plugins {
54+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
55+
plugins {
5256
id 'java'
5357
}
5458
5559
repositories {
5660
maven { url 'https://repo.spring.io/milestone' }
5761
}
58-
"""
62+
"""))
5963
)
6064
);
6165
}
6266

6367
@Test
68+
@DisabledIfEnvironmentVariable(named = "REWRITE_GRADLE_MIRROR_URL", matches = ".+",
69+
disabledReason = "An injected mirror adds a repository, defeating the empty-repositories scenario.")
6470
void emptyRepositories() {
6571
rewriteRun(
6672
buildGradle(
@@ -87,17 +93,17 @@ void multipleRepositoriesInBuildGradle() {
8793
mavenCentral()
8894
}
8995
""",
90-
"""
91-
/*~~(https://repo.spring.io/milestone
92-
https://repo.maven.apache.org/maven2/)~~>*/plugins {
96+
spec -> spec.after(actual -> expectedWithUrls(actual,
97+
List.of("https://repo.spring.io/milestone", "https://repo.maven.apache.org/maven2/"), """
98+
plugins {
9399
id 'java'
94100
}
95101
96102
repositories {
97103
maven { url 'https://repo.spring.io/milestone' }
98104
mavenCentral()
99105
}
100-
"""
106+
"""))
101107
)
102108
);
103109
}
@@ -115,15 +121,15 @@ void repositoryInBuildGradleKts() {
115121
maven { url = uri("https://repo.spring.io/milestone") }
116122
}
117123
""",
118-
"""
119-
/*~~(https://repo.spring.io/milestone)~~>*/plugins {
124+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
125+
plugins {
120126
id("java")
121127
}
122128
123129
repositories {
124130
maven { url = uri("https://repo.spring.io/milestone") }
125131
}
126-
"""
132+
"""))
127133
)
128134
);
129135
}
@@ -146,11 +152,11 @@ void repositoryInSettingsGradle() {
146152
id 'java'
147153
}
148154
""",
149-
"""
150-
/*~~(https://repo.spring.io/milestone)~~>*/plugins {
155+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
156+
plugins {
151157
id 'java'
152158
}
153-
"""
159+
"""))
154160
)
155161
);
156162
}
@@ -173,11 +179,11 @@ void repositoryInSettingsGradleKts() {
173179
id("java")
174180
}
175181
""",
176-
"""
177-
/*~~(https://repo.spring.io/milestone)~~>*/plugins {
182+
spec -> spec.after(actual -> expectedWithUrls(actual, List.of("https://repo.spring.io/milestone"), """
183+
plugins {
178184
id("java")
179185
}
180-
"""
186+
"""))
181187
)
182188
);
183189
}
@@ -187,12 +193,14 @@ void producesDataTable() {
187193
rewriteRun(
188194
spec -> spec
189195
.recipe(new EffectiveGradleRepositories(false))
190-
.dataTable(Row.class, rows -> assertThat(rows).containsExactlyInAnyOrder(
191-
new Row("build.gradle", "https://repo.spring.io/milestone"),
192-
new Row("build.gradle", "https://repo.maven.apache.org/maven2/"),
193-
new Row("module/build.gradle", "https://repo.spring.io/milestone"),
194-
new Row("module/build.gradle", "https://repo.maven.apache.org/maven2/")
195-
)),
196+
.dataTable(Row.class, rows -> {
197+
assertThat(rows).contains(
198+
new Row("build.gradle", "https://repo.spring.io/milestone"),
199+
new Row("build.gradle", "https://repo.maven.apache.org/maven2/"),
200+
new Row("module/build.gradle", "https://repo.spring.io/milestone"),
201+
new Row("module/build.gradle", "https://repo.maven.apache.org/maven2/")
202+
);
203+
}),
196204
settingsGradle(
197205
"""
198206
pluginManagement {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.gradle.search;
17+
18+
import java.util.List;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
final class EffectiveRepositoryAssertions {
25+
26+
private static final Pattern COMMENT = Pattern.compile("/\\*~~\\(([^)]*)\\)~~>\\*/");
27+
28+
private EffectiveRepositoryAssertions() {
29+
}
30+
31+
/**
32+
* Builds the expected output for tests of recipes that annotate Gradle scripts with the list
33+
* of effective repository URLs. Extracts the leading {@code /*~~(URL[\nURL...])~~>*\/} comment
34+
* from {@code actual}, asserts that every URL listed in {@code mustInclude} is present, and
35+
* returns the extracted comment concatenated with {@code body}. Allows mirror entries
36+
* prepended at runtime (e.g. an Artifactory cache configured via init script in CI) to coexist
37+
* with the URLs the test cares about.
38+
*
39+
* <p>If {@code mustInclude} is empty, the comment is optional: when the actual output has no
40+
* comment (e.g. local run with no mirror configured and no repositories declared in the
41+
* source), {@code body} is returned unchanged.
42+
*/
43+
static String expectedWithUrls(String actual, List<String> mustInclude, String body) {
44+
Matcher m = COMMENT.matcher(actual);
45+
if (!m.find()) {
46+
assertThat(mustInclude)
47+
.as("expected URLs in effective-repositories comment, but no comment present in:%n%s", actual)
48+
.isEmpty();
49+
return body;
50+
}
51+
String urlsBlock = m.group(1);
52+
for (String url : mustInclude) {
53+
assertThat(urlsBlock)
54+
.as("URL %s missing from effective-repositories comment %s", url, m.group())
55+
.contains(url);
56+
}
57+
return m.group() + body;
58+
}
59+
}

0 commit comments

Comments
 (0)