CoverageSensor implements Sensor { @Override public void execute(SensorContext context) { Set<String> reports = new HashSet<>(Arrays.asList(context.config().getStringArray(JavaScriptPlugin.LCOV_REPORT_PATHS))); if (context.config().hasKey(JavaScriptPlugin.TS_LCOV_REPORT_PATHS)) { LOG.warn("The use of " + JavaScriptPlugin.TS_LCOV_REPORT_PATHS + " for coverage import is deprecated, use " + JavaScriptPlugin.LCOV_REPORT_PATHS + " instead."); reports.addAll(Arrays.asList(context.config().getStringArray(JavaScriptPlugin.TS_LCOV_REPORT_PATHS))); } List<File> lcovFiles = reports.stream() .map(report -> getIOFile(context.fileSystem().baseDir(), report)) .filter(Objects::nonNull) .collect(Collectors.toList()); if (lcovFiles.isEmpty()) { LOG.warn("No coverage information will be saved because all LCOV files cannot be found."); return; } saveCoverageFromLcovFiles(context, lcovFiles); }  @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }
@Test public void report_not_found() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "/fake/path/lcov_report.dat"); coverageSensor.execute(context); assertThat(context.lineHits("moduleKey:file1.js", 1)).isNull(); }
@Test public void test_coverage() { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, TWO_REPORTS); coverageSensor.execute(context); assertTwoReportsCoverageDataPresent(); }
@Test public void should_work_and_log_warning_when_deprecated_key() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, ""); settings.setProperty(JavaScriptPlugin.TS_LCOV_REPORT_PATHS, TWO_REPORTS); coverageSensor.execute(context); assertTwoReportsCoverageDataPresent(); assertThat(logTester.logs(LoggerLevel.WARN)).contains(DEPRECATED_MESSAGE); }
@Test public void should_include_coverage_from_both_key() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, REPORT1); settings.setProperty(JavaScriptPlugin.TS_LCOV_REPORT_PATHS, REPORT2); coverageSensor.execute(context); assertTwoReportsCoverageDataPresent(); assertThat(logTester.logs(LoggerLevel.WARN)).contains(DEPRECATED_MESSAGE); }
@Test public void both_properties_reports_paths_overlap() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, TWO_REPORTS); settings.setProperty(JavaScriptPlugin.TS_LCOV_REPORT_PATHS, TWO_REPORTS); coverageSensor.execute(context); assertTwoReportsCoverageDataPresent(); assertThat(logTester.logs(LoggerLevel.WARN)).contains(DEPRECATED_MESSAGE); }
@Test public void should_ignore_and_log_warning_for_invalid_line() { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "reports/wrong_line_report.lcov"); coverageSensor.execute(context); assertThat(context.lineHits("moduleKey:file1.js", 0)).isNull(); assertThat(context.lineHits("moduleKey:file1.js", 2)).isEqualTo(1); assertThat(context.conditions("moduleKey:file1.js", 102)).isNull(); assertThat(context.conditions("moduleKey:file1.js", 2)).isEqualTo(3); assertThat(context.coveredConditions("moduleKey:file1.js", 2)).isEqualTo(1); assertThat(logTester.logs()).contains("Problem during processing LCOV report: can't save DA data for line 3 of coverage report file (java.lang.IllegalArgumentException: Line with number 0 doesn't belong to file file1.js)."); assertThat(logTester.logs()).contains("Problem during processing LCOV report: can't save BRDA data for line 8 of coverage report file (java.lang.IllegalArgumentException: Line with number 102 doesn't belong to file file1.js)."); }
@Test public void test_unresolved_path() { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "reports/report_with_unresolved_path.lcov"); coverageSensor.execute(context); String fileName = File.separator + "reports" + File.separator + "report_with_unresolved_path.lcov"; assertThat(logTester.logs(LoggerLevel.WARN)) .contains("Could not resolve 2 file paths in [" + moduleBaseDir.getAbsolutePath() + fileName + "]") .contains("First unresolved path: unresolved/file1.js (Run in DEBUG mode to get full list of unresolved paths)"); assertThat(logTester.logs(LoggerLevel.DEBUG)) .isEmpty(); }
@Test public void test_unresolved_path_with_debug_log() { logTester.setLevel(LoggerLevel.DEBUG); settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "reports/report_with_unresolved_path.lcov"); coverageSensor.execute(context); String fileName = File.separator + "reports" + File.separator + "report_with_unresolved_path.lcov"; assertThat(logTester.logs(LoggerLevel.WARN)) .contains("Could not resolve 2 file paths in [" + moduleBaseDir.getAbsolutePath() + fileName + "]"); assertThat(logTester.logs(LoggerLevel.DEBUG)) .contains("Unresolved paths:\n" + "unresolved/file1.js\n" + "unresolved/file2.js"); }
@Test public void should_log_warning_when_wrong_data() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "reports/wrong_data_report.lcov"); coverageSensor.execute(context); assertThat(context.lineHits("moduleKey:file1.js", 1)).isNull(); assertThat(context.lineHits("moduleKey:file1.js", 2)).isEqualTo(1); assertThat(context.conditions("moduleKey:file1.js", 2)).isEqualTo(2); assertThat(context.coveredConditions("moduleKey:file1.js", 2)).isEqualTo(2); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Problem during processing LCOV report: can't save DA data for line 3 of coverage report file (java.lang.NumberFormatException: For input string: \"1.\")."); String stringIndexOutOfBoundLogMessage = logTester.logs(LoggerLevel.DEBUG).get(1); assertThat(stringIndexOutOfBoundLogMessage).startsWith("Problem during processing LCOV report: can't save DA data for line 4 of coverage report file (java.lang.StringIndexOutOfBoundsException:"); assertThat(logTester.logs(LoggerLevel.DEBUG).get(logTester.logs(LoggerLevel.DEBUG).size() - 1)).startsWith("Problem during processing LCOV report: can't save BRDA data for line 6 of coverage report file (java.lang.ArrayIndexOutOfBoundsException: "); assertThat(logTester.logs(LoggerLevel.WARN)).contains("Found 3 inconsistencies in coverage report. Re-run analyse in debug mode to see details."); }
@Test public void should_resolve_relative_path() throws Exception { settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, "reports/report_relative_path.lcov"); inputFile("deep/nested/dir/js/file1.js", Type.MAIN); inputFile("deep/nested/dir/js/file2.js", Type.MAIN); coverageSensor.execute(context); String file1Key = "moduleKey:deep/nested/dir/js/file1.js"; assertThat(context.lineHits(file1Key, 0)).isNull(); assertThat(context.lineHits(file1Key, 1)).isEqualTo(2); assertThat(context.lineHits(file1Key, 2)).isEqualTo(2); assertThat(context.conditions(file1Key, 102)).isNull(); assertThat(context.conditions(file1Key, 2)).isEqualTo(4); assertThat(context.coveredConditions(file1Key, 2)).isEqualTo(2); String file2Key = "moduleKey:deep/nested/dir/js/file2.js"; assertThat(context.lineHits(file2Key, 0)).isNull(); assertThat(context.lineHits(file2Key, 1)).isEqualTo(5); assertThat(context.lineHits(file2Key, 2)).isEqualTo(5); }
@Test public void should_resolve_absolute_path() throws Exception { File lcovFile = temp.newFile(); String absolutePathFile1 = new File("src/test/resources/coverage/file1.js").getAbsolutePath(); String absolutePathFile2 = new File("src/test/resources/coverage/file2.js").getAbsolutePath(); FileUtils.writeStringToFile(lcovFile, "SF:" + absolutePathFile1 + "\n" + "DA:1,2\n" + "DA:2,2\n" + "DA:3,1\n" + "FN:2,(anonymous_1)\n" + "FNDA:2,(anonymous_1)\n" + "BRDA:2,1,0,2\n" + "BRDA:2,1,1,1\n" + "BRDA:2,2,0,0\n" + "BRDA:2,2,1,-\n" + "end_of_record\n" + "SF:" + absolutePathFile2 + "\n" + "DA:1,5\n" + "DA:2,5\n" + "end_of_record\n", "UTF-8", false); settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, lcovFile.getAbsolutePath()); inputFile("file1.js", Type.MAIN); inputFile("file2.js", Type.MAIN); coverageSensor.execute(context); String file1Key = "moduleKey:file1.js"; assertThat(context.lineHits(file1Key, 0)).isNull(); assertThat(context.lineHits(file1Key, 1)).isEqualTo(2); assertThat(context.lineHits(file1Key, 2)).isEqualTo(2); assertThat(context.conditions(file1Key, 102)).isNull(); assertThat(context.conditions(file1Key, 2)).isEqualTo(4); assertThat(context.coveredConditions(file1Key, 2)).isEqualTo(2); String file2Key = "moduleKey:file2.js"; assertThat(context.lineHits(file2Key, 0)).isNull(); assertThat(context.lineHits(file2Key, 1)).isEqualTo(5); assertThat(context.lineHits(file2Key, 2)).isEqualTo(5); }
@Test public void should_import_coverage_for_ts() throws Exception { DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", "src/file1.ts") .setModuleBaseDir(moduleBaseDir.toPath()) .setLanguage("ts") .setContents("function foo(x: any) {\n" + " if (x && !x)\n" + " console.log(\"file1\");\n" + "}\n") .build(); context.fileSystem().add(inputFile); File lcov = temp.newFile(); FileUtils.writeStringToFile(lcov, "SF:src/file1.ts\n" + "DA:1,2\n" + "DA:2,2\n" + "DA:3,1\n" + "FN:2,(anonymous_1)\n" + "FNDA:2,(anonymous_1)\n" + "BRDA:2,1,0,2\n" + "BRDA:2,1,1,1\n" + "BRDA:2,2,0,0\n" + "BRDA:2,2,1,-\n" + "end_of_record\n" + "SF:src/file2.ts\n" + "DA:1,5\n" + "DA:2,5\n" + "end_of_record\n", StandardCharsets.UTF_8); settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATHS, lcov.getAbsolutePath()); coverageSensor.execute(context); assertThat(context.lineHits(inputFile.key(), 1)).isEqualTo(2); assertThat(context.lineHits(inputFile.key(), 2)).isEqualTo(2); assertThat(context.lineHits(inputFile.key(), 3)).isEqualTo(1); assertThat(context.lineHits(inputFile.key(), 0)).isNull(); }