From 7744d9eef0c9be57e975b4096b2a9487508a630a Mon Sep 17 00:00:00 2001 From: Vladimir Orany Date: Thu, 9 Nov 2017 08:10:13 +0100 Subject: [PATCH] fix #54 multiple codenarc files (#60) * fix #54 multiple codenarc files * fixed import, used proper method from settings * fixed variable type to String array * ignoring report files which does not exist * renamed the configuration property to `sonar.groovy.codenarc.reportPaths` * changed the property name also in README * added fallback to old property name I guess otherwise it won't get translated if anyone pass the configuration property from the command line --- README.md | 2 +- .../sonar/plugins/groovy/GroovyPlugin.java | 13 ++++++---- .../groovy/codenarc/CodeNarcSensor.java | 24 +++++++++++++------ .../groovy/codenarc/CodeNarcSensorTest.java | 8 +++---- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 71689a5a..62ca4e51 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ GMetrics | 0.2 | 0.2 | 0.3 | 0.3 | 0.4 | 0.5 | 0.6 | 0.6 | 0.7 | 0.7 ## Notes *CodeNarc* -It is possible to reuse a previously generated report from CodeNarc by setting the `sonar.groovy.codenarc.reportPath` property. +It is possible to reuse a previously generated report from CodeNarc by setting the `sonar.groovy.codenarc.reportPaths` property. *Groovy File Suffixes* It is possible to define multiple groovy file suffixes to be recognized by setting the `sonar.groovy.file.suffixes` property. Note that by default, only files having `.groovy` as extension will be analyzed. diff --git a/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/GroovyPlugin.java b/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/GroovyPlugin.java index 45dbf731..69f47608 100644 --- a/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/GroovyPlugin.java +++ b/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/GroovyPlugin.java @@ -35,12 +35,13 @@ @Properties({ @Property( - key = GroovyPlugin.CODENARC_REPORT_PATH, - name = "CodeNarc Report", - description = "Path to the CodeNarc XML report. Path may be absolute or relative to the project base directory.", + key = GroovyPlugin.CODENARC_REPORT_PATHS, + name = "CodeNarc Reports", + description = "Path to the CodeNarc XML reports. Paths may be absolute or relative to the project base directory.", project = true, module = true, - global = true), + global = true, + deprecatedKey = GroovyPlugin.CODENARC_REPORT_PATH), @Property( key = GroovyPlugin.COBERTURA_REPORT_PATH, name = "Cobertura Report", @@ -76,7 +77,9 @@ }) public class GroovyPlugin implements Plugin { - public static final String CODENARC_REPORT_PATH = "sonar.groovy.codenarc.reportPath"; + @Deprecated public static final String CODENARC_REPORT_PATH = "sonar.groovy.codenarc.reportPath"; + public static final String CODENARC_REPORT_PATHS = "sonar.groovy.codenarc.reportPaths"; + public static final String COBERTURA_REPORT_PATH = "sonar.groovy.cobertura.reportPath"; public static final String IGNORE_HEADER_COMMENTS = "sonar.groovy.ignoreHeaderComments"; diff --git a/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensor.java b/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensor.java index 100be8e5..2780e3a9 100644 --- a/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensor.java +++ b/sonar-groovy-plugin/src/main/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensor.java @@ -22,8 +22,8 @@ import java.io.File; import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -71,15 +71,25 @@ public void describe(SensorDescriptor descriptor) { @Override public void execute(SensorContext context) { // Should we reuse existing report from CodeNarc ? - if (context.settings().hasKey(GroovyPlugin.CODENARC_REPORT_PATH)) { + if (context.settings().hasKey(GroovyPlugin.CODENARC_REPORT_PATHS)) { // Yes + String[] codeNarcReportPaths = context.settings().getStringArray(GroovyPlugin.CODENARC_REPORT_PATHS); String codeNarcReportPath = context.settings().getString(GroovyPlugin.CODENARC_REPORT_PATH); - File report = context.fileSystem().resolvePath(codeNarcReportPath); - if (!report.isFile()) { - LOG.warn("Groovy report " + GroovyPlugin.CODENARC_REPORT_PATH + " not found at {}", report); - return; + if (codeNarcReportPaths.length == 0) { + codeNarcReportPaths = new String[] { codeNarcReportPath }; + } + List reports = new ArrayList(); + for (String path : codeNarcReportPaths) { + File report = context.fileSystem().resolvePath(path); + if (!report.isFile() || !report.exists()) { + LOG.warn("Groovy report " + GroovyPlugin.CODENARC_REPORT_PATHS + " not found at {}", report); + } else { + reports.add(report); + } + } + if (!reports.isEmpty()) { + parseReport(context, reports); } - parseReport(context, Collections.singletonList(report)); } else { // No, run CodeNarc runCodeNarc(context); diff --git a/sonar-groovy-plugin/src/test/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensorTest.java b/sonar-groovy-plugin/src/test/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensorTest.java index 2fbbd187..aa964d6d 100644 --- a/sonar-groovy-plugin/src/test/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensorTest.java +++ b/sonar-groovy-plugin/src/test/java/org/sonar/plugins/groovy/codenarc/CodeNarcSensorTest.java @@ -100,7 +100,7 @@ public void should_parse() throws Exception { sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); - sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATH, reportUpdated.getAbsolutePath()); + sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, reportUpdated.getAbsolutePath()); addFileWithFakeContent("src/org/codenarc/sample/domain/SampleDomain.groovy"); addFileWithFakeContent("src/org/codenarc/sample/service/NewService.groovy"); @@ -120,7 +120,7 @@ public void should_parse_but_not_add_issue_if_rule_not_found() throws Exception sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); - sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATH, reportUpdated.getAbsolutePath()); + sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, reportUpdated.getAbsolutePath()); addFileWithFakeContent("src/org/codenarc/sample/domain/SampleDomain.groovy"); addFileWithFakeContent("src/org/codenarc/sample/service/NewService.groovy"); @@ -140,7 +140,7 @@ public void should_parse_but_not_add_issue_if_inputFile_not_found() throws Excep sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); - sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATH, reportUpdated.getAbsolutePath()); + sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, reportUpdated.getAbsolutePath()); addFileWithFakeContent("src/org/codenarc/sample/domain/Unknown.groovy"); @@ -170,7 +170,7 @@ public void should_run_code_narc() throws IOException { @Test public void should_do_nothing_when_can_not_find_report_path() throws Exception { - sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATH, "../missing_file.xml"); + sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, "../missing_file.xml"); addFileWithFakeContent("src/org/codenarc/sample/domain/Unknown.groovy");