diff options
5 files changed, 47 insertions, 62 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java index 37345e7d455..bc0a5dc87fb 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java @@ -133,7 +133,10 @@ public class DefaultFilePredicates implements FilePredicates { @Override public FilePredicate is(File ioFile) { - return new IsPredicate(ioFile.toPath()); + if (ioFile.isAbsolute()) { + return hasAbsolutePath(ioFile.getAbsolutePath()); + } + return hasRelativePath(ioFile.getPath()); } @Override diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java deleted file mode 100644 index 80b861e40f1..00000000000 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.batch.fs.internal.predicates; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.sonar.api.batch.fs.InputFile; - -public 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-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java index a9c81f2682c..17c6a424915 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java @@ -153,8 +153,7 @@ public class DefaultFilePredicatesTest { Files.touch(javaFile.file()); // relative file - Path workingDir = Paths.get(System.getProperty("user.dir")); - Path relativePath = workingDir.relativize(javaFile.path()); + Path relativePath = moduleBasePath.relativize(javaFile.path()); assertThat(predicates.is(relativePath.toFile()).apply(javaFile)).isTrue(); // absolute file 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 |