diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2023-06-09 11:30:21 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-12 20:02:49 +0000 |
commit | 713f0434372e85f5273ab7b544ca616a0f379f44 (patch) | |
tree | 00d5c73d5d3afca0bff9e8c253c61b32c6d809f0 /sonar-scanner-engine | |
parent | a86f840b160eeeca5c712689321cf4b205da8256 (diff) | |
download | sonarqube-713f0434372e85f5273ab7b544ca616a0f379f44.tar.gz sonarqube-713f0434372e85f5273ab7b544ca616a0f379f44.zip |
SONAR-19532 PythonXUnitSensor takes too long due to non-optimized file query
Diffstat (limited to 'sonar-scanner-engine')
2 files changed, 42 insertions, 16 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java index 010ebabb3bb..129d4b61d93 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java @@ -19,13 +19,17 @@ */ package org.sonar.scanner.externalissue.sarif; +import com.google.common.annotations.VisibleForTesting; import java.io.File; +import java.io.IOException; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.predicates.AbstractFilePredicate; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.issue.NewIssueLocation; import org.sonar.api.scanner.ScannerSide; @@ -86,8 +90,8 @@ public class LocationMapper { @CheckForNull private static InputFile findFile(SensorContext context, String filePath) { - FilePredicates predicates = context.fileSystem().predicates(); - return context.fileSystem().inputFile(predicates.is(getFileFromAbsoluteUriOrPath(filePath))); + // we use a custom predicate (which is not optimized) because fileSystem().predicates().is() doesn't handle symlinks correctly + return context.fileSystem().inputFile(new IsPredicate(getFileFromAbsoluteUriOrPath(filePath).toPath())); } private static File getFileFromAbsoluteUriOrPath(String filePath) { @@ -98,4 +102,22 @@ public class LocationMapper { return new File(filePath); } } + + @VisibleForTesting + static class IsPredicate extends AbstractFilePredicate { + private final Path path; + + public IsPredicate(Path path) { + this.path = path; + } + + @Override + public boolean apply(InputFile inputFile) { + try { + return Files.isSameFile(path, inputFile.path()); + } catch (IOException e) { + return false; + } + } + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java index 16f91663bd8..17508d90c34 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java @@ -19,6 +19,8 @@ */ package org.sonar.scanner.externalissue.sarif; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -82,23 +84,25 @@ public class LocationMapperTest { when(result.getMessage().getText()).thenReturn(TEST_MESSAGE); when(location.getPhysicalLocation().getArtifactLocation().getUri()).thenReturn(URI_TEST); - - FilePredicate filePredicate = mock(FilePredicate.class); - FilePredicates predicates = sensorContext.fileSystem().predicates(); - when(predicates.is(any())).thenReturn(filePredicate); - - when(sensorContext.fileSystem().inputFile(filePredicate)).thenReturn(inputFile); - + when(sensorContext.fileSystem().inputFile(any(FilePredicate.class))).thenReturn(inputFile); } @Test - public void fillIssueInProjectLocation_shouldFillRelevantFields() { - NewIssueLocation actualIssueLocation = locationMapper.fillIssueInProjectLocation(result, newIssueLocation); + public void isPredicate_whenDifferentFile_returnsFalse() { + Path path = Paths.get("file"); + InputFile inputFile = mock(InputFile.class); + when((inputFile.path())).thenReturn(Paths.get("file2")); + LocationMapper.IsPredicate isPredicate = new LocationMapper.IsPredicate(path); + assertThat(isPredicate.apply(inputFile)).isFalse(); + } - assertThat(actualIssueLocation).isEqualTo(newIssueLocation); - verify(newIssueLocation).message(TEST_MESSAGE); - verify(newIssueLocation).on(sensorContext.project()); - verifyNoMoreInteractions(newIssueLocation); + @Test + public void isPredicate_whenSameFile_returnsTrue() { + Path path = Paths.get("file"); + InputFile inputFile = mock(InputFile.class); + when((inputFile.path())).thenReturn(path); + LocationMapper.IsPredicate isPredicate = new LocationMapper.IsPredicate(path); + assertThat(isPredicate.apply(inputFile)).isTrue(); } @Test |