Skip to content

Commit

Permalink
fix suppressing findings within the AGP DSL
Browse files Browse the repository at this point in the history
fixes #710
  • Loading branch information
RBusarow committed Jun 7, 2022
1 parent 2f23f6f commit 9ea13ed
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,44 @@ class DisableAndroidResourcesTest : RunnerTest() {
logger.parsedReport() shouldBe listOf()
}

@Test
fun `unused resource generation should pass if suppressed`() {

val lib1 = androidLibrary(":lib1", "com.modulecheck.lib1") {
buildFile {
"""
plugins {
id("com.android.library")
kotlin("android")
}
android {
@Suppress("disable-android-resources")
buildFeatures.androidResources = true
}
"""
}
}

run(
autoCorrect = false
).isSuccess shouldBe true

lib1.buildFile shouldHaveText """
plugins {
id("com.android.library")
kotlin("android")
}
android {
@Suppress("disable-android-resources")
buildFeatures.androidResources = true
}
"""

logger.parsedReport() shouldBe listOf()
}

@Test
fun `unused resource generation should be ignored in dynamic-feature module`() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import org.jetbrains.kotlin.util.suffixIfNot
import java.io.File

data class UnusedResourcesGenerationFinding(
override val findingName: FindingName,
override val dependentProject: McProject,
override val dependentPath: ProjectPath.StringProjectPath,
override val buildFile: File
) : Finding, Fixable {

override val findingName = NAME

override val message: String
get() = "`androidResources` generation is enabled, but no resources are defined in this module."

Expand Down Expand Up @@ -140,4 +141,9 @@ data class UnusedResourcesGenerationFinding(

return buildFile.readText().replace(fullText, newBlockText)
}

companion object {
/** @suppress */
val NAME = FindingName("disable-android-resources")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal fun ParserRuleContext.printEverything() {
val levels = mutableMapOf<RuleContext, Int>(this to 0)
val dashes = "------------------------------------------------------------"

fun printMe(nodeSimpleName: String, parentName: String, nodeText: String, level: Int) {
fun printNode(nodeSimpleName: String, parentName: String, nodeText: String, level: Int) {
println(
"""
| $dashes $nodeSimpleName -- parent: $parentName
Expand All @@ -40,7 +40,7 @@ internal fun ParserRuleContext.printEverything() {
)
}

printMe(
printNode(
nodeSimpleName = javaClass.simpleName, parentName = "null", nodeText = text, level = 0
)

Expand All @@ -57,7 +57,7 @@ internal fun ParserRuleContext.printEverything() {

val parentName = parent.javaClass.simpleName

printMe(
printNode(
nodeSimpleName = node.javaClass.simpleName,
parentName = parentName,
nodeText = node.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import modulecheck.api.context.androidResourceReferencesForSourceSetName
import modulecheck.api.context.dependents
import modulecheck.api.context.referencesForSourceSetName
import modulecheck.config.ModuleCheckSettings
import modulecheck.finding.FindingName
import modulecheck.finding.android.UnusedResourcesGenerationFinding
import modulecheck.parsing.gradle.model.AndroidPlatformPlugin.AndroidLibraryPlugin
import modulecheck.project.McProject
Expand All @@ -33,7 +32,7 @@ import javax.inject.Inject
class DisableAndroidResourcesRule @Inject constructor() :
DocumentedRule<UnusedResourcesGenerationFinding>() {

override val name = FindingName("disable-android-resources")
override val name = UnusedResourcesGenerationFinding.NAME
override val description =
"Finds modules which have android resources R file generation enabled, " +
"but don't actually use any resources from the module"
Expand All @@ -48,7 +47,6 @@ class DisableAndroidResourcesRule @Inject constructor() :

fun findingList() = listOf(
UnusedResourcesGenerationFinding(
findingName = name,
dependentProject = project, dependentPath = project.path, buildFile = project.buildFile
)
)
Expand Down

0 comments on commit 9ea13ed

Please sign in to comment.