--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.xoo.rule;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorDescriptor;
+import org.sonar.api.batch.sensor.issue.NewIssue;
+import org.sonar.api.rule.RuleKey;
+
+public class OneIssuePerUnknownFileSensor implements Sensor {
+
+ public static final String RULE_KEY = "OneIssuePerUnknownFile";
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ descriptor.name("One Issue Per Unknown File");
+ }
+
+ @Override
+ public void execute(SensorContext context) {
+ RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
+ FilePredicate unknownFilesPredicate = context.fileSystem().predicates().matchesPathPattern("**/*.unknown");
+ Iterable<InputFile> unknownFiles = context.fileSystem().inputFiles(unknownFilesPredicate);
+
+ unknownFiles.forEach(inputFile -> {
+ NewIssue newIssue = context.newIssue();
+ newIssue
+ .forRule(ruleKey)
+ .at(newIssue.newLocation()
+ .on(inputFile)
+ .message("This issue is generated on each file with extension 'unknown'"))
+ .save();
+ });
+ }
+
+}
tester.stop();
logs = new LogOutputRecorder();
}
-
- @Test
- public void scanProjectWithoutProjectName() throws IOException {
- builder = ImmutableMap.<String, String>builder()
+
+ private ImmutableMap.Builder<String, String> createBuilder() {
+ return 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");
+ }
+
+ @Test
+ public void scanProjectWithoutProjectName() throws IOException {
+ builder = createBuilder();
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
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 not published
- assertThat(file.publish()).isFalse();
- assertThat(result.getReportComponent(dir.key())).isNull();
- assertThat(result.getReportComponent(file.key())).isNull();
+ // 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();
}
@Test
public void onlyGenerateMetadataIfNeeded() throws IOException {
- 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");
+ builder = createBuilder();
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
- File unknownFile = new File(srcDir, "sample.unknown");
- FileUtils.write(unknownFile, "Sample xoo\ncontent");
+ File javaFile = new File(srcDir, "sample.java");
+ FileUtils.write(javaFile, "Sample xoo\ncontent");
tester.newTask()
.properties(builder
assertThat(logs.getAllAsString()).contains("2 files indexed");
assertThat(logs.getAllAsString()).contains("'src/sample.xoo' generated metadata");
- assertThat(logs.getAllAsString()).doesNotContain("'src/sample.unknown' generated metadata");
+ assertThat(logs.getAllAsString()).doesNotContain("'src/sample.java' generated metadata");
}
@Test
public void preloadFileMetadata() throws IOException {
- 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.preloadFileMetadata", "true")
- .put("sonar.projectDescription", "Description of Foo Project");
+ builder = createBuilder()
+ .put("sonar.preloadFileMetadata", "true");
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
- File unknownFile = new File(srcDir, "sample.unknown");
- FileUtils.write(unknownFile, "Sample xoo\ncontent");
+ File javaFile = new File(srcDir, "sample.java");
+ FileUtils.write(javaFile, "Sample xoo\ncontent");
tester.newTask()
.properties(builder
assertThat(logs.getAllAsString()).contains("2 files indexed");
assertThat(logs.getAllAsString()).contains("'src/sample.xoo' generated metadata");
+ assertThat(logs.getAllAsString()).contains("'src/sample.java' generated metadata");
+ }
+
+ @Test
+ public void dontPublishFilesWithoutDetectedLanguage() 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");
+
+ File javaFile = new File(srcDir, "sample.java");
+ FileUtils.write(javaFile, "Sample xoo\ncontent");
+
+ TaskResult result = tester.newTask()
+ .properties(builder
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+
+ assertThat(logs.getAllAsString()).contains("2 files indexed");
+ assertThat(logs.getAllAsString()).contains("'src/sample.xoo' generated metadata");
+ assertThat(logs.getAllAsString()).doesNotContain("'src/sample.java' generated metadata");
+ DefaultInputFile javaInputFile = (DefaultInputFile) result.inputFile("src/sample.java");
+ assertThat(result.getReportComponent(javaInputFile.key())).isNull();
+ }
+
+ @Test
+ public void createIssueOnAnyFile() throws IOException {
+ LogOutputRecorder logs = new LogOutputRecorder();
+ ScannerMediumTester tester2 = ScannerMediumTester.builder()
+ .registerPlugin("xoo", new XooPlugin())
+ .addDefaultQProfile("xoo", "Sonar Way")
+ .addRules(new XooRulesDefinition())
+ .setLogOutput(logs)
+ .addActiveRule("xoo", "OneIssuePerUnknownFile", null, "OneIssuePerUnknownFile", "MAJOR", null, "xoo")
+ .build();
+ tester2.start();
+
+ builder = createBuilder();
+
+ File srcDir = new File(baseDir, "src");
+ srcDir.mkdir();
+
+ File xooFile = new File(srcDir, "sample.unknown");
+ FileUtils.write(xooFile, "Sample xoo\ncontent");
+
+ TaskResult result = tester2.newTask()
+ .properties(builder
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+
+ assertThat(logs.getAllAsString()).contains("1 file indexed");
+ assertThat(logs.getAllAsString()).contains("'src/sample.unknown' indexed with language 'null'");
assertThat(logs.getAllAsString()).contains("'src/sample.unknown' generated metadata");
+ DefaultInputFile javaInputFile = (DefaultInputFile) result.inputFile("src/sample.unknown");
+ assertThat(result.getReportComponent(javaInputFile.key())).isNotNull();
+
+ tester2.stop();
}
@Test
.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");
+ builder = createBuilder();
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
TaskResult result = tester.newTask()
.properties(builder
.put("sonar.sources", "src")
- .put("sonar.import_unknown_files", "true")
.build())
.start();