Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax Jar path matching for coordinates extraction #144

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand All @@ -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()
}
Expand Down