From 372246cbdc55d39dc8507f1cc5c7dae7c84b8d2e Mon Sep 17 00:00:00 2001 From: sullis Date: Tue, 17 Dec 2024 13:51:28 -0800 Subject: [PATCH] enhance UpgradeDependencyVersionTest (#4788) * enhance UpgradeDependencyVersionTest * Make the tests more expressive when they fail --------- Co-authored-by: Tim te Beek --- .../gradle/UpgradeDependencyVersionTest.java | 260 +++++++++++------- 1 file changed, 158 insertions(+), 102 deletions(-) diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java index 5e9ecf85f34..e8d6d145923 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java @@ -44,18 +44,18 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void guava() { + void guavaCompileOnly() { rewriteRun( buildGradle( """ plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { compileOnly 'com.google.guava:guava:29.0-jre' runtimeOnly ('com.google.guava:guava:29.0-jre') @@ -65,11 +65,11 @@ void guava() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { compileOnly 'com.google.guava:guava:30.1.1-jre' runtimeOnly ('com.google.guava:guava:30.1.1-jre') @@ -78,21 +78,77 @@ void guava() { spec -> spec.afterRecipe(after -> { Optional maybeGp = after.getMarkers().findFirst(GradleProject.class); assertThat(maybeGp).isPresent(); - GradleProject gp = maybeGp.get(); - GradleDependencyConfiguration compileClasspath = gp.getConfiguration("compileClasspath"); + GradleDependencyConfiguration compileClasspath = maybeGp.get().getConfiguration("compileClasspath"); assertThat(compileClasspath).isNotNull(); - assertThat( - compileClasspath.getRequested().stream() - .filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion())) - .findAny()) + assertThat(compileClasspath.getRequested()) .as("GradleProject requested dependencies should have been updated with the new version of guava") - .isPresent(); - assertThat( - compileClasspath.getResolved().stream() - .filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion())) - .findAny()) - .as("GradleProject requested dependencies should have been updated with the new version of guava") - .isPresent(); + .anySatisfy(dep -> { + assertThat(dep.getGroupId()).isEqualTo("com.google.guava"); + assertThat(dep.getArtifactId()).isEqualTo("guava"); + assertThat(dep.getVersion()).isEqualTo("30.1.1-jre"); + }); + assertThat(compileClasspath.getResolved()) + .as("GradleProject resolved dependencies should have been updated with the new version of guava") + .anySatisfy(dep -> { + assertThat(dep.getGroupId()).isEqualTo("com.google.guava"); + assertThat(dep.getArtifactId()).isEqualTo("guava"); + assertThat(dep.getVersion()).isEqualTo("30.1.1-jre"); + }); + }) + ) + ); + } + + @Test + void mockitoTestImplementation() { + rewriteRun(recipeSpec -> { + recipeSpec.beforeRecipe(withToolingApi()) + .recipe(new UpgradeDependencyVersion("org.mockito", "*", "4.11.0", null)); + }, + buildGradle( + """ + plugins { + id 'java-library' + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.mockito:mockito-junit-jupiter:3.12.4") + } + """, + """ + plugins { + id 'java-library' + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.mockito:mockito-junit-jupiter:4.11.0") + } + """, + spec -> spec.afterRecipe(after -> { + Optional maybeGp = after.getMarkers().findFirst(GradleProject.class); + assertThat(maybeGp).isPresent(); + GradleDependencyConfiguration testCompileClasspath = maybeGp.get().getConfiguration("testCompileClasspath"); + assertThat(testCompileClasspath).isNotNull(); + assertThat(testCompileClasspath.getRequested()) + .as("GradleProject requested dependencies should have been updated with the new version of mockito") + .anySatisfy(dep -> { + assertThat(dep.getGroupId()).isEqualTo("org.mockito"); + assertThat(dep.getArtifactId()).isEqualTo("mockito-junit-jupiter"); + }); + assertThat(testCompileClasspath.getResolved()) + .as("GradleProject resolved dependencies should have been updated with the new version of mockito") + .anySatisfy(dep -> { + assertThat(dep.getGroupId()).isEqualTo("org.mockito"); + assertThat(dep.getArtifactId()).isEqualTo("mockito-junit-jupiter"); + }); }) ) ); @@ -106,13 +162,13 @@ void updateVersionInVariable() { plugins { id 'java-library' } - + def guavaVersion = '29.0-jre' def otherVersion = "latest.release" repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion" @@ -122,13 +178,13 @@ void updateVersionInVariable() { plugins { id 'java-library' } - + def guavaVersion = '30.1.1-jre' def otherVersion = "latest.release" repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion" @@ -146,7 +202,7 @@ void deeplyNestedProjectDependency() { plugins { id 'java-library' } - + dependencies { implementation project(":foo:bar:baz:qux:quux") } @@ -178,11 +234,11 @@ void varargsDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation( 'com.google.guava:guava-gwt:29.0-jre', @@ -193,11 +249,11 @@ void varargsDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation( 'com.google.guava:guava-gwt:29.0-jre', @@ -215,12 +271,12 @@ void mapNotationVariable() { plugins { id 'java-library' } - + def guavaVersion = '29.0-jre' repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -229,12 +285,12 @@ void mapNotationVariable() { plugins { id 'java-library' } - + def guavaVersion = '30.1.1-jre' repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -251,11 +307,11 @@ void mapNotationLiteral() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation (group: "com.google.guava", name: "guava", version: '29.0-jre') } @@ -264,11 +320,11 @@ void mapNotationLiteral() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation (group: "com.google.guava", name: "guava", version: '30.1.1-jre') } @@ -286,11 +342,11 @@ void worksWithPlatform() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation platform("com.google.guava:guava:29.0-jre") } @@ -299,11 +355,11 @@ implementation platform("com.google.guava:guava:29.0-jre") plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation platform("com.google.guava:guava:30.1.1-jre") } @@ -328,19 +384,19 @@ void upgradesVariablesDefinedInExtraProperties() { classpath("com.google.guava:guava:${guavaVersion}") } } - + plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion2 = "29.0-jre" } - + dependencies { implementation "com.google.guava:guava:${guavaVersion2}" } @@ -357,19 +413,19 @@ void upgradesVariablesDefinedInExtraProperties() { classpath("com.google.guava:guava:${guavaVersion}") } } - + plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion2 = "30.1.1-jre" } - + dependencies { implementation "com.google.guava:guava:${guavaVersion2}" } @@ -387,15 +443,15 @@ void matchesGlobs() { plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion = "29.0-jre" } - + def guavaVersion2 = "29.0-jre" dependencies { implementation("com.google.guava:guava:29.0-jre") @@ -408,15 +464,15 @@ void matchesGlobs() { plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion = "30.1.1-jre" } - + def guavaVersion2 = "30.1.1-jre" dependencies { implementation("com.google.guava:guava:30.1.1-jre") @@ -438,11 +494,11 @@ void defaultsToLatestRelease() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:29.0-jre' } @@ -488,11 +544,11 @@ void versionInPropertiesFile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -515,11 +571,11 @@ void versionInPropertiesFileNotUpdatedIfNoDependencyVariable() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:30.1.1-jre") } @@ -554,11 +610,11 @@ void versionInParentAndMultiModulePropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -570,11 +626,11 @@ void versionInParentAndMultiModulePropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -601,18 +657,18 @@ void versionInParentSubprojectDefinitionWithPropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + subprojects { repositories { mavenCentral() } - + apply plugin: "java-library" - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -631,7 +687,7 @@ void versionInParentSubprojectDefinitionWithPropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -659,11 +715,11 @@ void versionOnlyInMultiModuleChildPropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -694,7 +750,7 @@ void mapNotationVariableInPropertiesFile() { repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -720,11 +776,11 @@ void mapNotationGStringVariableInPropertiesFile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: "${guavaVersion}" } @@ -753,11 +809,11 @@ void globVersionsInPropertiesFileWithMultipleVersionsOnlyUpdatesCorrectProperty( plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}") implementation("org.springframework.security:spring-security-oauth2-core:${springSecurityVersion}") @@ -783,11 +839,11 @@ void disallowDowngrade() { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}") implementation("org.springframework.security:spring-security-oauth2-core:${springSecurityVersion}") @@ -806,11 +862,11 @@ void dontDowngradeWhenExactVersion() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:29.0-jre' } @@ -828,11 +884,11 @@ void leaveConstraintsAlone() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { constraints { implementation("com.google.guava:guava:28.0-jre") @@ -854,11 +910,11 @@ void unknownConfiguration() { id 'java' id "org.hidetake.swagger.generator" version "2.18.2" } - + repositories { mavenCentral() } - + dependencies { swaggerCodegen "org.openapitools:openapi-generator-cli:5.2.0" } @@ -868,11 +924,11 @@ void unknownConfiguration() { id 'java' id "org.hidetake.swagger.generator" version "2.18.2" } - + repositories { mavenCentral() } - + dependencies { swaggerCodegen "org.openapitools:openapi-generator-cli:5.2.1" } @@ -890,11 +946,11 @@ void noActionForNonStringLiterals() { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { implementation(gradleApi()) jar { @@ -908,7 +964,7 @@ void noActionForNonStringLiterals() { @Test @Issue("https://github.com/openrewrite/rewrite-java-dependencies/pull/106") - void isAcceptable(){ + void isAcceptable() { // Mimic org.openrewrite.java.dependencies.UpgradeTransitiveDependencyVersion#getVisitor UpgradeDependencyVersion guava = new UpgradeDependencyVersion("com.google.guava", "guava", "30.x", "-jre"); TreeVisitor visitor = guava.getVisitor(); @@ -930,11 +986,11 @@ void exactVersionWithExactPattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation('com.google.guava:guava:29.0-jre') } @@ -943,11 +999,11 @@ void exactVersionWithExactPattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation('com.google.guava:guava:32.1.1-jre') } @@ -966,11 +1022,11 @@ void exactVersionWithRegexPattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation('com.google.guava:guava:29.0-android') } @@ -979,11 +1035,11 @@ void exactVersionWithRegexPattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation('com.google.guava:guava:32.1.1-android') } @@ -1001,11 +1057,11 @@ void upgradesDependencyVersionDefinedInJvmTestSuite() { id "java-library" id 'jvm-test-suite' } - + repositories { mavenCentral() } - + testing { suites { test { @@ -1021,11 +1077,11 @@ void upgradesDependencyVersionDefinedInJvmTestSuite() { id "java-library" id 'jvm-test-suite' } - + repositories { mavenCentral() } - + testing { suites { test { @@ -1091,11 +1147,11 @@ void issue4655() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + version='ORC-246-1-SNAPSHOT' dependencies { implementation "com.veon.eurasia.oraculum:jira-api:$version"