Skip to content

Commit

Permalink
Merge pull request #132 from littlerobots/fix/module-replacement
Browse files Browse the repository at this point in the history
Skip redirected artifacts from version selector
  • Loading branch information
hvisser committed Dec 24, 2023
2 parents 94747fe + e670115 commit 6b05de7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ internal class DependencyResolver {
val selector: (ComponentSelection) -> Unit = {
// Any error here is swallowed by Gradle, so work around
try {
val currentVersion = requireNotNull(currentVersions["${it.candidate.group}:${it.candidate.module}"])
if (!moduleVersionSelector.select(ModuleVersionCandidate(it.candidate, currentVersion))) {
val currentVersion = currentVersions["${it.candidate.group}:${it.candidate.module}"]
// if the version is null, then we don't have this specific artifact in the catalog
// This can be caused if the resolved artifact is an alias to a different artifact
// Just selecting whatever is presented here _should_ be fine in that case, as the original
// artifact passes the version selection first.
if (currentVersion != null && !moduleVersionSelector.select(ModuleVersionCandidate(it.candidate, currentVersion))) {
it.reject("${it.candidate.version} rejected by version selector as an upgrade for version $currentVersion")
}
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,47 @@ class DependencyResolverTest {
assertTrue(result.richVersions.libraries.isNotEmpty())
}

@Test
// https://github.com/littlerobots/version-catalog-update-plugin/issues/129
fun `Resolves the original library for variants though Gradle module data`() {
val project = ProjectBuilder.builder().withName("test").build()
val resolver = DependencyResolver()
val catalog = VersionCatalogParser().parse(
"""
[libraries]
# on the jvm this will actually resolve to apollo-api-jvm
apollo-api = "com.apollographql.apollo3:apollo-api:3.8.1"
""".trimIndent().asStream()
)

project.repositories.add(project.repositories.mavenCentral())

val checkedCandidates = mutableSetOf<ModuleVersionCandidate>()

val result = resolver.resolveFromCatalog(
project.configurations.detachedConfiguration(),
project.configurations.detachedConfiguration(),
project.buildscript.configurations.detachedConfiguration(),
project.buildscript.configurations.detachedConfiguration(),
project.dependencies,
catalog,
object : ModuleVersionSelector {
override fun select(candidate: ModuleVersionCandidate): Boolean {
checkedCandidates.add(candidate)
return candidate.candidate.version == "3.8.2"
}
}
)

assertTrue(result.versionCatalog.libraries.isNotEmpty())
assertEquals("3.8.2", (result.versionCatalog.libraries.values.first().version as? VersionDefinition.Simple)?.version)
assertEquals(
setOf("com.apollographql.apollo3:apollo-api"),
checkedCandidates.map { "${it.candidate.group}:${it.candidate.module}" }.toSet()
)
}

private fun String.asStream(): ByteArrayInputStream {
return ByteArrayInputStream(toByteArray())
}
Expand Down

0 comments on commit 6b05de7

Please sign in to comment.