diff --git a/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/CollectDetektViolationsTask.groovy b/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/CollectDetektViolationsTask.groovy deleted file mode 100644 index 845bc04..0000000 --- a/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/CollectDetektViolationsTask.groovy +++ /dev/null @@ -1,17 +0,0 @@ -package com.novoda.staticanalysis.internal.detekt - -import com.novoda.staticanalysis.Violations -import com.novoda.staticanalysis.internal.CollectViolationsTask -import groovy.util.slurpersupport.GPathResult - -class CollectDetektViolationsTask extends CollectViolationsTask { - - @Override - void collectViolations(File xmlReportFile, File htmlReportFile, Violations violations) { - GPathResult xml = new XmlSlurper().parse(xmlReportFile) - int errors = xml.'**'.findAll { node -> node.name() == 'error' && node.@severity == 'error' }.size() - int warnings = xml.'**'.findAll { node -> node.name() == 'error' && node.@severity == 'warning' }.size() - violations.addViolations(errors, warnings, htmlReportFile ?: xmlReportFile) - } - -} diff --git a/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/DetektConfigurator.groovy b/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/DetektConfigurator.groovy index 63ebbc4..f95775d 100644 --- a/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/DetektConfigurator.groovy +++ b/plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/DetektConfigurator.groovy @@ -3,6 +3,7 @@ package com.novoda.staticanalysis.internal.detekt import com.novoda.staticanalysis.StaticAnalysisExtension import com.novoda.staticanalysis.Violations import com.novoda.staticanalysis.internal.Configurator +import com.novoda.staticanalysis.internal.checkstyle.CollectCheckstyleViolationsTask import org.gradle.api.GradleException import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project @@ -11,15 +12,17 @@ import org.gradle.api.Task class DetektConfigurator implements Configurator { private static final String DETEKT_PLUGIN = 'io.gitlab.arturbosch.detekt' - private static final String LAST_COMPATIBLE_DETEKT_VERSION = '1.0.0.RC9.2' + private static final String LAST_COMPATIBLE_DETEKT_VERSION = '1.0.0-RC10' private static final String DETEKT_NOT_APPLIED = 'The Detekt plugin is configured but not applied. Please apply the plugin in your build script.\nFor more information see https://github.com/arturbosch/detekt.' private static final String OUTPUT_NOT_DEFINED = 'Output not defined! To analyze the results, `output` needs to be defined in Detekt profile.' private static final String DETEKT_CONFIGURATION_ERROR = "A problem occurred while configuring Detekt. Please make sure to use a compatible version (All versions up to $LAST_COMPATIBLE_DETEKT_VERSION)" + private static final String XML_REPORT_NOT_ENABLED = 'XML report must be enabled. Please make sure to enable "reports.xml" in your Detekt configuration' private final Project project private final Violations violations private final Task evaluateViolations + static DetektConfigurator create(Project project, NamedDomainObjectContainer violationsContainer, Task evaluateViolations) { @@ -45,6 +48,7 @@ class DetektConfigurator implements Configurator { } def detekt = project.extensions.findByName('detekt') + setDefaultXmlReport(detekt) config.delegate = detekt config() @@ -52,11 +56,23 @@ class DetektConfigurator implements Configurator { evaluateViolations.dependsOn collectViolations } } - - private CollectDetektViolationsTask configureToolTask(detekt) { + + private void setDefaultXmlReport(detekt) { + if (detekt.hasProperty('reports')) { + detekt.reports { + xml.enabled = true + xml.destination = new File(project.buildDir, 'reports/detekt/detekt.xml') + } + } + } + + private CollectCheckstyleViolationsTask configureToolTask(detekt) { def detektTask = project.tasks.findByName('detekt') if (detektTask?.hasProperty('reports')) { def reports = detektTask.reports + if (!reports.xml.enabled) { + throw new IllegalStateException(XML_REPORT_NOT_ENABLED) + } return createCollectViolationsTask( violations, detektTask, @@ -89,8 +105,8 @@ class DetektConfigurator implements Configurator { } } - private CollectDetektViolationsTask createCollectViolationsTask(Violations violations, detektTask, File xmlReportFile, File htmlReportFile) { - project.tasks.create('collectDetektViolations', CollectDetektViolationsTask) { task -> + private CollectCheckstyleViolationsTask createCollectViolationsTask(Violations violations, detektTask, File xmlReportFile, File htmlReportFile) { + project.tasks.create('collectDetektViolations', CollectCheckstyleViolationsTask) { task -> task.xmlReportFile = xmlReportFile task.htmlReportFile = htmlReportFile task.violations = violations diff --git a/plugin/src/test/fixtures/rules/detekt/detekt.yml b/plugin/src/test/fixtures/rules/detekt/detekt.yml index 6f3c5f6..a29e766 100644 --- a/plugin/src/test/fixtures/rules/detekt/detekt.yml +++ b/plugin/src/test/fixtures/rules/detekt/detekt.yml @@ -29,22 +29,10 @@ build: comments: 1 processors: - active: true - exclude: - # - 'FunctionCountProcessor' - # - 'PropertyCountProcessor' - # - 'ClassCountProcessor' - # - 'PackageCountProcessor' - # - 'KtFileCountProcessor' + active: false console-reports: - active: true - exclude: - # - 'ProjectStatisticsReport' - # - 'ComplexityReport' - # - 'NotificationReport' - # - 'FindingsReport' - # - 'BuildFailureReport' + active: false output-reports: active: true @@ -364,4 +352,4 @@ style: active: false WildcardImport: active: true - excludeImports: 'java.util.*,kotlinx.android.synthetic.*' \ No newline at end of file + excludeImports: 'java.util.*,kotlinx.android.synthetic.*' diff --git a/plugin/src/test/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy b/plugin/src/test/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy index 83e222c..ca65ded 100644 --- a/plugin/src/test/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy +++ b/plugin/src/test/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy @@ -1,5 +1,6 @@ package com.novoda.staticanalysis.internal.detekt + import com.novoda.test.Fixtures import com.novoda.test.TestProject import com.novoda.test.TestProjectRule @@ -14,12 +15,16 @@ import static com.novoda.test.LogsSubject.assertThat class DetektNewIntegrationTest { private static final String DETEKT_NOT_APPLIED = 'The Detekt plugin is configured but not applied. Please apply the plugin in your build script.' + private static final String XML_REPORT_NOT_ENABLED = 'XML report must be enabled. Please make sure to enable "reports.xml" in your Detekt configuration' + @Parameterized.Parameters(name = "{0} with Detekt: {1}") static Iterable rules() { return [ [TestProjectRule.forKotlinProject(), "1.0.0.RC9.2"], [TestProjectRule.forAndroidKotlinProject(), "1.0.0.RC9.2"], + [TestProjectRule.forKotlinProject(), "1.0.0-RC10"], + [TestProjectRule.forAndroidKotlinProject(), "1.0.0-RC10"], ]*.toArray() } @@ -41,6 +46,20 @@ class DetektNewIntegrationTest { assertThat(result.logs).contains(DETEKT_NOT_APPLIED) } + @Test + void shouldFailBuildOnConfigurationWhenDetektConfiguredWithoutXmlReport() { + def result = projectRule.newProject() + .withPlugin("io.gitlab.arturbosch.detekt", detektVersion) + .withToolsConfig('''detekt { + reports { + xml.enabled = false + } + }''') + .buildAndFail('check') + + assertThat(result.logs).contains(XML_REPORT_NOT_ENABLED) + } + @Test void shouldFailBuildWhenDetektWarningsOverTheThreshold() { def result = createProjectWithZeroThreshold(Fixtures.Detekt.SOURCES_WITH_WARNINGS)