Skip to content

Commit

Permalink
fix pmayweg#54 multiple codenarc files (pmayweg#60)
Browse files Browse the repository at this point in the history
* fix pmayweg#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
  • Loading branch information
musketyr authored and pmayweg committed Nov 9, 2017
1 parent c216e9e commit 7744d9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<File> reports = new ArrayList<File>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");

Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 7744d9e

Please sign in to comment.