use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.
the class PathPatternTest method match_relative_path.
@Test
public void match_relative_path() {
PathPattern pattern = PathPattern.create("**/*Foo.java");
assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.java", null);
assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue();
// case sensitive by default
indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.java", null);
assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
}
use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.
the class PathPatternTest method match_absolute_path_and_insensitive_file_extension.
@Test
public void match_absolute_path_and_insensitive_file_extension() {
PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isTrue();
indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.JAVA", null);
assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isFalse();
}
use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.
the class FileIndexer method indexFile.
void indexFile(DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, Path sourceFile, Type type, ProgressReport progressReport, ProjectFileIndexer.ExclusionCounter exclusionCounter, @Nullable IgnoreCommand ignoreCommand) throws IOException {
// get case of real file without resolving link
Path realAbsoluteFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
Path projectRelativePath = project.getBaseDir().relativize(realAbsoluteFile);
Path moduleRelativePath = module.getBaseDir().relativize(realAbsoluteFile);
boolean included = evaluateInclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
if (!included) {
exclusionCounter.increaseByPatternsCount();
return;
}
boolean excluded = evaluateExclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
if (excluded) {
exclusionCounter.increaseByPatternsCount();
return;
}
if (!realAbsoluteFile.startsWith(project.getBaseDir())) {
LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir());
return;
}
if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
return;
}
Language language = langDetection.language(realAbsoluteFile, projectRelativePath);
if (ignoreCommand != null && ignoreCommand.isIgnored(realAbsoluteFile)) {
LOG.debug("File '{}' is excluded by the scm ignore settings.", realAbsoluteFile);
exclusionCounter.increaseByScmCount();
return;
}
DefaultIndexedFile indexedFile = new DefaultIndexedFile(realAbsoluteFile, project.key(), projectRelativePath.toString(), moduleRelativePath.toString(), type, language != null ? language.key() : null, scannerComponentIdGenerator.getAsInt(), sensorStrategy);
DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()));
if (language != null && language.isPublishAllFiles()) {
inputFile.setPublished(true);
}
if (!accept(inputFile)) {
return;
}
checkIfAlreadyIndexed(inputFile);
componentStore.put(module.key(), inputFile);
issueExclusionsLoader.addMulticriteriaPatterns(inputFile);
String langStr = inputFile.language() != null ? format("with language '%s'", inputFile.language()) : "with no language";
LOG.debug("'{}' indexed {}{}", projectRelativePath, type == Type.TEST ? "as test " : "", langStr);
evaluateCoverageExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
evaluateDuplicationExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
if (properties.preloadFileMetadata()) {
inputFile.checkMetadata();
}
int count = componentStore.inputFiles().size();
progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.getProjectRelativePath() + ")");
}
use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.
the class DefaultInputFileTest method selectLine.
@Test
public void selectLine() {
Metadata metadata = new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new StringReader("bla bla a\nabcde\n\nabc"));
DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.selectLine(1).start().line()).isOne();
assertThat(file.selectLine(1).start().lineOffset()).isZero();
assertThat(file.selectLine(1).end().line()).isOne();
assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9);
// Don't fail when selecting empty line
assertThat(file.selectLine(3).start().line()).isEqualTo(3);
assertThat(file.selectLine(3).start().lineOffset()).isZero();
assertThat(file.selectLine(3).end().line()).isEqualTo(3);
assertThat(file.selectLine(3).end().lineOffset()).isZero();
try {
file.selectLine(5);
fail();
} catch (Exception e) {
assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)");
}
}
use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.
the class DefaultInputFileTest method checkValidPointer.
@Test
public void checkValidPointer() {
Metadata metadata = new Metadata(2, 2, "", new int[] { 0, 10 }, new int[] { 9, 15 }, 16);
DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.newPointer(1, 0).line()).isOne();
assertThat(file.newPointer(1, 0).lineOffset()).isZero();
// Don't fail
file.newPointer(1, 9);
file.newPointer(2, 0);
file.newPointer(2, 5);
try {
file.newPointer(0, 1);
fail();
} catch (Exception e) {
assertThat(e).hasMessage("0 is not a valid line for a file");
}
try {
file.newPointer(3, 1);
fail();
} catch (Exception e) {
assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)");
}
try {
file.newPointer(1, -1);
fail();
} catch (Exception e) {
assertThat(e).hasMessage("-1 is not a valid line offset for a file");
}
try {
file.newPointer(1, 10);
fail();
} catch (Exception e) {
assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
}
}
Aggregations