@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 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 files() { assertThat(fs.inputFiles(fs.predicates().all())).isEmpty(); fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build()); fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build()); fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build()); // no language fs.add(new TestInputFileBuilder("foo", "src/readme.txt").build()); assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "src/Bar.java").getAbsolutePath()))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "does/not/exist").getAbsolutePath()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "../src/Bar.java").getAbsolutePath()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "src/Bar.java").toURI()))).isNotNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "does/not/exist").toURI()))).isNull(); assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "../src/Bar.java").toURI()))).isNull(); assertThat(fs.files(fs.predicates().all())).hasSize(4); assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2); assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty(); assertThat(fs.hasFiles(fs.predicates().all())).isTrue(); assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue(); assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse(); assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4); assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1); assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2); assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty(); assertThat(fs.languages()).containsOnly("java", "php"); }
@CheckForNull private InputFile getInputFile(String filename, List<String> sourceDirs) { for (String sourceDir : sourceDirs) { String fileAbsolutePath = sourceDir + "/" + filename; InputFile file = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(fileAbsolutePath)); if (file != null) { return file; } } return null; }
@Nullable private Symbolizable symbolizableFor(File file) { if (resourcePerspectives == null) { return null; } InputFile inputFile = fs.inputFile(fs.predicates().hasAbsolutePath(file.getAbsolutePath())); if (inputFile != null) { return resourcePerspectives.as(Symbolizable.class, inputFile); } else { LOG.warn("No symbol highlighting for file: " + file.getPath() + ". Unable to find associate SonarQube resource."); return null; } }
private static FilePredicate testFilePredicate(FilePredicates predicates, Path path) { return predicates.and( predicates.hasType(Type.TEST), predicates.hasAbsolutePath(path.toString()), predicates.hasLanguage(GoLanguage.KEY)); }
private void initResource(AuditEvent event) { if (currentResource == null) { String absoluteFilename = event.getFileName(); currentResource = fs.inputFile(fs.predicates().hasAbsolutePath(absoluteFilename)); } }
private void initResource(AuditEvent event) { if (currentResource == null) { final String absoluteFilename = event.getFileName(); currentResource = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath( absoluteFilename)); } }
@CheckForNull private InputFile inputFileFor(SensorContext context, String path) { return context.fileSystem().inputFile(context.fileSystem().predicates().hasAbsolutePath(path)); }
private InputFile findResourceFor(RuleViolation violation) { return fs.inputFile(fs.predicates().hasAbsolutePath(violation.getFilename())); }
private String getFilename(List<String> sourceDirectories, String packPath, String attrFilename) throws XMLStreamException { FilePredicates pred = fileSystem.predicates(); for (String directory : sourceDirectories) { String path = directory + packPath + "/" + attrFilename; if (fileSystem.hasFiles(pred.and(pred.hasType(Type.MAIN), pred.hasAbsolutePath(path)))) { return path; } } return packPath + "/" + attrFilename; }
static void saveIssues(SensorContext sensorContext, Issue[] issues, TypeScriptRules typeScriptRules) { FileSystem fs = sensorContext.fileSystem(); for (Issue issue : issues) { InputFile inputFile = fs.inputFile(fs.predicates().hasAbsolutePath(issue.name)); if (inputFile != null) { saveIssue(sensorContext, typeScriptRules, issue, inputFile); } } }
/** * It is possible that absolutePath references a file that does not exist in the file system. * It happens when go tests where executed on a different computer. * Even when absolute path does not match a file of the project, this method try to find a valid * mach using a shorter relative path. * @see <a href="https://github.com/SonarSource/sonar-go/issues/218">sonar-go/issues/218</a> */ private static InputFile findInputFile(String absolutePath, FileSystem fileSystem) { FilePredicates predicates = fileSystem.predicates(); InputFile inputFile = fileSystem.inputFile(predicates.hasAbsolutePath(absolutePath)); if (inputFile != null) { return inputFile; } LOG.debug("Resolving file {} using relative path", absolutePath); Path path = Paths.get(absolutePath); inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString())); while (inputFile == null && path.getNameCount() > 1) { path = path.subpath(1, path.getNameCount()); inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString())); } return inputFile; }
private void recordIssue(final String line) { LOGGER.debug("record issue {}", line); Pattern pattern = Pattern.compile("(.*.swift):(\\w+):?(\\w+)?: (warning|error): (.*) \\((\\w+)"); Matcher matcher = pattern.matcher(line); while (matcher.find()) { String filePath = matcher.group(1); int lineNum = Integer.parseInt(matcher.group(2)); String message = matcher.group(5); String ruleId = matcher.group(6); FilePredicate fp = context.fileSystem().predicates().hasAbsolutePath(filePath); if (!context.fileSystem().hasFiles(fp)) { LOGGER.warn("file not included in sonar {}", filePath); continue; } InputFile inputFile = context.fileSystem().inputFile(fp); NewIssueLocation dil = new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(lineNum)) .message(message); context.newIssue() .forRule(RuleKey.of(SwiftLintRulesDefinition.REPOSITORY_KEY, ruleId)) .at(dil) .save(); } } }
private void collectFileViolations(String filePath, NodeList nodeList) { File file = new File(filePath); FilePredicate fp = context.fileSystem().predicates().hasAbsolutePath(file.getAbsolutePath()); if(!context.fileSystem().hasFiles(fp)){ LOGGER.warn("file not included in sonar {}", filePath); } InputFile inputFile = context.fileSystem().inputFile(fp); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NewIssueLocation dil = new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(Integer.valueOf(element.getAttribute(LINE)))) .message(element.getTextContent()); context.newIssue() .forRule(RuleKey.of(OCLintRulesDefinition.REPOSITORY_KEY, element.getAttribute(RULE))) .at(dil) .save(); } } } }
@CheckForNull private Resource getResource(String fileRelativePath, SensorContext context) { for (InputFile sourceDir : fileSystem.inputFiles(fileSystem.predicates().hasType(InputFile.Type.MAIN))) { String absolutePath = sourceDir.absolutePath(); if (absolutePath.endsWith(fileRelativePath)) { InputFile scalaFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(sourceDir.absolutePath())); if (scalaFile != null) { return context.getResource(scalaFile); } } } return null; }
private void recordIssue(final String line) { LOGGER.debug("record issue {}", line); Pattern pattern = Pattern.compile("(.*.swift):(\\w+):(\\w+): (.*): \\[(.*)\\] (.*)"); Matcher matcher = pattern.matcher(line); while (matcher.find()) { String filePath = matcher.group(1); int lineNum = Integer.parseInt(matcher.group(2)); String ruleId = matcher.group(5); String message = matcher.group(6); FilePredicate fp = context.fileSystem().predicates().hasAbsolutePath(filePath); if (!context.fileSystem().hasFiles(fp)) { LOGGER.warn("file not included in sonar {}", filePath); continue; } InputFile inputFile = context.fileSystem().inputFile(fp); NewIssueLocation dil = new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(lineNum)) .message(message); context.newIssue() .forRule(RuleKey.of(TailorRulesDefinition.REPOSITORY_KEY, ruleId)) .at(dil) .save(); } } }
private static InputFile getInputFile(SensorContext context, String fileName) { FilePredicates predicates = context.fileSystem().predicates(); InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(fileName), predicates.hasAbsolutePath(fileName))); if (inputFile == null) { LOG.warn("No input file found for {}. No {} issues will be imported on this file.", fileName, LINTER_NAME); return null; } return inputFile; }
private static InputFile getInputFile(SensorContext context, String fileName) { FilePredicates predicates = context.fileSystem().predicates(); InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(fileName), predicates.hasAbsolutePath(fileName))); if (inputFile == null) { LOG.warn("No input file found for {}. No {} issues will be imported on this file.", fileName, LINTER_NAME); return null; } return inputFile; }
InputFile getInputFile(SensorContext context, String filePath) { FilePredicates predicates = context.fileSystem().predicates(); InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath))); if (inputFile == null) { LOG.warn(logPrefix() + "No input file found for {}. No {} issues will be imported on this file.", filePath, linterName()); return null; } return inputFile; }