@Override
public FilePredicate is(File ioFile) {
- return new IsPredicate(ioFile.toPath());
+ if (ioFile.isAbsolute()) {
+ return hasAbsolutePath(ioFile.getAbsolutePath());
+ }
+ return hasRelativePath(ioFile.getPath());
}
@Override
+++ /dev/null
-/*
- * 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;
- }
- }
-}
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
*/
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;
@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) {
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;
+ }
+ }
+ }
}
*/
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;
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