@Override public void execute(SensorContext sensorContext, InputFile file, RuleKey ruleKey) { NewIssue newIssue = sensorContext.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line))) .save(); }
private static void analyse(SensorContext context) { FileSystem fs = context.fileSystem(); FilePredicates p = fs.predicates(); RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); StreamSupport.stream(fs.inputFiles(p.hasType(Type.MAIN)).spliterator(), false) .map(file -> fs.inputDir(file.file().getParentFile())) .filter(Objects::nonNull) .distinct() .forEach(inputDir -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputDir) .message("This issue is generated for any non-empty directory")) .save(); }); } }
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(); } } }
/** * Compute a MD5 hash of each line of the file after removing of all blank chars */ public static void computeLineHashesForIssueTracking(InputFile f, LineHashConsumer consumer) { try { readFile(f.inputStream(), f.charset(), f.absolutePath(), new CharHandler[] {new LineHashComputer(consumer, f.file())}); } catch (IOException e) { throw new IllegalStateException("Failed to compute line hashes for " + f.absolutePath(), e); } } }
@Override protected void doAdd(InputFile inputFile) { if (inputFile.language() != null) { languages.add(inputFile.language()); } fileMap.put(inputFile.relativePath(), inputFile); filesByNameCache.put(inputFile.filename(), inputFile); filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile); }
@Override public boolean apply(InputFile f) { return pattern.match(f.path(), Paths.get(f.relativePath())); }
@Test public void returns_false_if_doesnt_match() { RelativePathPredicate predicate = new RelativePathPredicate("path1"); InputFile inputFile = mock(InputFile.class); when(inputFile.relativePath()).thenReturn("path2"); assertThat(predicate.apply(inputFile)).isFalse(); } }
@Test public void should_match_file_by_filename() throws IOException { String filename = "some name"; InputFile inputFile = mock(InputFile.class); when(inputFile.filename()).thenReturn(filename); assertThat(new FilenamePredicate(filename).apply(inputFile)).isTrue(); }
@Test public void is_file() throws Exception { // relative file assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue(); // absolute file assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue(); assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue(); assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue(); assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse(); }
@Test public void has_path() throws Exception { // is relative path assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue(); assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse(); // is absolute path String path = javaFile.file().getAbsolutePath(); assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue(); assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse(); }
@Test public void has_absolute_path() throws Exception { String path = javaFile.file().getAbsolutePath(); assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue(); assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue(); assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse(); assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse(); }
@Test public void indexAnyFile() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent"); File otherFile = new File(srcDir, "sample.other"); FileUtils.write(otherFile, "Sample other\ncontent"); AnalysisResult result = tester.newAnalysis() .properties(builder .put("sonar.sources", "src") .build()) .execute(); assertThat(result.inputFiles()).hasSize(2); assertThat(result.inputFile("src/sample.other").type()).isEqualTo(InputFile.Type.MAIN); assertThat(result.inputFile("src/sample.other").relativePath()).isEqualTo("src/sample.other"); assertThat(result.inputFile("src/sample.other").language()).isNull(); }
@Test public void scanProjectWithSourceDir() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8); AnalysisResult result = tester.newAnalysis() .properties(builder .put("sonar.sources", "src") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("src/sample.xoo").type()).isEqualTo(InputFile.Type.MAIN); assertThat(result.inputFile("src/sample.xoo").relativePath()).isEqualTo("src/sample.xoo"); }
@Test public void scanProjectWithTestDir() throws IOException { File test = new File(baseDir, "test"); test.mkdir(); File xooFile = new File(test, "sampleTest.xoo"); FileUtils.write(xooFile, "Sample test xoo\ncontent", StandardCharsets.UTF_8); AnalysisResult result = tester.newAnalysis() .properties(builder .put("sonar.sources", "") .put("sonar.tests", "test") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("test/sampleTest.xoo").type()).isEqualTo(InputFile.Type.TEST); }
@Test public void filesWithDefaultPredicate() { DefaultInputFile file1 = new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build(); fs.add(file1); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); fs.setDefaultPredicate(p -> f -> f.relativePath().endsWith("Foo.php")); Iterator<File> iterator = fs.files(fs.predicates().all()).iterator(); assertThat(iterator.hasNext()).isTrue(); assertThat(iterator.next()).isEqualTo(file1.file()); assertThat(iterator.hasNext()).isFalse(); }
private void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); String severity = context.settings().getString(FORCE_SEVERITY_PROPERTY); for (int line = 1; line <= file.lines(); line++) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) .overrideSeverity(severity != null ? Severity.valueOf(severity) : null); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(5, 5))) { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } else { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } newIssue.save(); } }
@Override protected void processFile(InputFile inputFile, SensorContext context, RuleKey ruleKey, String languageKey) { try { int[] lineCounter = {1}; try (InputStreamReader isr = new InputStreamReader(inputFile.inputStream(), inputFile.charset()); BufferedReader reader = new BufferedReader(isr)) { reader.lines().forEachOrdered(lineStr -> { int startIndex = -1; while ((startIndex = lineStr.indexOf(TAG, startIndex + 1)) != -1) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputFile) .at(inputFile.newRange(lineCounter[0], startIndex, lineCounter[0], startIndex + TAG.length()))) .save(); } lineCounter[0]++; }); } } catch (IOException e) { throw new IllegalStateException("Fail to process " + inputFile, e); } } }
NewIssue newIssue = context.newIssue().forRule(ruleKey); Integer issueId = entry.getKey(); NewIssueLocation primaryLocation = newIssue.newLocation() .on(file) .at(file.newRange(entry.getValue(), endPositions.get(issueId))); newIssue.at(primaryLocation.message("Primary location")); if (startFlowsPositions.containsKey(issueId)) { Table<Integer, Integer, TextPointer> flows = startFlowsPositions.get(issueId); NewIssueLocation newLocation = newIssue.newLocation() .on(file) .at(file.newRange(start, end)) .message("Flow step #" + flowNum); flowLocations.add(newLocation);
@Override public void execute(SensorContext context) { context.fileSystem().inputFiles(context.fileSystem().predicates().all()).forEach(inputFile -> LOG.info("Deprecated Global Sensor: {}", inputFile.relativePath())); } }
private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue .engineId(ENGINE_ID) .ruleId(RULE_ID) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line and the rule is predefined")) .severity(Severity.valueOf(SEVERITY)) .remediationEffortMinutes(EFFORT) .type(TYPE) .save(); } } }