@Test public void has_relative_path() { assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue(); assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse(); // path is normalized assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue(); assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue(); assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse(); assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue(); }
@Test public void input_file_returns_null_if_file_not_found() { assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNull(); assertThat(fs.inputFile(fs.predicates().hasLanguage("cobol"))).isNull(); }
@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 filePath) { return fs.inputFile(fs.predicates().hasRelativePath(filePath)); }
private static FilePredicate searchFilePredicate(FileSystem fileSystem, String path) { FilePredicates predicatesFactory = fileSystem.predicates(); Collection<FilePredicate> searchPredicates = new ArrayList<FilePredicate>(); // try to find the file directly using the provided path (absolute or // relative) searchPredicates.add(predicatesFactory.hasPath(path)); // if not found, maybe the path starts with '/' // in this case, Sonar thinks it's an absolute path => manually try // relative if (path.startsWith("/")) { searchPredicates.add(predicatesFactory.hasRelativePath(path.substring(1))); } // if not found, try to search it everywhere searchPredicates.add(predicatesFactory.matchesPathPattern("**" + path)); return predicatesFactory.or(searchPredicates); }
private void processLine(File coverPerTest, int lineNumber, SensorContext context, String line, InputFile file) { try { Iterator<String> split = Splitter.on(":").split(line).iterator(); String otherFileRelativePath = split.next(); FileSystem fs = context.fileSystem(); InputFile otherFile = fs.inputFile(fs.predicates().hasRelativePath(otherFileRelativePath)); if (otherFile == null) { throw new IllegalStateException("Unable to find file " + otherFileRelativePath); } int weight = Integer.parseInt(split.next()); context.newDependency() .from(file) .to(otherFile) .weight(weight) .save(); } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverPerTest.getAbsolutePath(), e); } }
/** * 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 InputFile getSourceFile(InputFile file, Element refElement) { Element parentNode = (Element) refElement.getParentNode(); String fileNum = getChildNodeValue(refElement, "File-num"); if ("1".equals(fileNum)) { return file; } else { return getContext().fileSystem().inputFile( getContext().fileSystem().predicates().hasRelativePath(parentNode.getAttribute("File-name"))); } } }
private InputFile getSourceFile(InputFile file, Element refElement) { Element parentNode = (Element) refElement.getParentNode(); String fileNum = getChildNodeValue(refElement, "File-num"); if ("1".equals(fileNum)) { return file; } else { return getContext().fileSystem().inputFile( getContext().fileSystem().predicates().hasRelativePath(parentNode.getAttribute("File-name"))); } } }
/** * Look in the potential source directories to find the given source file * @param fileName Path to the source file (/package/MyClass.java) * @param fs File system * @return The InputFile instance to the source file */ private InputFile buildInputFile(String fileName,FileSystem fs) { for(String sourceDir : SOURCE_DIRECTORIES) { //Quick lookup to skip manual iteration (see next loop) //System.out.println("Source file tested : "+sourceDir+"/"+fileName); Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasRelativePath(sourceDir+"/"+fileName)); for (InputFile f : files) { return f; } } //Search for path _ending_ with the filename (https://github.com/SonarQubeCommunity/sonar-findbugs/issues/51) for (InputFile f : fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN))) { if (f.relativePath().endsWith(fileName)) { return f; } } return null; }
private void save(Collection<SourceCode> squidSourceFiles) { for (SourceCode squidSourceFile : squidSourceFiles) { SourceFile squidFile = (SourceFile) squidSourceFile; String relativePath = pathResolver.relativePath(context.fileSystem().baseDir(), new File(squidFile.getKey())); InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasRelativePath(relativePath)); saveMeasures(inputFile, squidFile); saveIssues(inputFile, squidFile); } }
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 void save(Collection<SourceCode> squidSourceFiles) { for (SourceCode squidSourceFile : squidSourceFiles) { SourceFile squidFile = (SourceFile) squidSourceFile; String relativePath = pathResolver.relativePath(context.fileSystem().baseDir(), new java.io.File(squidFile.getKey())); InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasRelativePath(relativePath)); saveMeasures(inputFile, squidFile); saveIssues(inputFile, squidFile); } }
private long getDevelopmentCost(DecoratorContext context) { InputFile file = fs.inputFile(fs.predicates().hasRelativePath(context.getResource().getKey())); if (file != null) { String language = file.language(); return getMeasureValue(context, sqaleRatingSettings.getSizeMetric(language, metrics)) * sqaleRatingSettings.getDevCost(language); } else { Collection<Measure> childrenMeasures = context.getChildrenMeasures(CoreMetrics.DEVELOPMENT_COST); Double sum = sum(childrenMeasures); return sum.longValue(); } }
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; }
protected InputFile getInputFile(InputFile file, JPNode node) { if (node.getFileIndex() == 0) { return file; } else { return getContext().fileSystem().inputFile( getContext().fileSystem().predicates().hasRelativePath(node.getFilename())); } }
protected InputFile getInputFile(InputFile file, JPNode node) { if (node.getFileIndex() == 0) { return file; } else { return getContext().fileSystem().inputFile( getContext().fileSystem().predicates().hasRelativePath(node.getFilename())); } }
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; }
void getResourceAndSaveIssue(PerlCriticViolation violation) { log.debug(violation.toString()); InputFile inputFile = fileSystem .inputFile(fileSystem.predicates().hasRelativePath(violation.getFilePath())); if (inputFile != null) { saveIssue(inputFile, violation.getLine(), violation.getType(), violation.getDescription()); } else { log.warn("Not able to find an input file with path '{}'", violation.getFilePath()); } }
private void getResourceAndSaveIssue(final ErrorDataFromExternalLinter error) { LOGGER.debug(error.toString()); InputFile inputFile = fileSystem.inputFile( fileSystem.predicates().and( fileSystem.predicates().hasRelativePath(error.getFilePath()), fileSystem.predicates().hasType(InputFile.Type.MAIN))); LOGGER.debug("inputFile null ? " + (inputFile == null)); if (inputFile != null) { saveIssue(inputFile, error.getLine(), error.getType(), error.getDescription()); } else { LOGGER.error("Not able to find a InputFile with " + error.getFilePath()); } }