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();
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations