From 6e6e6b651469b30d183e54ee850ca24fe2122478 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Sat, 21 Sep 2024 11:56:16 +0200 Subject: [PATCH] Relax Jar path matching for coordinates extraction (#144) Fixes #129 --- .../FilePathToModuleCoordinates.java | 13 +++-- .../FilePathToModuleCoordinatesTest.groovy | 48 +++++++++++++++---- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinates.java b/src/main/java/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinates.java index 3c38fe7..953593e 100644 --- a/src/main/java/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinates.java +++ b/src/main/java/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinates.java @@ -65,7 +65,7 @@ static boolean isInGradleCache(Path path) { return false; } String version = getVersionFromGradleCachePath(path); - return path.getFileName().toString().startsWith(name + "-" + version); + return matchesPath(path, name, version); } static boolean isInM2Cache(Path path) { @@ -74,7 +74,7 @@ static boolean isInM2Cache(Path path) { return false; } String version = getVersionFromM2CachePath(path); - return path.getFileName().toString().startsWith(name + "-" + version); + return matchesPath(path, name, version); } @Nullable @@ -85,12 +85,12 @@ private static String nameCoordinateFromFilePath(Path path) { String nameFromGradleCachePath = path.getName(path.getNameCount() - 4).toString(); String versionFromGradleCachePath = getVersionFromGradleCachePath(path); - if (path.getFileName().toString().startsWith(nameFromGradleCachePath + "-" + versionFromGradleCachePath)) { + if (matchesPath(path, nameFromGradleCachePath, versionFromGradleCachePath)) { return nameFromGradleCachePath; } String nameFromM2CachePath = path.getName(path.getNameCount() - 3).toString(); String versionFromM2CachePath = getVersionFromM2CachePath(path); - if (path.getFileName().toString().startsWith(nameFromM2CachePath + "-" + versionFromM2CachePath)) { + if (matchesPath(path, nameFromM2CachePath, versionFromM2CachePath)) { return nameFromM2CachePath; } @@ -104,4 +104,9 @@ private static String getVersionFromGradleCachePath(Path path) { private static String getVersionFromM2CachePath(Path path) { return path.getName(path.getNameCount() - 2).toString(); } + + private static boolean matchesPath(Path path, String name, String version) { + String jarFileName = path.getFileName().toString(); + return jarFileName.startsWith(name + "-") && !jarFileName.startsWith(version); + } } diff --git a/src/test/groovy/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinatesTest.groovy b/src/test/groovy/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinatesTest.groovy index dcd442e..41b0003 100644 --- a/src/test/groovy/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinatesTest.groovy +++ b/src/test/groovy/org/gradlex/javamodule/moduleinfo/FilePathToModuleCoordinatesTest.groovy @@ -14,7 +14,15 @@ class FilePathToModuleCoordinatesTest extends Specification { def path = path('/Users/someone/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.36/6c62681a2f655b49963a5983b8b0950a6120ae14/slf4j-api-1.7.36.jar') expect: - versionFromFilePath(path) == "1.7.36" + versionFromFilePath(path) == "1.7.36" + } + + def "version from gradle cache file path (version in file name does not match)"() { + given: + def path = path('/Users/jendrik/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/33.2.1-jre/818e780da2c66c63bbb6480fef1f3855eeafa3e4/guava-33.2.1-android.jar') + + expect: + versionFromFilePath(path) == "33.2.1-jre" } def "ga coordinates from gradle cache file path"() { @@ -25,30 +33,54 @@ class FilePathToModuleCoordinatesTest extends Specification { gaCoordinatesFromFilePathMatch(path, "org.slf4j:slf4j-api") } + def "ga coordinates from gradle cache file path (version in file name does not match)"() { + given: + def path = path('/Users/jendrik/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/33.2.1-jre/818e780da2c66c63bbb6480fef1f3855eeafa3e4/guava-33.2.1-android.jar') + + expect: + gaCoordinatesFromFilePathMatch(path, "com.google.guava:guava") + } + def "version from m2 repo file path"() { given: - def path = path('/Users/someone/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar.') + def path = path('/Users/someone/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar') + + expect: + versionFromFilePath(path) == "3.0.2" + } + + def "version from m2 repo file path (version in file name does not match)"() { + given: + def path = path('/Users/someone/.m2/repository/com/google/guava/guava/33.2.1-jre/guava-33.2.1-android.jar') expect: - versionFromFilePath(path) == "3.0.2" + versionFromFilePath(path) == "33.2.1-jre" } def "ga coordinates from m2 repo file path"() { Path jarPath - when: - jarPath = path('/Users/someone/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar.') + when: + jarPath = path('/Users/someone/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar') - then: - gaCoordinatesFromFilePathMatch(jarPath, "com.google.code.findbugs:jsr305") + then: + gaCoordinatesFromFilePathMatch(jarPath, "com.google.code.findbugs:jsr305") when: - jarPath = path('/Users/someone/.m2/repository/de/odysseus/juel/juel-impl/2.2.7/juel-impl-2.2.7.jar.') + jarPath = path('/Users/someone/.m2/repository/de/odysseus/juel/juel-impl/2.2.7/juel-impl-2.2.7.jar') then: gaCoordinatesFromFilePathMatch(jarPath, "de.odysseus.juel:juel-impl") } + def "ga coordinates from m2 repo file path (version in file name does not match)"() { + given: + def path = path('/Users/someone/.m2/repository/com/google/guava/guava/33.2.1-jre/guava-33.2.1-android.jar') + + expect: + gaCoordinatesFromFilePathMatch(path, "com.google.guava:guava") + } + private Path path(String path) { new File(path).toPath() }