aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2021-09-16 13:43:01 -0500
committersonartech <sonartech@sonarsource.com>2021-09-22 20:03:21 +0000
commita96ceaf6583124def74e973a67c22b2db75370d3 (patch)
treeff59d93c8cdb0323e3125d7d4915f501b82171fb /sonar-scanner-engine
parentbee7ee5ec9f07f9f54804eeb22c1e2e547afc2e8 (diff)
downloadsonarqube-a96ceaf6583124def74e973a67c22b2db75370d3.tar.gz
sonarqube-a96ceaf6583124def74e973a67c22b2db75370d3.zip
SONAR-15387 Import of Generic Tests data shouldn't fail if a file has no recognized language
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java16
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java10
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java26
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java21
4 files changed, 55 insertions, 18 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java
index 54bec9e78d3..7a7592e02d0 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java
@@ -33,10 +33,11 @@ import org.sonar.api.batch.fs.InputFile;
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";
@@ -79,18 +80,17 @@ public class GenericCoverageReportParser {
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);
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java
index e4cb78be0c4..2bf7f38cc39 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java
@@ -109,19 +109,17 @@ public class GenericTestExecutionReportParser {
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",
fileCursor.getCursorLocation().getLineNumber(),
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
index c07392326e5..03e5590e1ba 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
@@ -27,10 +27,11 @@ import org.junit.Rule;
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;
@@ -38,7 +39,8 @@ public class GenericCoverageReportParserTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
-
+ @Rule
+ public LogTester logs = new LogTester();
private DefaultInputFile fileWithBranches;
private DefaultInputFile fileWithoutBranch;
private DefaultInputFile emptyFile;
@@ -63,6 +65,24 @@ public class GenericCoverageReportParserTest {
}
@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);
GenericCoverageReportParser parser = new GenericCoverageReportParser();
@@ -208,7 +228,7 @@ public class GenericCoverageReportParserTest {
}
@Test(expected = MessageException.class)
- public void testUnknownFile() throws Exception {
+ public void testUnknownFile() {
parseCoverageReportFile("xxx.xml");
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
index 8afe338e6e3..81e38534878 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
@@ -31,6 +31,7 @@ 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 org.sonar.scanner.deprecated.test.DefaultTestCase;
import org.sonar.scanner.deprecated.test.DefaultTestPlan;
import org.sonar.scanner.deprecated.test.TestPlanBuilder;
@@ -47,7 +48,8 @@ public class GenericTestExecutionReportParserTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
-
+ @Rule
+ public LogTester logs = new LogTester();
private TestPlanBuilder testPlanBuilder;
private DefaultInputFile fileWithBranches;
private DefaultInputFile emptyFile;
@@ -68,6 +70,23 @@ public class GenericTestExecutionReportParserTest {
}
@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);
GenericTestExecutionReportParser parser = parseReportFile("unittest.xml");