Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Commit

Permalink
Merge f2241b9 into 840bcff
Browse files Browse the repository at this point in the history
  • Loading branch information
kingatlas committed Jul 7, 2017
2 parents 840bcff + f2241b9 commit 7ece93f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@
<groupId>com.pablissimo.sonar</groupId>
<artifactId>sonar-typescript-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>1.1.0</version>
<version>1.2.0-rc1</version>

<name>SonarTsPlugin</name>
<description>Analyse TypeScript projects</description>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/pablissimo/sonar/LCOVParser.java
Expand Up @@ -10,6 +10,7 @@

@BatchSide
public interface LCOVParser {
Map<InputFile, NewCoverage> coverageByFile();
Map<InputFile, NewCoverage> parseFile(File file);
Map<InputFile, NewCoverage> parse(List<String> lines);
}
2 changes: 1 addition & 1 deletion src/main/java/com/pablissimo/sonar/LCOVParserImpl.java
Expand Up @@ -74,7 +74,7 @@ static LCOVParser create(SensorContext context, File... files) {
return new LCOVParserImpl(lines, context);
}

Map<InputFile, NewCoverage> coverageByFile() {
public Map<InputFile, NewCoverage> coverageByFile() {
return coverageByFile;
}

Expand Down
55 changes: 37 additions & 18 deletions src/main/java/com/pablissimo/sonar/TsCoverageSensorImpl.java
Expand Up @@ -28,14 +28,26 @@
import org.sonar.api.batch.sensor.coverage.NewCoverage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

public class TsCoverageSensorImpl implements TsCoverageSensor {

private static final Logger LOG = LoggerFactory.getLogger(TsCoverageSensorImpl.class);

/**
* Returns list of paths provided by "propertyValue" (divided by comma)
*/
private static List<String> parseReportsProperty(String propertyValue) {
List<String> reportPaths = new ArrayList<>();
for (String path : propertyValue.split(",")) {
if (!path.trim().isEmpty()) {
reportPaths.add(path.trim());
}
}

return reportPaths;
}

private void saveZeroValueForAllFiles(SensorContext context, Map<InputFile, Set<Integer>> nonCommentLineNumbersByFile) {
for (InputFile inputFile : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguage(TypeScriptLanguage.LANGUAGE_KEY))) {
saveZeroValue(inputFile, context, nonCommentLineNumbersByFile.get(inputFile));
Expand Down Expand Up @@ -63,19 +75,29 @@ private void saveZeroValue(InputFile inputFile, SensorContext context, Set<Integ
newCoverage.save();
}

protected void saveMeasureFromLCOVFile(SensorContext context, Map<InputFile, Set<Integer>> nonCommentLineNumbersByFile) {
String providedPath = context.settings().getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH);
File lcovFile = getIOFile(context.fileSystem().baseDir(), providedPath);
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()) {
LOG.warn("No coverage information will be saved because LCOV file cannot be analysed. Provided LCOV file path: {}", 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 {}", lcovFile);
LOG.info("Analysing {}", lcovFiles);

LCOVParser parser = getParser(context, new File[]{ lcovFile });
Map<InputFile, NewCoverage> coveredFiles = parser.parseFile(lcovFile);
LCOVParser parser = getParser(context, lcovFiles.toArray(new File[lcovFiles.size()]));
Map<InputFile, NewCoverage> coveredFiles = parser.coverageByFile();

final boolean ignoreNotFound = isIgnoreNotFoundActivated(context);

Expand Down Expand Up @@ -115,10 +137,6 @@ private boolean isIgnoreNotFoundActivated(SensorContext ctx) {
return ctx.settings().getBoolean(TypeScriptPlugin.SETTING_IGNORE_NOT_FOUND);
}

private boolean isLCOVReportProvided(SensorContext ctx) {
return StringUtils.isNotBlank(ctx.settings().getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH));
}

public File getIOFile(File baseDir, String path) {
File file = new File(path);
if (!file.isAbsolute()) {
Expand All @@ -131,13 +149,14 @@ public File getIOFile(File baseDir, String path) {
@Override
public void execute(SensorContext ctx, Map<InputFile, Set<Integer>> nonCommentLineNumbersByFile) {
Map<InputFile, Set<Integer>> nonCommentLineMap = nonCommentLineNumbersByFile;

List<String> reportPaths = parseReportsProperty(ctx.settings().getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH));

if (nonCommentLineMap == null) {
nonCommentLineMap = new HashMap<>();
}

if (isLCOVReportProvided(ctx)) {
saveMeasureFromLCOVFile(ctx, nonCommentLineMap);
if (!reportPaths.isEmpty()) {
saveMeasureFromLCOVFile(ctx, nonCommentLineMap, reportPaths);
} else if (isForceZeroCoverageActivated(ctx)) {
saveZeroValueForAllFiles(ctx, nonCommentLineMap);
}
Expand Down
28 changes: 26 additions & 2 deletions src/test/java/com/pablissimo/sonar/TsCoverageSensorImplTest.java
Expand Up @@ -78,7 +78,7 @@ public void doesNotCallParser_WhenNoLCOVPathSupplied() {
when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn("");

this.sensor.execute(this.context, null);
verify(this.parser, never()).parseFile(any(java.io.File.class));
verify(this.parser, never()).coverageByFile();
}

@Test
Expand Down Expand Up @@ -129,10 +129,34 @@ public void savesCoverage_IfParserOutputHasDetailsForFile() {
NewCoverage fileCoverage = spy(this.context.newCoverage());
allFilesCoverage.put(this.file, fileCoverage);

when(this.parser.parseFile(this.lcovFile)).thenReturn(allFilesCoverage);
when(this.parser.coverageByFile()).thenReturn(allFilesCoverage);

this.sensor.execute(this.context, null);

verify(fileCoverage, times(1)).save();
}

@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));
}

@Test
public void doesNotCallParser_WhenBadLCOVPathSupplied() {
when(this.lcovFile.isFile()).thenReturn(false);

this.sensor.execute(this.context, null);
verify(this.parser, never()).coverageByFile();
}
}

0 comments on commit 7ece93f

Please sign in to comment.