Search in sources :

Example 1 with InputDir

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

the class OneIssueOnDirPerFileSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    InputDir inputDir = context.fileSystem().inputDir(file.file().getParentFile());
    if (inputDir != null) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(inputDir).message("This issue is generated for file " + file.relativePath())).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) InputDir(org.sonar.api.batch.fs.InputDir)

Example 2 with InputDir

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

the class FileSystemMediumTest method publishFilesWithIssues.

@Test
public void publishFilesWithIssues() throws IOException {
    ScannerMediumTester tester2 = ScannerMediumTester.builder().registerPlugin("xoo", new XooPlugin()).addDefaultQProfile("xoo", "Sonar Way").addRules(new XooRulesDefinition()).addActiveRule("xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo").build();
    tester2.start();
    builder = createBuilder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, "Sample xoo\ncontent");
    TaskResult result = tester2.newTask().properties(builder.put("sonar.sources", "src").build()).start();
    DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo");
    InputDir dir = result.inputDir("src");
    assertThat(file.publish()).isTrue();
    assertThat(result.getReportComponent(dir.key())).isNotNull();
    assertThat(result.getReportComponent(file.key())).isNotNull();
    tester2.stop();
}
Also used : XooPlugin(org.sonar.xoo.XooPlugin) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) XooRulesDefinition(org.sonar.xoo.rule.XooRulesDefinition) ScannerMediumTester(org.sonar.scanner.mediumtest.ScannerMediumTester) TaskResult(org.sonar.scanner.mediumtest.TaskResult) InputDir(org.sonar.api.batch.fs.InputDir) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 3 with InputDir

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

the class FileSystemMediumTest method scanProjectWithoutProjectName.

@Test
public void scanProjectWithoutProjectName() throws IOException {
    builder = createBuilder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, "Sample xoo\ncontent");
    TaskResult result = tester.newTask().properties(builder.put("sonar.sources", "src").build()).start();
    int ref = result.getReportReader().readMetadata().getRootComponentRef();
    assertThat(result.getReportReader().readComponent(ref).getName()).isEmpty();
    assertThat(result.inputFiles()).hasSize(1);
    assertThat(result.inputDirs()).hasSize(1);
    DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo");
    InputDir dir = result.inputDir("src");
    assertThat(file.type()).isEqualTo(InputFile.Type.MAIN);
    assertThat(file.relativePath()).isEqualTo("src/sample.xoo");
    assertThat(file.language()).isEqualTo("xoo");
    assertThat(dir.relativePath()).isEqualTo("src");
    // file and dirs were published, since language matched xoo
    assertThat(file.publish()).isTrue();
    assertThat(result.getReportComponent(dir.key())).isNotNull();
    assertThat(result.getReportComponent(file.key())).isNotNull();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TaskResult(org.sonar.scanner.mediumtest.TaskResult) InputDir(org.sonar.api.batch.fs.InputDir) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 4 with InputDir

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

the class JSONReport method writeJsonComponents.

private void writeJsonComponents(JsonWriter json) throws IOException {
    json.name("components").beginArray();
    // Dump modules
    writeJsonModuleComponents(json, rootModule);
    for (DefaultInputFile inputFile : componentStore.allFilesToPublish()) {
        String moduleKey = getModule(inputFile).definition().getKeyWithBranch();
        String key = ComponentKeys.createEffectiveKey(moduleKey, inputFile);
        json.beginObject().prop("key", key).prop("path", inputFile.relativePath()).prop("moduleKey", moduleKey).prop("status", inputFile.status().name()).endObject();
    }
    for (InputDir inputDir : componentStore.allDirs()) {
        String moduleKey = getModule(inputDir).definition().getKeyWithBranch();
        String key = ComponentKeys.createEffectiveKey(moduleKey, inputDir);
        json.beginObject().prop("key", key).prop("path", inputDir.relativePath()).prop("moduleKey", moduleKey).endObject();
    }
    json.endArray();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) InputDir(org.sonar.api.batch.fs.InputDir)

Example 5 with InputDir

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

the class FileSystemMediumTest method publishDirsWithIssues.

@Test
public void publishDirsWithIssues() throws IOException {
    ScannerMediumTester tester2 = ScannerMediumTester.builder().registerPlugin("xoo", new XooPlugin()).addDefaultQProfile("xoo", "Sonar Way").addRules(new XooRulesDefinition()).addActiveRule("xoo", "OneIssuePerDirectory", null, "OneIssuePerDirectory", "MAJOR", null, "xoo").build();
    tester2.start();
    builder = ImmutableMap.<String, String>builder().put("sonar.task", "scan").put("sonar.verbose", "true").put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.projectVersion", "1.0-SNAPSHOT").put("sonar.projectDescription", "Description of Foo Project");
    Path unknownRelative = Paths.get("src/unknown/file.notanalyzed");
    Path unknown = baseDir.toPath().resolve(unknownRelative);
    Files.createDirectories(unknown.getParent());
    Files.write(unknown, "dummy content".getBytes());
    Path emptyDirRelative = Paths.get("src/emptydir");
    Files.createDirectories(emptyDirRelative);
    TaskResult result = tester2.newTask().properties(builder.put("sonar.sources", "src").build()).start();
    DefaultInputFile unknownInputFile = (DefaultInputFile) result.inputFile(unknownRelative.toString());
    InputDir unknownInputDir = result.inputDir(unknownRelative.getParent().toString());
    assertThat(unknownInputFile.publish()).isFalse();
    assertThat(result.getReportComponent(unknownInputDir.key())).isNotNull();
    // no issues on empty dir
    InputDir emptyInputDir = result.inputDir(emptyDirRelative.toString());
    assertThat(emptyInputDir).isNull();
    // no issues on parent dir
    InputDir parentInputDir = result.inputDir(unknownRelative.getParent().getParent().toString());
    assertThat(parentInputDir).isNull();
    tester2.stop();
}
Also used : Path(java.nio.file.Path) XooPlugin(org.sonar.xoo.XooPlugin) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) XooRulesDefinition(org.sonar.xoo.rule.XooRulesDefinition) ScannerMediumTester(org.sonar.scanner.mediumtest.ScannerMediumTester) TaskResult(org.sonar.scanner.mediumtest.TaskResult) InputDir(org.sonar.api.batch.fs.InputDir) Test(org.junit.Test)

Aggregations

InputDir (org.sonar.api.batch.fs.InputDir)6 Test (org.junit.Test)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 InputFile (org.sonar.api.batch.fs.InputFile)3 TaskResult (org.sonar.scanner.mediumtest.TaskResult)3 File (java.io.File)2 ScannerMediumTester (org.sonar.scanner.mediumtest.ScannerMediumTester)2 XooPlugin (org.sonar.xoo.XooPlugin)2 XooRulesDefinition (org.sonar.xoo.rule.XooRulesDefinition)2 Path (java.nio.file.Path)1 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)1 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)1 RuleKey (org.sonar.api.rule.RuleKey)1