org.sonar.api.batch.sensor.coverage.NewCoverage Java Examples
The following examples show how to use
org.sonar.api.batch.sensor.coverage.NewCoverage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleCovParser.java From sonar-ruby-plugin with MIT License | 6 votes |
@SuppressWarnings("unchecked") public Map<String, NewCoverage> parse(SensorContext ctx, File resultFile) throws IOException { Map<String, Map<String, Map<String, List<Integer>>>> results = parseResultsFile(resultFile); String testFramework = ctx.settings().getString(RubyPlugin.TEST_FRAMEWORK); if (isNull(testFramework)) { LOG.warn("Test framework is not set, unable to parse coverage metrics"); return null; } Map<String, List<Integer>> coverageByFile = results.get(testFramework).get("coverage"); Iterable<InputFile> inputFiles = ctx.fileSystem().inputFiles(ctx.fileSystem().predicates().hasLanguage(Ruby.LANGUAGE_KEY)); return StreamSupport.stream(inputFiles.spliterator(), false) .collect(Collectors.toMap(InputFile::absolutePath, file -> buildCoverageForFile(ctx, file, coverageByFile.get(file.absolutePath())))); }
Example #2
Source File: CoberturaReportParser.java From sonar-lua with GNU Lesser General Public License v3.0 | 6 votes |
private static void collectFileData(SMInputCursor clazz, NewCoverage newCoverage) throws XMLStreamException { SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { int lineId = Integer.parseInt(line.getAttrValue("number")); try { newCoverage.lineHits(lineId, (int) ParsingUtils.parseNumber(line.getAttrValue("hits"), Locale.ENGLISH)); } catch (ParseException e) { throw new IllegalStateException(e); } String isBranch = line.getAttrValue("branch"); String text = line.getAttrValue("condition-coverage"); if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); newCoverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); } } newCoverage.save(); }
Example #3
Source File: TsCoverageSensorImpl.java From SonarTsPlugin with MIT License | 6 votes |
private void saveZeroValue(InputFile inputFile, SensorContext context, Set<Integer> nonCommentLineNumbers) { NewCoverage newCoverage = context .newCoverage() .ofType(CoverageType.UNIT) .onFile(inputFile); if (nonCommentLineNumbers != null) { for (Integer nonCommentLineNumber : nonCommentLineNumbers) { newCoverage.lineHits(nonCommentLineNumber, 0); } } else { for (int i = 1; i <= inputFile.lines(); i++) { newCoverage.lineHits(i, 0); } } newCoverage.save(); }
Example #4
Source File: SimpleCovSensor.java From sonar-ruby-plugin with MIT License | 6 votes |
public void execute(SensorContext context) { String reportPath = context.settings().getString(RubyPlugin.SIMPLECOV_REPORT_PATH); if (isNull(reportPath)) { LOG.warn("Report path is not set, unable to generate coverage metrics"); return; } SimpleCovParser parser = new SimpleCovParser(); try { Map<String, NewCoverage> fileCoverages = parser.parse(context, new File(reportPath)); fileCoverages.values().forEach(NewCoverage::save); } catch (IOException e) { LOG.warn("Unable to generate coverage metrics", e); } }
Example #5
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void parsesAngularTemplateLoaderOutput() { Map<InputFile, NewCoverage> coverage = executeForTestCase("angular"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertEquals((Integer) 3, c.hitsByLine().get(1)); assertEquals((Integer) 0, c.hitsByLine().get(2)); assertEquals((Integer) 1, c.hitsByLine().get(3)); assertEquals(3, c.linesToCover()); }
Example #6
Source File: CloverageSensor.java From sonar-clojure with MIT License | 5 votes |
private void saveCoverageForFile(CoverageReport report, SensorContext context) { report.getFileEntries().forEach(fileAnalysis -> { NewCoverage coverage = context.newCoverage().onFile(fileAnalysis.getFile()); fileAnalysis.getEntries().forEach(lineAnalysis -> coverage.lineHits(lineAnalysis.getLineNumber(), lineAnalysis.getHits())); coverage.save(); }); }
Example #7
Source File: CloverXmlReportParser.java From sonar-clover with Apache License 2.0 | 5 votes |
private void saveHitsData(InputFile resource, SMInputCursor lineCursor) throws ParseException, XMLStreamException { final NewCoverage coverage = context.newCoverage().onFile(resource); // cursor should be on the metrics element if (!canBeIncludedInFileMetrics(lineCursor)) { // cursor should now be on the line cursor; exclude this file if there are no elements to cover ((DefaultInputFile) resource).setExcludedForCoverage(true); } while (lineCursor.getNext() != null) { // skip class elements on format 2_3_2 if (isClass(lineCursor)) { continue; } final int lineId = Integer.parseInt(lineCursor.getAttrValue("num")); String count = lineCursor.getAttrValue("count"); if (StringUtils.isNotBlank(count)) { final int hits = Integer.parseInt(count); coverage.lineHits(lineId, hits); } else { int trueCount = (int) ParsingUtils.parseNumber(lineCursor.getAttrValue("truecount")); int falseCount = (int) ParsingUtils.parseNumber(lineCursor.getAttrValue("falsecount")); int coveredConditions = 0; if (trueCount > 0) { coveredConditions++; } if (falseCount > 0) { coveredConditions++; } coverage.conditions(lineId, 2, coveredConditions); } } coverage.save(); }
Example #8
Source File: TsCoverageSensorImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void saveCoverage_WhenMultipleLCOVPathsSupplied() { when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn("lcovpath,lcovpath2"); HashMap<InputFile, NewCoverage> allFilesCoverage = new HashMap<InputFile, NewCoverage>(); NewCoverage fileCoverage = spy(this.context.newCoverage()); allFilesCoverage.put(this.file, fileCoverage); when(this.parser.coverageByFile()).thenReturn(allFilesCoverage); doReturn(this.lcovFile).when(this.sensor).getIOFile(any(File.class), eq("lcovpath2")); this.sensor.execute(this.context, null); verify(fileCoverage, times(1)).save(); verify(this.sensor).getParser(eq(this.context),argThat(files -> files.length == 2)); }
Example #9
Source File: TsCoverageSensorImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void savesCoverage_IfParserOutputHasDetailsForFile() { HashMap<InputFile, NewCoverage> allFilesCoverage = new HashMap<InputFile, NewCoverage>(); NewCoverage fileCoverage = spy(this.context.newCoverage()); allFilesCoverage.put(this.file, fileCoverage); when(this.parser.coverageByFile()).thenReturn(allFilesCoverage); this.sensor.execute(this.context, null); verify(fileCoverage, times(1)).save(); }
Example #10
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void handlesOutOfRangeLineNumbers() { Map<InputFile, NewCoverage> coverage = executeForTestCase("outofrangelines"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertNotNull(c); assertEquals(1, coverage.size()); assertEquals((Integer) 3, c.hitsByLine().get(1)); }
Example #11
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void handlesFilesEndingWithExclamationMarkIfNotPartOfSet() { Map<InputFile, NewCoverage> coverage = executeForTestCase("angularendswithbang"); assertNotNull(coverage); assertEquals(0, coverage.size()); }
Example #12
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void ignoresFilesNotPartOfAnalysisSet() { Map<InputFile, NewCoverage> coverage = executeForTestCase("existingandnot"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertNotNull(c); assertEquals(1, coverage.size()); }
Example #13
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void handlesNoLineHitsForASingleFile() { Map<InputFile, NewCoverage> coverage = executeForTestCase("nolinehits"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertEquals(1, coverage.size()); assertNotNull(c); assertNull(c.hitsByLine().get(1)); assertNull(c.hitsByLine().get(2)); assertNull(c.hitsByLine().get(3)); }
Example #14
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void handlesNoContent() { Map<InputFile, NewCoverage> coverage = executeForTestCase("blank"); assertNotNull(coverage); assertEquals(0, coverage.size()); }
Example #15
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void parsesAngularTemplateLoaderOutput_IfPassedThroughMultipleLoaders() { Map<InputFile, NewCoverage> coverage = executeForTestCase("angularmanybangs"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertEquals((Integer) 3, c.hitsByLine().get(1)); assertEquals((Integer) 0, c.hitsByLine().get(2)); assertEquals((Integer) 1, c.hitsByLine().get(3)); assertEquals(3, c.linesToCover()); }
Example #16
Source File: SimpleCovParser.java From sonar-ruby-plugin with MIT License | 5 votes |
private void updateForZeroCoverage(InputFile inputFile, NewCoverage newCoverage, Set<Integer> nonCommentLineNumbers) { if (nonCommentLineNumbers != null) { nonCommentLineNumbers.forEach(lineNumber -> newCoverage.lineHits(lineNumber, 0)); } else { IntStream.rangeClosed(0, inputFile.lines()) .forEach(idx -> newCoverage.lineHits(idx, 0)); } }
Example #17
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 5 votes |
@Test public void parsesBasicLcovFiles() { Map<InputFile, NewCoverage> coverage = executeForTestCase("basic"); DefaultCoverage c = (DefaultCoverage) coverage.get(this.inputFile); assertEquals((Integer) 3, c.hitsByLine().get(1)); assertEquals((Integer) 0, c.hitsByLine().get(2)); assertEquals((Integer) 1, c.hitsByLine().get(3)); assertEquals(3, c.linesToCover()); }
Example #18
Source File: SimpleCovParser.java From sonar-ruby-plugin with MIT License | 5 votes |
private NewCoverage buildCoverageForFile(SensorContext ctx, InputFile file, List<Integer> lineCounts) { NewCoverage coverage = ctx.newCoverage() .onFile(file) .ofType(CoverageType.UNIT); if (lineCounts == null || lineCounts.isEmpty()) { updateForZeroCoverage(file, coverage, gatherNonCommentLinesOfCodeForFile(file)); } else { IntStream.range(0, lineCounts.size()) .forEach(idx -> coverageForLine(coverage, idx, lineCounts.get(idx))); } return coverage; }
Example #19
Source File: LCOVParserImpl.java From SonarTsPlugin with MIT License | 5 votes |
@Override public Map<InputFile, NewCoverage> parseFile(File file) { final List<String> lines; try { lines = Files.readAllLines(file.toPath()); } catch (IOException e) { throw new IllegalArgumentException("Could not read content from file: " + file, e); } return parse(lines); }
Example #20
Source File: SimpleCovParserTest.java From sonar-ruby-plugin with MIT License | 5 votes |
@Test public void testParse() throws URISyntaxException, IOException { SimpleCovParser parser = new SimpleCovParser(); URL filePath = SimpleCovParserTest.class.getClassLoader().getResource(".resultset.json"); File resultFile = new File(filePath.toURI()); Map<String, NewCoverage> coverage = parser.parse(context, resultFile); assertThat(coverage).isNotNull(); DefaultCoverage defaultCoverage = (DefaultCoverage) coverage.get(rubyFile.getAbsolutePath()); assertThat(defaultCoverage.coveredLines()).isEqualTo(4); }
Example #21
Source File: TsCoverageSensorImpl.java From SonarTsPlugin with MIT License | 4 votes |
protected void saveMeasureFromLCOVFile(SensorContext context, Map<InputFile, Set<Integer>> nonCommentLineNumbersByFile, List<String> reportPaths) { LinkedList<File> lcovFiles =new LinkedList<>(); for(String providedPath: reportPaths) { File lcovFile = getIOFile(context.fileSystem().baseDir(), providedPath); if (lcovFile.isFile()) { lcovFiles.add(lcovFile); } else { LOG.warn("No coverage information will be saved because LCOV file cannot be found."); LOG.warn("Provided LCOV file path: {}. Seek file with path: {}", providedPath, lcovFile.getAbsolutePath()); } } if(lcovFiles.isEmpty()) { LOG.warn("No coverage information will be saved because all LCOV files cannot be found."); return; } LOG.info("Analysing {}", lcovFiles); LCOVParser parser = getParser(context, lcovFiles.toArray(new File[lcovFiles.size()])); Map<InputFile, NewCoverage> coveredFiles = parser.coverageByFile(); final boolean ignoreNotFound = isIgnoreNotFoundActivated(context); for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguage(TypeScriptLanguage.LANGUAGE_KEY))) { try { NewCoverage fileCoverage = coveredFiles.get(file); if (fileCoverage != null) { fileCoverage.save(); } else if (!ignoreNotFound) { // colour all lines as not executed LOG.debug("Default value of zero will be saved for file: {}", file.relativePath()); LOG.debug("Because was not present in LCOV report."); saveZeroValue(file, context, nonCommentLineNumbersByFile.get(file)); } } catch (Exception e) { LOG.error("Problem while calculating coverage for " + file.absolutePath(), e); } } }
Example #22
Source File: LCOVParserImplTest.java From SonarTsPlugin with MIT License | 4 votes |
private Map<InputFile, NewCoverage> executeForTestCase(String testName) { File lcovFile = resource(testName); LCOVParser parser = getParser(lcovFile); return parser.parseFile(lcovFile); }
Example #23
Source File: LCOVParserImpl.java From SonarTsPlugin with MIT License | 4 votes |
public Map<InputFile, NewCoverage> coverageByFile() { return coverageByFile; }
Example #24
Source File: SimpleCovParser.java From sonar-ruby-plugin with MIT License | 4 votes |
private void coverageForLine(NewCoverage coverage, int lineNumber, Integer lineCount) { if (nonNull(lineCount)) { coverage.lineHits(lineNumber + 1, lineCount); } }
Example #25
Source File: LCOVParser.java From SonarTsPlugin with MIT License | votes |
Map<InputFile, NewCoverage> parse(List<String> lines);
Example #26
Source File: LCOVParser.java From SonarTsPlugin with MIT License | votes |
Map<InputFile, NewCoverage> parseFile(File file);
Example #27
Source File: LCOVParser.java From SonarTsPlugin with MIT License | votes |
Map<InputFile, NewCoverage> coverageByFile();