From a914d49419b637aae2b4be61a7d3c24b223247c7 Mon Sep 17 00:00:00 2001 From: Said Tahsin Dane Date: Wed, 7 Nov 2018 16:57:41 +0100 Subject: [PATCH 1/3] Update detekt config to clean the output for test projects --- .../src/test/fixtures/rules/detekt/detekt.yml | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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.*' From 5f2a79008d1db6ac066c63303b635afb127e77f0 Mon Sep 17 00:00:00 2001 From: Said Tahsin Dane Date: Wed, 7 Nov 2018 16:59:45 +0100 Subject: [PATCH 2/3] Use the same checkstyle collection task since Detekt uses the standard checkstyle output --- .../detekt/CollectDetektViolationsTask.groovy | 17 ----------------- .../internal/detekt/DetektConfigurator.groovy | 7 ++++--- .../detekt/DetektNewIntegrationTest.groovy | 6 ++++-- 3 files changed, 8 insertions(+), 22 deletions(-) delete mode 100644 plugin/src/main/groovy/com/novoda/staticanalysis/internal/detekt/CollectDetektViolationsTask.groovy 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..d33b3c0 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 @@ -53,7 +54,7 @@ class DetektConfigurator implements Configurator { } } - private CollectDetektViolationsTask configureToolTask(detekt) { + private CollectCheckstyleViolationsTask configureToolTask(detekt) { def detektTask = project.tasks.findByName('detekt') if (detektTask?.hasProperty('reports')) { def reports = detektTask.reports @@ -89,8 +90,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/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy b/plugin/src/test/groovy/com/novoda/staticanalysis/internal/detekt/DetektNewIntegrationTest.groovy index 83e222c..380302e 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 @@ -18,8 +18,10 @@ class DetektNewIntegrationTest { @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.RC9.2"], +// [TestProjectRule.forAndroidKotlinProject(), "1.0.0.RC9.2"], + [TestProjectRule.forKotlinProject(), "1.0.0-RC10"], +// [TestProjectRule.forAndroidKotlinProject(), "1.0.0-RC10"], ]*.toArray() } From 7d3d1dd06ecaaf34e8e6400b5004a031554421ff Mon Sep 17 00:00:00 2001 From: Said Tahsin Dane Date: Wed, 7 Nov 2018 17:56:44 +0100 Subject: [PATCH 3/3] Add xml report path by default for newer version of detekt --- .../internal/detekt/DetektConfigurator.groovy | 19 +++++++++++++-- .../detekt/DetektNewIntegrationTest.groovy | 23 ++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) 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 d33b3c0..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 @@ -12,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) { @@ -46,6 +48,7 @@ class DetektConfigurator implements Configurator { } def detekt = project.extensions.findByName('detekt') + setDefaultXmlReport(detekt) config.delegate = detekt config() @@ -53,11 +56,23 @@ class DetektConfigurator implements Configurator { evaluateViolations.dependsOn collectViolations } } - + + 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, 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 380302e..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,14 +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.RC9.2"], + [TestProjectRule.forAndroidKotlinProject(), "1.0.0.RC9.2"], [TestProjectRule.forKotlinProject(), "1.0.0-RC10"], -// [TestProjectRule.forAndroidKotlinProject(), "1.0.0-RC10"], + [TestProjectRule.forAndroidKotlinProject(), "1.0.0-RC10"], ]*.toArray() } @@ -43,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)