Skip to content

Commit 562f73a

Browse files
authored
ModuleHasDependencies and VersionPatterns (#150)
* add reproducing tests suit for version patterns in `ModuleHasDependencies` and its delegates. Also fix the parameter swap in `ModuleHasDependency`. * fix version patterns to match with the DependencyInsight logic * remove unused dependencies * remove comment
1 parent fd11604 commit 562f73a

2 files changed

Lines changed: 129 additions & 1 deletion

File tree

src/main/java/org/openrewrite/java/dependencies/search/ModuleHasDependency.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
9393
tree.getMarkers()
9494
.findFirst(JavaProject.class)
9595
.ifPresent(jp -> {
96-
Tree t = new DependencyInsight(groupIdPattern, artifactIdPattern, scope, version)
96+
Tree t = new DependencyInsight(groupIdPattern, artifactIdPattern, version, scope)
9797
.getVisitor()
9898
.visit(tree, ctx);
9999
if (t != tree) {

src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.dependencies.search;
1717

1818
import org.intellij.lang.annotations.Language;
19+
import org.junit.jupiter.api.Nested;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.params.ParameterizedTest;
2122
import org.junit.jupiter.params.provider.NullSource;
@@ -344,4 +345,131 @@ void whenModuleDoesNotHaveDependencyButInvertedMarkingMarks() {
344345
)
345346
);
346347
}
348+
349+
@Nested
350+
class WithVersionsPattern {
351+
@ParameterizedTest
352+
@ValueSource(strings = {
353+
"1.0.1", // exact
354+
"1.0.1-1.0.5", // hyphenated
355+
"[1.0.1,1.0.5)", "[1.0.1,1.0.5]", "[1.0.1,1.0.5]", "(1.0.0,1.0.5]", // full range
356+
"~1.0.1"// tilde range
357+
})
358+
void maven(String versionPattern) {
359+
rewriteRun(
360+
recipeSpec -> recipeSpec.recipe(new ModuleHasDependency("jakarta.data", "*", null, versionPattern, null)),
361+
mavenProject("project-maven",
362+
//language=xml
363+
pomXml(
364+
"""
365+
<project>
366+
<groupId>com.example</groupId>
367+
<artifactId>foo</artifactId>
368+
<version>1.0.0</version>
369+
370+
<dependencies>
371+
<dependency>
372+
<groupId>jakarta.data</groupId>
373+
<artifactId>jakarta.data-api</artifactId>
374+
<version>1.0.1</version>
375+
</dependency>
376+
<dependency>
377+
<groupId>jakarta.data</groupId>
378+
<artifactId>jakarta.data-spec</artifactId>
379+
<version>1.0.0</version>
380+
</dependency>
381+
</dependencies>
382+
</project>
383+
""",
384+
"""
385+
<!--~~(Module has dependency: jakarta.data:*:%s)~~>--><project>
386+
<groupId>com.example</groupId>
387+
<artifactId>foo</artifactId>
388+
<version>1.0.0</version>
389+
390+
<dependencies>
391+
<dependency>
392+
<groupId>jakarta.data</groupId>
393+
<artifactId>jakarta.data-api</artifactId>
394+
<version>1.0.1</version>
395+
</dependency>
396+
<dependency>
397+
<groupId>jakarta.data</groupId>
398+
<artifactId>jakarta.data-spec</artifactId>
399+
<version>1.0.0</version>
400+
</dependency>
401+
</dependencies>
402+
</project>
403+
""".formatted(versionPattern)
404+
)
405+
)
406+
);
407+
}
408+
409+
@ParameterizedTest
410+
@ValueSource(strings = {
411+
"1.0.1", // exact
412+
"1.0.1-1.0.5", // hyphenated
413+
"[1.0.1,1.0.5)", "[1.0.1,1.0.5]", "[1.0.1,1.0.5]", "(1.0.0,1.0.5]", // full range
414+
"~1.0.1"// tilde range
415+
})
416+
void gradle(String versionPattern) {
417+
rewriteRun(
418+
recipeSpec -> recipeSpec.recipe(new ModuleHasDependency("jakarta.data", "*", null, versionPattern, null)),
419+
mavenProject("project-maven",
420+
//language=groovy
421+
buildGradle(
422+
"""
423+
plugins {
424+
id 'java-library'
425+
}
426+
repositories {
427+
mavenCentral()
428+
}
429+
dependencies {
430+
implementation 'jakarta.data:jakarta.data-api:1.0.1'
431+
implementation 'jakarta.data:jakarta.data-spec:1.0.0'
432+
}
433+
""",
434+
"""
435+
/*~~(Module has dependency: jakarta.data:*:%s)~~>*/plugins {
436+
id 'java-library'
437+
}
438+
repositories {
439+
mavenCentral()
440+
}
441+
dependencies {
442+
implementation 'jakarta.data:jakarta.data-api:1.0.1'
443+
implementation 'jakarta.data:jakarta.data-spec:1.0.0'
444+
}
445+
""".formatted(versionPattern)
446+
)
447+
)
448+
);
449+
}
450+
451+
@Test
452+
void noPresentVersion() {
453+
rewriteRun(
454+
recipeSpec -> recipeSpec.recipe(new ModuleHasDependency("org.springframework", "*", null, "5.1.2", null)),
455+
mavenProject("project-maven",
456+
//language=groovy
457+
buildGradle(
458+
"""
459+
plugins {
460+
id 'java-library'
461+
}
462+
repositories {
463+
mavenCentral()
464+
}
465+
dependencies {
466+
implementation 'org.springframework:spring-core:6.1.5'
467+
implementation 'org.springframework:spring-aop:6.1.5'
468+
}
469+
"""
470+
)
471+
)
472+
);
473+
}
474+
}
347475
}

0 commit comments

Comments
 (0)