Search in sources :

Example 21 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-java by SonarSource.

the class InternalFileSystemTest method methods.

@Test
void methods() {
    FileSystem fs = new InternalFileSystem();
    assertThat(fs.baseDir()).isNotNull();
    assertThat(fs.encoding()).isEqualTo(StandardCharsets.UTF_8);
    assertThat(fs.files(null)).isEmpty();
    assertThat(fs.hasFiles(null)).isFalse();
    assertThat(fs.languages()).containsOnly("java");
    assertMethodNotSupported(() -> fs.inputDir(null), "InternalFileSystem::inputDir(File)");
    assertMethodNotSupported(() -> fs.inputFile(null), "InternalFileSystem::inputFile(FilePredicate)");
    assertMethodNotSupported(() -> fs.inputFiles(null), "InternalFileSystem::inputFiles(FilePredicate)");
    assertMethodNotSupported(() -> fs.predicates(), "InternalFileSystem::predicates()");
    assertMethodNotSupported(() -> fs.resolvePath(null), "InternalFileSystem::resolvePath(String)");
    assertMethodNotSupported(() -> fs.workDir(), "InternalFileSystem::workDir()");
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) Test(org.junit.jupiter.api.Test)

Example 22 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-checkstyle by checkstyle.

the class CheckstyleAuditListenerTest method before.

@Before
public void before() {
    fileSystem = mock(FileSystem.class);
    context = mock(SensorContext.class);
    inputFile = mock(InputFile.class);
    ruleFinder = mock(ActiveRules.class);
    final FilePredicates predicates = mock(FilePredicates.class);
    final FilePredicate filePredicate = mock(FilePredicate.class);
    when(fileSystem.inputFile(any(FilePredicate.class))).thenReturn(inputFile);
    when(fileSystem.predicates()).thenReturn(predicates);
    when(predicates.hasAbsolutePath(anyString())).thenReturn(filePredicate);
}
Also used : SensorContext(org.sonar.api.batch.sensor.SensorContext) FilePredicate(org.sonar.api.batch.fs.FilePredicate) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) ActiveRules(org.sonar.api.batch.rule.ActiveRules) InputFile(org.sonar.api.batch.fs.InputFile) Before(org.junit.Before)

Example 23 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-bsl-plugin-community by 1c-syntax.

the class BSLCoreSensor method execute.

@Override
public void execute(SensorContext context) {
    LOGGER.info("Parsing files...");
    FileSystem fileSystem = context.fileSystem();
    File baseDir = fileSystem.baseDir();
    var absoluteSourceDirs = sourcesList.stream().map((String sourceDir) -> {
        Path sourcePath = Path.of(sourceDir.trim());
        if (sourcePath.isAbsolute()) {
            return sourcePath;
        } else {
            return Path.of(baseDir.toString(), sourceDir);
        }
    }).map(Absolute::path).collect(Collectors.toList());
    FilePredicates predicates = fileSystem.predicates();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.hasLanguage(BSLLanguage.KEY));
    Map<Path, List<InputFile>> inputFilesByPath = StreamSupport.stream(inputFiles.spliterator(), true).collect(Collectors.groupingBy((InputFile inputFile) -> {
        var filePath = Absolute.path(inputFile.uri());
        return absoluteSourceDirs.stream().filter(filePath::startsWith).findAny().orElse(baseDir.toPath());
    }));
    LanguageServerConfiguration languageServerConfiguration = getLanguageServerConfiguration();
    inputFilesByPath.forEach((Path sourceDir, List<InputFile> inputFilesList) -> {
        LOGGER.info("Source dir: {}", sourceDir);
        Path configurationRoot = LanguageServerConfiguration.getCustomConfigurationRoot(languageServerConfiguration, sourceDir);
        var bslServerContext = BSLLSBinding.getServerContext();
        bslServerContext.setConfigurationRoot(configurationRoot);
        bslServerContext.populateContext();
        try (ProgressBar pb = new ProgressBarBuilder().setTaskName("").setInitialMax(inputFilesList.size()).setStyle(ProgressBarStyle.ASCII).build()) {
            inputFilesList.parallelStream().forEach((InputFile inputFile) -> {
                URI uri = inputFile.uri();
                LOGGER.debug(uri.toString());
                pb.step();
                processFile(inputFile, bslServerContext);
            });
        }
        bslServerContext.clear();
    });
    BSLLSBinding.getApplicationContext().close();
}
Also used : Path(java.nio.file.Path) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) URI(java.net.URI) InputFile(org.sonar.api.batch.fs.InputFile) ProgressBarBuilder(me.tongfei.progressbar.ProgressBarBuilder) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) List(java.util.List) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) ProgressBar(me.tongfei.progressbar.ProgressBar)

Example 24 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonarqube by SonarSource.

the class CreateIssueByInternalKeySensor method execute.

@Override
public void execute(SensorContext context) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) {
        createIssues(file, context);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 25 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonarqube by SonarSource.

the class OneBugIssuePerLineSensor method analyse.

private void analyse(SensorContext context, String language, String repo) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(language), p.hasType(Type.MAIN)))) {
        createIssues(file, context, repo);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

FileSystem (org.sonar.api.batch.fs.FileSystem)34 InputFile (org.sonar.api.batch.fs.InputFile)31 FilePredicates (org.sonar.api.batch.fs.FilePredicates)21 File (java.io.File)7 FilePredicate (org.sonar.api.batch.fs.FilePredicate)5 SensorContext (org.sonar.api.batch.sensor.SensorContext)4 ArrayList (java.util.ArrayList)3 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)3 RecognitionException (com.sonar.sslr.api.RecognitionException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 NewIssueLocation (org.sonar.api.batch.sensor.issue.NewIssueLocation)2 Configuration (org.sonar.api.config.Configuration)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)2