]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15387 Import of Generic Tests data shouldn't fail if a file has no recognized...
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 16 Sep 2021 18:43:01 +0000 (13:43 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Sep 2021 20:03:21 +0000 (20:03 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParser.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParser.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java

index 54bec9e78d3113c13628ff31e91c65dd0a431f11..7a7592e02d085ff7ccfb5add3627e70510e4f236 100644 (file)
@@ -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);
index e4cb78be0c4566cbae881687f6dddf6f1182f333..2bf7f38cc39406e82a806291f72218b87dfa5345 100644 (file)
@@ -109,18 +109,16 @@ 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",
index c07392326e5053e8bfda4fbc248f2200cfe8a57c..03e5590e1bad8828d7bb1d563e28687a50cf32f7 100644 (file)
@@ -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;
@@ -62,6 +64,24 @@ public class GenericCoverageReportParserTest {
     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);
@@ -208,7 +228,7 @@ public class GenericCoverageReportParserTest {
   }
 
   @Test(expected = MessageException.class)
-  public void testUnknownFile() throws Exception {
+  public void testUnknownFile() {
     parseCoverageReportFile("xxx.xml");
   }
 
index 8afe338e6e3502df38347b2eb83382c67196b1bf..81e385348787d7e6a9ef331bda6645c13a560515 100644 (file)
@@ -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;
@@ -67,6 +69,23 @@ public class GenericTestExecutionReportParserTest {
     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);