diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt index 86512fb1..516d9085 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt @@ -77,10 +77,7 @@ class AffectedModuleDetectorPlugin : Plugin { } private fun registerCustomTasks(rootProject: Project) { - val mainConfiguration = requireNotNull( - value = rootProject.extensions.findByName(AffectedModuleConfiguration.name), - lazyMessage = { "Unable to find ${AffectedTestConfiguration.name} in $rootProject" } - ) as AffectedModuleConfiguration + val mainConfiguration = requireConfiguration(rootProject) rootProject.afterEvaluate { registerCustomTasks(rootProject, mainConfiguration.customTasks) @@ -166,9 +163,15 @@ class AffectedModuleDetectorPlugin : Plugin { testType: AffectedModuleTaskType, project: Project ) { + val config = requireConfiguration(project) + + fun isExcludedModule(configuration: AffectedModuleConfiguration, path: String): Boolean { + return configuration.excludedModules.find { path.startsWith(":$it") } != null + } + project.pluginManager.withPlugin(pluginId) { getAffectedPath(testType, project)?.let { path -> - if (AffectedModuleDetector.isProjectProvided(project)) { + if (AffectedModuleDetector.isProjectProvided(project) && !isExcludedModule(config, path)) { task.dependsOn(path) } @@ -249,6 +252,13 @@ class AffectedModuleDetectorPlugin : Plugin { } } + private fun requireConfiguration(project: Project): AffectedModuleConfiguration { + return requireNotNull( + value = project.rootProject.extensions.findByName(AffectedModuleConfiguration.name), + lazyMessage = { "Unable to find ${AffectedModuleConfiguration.name} in ${project.rootProject}" } + ) as AffectedModuleConfiguration + } + companion object { @VisibleForTesting diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt index 5d3cfce5..732a8647 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorIntegrationTest.kt @@ -114,4 +114,82 @@ class AffectedModuleDetectorIntegrationTest { assertThat(result.output).contains(":sample-core:assembleAndroidTest SKIPPED") assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") } + + @Test + fun `GIVEN multiple project with one excluded WHEN plugin is applied THEN tasks has dependencies minus the exclusions`() { + // GIVEN + tmpFolder.newFolder("sample-app") + tmpFolder.newFolder("sample-core") + tmpFolder.newFile("settings.gradle").writeText( + """ + |include ':sample-app' + |include ':sample-core' + """.trimMargin() + ) + + tmpFolder.newFile("build.gradle").writeText( + """buildscript { + | repositories { + | google() + | jcenter() + | } + | dependencies { + | classpath "com.android.tools.build:gradle:4.1.0" + | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" + | } + |} + |plugins { + | id "com.dropbox.affectedmoduledetector" + |} + |affectedModuleDetector { + | excludedModules = [ "sample-core" ] + |} + |allprojects { + | repositories { + | google() + | jcenter() + | } + |}""".trimMargin() + ) + + tmpFolder.newFile("sample-app/build.gradle").writeText( + """plugins { + | id 'com.android.application' + | id 'kotlin-android' + | } + | android { + | compileSdkVersion 30 + | buildToolsVersion "30.0.2" + | } + | dependencies { + | implementation project(":sample-core") + | }""".trimMargin() + ) + + tmpFolder.newFile("sample-core/build.gradle").writeText( + """plugins { + | id 'com.android.library' + | id 'kotlin-android' + | } + | affectedTestConfiguration { + | assembleAndroidTestTask = "assembleAndroidTest" + | } + | android { + | compileSdkVersion 30 + | buildToolsVersion "30.0.2" + | }""".trimMargin() + ) + + // WHEN + val result = GradleRunner.create() + .withProjectDir(tmpFolder.root) + .withPluginClasspath() + .withArguments("assembleAffectedAndroidTests", "--dry-run") + .build() + + // THEN + assertThat(result.output).contains(":sample-app:assembleDebugAndroidTest SKIPPED") + assertThat(result.output).doesNotContain(":sample-core:assembleAndroidTest SKIPPED") + assertThat(result.output).contains(":assembleAffectedAndroidTests SKIPPED") + } } \ No newline at end of file