CodeNarcSensor implements Sensor { @Override public void execute(SensorContext context) { if (context.settings().hasKey(GroovyPlugin.CODENARC_REPORT_PATHS)) { String[] codeNarcReportPaths = context.settings().getStringArray(GroovyPlugin.CODENARC_REPORT_PATHS); String codeNarcReportPath = context.settings().getString(GroovyPlugin.CODENARC_REPORT_PATH); 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); } } else { runCodeNarc(context); } } CodeNarcSensor(RulesProfile profile, GroovyFileSystem groovyFileSystem); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }
@Test public void should_parse() throws Exception { ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "BooleanInstantiation"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "DuplicateImport"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyCatchBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyElseBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyFinallyBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyForStatement"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyIfStatement"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyTryBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "EmptyWhileStatement"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "ImportFromSamePackage"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "ReturnFromFinallyBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "StringInstantiation"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "ThrowExceptionFromFinallyBlock"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "UnnecessaryGroovyImport"); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "UnusedImport"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); 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"); addFileWithFakeContent("src/org/codenarc/sample/service/OtherService.groovy"); addFileWithFakeContent("src/org/codenarc/sample/service/SampleService.groovy"); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).hasSize(17); }
@Test public void should_parse_but_not_add_issue_if_rule_not_found() throws Exception { ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "UnknownRule"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); 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"); addFileWithFakeContent("src/org/codenarc/sample/service/OtherService.groovy"); addFileWithFakeContent("src/org/codenarc/sample/service/SampleService.groovy"); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).isEmpty(); }
@Test public void should_parse_but_not_add_issue_if_inputFile_not_found() throws Exception { ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateFakeRule(activeRulesBuilder, "BooleanInstantiation"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); File reportUpdated = getReportWithUpdatedSourceDir(); sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, reportUpdated.getAbsolutePath()); addFileWithFakeContent("src/org/codenarc/sample/domain/Unknown.groovy"); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).isEmpty(); }
@Test public void should_run_code_narc() throws IOException { addFileWithContent("src/sample.groovy", "package source\nclass SourceFile1 {\n}"); ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateRule(activeRulesBuilder, "org.codenarc.rule.basic.EmptyClassRule", "EmptyClass"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); ActiveRule activeRule = mock(ActiveRule.class); when(activeRule.getRuleKey()).thenReturn("org.codenarc.rule.basic.EmptyClassRule"); when(profile.getActiveRulesByRepository(CodeNarcRulesDefinition.REPOSITORY_KEY)).thenReturn(Arrays.asList(activeRule)); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).hasSize(1); }
@Test public void should_do_nothing_when_can_not_find_report_path() throws Exception { sensorContextTester.settings().setProperty(GroovyPlugin.CODENARC_REPORT_PATHS, "../missing_file.xml"); addFileWithFakeContent("src/org/codenarc/sample/domain/Unknown.groovy"); ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateRule(activeRulesBuilder, "org.codenarc.rule.basic.EmptyClassRule", "EmptyClass"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).isEmpty(); }
@Test public void should_run_code_narc_with_multiple_files() throws IOException { addFileWithContent("src/sample.groovy", "package source\nclass SourceFile1 {\n}"); addFileWithContent("src/foo/bar/qix/sample.groovy", "package source\nclass SourceFile1 {\n}"); ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder(); activeRulesBuilder = activateRule(activeRulesBuilder, "org.codenarc.rule.basic.EmptyClassRule", "EmptyClass"); sensorContextTester.setActiveRules(activeRulesBuilder.build()); ActiveRule activeRule = mock(ActiveRule.class); when(activeRule.getRuleKey()).thenReturn("org.codenarc.rule.basic.EmptyClassRule"); when(profile.getActiveRulesByRepository(CodeNarcRulesDefinition.REPOSITORY_KEY)).thenReturn(Arrays.asList(activeRule)); sensor.execute(sensorContextTester); assertThat(sensorContextTester.allIssues()).hasSize(2); }