import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.utils.MessageException;
-
-import static org.sonar.api.utils.Preconditions.checkState;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
public class GenericCoverageReportParser {
+ private static final Logger LOG = Loggers.get(GenericCoverageReportParser.class);
private static final String LINE_NUMBER_ATTR = "lineNumber";
private static final String COVERED_ATTR = "covered";
checkElementName(fileCursor, "file");
String filePath = mandatoryAttribute(fileCursor, "path");
InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
- if (inputFile == null) {
+ if (inputFile == null || inputFile.language() == null) {
numberOfUnknownFiles++;
if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
firstUnknownFiles.add(filePath);
}
+ if (inputFile != null) {
+ LOG.debug("Skipping file '{}' in the generic coverage report because it doesn't have a known language", filePath);
+ }
continue;
}
- checkState(
- inputFile.language() != null,
- "Line %s of report refers to a file with an unknown language: %s",
- fileCursor.getCursorLocation().getLineNumber(),
- filePath);
+
matchedFileKeys.add(inputFile.key());
NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
checkElementName(fileCursor, "file");
String filePath = mandatoryAttribute(fileCursor, "path");
InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
- if (inputFile == null) {
+ if (inputFile == null || inputFile.language() == null) {
numberOfUnknownFiles++;
if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
firstUnknownFiles.add(filePath);
}
+ if (inputFile != null) {
+ LOG.debug("Skipping file '{}' in the generic test execution report because it doesn't have a known language", filePath);
+ }
continue;
}
- checkState(
- inputFile.language() != null,
- "Line %s of report refers to a file with an unknown language: %s",
- fileCursor.getCursorLocation().getLineNumber(),
- filePath);
checkState(
inputFile.type() != InputFile.Type.MAIN,
"Line %s of report refers to a file which is not configured as a test file: %s",
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.utils.MessageException;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
+import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.LogTester;
import static org.assertj.core.api.Assertions.assertThat;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
-
+ @Rule
+ public LogTester logs = new LogTester();
private DefaultInputFile fileWithBranches;
private DefaultInputFile fileWithoutBranch;
private DefaultInputFile emptyFile;
assertThat(parser.firstUnknownFiles()).hasSize(3);
}
+ @Test
+ public void file_without_language_should_be_skipped() throws Exception {
+ String filePath = "src/main/java/com/example/ClassWithBranches.java";
+ DefaultInputFile file = new TestInputFileBuilder(context.module().key(), filePath)
+ .setLanguage(null)
+ .setType(InputFile.Type.TEST)
+ .initMetadata("1\n2\n3\n4\n5\n6")
+ .build();
+ addFileToFs(file);
+ GenericCoverageReportParser parser = new GenericCoverageReportParser();
+ parser.parse(new File(this.getClass().getResource("coverage.xml").toURI()), context);
+ assertThat(parser.numberOfMatchedFiles()).isZero();
+ assertThat(parser.numberOfUnknownFiles()).isEqualTo(4);
+ assertThat(parser.firstUnknownFiles()).hasSize(4);
+ assertThat(logs.logs())
+ .contains("Skipping file 'src/main/java/com/example/ClassWithBranches.java' in the generic coverage report because it doesn't have a known language");
+ }
+
@Test
public void file_without_branch() throws Exception {
addFileToFs(fileWithoutBranch);
}
@Test(expected = MessageException.class)
- public void testUnknownFile() throws Exception {
+ public void testUnknownFile() {
parseCoverageReportFile("xxx.xml");
}
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.LogTester;
import org.sonar.scanner.deprecated.test.DefaultTestCase;
import org.sonar.scanner.deprecated.test.DefaultTestPlan;
import org.sonar.scanner.deprecated.test.TestPlanBuilder;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
-
+ @Rule
+ public LogTester logs = new LogTester();
private TestPlanBuilder testPlanBuilder;
private DefaultInputFile fileWithBranches;
private DefaultInputFile emptyFile;
when(testPlanBuilder.getTestPlan(any(InputFile.class))).thenReturn(testPlan);
}
+ @Test
+ public void file_without_language_should_be_skipped() throws Exception {
+ String filePath = "src/main/java/com/example/EmptyClass.java";
+ DefaultInputFile file = new TestInputFileBuilder(context.module().key(), filePath)
+ .setLanguage(null)
+ .setType(InputFile.Type.TEST)
+ .initMetadata("1\n2\n3\n4\n5\n6")
+ .build();
+ addFileToFs(file);
+ GenericTestExecutionReportParser parser = parseReportFile("unittest.xml");
+ assertThat(parser.numberOfMatchedFiles()).isZero();
+ assertThat(parser.numberOfUnknownFiles()).isEqualTo(2);
+ assertThat(parser.firstUnknownFiles()).hasSize(2);
+ assertThat(logs.logs())
+ .contains("Skipping file 'src/main/java/com/example/EmptyClass.java' in the generic test execution report because it doesn't have a known language");
+ }
+
@Test
public void ut_empty_file() throws Exception {
addFileToFs(emptyFile);