Search in sources :

Example 51 with DefaultInputModule

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

the class InputComponentStoreTest method should_add_input_file.

@Test
public void should_add_input_file() throws Exception {
    String rootModuleKey = "struts";
    String subModuleKey = "struts-core";
    File rootBaseDir = temp.newFolder();
    ProjectDefinition moduleDef = ProjectDefinition.create().setKey(subModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder());
    ProjectDefinition rootDef = ProjectDefinition.create().setKey(rootModuleKey).setBaseDir(rootBaseDir).setWorkDir(temp.newFolder()).addSubProject(moduleDef);
    DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(rootDef);
    DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef);
    InputComponentStore store = new InputComponentStore(mock(BranchConfiguration.class), sonarRuntime);
    store.put(subModule);
    DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java").setModuleBaseDir(rootBaseDir.toPath()).setPublish(true).build();
    store.put(rootProject.key(), fooFile);
    store.put(subModuleKey, new TestInputFileBuilder(rootModuleKey, "src/main/java/Bar.java").setLanguage("bla").setPublish(false).setType(Type.MAIN).setStatus(Status.ADDED).setLines(2).setCharset(StandardCharsets.UTF_8).setModuleBaseDir(temp.newFolder().toPath()).build());
    DefaultInputFile loadedFile = (DefaultInputFile) store.getFile(subModuleKey, "src/main/java/Bar.java");
    assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
    assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8);
    assertThat(store.filesByModule(rootModuleKey)).hasSize(1);
    assertThat(store.filesByModule(subModuleKey)).hasSize(1);
    assertThat(store.inputFiles()).hasSize(2);
    for (InputPath inputPath : store.inputFiles()) {
        assertThat(inputPath.relativePath()).startsWith("src/main/java/");
    }
    List<InputFile> toPublish = new LinkedList<>();
    store.allFilesToPublish().forEach(toPublish::add);
    assertThat(toPublish).containsExactly(fooFile);
}
Also used : BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) InputPath(org.sonar.api.batch.fs.InputPath) LinkedList(java.util.LinkedList) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputProject(org.sonar.api.batch.fs.internal.DefaultInputProject) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 52 with DefaultInputModule

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

the class AnalysisContextReportPublisherTest method dumpServerSideProjectProps.

@Test
public void dumpServerSideProjectProps() throws Exception {
    logTester.setLevel(LoggerLevel.DEBUG);
    ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
    DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()).setProperty("sonar.projectKey", "foo"));
    when(store.allModules()).thenReturn(singletonList(rootModule));
    when(hierarchy.root()).thenReturn(rootModule);
    when(projectServerSettings.properties()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true"));
    publisher.init(writer);
    List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8);
    assertThat(lines).containsExactly("SonarQube plugins:", "Global server settings:", "Project server settings:", "  - com.foo=bar", "  - sonar.skip=true", "Project scanner properties:", "  - sonar.projectKey=foo");
}
Also used : DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Test(org.junit.Test)

Example 53 with DefaultInputModule

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

the class AnalysisContextReportPublisherTest method shouldOnlyDumpPluginsByDefault.

@Test
public void shouldOnlyDumpPluginsByDefault() throws Exception {
    when(pluginRepo.getPluginInfos()).thenReturn(singletonList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0"))));
    ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
    DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
    when(store.allModules()).thenReturn(singletonList(rootModule));
    when(hierarchy.root()).thenReturn(rootModule);
    publisher.init(writer);
    assertThat(writer.getFileStructure().analysisLog()).exists();
    assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).contains("Xoo 1.0 (xoo)");
    verifyZeroInteractions(system2);
}
Also used : DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) PluginInfo(org.sonar.core.platform.PluginInfo) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Test(org.junit.Test)

Example 54 with DefaultInputModule

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

the class AnalysisContextReportPublisherTest method shouldShortenModuleProperties.

@Test
public void shouldShortenModuleProperties() throws Exception {
    File baseDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
    DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(temp.newFolder()).setProperty("sonar.projectKey", "foo").setProperty("sonar.projectBaseDir", baseDir.toString()).setProperty("sonar.aVeryLongProp", StringUtils.repeat("abcde", 1000)));
    when(store.allModules()).thenReturn(singletonList(rootModule));
    when(hierarchy.root()).thenReturn(rootModule);
    publisher.init(writer);
    assertThat(writer.getFileStructure().analysisLog()).exists();
    assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence("sonar.aVeryLongProp=" + StringUtils.repeat("abcde", 199) + "ab...", "sonar.projectBaseDir=" + baseDir.toString(), "sonar.projectKey=foo");
}
Also used : DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) File(java.io.File) Test(org.junit.Test)

Example 55 with DefaultInputModule

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

the class AbstractDefaultIssue method rewriteLocation.

private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) {
    InputComponent component = location.inputComponent();
    Optional<Path> dirOrModulePath = Optional.empty();
    if (component instanceof DefaultInputDir) {
        DefaultInputDir dirComponent = (DefaultInputDir) component;
        dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path()));
    } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) {
        DefaultInputModule moduleComponent = (DefaultInputModule) component;
        dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir()));
    }
    if (dirOrModulePath.isPresent()) {
        String path = PathUtils.sanitize(dirOrModulePath.get().toString());
        DefaultIssueLocation fixedLocation = new DefaultIssueLocation();
        fixedLocation.on(project);
        StringBuilder fullMessage = new StringBuilder();
        if (path != null && !path.isEmpty()) {
            fullMessage.append("[").append(path).append("] ");
        }
        fullMessage.append(location.message());
        fixedLocation.message(fullMessage.toString());
        return fixedLocation;
    } else {
        return location;
    }
}
Also used : Path(java.nio.file.Path) InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir)

Aggregations

DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)57 Test (org.junit.Test)34 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)17 File (java.io.File)9 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)9 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)9 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)8 Path (java.nio.file.Path)6 InputFile (org.sonar.api.batch.fs.InputFile)6 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)6 InputModuleHierarchy (org.sonar.api.batch.fs.internal.InputModuleHierarchy)5 Before (org.junit.Before)4 InputComponent (org.sonar.api.batch.fs.InputComponent)4 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)4 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)4 ProjectAnalysisInfo (org.sonar.scanner.ProjectAnalysisInfo)4 CheckForNull (javax.annotation.CheckForNull)3 InputDir (org.sonar.api.batch.fs.InputDir)3 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)3 InputModuleHierarchy (org.sonar.scanner.fs.InputModuleHierarchy)3