Skip to content

Commit 657b422

Browse files
committed
refactor: simplify Muzzle failure diagnostics
1 parent 629e88a commit 657b422

2 files changed

Lines changed: 23 additions & 44 deletions

File tree

buildSrc/src/main/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtils.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,18 @@ internal object MuzzleMavenRepoUtils {
158158
val resultExceptions = mutableListOf<Pair<Int, List<Exception>>>()
159159
fun attemptResolve(): VersionRangeResult? {
160160
attemptCount++
161-
return try {
162-
range = system.resolveVersionRange(session, rangeRequest)
161+
val result = try {
163162
failure = null
164-
range?.exceptions?.takeIf { it.isNotEmpty() }?.let { exceptions ->
165-
resultExceptions += attemptCount to exceptions.toList()
166-
}
167-
range?.takeIf { it.hasBounds() }
163+
system.resolveVersionRange(session, rangeRequest)
168164
} catch (e: VersionRangeResolutionException) {
169165
failure = e
170-
range = e.result ?: range
171-
e.result?.exceptions?.takeIf { it.isNotEmpty() }?.let { exceptions ->
172-
resultExceptions += attemptCount to exceptions.toList()
173-
}
174-
null
166+
e.result ?: return null
167+
}
168+
range = result
169+
if (result.exceptions.isNotEmpty()) {
170+
resultExceptions += attemptCount to result.exceptions.toList()
175171
}
172+
return result.takeIf { failure == null && it.hasBounds() }
176173
}
177174

178175
repeat(4) {

buildSrc/src/test/kotlin/datadog/gradle/plugin/muzzle/MuzzleMavenRepoUtilsTest.kt

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MuzzleMavenRepoUtilsTest {
7272
versions = "[1.0,)"
7373
}
7474
val attempts = AtomicInteger()
75-
val retryingSystem = repositorySystemThrowingThenResolving(
75+
val retryingSystem = repositorySystemReturningAfterFailures(
7676
failuresBeforeSuccess = 3,
7777
result = createVersionRangeResult("1.0.0"),
7878
attempts = attempts
@@ -150,7 +150,7 @@ class MuzzleMavenRepoUtilsTest {
150150
versions = "[1.0,)"
151151
}
152152
val attempts = AtomicInteger()
153-
val throwingSystem = repositorySystemThrowingThenResolving(
153+
val throwingSystem = repositorySystemReturningAfterFailures(
154154
failuresBeforeSuccess = 4,
155155
result = createVersionRangeResult("1.0.0"),
156156
attempts = attempts
@@ -180,7 +180,15 @@ class MuzzleMavenRepoUtilsTest {
180180
versions = "[1.0,)"
181181
}
182182
val attempts = AtomicInteger()
183-
val failingSystem = repositorySystemReturningEmptyResultsWithExceptions(attempts)
183+
val emptyResult = createVersionRangeResult().apply {
184+
addException(
185+
IllegalStateException(
186+
"metadata failure",
187+
IOException("download failure")
188+
)
189+
)
190+
}
191+
val failingSystem = repositorySystemReturningAfterFailures(0, emptyResult, attempts)
184192

185193
assertThatThrownBy {
186194
MuzzleMavenRepoUtils.resolveVersionRange(
@@ -194,10 +202,8 @@ class MuzzleMavenRepoUtilsTest {
194202
.hasMessageContaining("Resolution result exceptions:")
195203
.hasMessageContaining("Attempt 1:")
196204
.hasMessageContaining("Attempt 4:")
197-
.hasMessageContaining("java.lang.IllegalStateException: metadata failure 1")
198-
.hasMessageContaining("Caused by: java.io.IOException: download failure 1")
199-
.hasMessageContaining("java.lang.IllegalStateException: metadata failure 4")
200-
.hasMessageContaining("Caused by: java.io.IOException: download failure 4")
205+
.hasMessageContaining("java.lang.IllegalStateException: metadata failure")
206+
.hasMessageContaining("Caused by: java.io.IOException: download failure")
201207
assertThat(attempts).hasValue(4)
202208
}
203209

@@ -358,7 +364,7 @@ class MuzzleMavenRepoUtilsTest {
358364
return VersionRangeResult(request).apply { this.versions = versions }
359365
}
360366

361-
private fun repositorySystemThrowingThenResolving(
367+
private fun repositorySystemReturningAfterFailures(
362368
failuresBeforeSuccess: Int,
363369
result: VersionRangeResult,
364370
attempts: AtomicInteger
@@ -379,33 +385,9 @@ class MuzzleMavenRepoUtilsTest {
379385
}
380386
result
381387
}
382-
"toString" -> "repositorySystemThrowingThenResolving"
388+
"toString" -> "repositorySystemReturningAfterFailures"
383389
else -> throw UnsupportedOperationException(method.name)
384390
}
385391
} as RepositorySystem
386392

387-
private fun repositorySystemReturningEmptyResultsWithExceptions(
388-
attempts: AtomicInteger
389-
): RepositorySystem =
390-
Proxy.newProxyInstance(
391-
RepositorySystem::class.java.classLoader,
392-
arrayOf(RepositorySystem::class.java)
393-
) { _, method, args ->
394-
when (method.name) {
395-
"resolveVersionRange" -> {
396-
val attempt = attempts.incrementAndGet()
397-
val request = args?.get(1) as VersionRangeRequest
398-
VersionRangeResult(request).apply {
399-
addException(
400-
IllegalStateException(
401-
"metadata failure $attempt",
402-
IOException("download failure $attempt")
403-
)
404-
)
405-
}
406-
}
407-
"toString" -> "repositorySystemReturningEmptyResultsWithExceptions"
408-
else -> throw UnsupportedOperationException(method.name)
409-
}
410-
} as RepositorySystem
411393
}

0 commit comments

Comments
 (0)