Skip to content

Commit 533b8eb

Browse files
committed
Add Kotlin DSL variable downgrade tests for UpgradePluginVersion
Add tests covering the scenario from customer-requests#1809 where UpgradePluginVersion with an exact version could downgrade plugins when versions are stored in a Kotlin DSL val variable shared across multiple plugin declarations. Also replace assert statements with proper null checks in the scanner for the GString and Identifier variable branches to avoid silent null storage when assertions are disabled.
1 parent daf9e11 commit 533b8eb

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/UpgradePluginVersion.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,20 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
188188
String resolvedPluginVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
189189
.select(new GroupArtifact(pluginId, pluginId + ".gradle.plugin"), "classpath", newVersion, versionPattern, ctx);
190190

191-
acc.versionPropNameToPluginId.put(versionVariableName, pluginId);
192-
assert resolvedPluginVersion != null;
193-
acc.pluginIdToNewVersion.put(pluginId, resolvedPluginVersion);
191+
if (resolvedPluginVersion != null) {
192+
acc.versionPropNameToPluginId.put(versionVariableName, pluginId);
193+
acc.pluginIdToNewVersion.put(pluginId, resolvedPluginVersion);
194+
}
194195
} else if (versionArgs.get(0) instanceof J.Identifier) {
195196
J.Identifier identifier = (J.Identifier) versionArgs.get(0);
196197
String versionVariableName = identifier.getSimpleName();
197198
String resolvedPluginVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
198199
.select(new GroupArtifact(pluginId, pluginId + ".gradle.plugin"), "classpath", newVersion, versionPattern, ctx);
199200

200-
acc.versionPropNameToPluginId.put(versionVariableName, pluginId);
201-
assert resolvedPluginVersion != null;
202-
acc.pluginIdToNewVersion.put(pluginId, resolvedPluginVersion);
201+
if (resolvedPluginVersion != null) {
202+
acc.versionPropNameToPluginId.put(versionVariableName, pluginId);
203+
acc.pluginIdToNewVersion.put(pluginId, resolvedPluginVersion);
204+
}
203205
}
204206
} catch (MavenDownloadingException e) {
205207
// continue

rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/UpgradePluginVersionTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,59 @@ void doesNotPinPropertyManagedVersions() {
474474
)
475475
);
476476
}
477+
478+
@Test
479+
void dontDowngradeKotlinDslSharedVariable() {
480+
rewriteRun(
481+
spec -> spec.recipe(new UpgradePluginVersion("org.jetbrains.kotlin.*", "1.5.31", null)),
482+
buildGradleKts(
483+
"""
484+
plugins {
485+
val kotlinVersion = "1.8.21"
486+
id("org.jetbrains.kotlin.jvm") version kotlinVersion
487+
id("org.jetbrains.kotlin.plugin.allopen") version kotlinVersion
488+
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
489+
}
490+
"""
491+
)
492+
);
493+
}
494+
495+
@Test
496+
void dontDowngradeKotlinDslSinglePluginVariable() {
497+
rewriteRun(
498+
spec -> spec.recipe(new UpgradePluginVersion("org.jetbrains.kotlin.*", "1.5.31", null)),
499+
buildGradleKts(
500+
"""
501+
plugins {
502+
val kotlinVersion = "1.8.21"
503+
id("org.jetbrains.kotlin.jvm") version kotlinVersion
504+
}
505+
"""
506+
)
507+
);
508+
}
509+
510+
@Test
511+
void upgradeKotlinDslSharedVariable() {
512+
rewriteRun(
513+
spec -> spec.recipe(new UpgradePluginVersion("org.jetbrains.kotlin.*", "2.0.0", null)),
514+
buildGradleKts(
515+
"""
516+
plugins {
517+
val kotlinVersion = "1.9.24"
518+
id("org.jetbrains.kotlin.jvm") version kotlinVersion
519+
id("org.jetbrains.kotlin.plugin.allopen") version kotlinVersion
520+
}
521+
""",
522+
"""
523+
plugins {
524+
val kotlinVersion = "2.0.0"
525+
id("org.jetbrains.kotlin.jvm") version kotlinVersion
526+
id("org.jetbrains.kotlin.plugin.allopen") version kotlinVersion
527+
}
528+
"""
529+
)
530+
);
531+
}
477532
}

0 commit comments

Comments
 (0)