From a96ceaf6583124def74e973a67c22b2db75370d3 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 16 Sep 2021 13:43:01 -0500 Subject: [PATCH] SONAR-15387 Import of Generic Tests data shouldn't fail if a file has no recognized language --- .../GenericCoverageReportParser.java | 16 ++++++------ .../GenericTestExecutionReportParser.java | 10 +++---- .../GenericCoverageReportParserTest.java | 26 ++++++++++++++++--- .../GenericTestExecutionReportParserTest.java | 21 ++++++++++++++- 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,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", 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; @@ -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"); } 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; @@ -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); -- 2.39.5