[maven][IDEA-370993] - fix JarHttpDownloaderJps to make tests readable and handle errors better

GitOrigin-RevId: c40b59437a9c1fc47f1da3f5a3e33481e8a878fe
This commit is contained in:
Alexander Bubenchikov
2025-05-09 09:15:30 +02:00
committed by intellij-monorepo-bot
parent 3def02f2d0
commit 2f53c07260
4 changed files with 28 additions and 26 deletions

View File

@@ -23,6 +23,7 @@ import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
import org.jetbrains.concurrency.AsyncPromise
import org.jetbrains.concurrency.Promise
import org.jetbrains.concurrency.rejectedPromise
import org.jetbrains.idea.maven.aether.RetryProvider
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties
import org.jetbrains.jps.model.serialization.JpsMavenSettings
@@ -71,17 +72,16 @@ class JarHttpDownloaderJps(val project: Project, val coroutineScope: CoroutineSc
// 1. canonical path (symlinks can be resolved or not resolved)
// 2. in tests, JarRepositoryManager.localRepositoryPath can be overridden
val possibleMavenLocalRepositoryRoots = listOfNotNull(
// could be overridden, like in tests
project?.let { JarRepositoryManager.getJPSLocalMavenRepositoryForIdeaProject(it).toString() },
project?.let { JarRepositoryManager.getJPSLocalMavenRepositoryForIdeaProject(it).toString() },
JarRepositoryManager.getLocalRepositoryPath().path,
// always returns a canonical path (symlinks resolved), so can be anything even if it was not overridden
PathMacroManager.getInstance(ApplicationManager.getApplication()).expandPath(JarRepositoryManager.MAVEN_REPOSITORY_MACRO),
// in some cases, we may receive a non-canonical path (without symlinks resolved)
JpsMavenSettings.getMavenRepositoryPath(),
// in some cases, we may receive a non-canonical path (without symlinks resolved)
//JpsMavenSettings.getMavenRepositoryPath(),
).map { Path.of(FileUtil.toSystemDependentName(it)).normalize() }
val files = OrderRootType.getAllTypes().flatMap { rootType -> library.getUrls(rootType).map { rootType to it } }
@@ -183,14 +183,10 @@ class JarHttpDownloaderJps(val project: Project, val coroutineScope: CoroutineSc
}
}
/**
* return rejew if `library` could not be downloaded by JarHttpDownloader
*/
fun downloadLibraryFilesAsync(library: LibraryEx): Promise<*>? {
fun downloadLibraryFilesAsync(library: LibraryEx): Promise<*> {
val relativePaths = when (val result = collectRelativePathsForJarHttpDownloaderOrLog(project, library)) {
is CollectResult.Failure -> {
LOG.debug(result.reason)
return null
return rejectedPromise<Unit>(result.reason)
}
is CollectResult.Success -> result.files
}

View File

@@ -271,21 +271,18 @@ public final class RepositoryUtils {
if (JarHttpDownloaderJps.enabled()) {
Promise<?> promise = JarHttpDownloaderJps.getInstance(project).downloadLibraryFilesAsync(library);
// null means this library should be handled by standard resolver
if (promise != null) {
// callers of this function typically do not log, so do it for them
promise.onError(error -> {
LOG.warn("Failed to download repository library '" + library.getName() + "' with JarHttpDownloader", error);
// callers of this function typically do not log, so do it for them
promise.onError(error -> {
LOG.warn("Failed to download repository library '" + library.getName() + "' with JarHttpDownloader", error);
});
if (LOG.isDebugEnabled()) {
promise.onSuccess(result -> {
LOG.debug("Downloaded repository library '" + library.getName() + "' with JarHttpDownloader");
});
if (LOG.isDebugEnabled()) {
promise.onSuccess(result -> {
LOG.debug("Downloaded repository library '" + library.getName() + "' with JarHttpDownloader");
});
}
return promise;
}
return promise;
}
Promise<List<OrderRoot>> mavenResolverPromise = loadDependenciesToLibrary(

View File

@@ -2,17 +2,17 @@
<library name="apache.commons.math3" type="repository">
<properties include-transitive-deps="false" maven-id="org.apache.commons:commons-math3:3.6">
<verification>
<artifact url="file://$MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6.jar">
<artifact url="file://$REPOSITORY_LIBRARY_UTILS_TEST_LOCAL_MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6.jar">
<sha256sum>79b0baf88d2bc643f652f413e52702d81ac40a9b782d7f00fc431739e8d1c28a</sha256sum>
</artifact>
</verification>
</properties>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6.jar!/" />
<root url="jar://$REPOSITORY_LIBRARY_UTILS_TEST_LOCAL_MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6-sources.jar!/" />
<root url="jar://$REPOSITORY_LIBRARY_UTILS_TEST_LOCAL_MAVEN_REPOSITORY$/org/apache/commons/commons-math3/3.6/commons-math3-3.6-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -38,6 +38,7 @@ import kotlin.test.assertTrue
@TestApplication
class JarHttpDownloaderJpsTest {
private val TEST_MAVEN_LOCAL_REPOSITORY_MACRO = "REPOSITORY_LIBRARY_UTILS_TEST_LOCAL_MAVEN_REPOSITORY"
private val TEST_REMOTE_REPOSITORIES_ROOT_MACRO = "REPOSITORY_LIBRARY_UTILS_TEST_REMOTE_REPOSITORIES_ROOT"
@JvmField
@@ -78,10 +79,18 @@ class JarHttpDownloaderJpsTest {
@BeforeEach
fun beforeEach() {
val pathMacros: PathMacros = PathMacros.getInstance()
pathMacros.setMacro(TEST_MAVEN_LOCAL_REPOSITORY_MACRO, m2DirectoryPath.toString())
Disposer.register(disposable) {
pathMacros.setMacro(TEST_MAVEN_LOCAL_REPOSITORY_MACRO, null)
}
pathMacros.setMacro(TEST_REMOTE_REPOSITORIES_ROOT_MACRO, server.url)
Disposer.register(disposable) {
pathMacros.setMacro(TEST_REMOTE_REPOSITORIES_ROOT_MACRO, null)
}
JarRepositoryManager.setLocalRepositoryPath(m2DirectoryPath.toFile())
Disposer.register(disposable) {
JarRepositoryManager.setLocalRepositoryPath(null)
}
val repo = PathMacros.getInstance().getValue("MAVEN_REPOSITORY")
PathMacros.getInstance().setMacro("MAVEN_REPOSITORY", m2DirectoryPath.toString())
@@ -135,7 +144,7 @@ class JarHttpDownloaderJpsTest {
val promise = JarHttpDownloaderJps.getInstance(project).downloadLibraryFilesAsync(libraryRelease)
val exception = assertFailsWith<IllegalStateException> {
promise!!.await()
promise.await()
}
assertTrue(exception.message!!.startsWith("Failed to download 1 artifact(s): (first exception) Wrong file checksum"), exception.message!!)