Skip to content

Commit 714b679

Browse files
committed
don't crash if Anvil isn't in the buildScript classpath
This is something of a band-aid, because it means that if the plugin is only applied to a sub-project, it won't be parsed at all.
1 parent d38f84d commit 714b679

5 files changed

Lines changed: 118 additions & 7 deletions

File tree

modulecheck-gradle/plugin/src/main/kotlin/modulecheck/gradle/internal/GradleProjectProvider.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -206,18 +206,25 @@ class GradleProjectProvider @Inject constructor(
206206
`kotlinCompilerPluginClasspathMain`, `kotlinCompilerPluginClasspathTest`, etc.
207207
*/
208208
val version = configurations
209-
.filter { it.name.startsWith("kotlinCompilerPluginClasspath") }
210209
.asSequence()
210+
.filter { it.name.startsWith("kotlinCompilerPluginClasspath") }
211211
.flatMap { it.dependencies }
212212
.firstOrNull { it.group == "com.squareup.anvil" }
213213
?.version
214214
?.let { versionString -> SemVer.parse(versionString) }
215215
?: return null
216216

217-
val enabled = extensions
218-
.findByType(AnvilExtension::class.java)
219-
?.generateDaggerFactories
220-
?.get() == true
217+
// TODO This is something of a band-aid, because it means that if the plugin is only applied to
218+
// a sub-project, it won't be parsed at all. Anvil should probably be shaded, unless/until
219+
// classpath isolation with the Worker API is working.
220+
@Suppress("SwallowedException")
221+
val enabled = try {
222+
extensions.findByType(AnvilExtension::class.java)
223+
?.generateDaggerFactories
224+
?.get() == true
225+
} catch (e: NoClassDefFoundError) {
226+
return null
227+
}
221228

222229
return AnvilGradlePlugin(version, enabled)
223230
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (C) 2021-2023 Rick Busarow
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package modulecheck.gradle
17+
18+
import modulecheck.testing.replaceOrFail
19+
import org.junit.jupiter.api.TestFactory
20+
21+
class AnvilPluginParsingTest : BaseGradleTest() {
22+
23+
@TestFactory
24+
fun `anvil may be added to the root project buildScript classpath`() = anvil {
25+
rootBuild {
26+
DEFAULT_BUILD_FILE.replaceOrFail(
27+
"""(\s*buildscript\s*\{\s*dependencies\s*\{)(\s*)""".toRegex(),
28+
"$1$2classpath(\"com.squareup.anvil:gradle-plugin:$anvilVersion\")$2"
29+
)
30+
}
31+
32+
kotlinProject(":lib1") {
33+
buildFile {
34+
"""
35+
plugins {
36+
kotlin("jvm")
37+
id("com.squareup.anvil")
38+
}
39+
"""
40+
}
41+
42+
addKotlinSource(
43+
"""
44+
package com.modulecheck.lib1
45+
46+
class Lib1Class
47+
""",
48+
sourceDirName = "kotlin"
49+
)
50+
}
51+
52+
shouldSucceed("moduleCheck")
53+
}
54+
55+
@TestFactory
56+
fun `anvil may be added directly to a subproject only`() = anvil {
57+
58+
kotlinProject(":lib1") {
59+
buildFile {
60+
"""
61+
plugins {
62+
kotlin("jvm")
63+
id("com.squareup.anvil") version "$anvilVersion"
64+
}
65+
"""
66+
}
67+
68+
addKotlinSource(
69+
"""
70+
package com.modulecheck.lib1
71+
72+
class Lib1Class
73+
""",
74+
sourceDirName = "kotlin"
75+
)
76+
}
77+
78+
shouldSucceed("moduleCheck")
79+
}
80+
}

modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/TestVersions.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -71,6 +71,19 @@ interface VersionsMatrixTest : DynamicTests, ResetManager {
7171
)
7272
}
7373

74+
fun anvil(
75+
filter: (String) -> Boolean = { true },
76+
action: (String) -> Unit
77+
): List<DynamicTest> {
78+
79+
return anvilVersions.dynamic(
80+
filter = { filter(this) },
81+
testName = { "anvil $it" },
82+
setup = { anvilVersion = it },
83+
action = action
84+
)
85+
}
86+
7487
fun kotlin(
7588
filter: (String) -> Boolean = { true },
7689
action: (String) -> Unit

modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/FancyShould.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ fun <T, U : T> T.trimmedShouldBe(expected: U?, vararg excludeFromStack: KClass<*
4949
}
5050
}
5151

52+
/**
53+
* returns the output of [assertion], or cleans up the stacktrace of any assertion errors before rethrowing
54+
*
55+
* @since 0.12.4
56+
*/
5257
fun <R> trimmedAssert(
5358
vararg excludeFromStack: KClass<*>,
5459
assertion: suspend () -> R
5560
): R = Unit.trimmedAssert(*excludeFromStack) { assertion() }
5661

62+
/**
63+
* returns the output of [assertion], or cleans up the stacktrace of any assertion errors before rethrowing
64+
*
65+
* @since 0.12.4
66+
*/
5767
fun <T, R> T.trimmedAssert(
5868
vararg excludeFromStack: KClass<*>,
5969
assertion: suspend T.() -> R

modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/asserts.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private fun assertChanged(
6363
) = newString.also { new ->
6464
trimmedAssert {
6565

66+
@Suppress("MagicNumber")
6667
val tokenName = (if (token is Regex) "regex" else "oldValue").padStart(9)
6768

6869
"""

0 commit comments

Comments
 (0)