@Test public void test() throws Exception { File baseDir = temp.newFolder(); DefaultInputDir inputDir = new DefaultInputDir("ABCDE", "src") .setModuleBaseDir(baseDir.toPath()); assertThat(inputDir.key()).isEqualTo("ABCDE:src"); assertThat(inputDir.file().getAbsolutePath()).isEqualTo(new File(baseDir, "src").getAbsolutePath()); assertThat(inputDir.relativePath()).isEqualTo("src"); assertThat(new File(inputDir.relativePath())).isRelative(); assertThat(inputDir.absolutePath()).endsWith("src"); assertThat(new File(inputDir.absolutePath())).isAbsolute(); }
@Override public URI uri() { return path().toUri(); } }
@Test public void testEqualsAndHashCode() throws Exception { DefaultInputDir inputDir1 = new DefaultInputDir("ABCDE", "src"); DefaultInputDir inputDir2 = new DefaultInputDir("ABCDE", "src"); assertThat(inputDir1.equals(inputDir1)).isTrue(); assertThat(inputDir1.equals(inputDir2)).isTrue(); assertThat(inputDir1.equals("foo")).isFalse(); assertThat(inputDir1.hashCode()).isEqualTo(63545559); assertThat(inputDir1.toString()).contains("[moduleKey=ABCDE, relative=src, basedir=null"); }
@Override public InputDir inputDir(File dir) { String relativePath = PathUtils.sanitize(new PathResolver().relativePath(baseDir.toFile(), dir)); if (relativePath == null) { return null; } // Issues on InputDir are moved to the project, so we just return a fake InputDir for backward compatibility return new DefaultInputDir("unused", relativePath).setModuleBaseDir(baseDir); }
@Test public void shouldIgnoreMeasuresOnFolders() { underTest.store(new DefaultMeasure() .on(new DefaultInputDir("foo", "bar")) .forMetric(CoreMetrics.LINES) .withValue(10)); verifyNoMoreInteractions(measureCache); }
@CheckForNull private String getKey(@Nullable InputPath inputPath) { if (inputPath == null) { return null; } if (inputPath instanceof InputFile) { return ((DefaultInputFile) inputPath).key(); } if (inputPath instanceof InputDir) { return ((DefaultInputDir) inputPath).key(); } throw new IllegalStateException("Unknow component " + inputPath); }
/** * Adds InputDir to the list. * Synchronized because PersistIt Exchange is not concurrent */ public synchronized DefaultFileSystem add(DefaultInputDir inputDir) { if (this.baseDir == null) { throw new IllegalStateException("Please set basedir on filesystem before adding dirs"); } inputDir.setModuleBaseDir(this.baseDir); cache.add(inputDir); return this; }
public static DefaultInputDir newDefaultInputDir(AbstractProjectOrModule module, String relativePath) throws IOException { Path basedir = module.getBaseDir().resolve(relativePath); Files.createDirectory(basedir); return new DefaultInputDir(module.key(), relativePath) .setModuleBaseDir(module.getBaseDir()); }
@Override public Void call() { DefaultInputFile completedInputFile = inputFileBuilder.completeAndComputeMetadata(inputFile, type); if (completedInputFile != null && accept(completedInputFile)) { fs.add(completedInputFile); status.markAsIndexed(completedInputFile); File parentDir = completedInputFile.file().getParentFile(); String relativePath = new PathResolver().relativePath(fs.baseDir(), parentDir); if (relativePath != null) { DefaultInputDir inputDir = new DefaultInputDir(fs.moduleKey(), relativePath); fs.add(inputDir); } } return null; } }));
private static String key(InputPath inputPath) { return inputPath instanceof InputFile ? ((DefaultInputFile) inputPath).key() : ((DefaultInputDir) inputPath).key(); }
@Test public void move_directory_issue_to_project_root() { SensorStorage storage = mock(SensorStorage.class); DefaultIssue issue = new DefaultIssue(project, storage) .at(new DefaultIssueLocation() .on(new DefaultInputDir("foo", "src/main").setModuleBaseDir(project.getBaseDir())) .message("Wrong way!")) .forRule(RuleKey.of("repo", "rule")) .overrideSeverity(Severity.BLOCKER); assertThat(issue.primaryLocation().inputComponent()).isEqualTo(project); assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); assertThat(issue.primaryLocation().textRange()).isNull(); assertThat(issue.primaryLocation().message()).isEqualTo("[src/main] Wrong way!"); assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER); issue.save(); verify(storage).store(issue); }
@Override public Void call() { InputFileMetadata metadata = inputFileBuilder.completeAndComputeMetadata(inputFile, type); if (metadata != null && accept(inputFile)) { fs.add(inputFile); status.markAsIndexed(inputFile); inputPathCache.put(inputFile.moduleKey(), inputFile.relativePath(), metadata); File parentDir = inputFile.file().getParentFile(); String relativePath = new PathResolver().relativePath(fs.baseDir(), parentDir); if (relativePath != null) { DefaultInputDir inputDir = new DefaultInputDir(fs.moduleKey(), relativePath); fs.add(inputDir); } } return null; } }));
@Override public File file() { return path().toFile(); }
private void writeJsonComponents(JsonWriter json) throws IOException { json.name("components").beginArray(); // Dump modules writeJsonModuleComponents(json, rootModule); for (InputFile inputFile : fileCache.allFiles()) { String key = ((DefaultInputFile) inputFile).key(); json .beginObject() .prop("key", key) .prop("path", inputFile.relativePath()) .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) .prop("status", inputFile.status().name()) .endObject(); } for (InputDir inputDir : fileCache.allDirs()) { String key = ((DefaultInputDir) inputDir).key(); json .beginObject() .prop("key", key) .prop("path", inputDir.relativePath()) .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) .endObject(); } json.endArray(); }
@Override public InputDir inputDir(File dir) { String relativePath = PathUtils.sanitize(new PathResolver().relativePath(baseDir.toFile(), dir)); if (relativePath == null) { return null; } // Issues on InputDir are moved to the project, so we just return a fake InputDir for backward compatibility return new DefaultInputDir("unused", relativePath).setModuleBaseDir(baseDir); }
@Override public String absolutePath() { return PathUtils.sanitize(path().toString()); }
private void writeJsonComponents(JsonWriter json) throws IOException { json.name("components").beginArray(); // Dump modules writeJsonModuleComponents(json, rootModule); for (InputFile inputFile : fileCache.allFiles()) { String key = ((DefaultInputFile) inputFile).key(); json .beginObject() .prop("key", key) .prop("path", inputFile.relativePath()) .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) .prop("status", inputFile.status().name()) .endObject(); } for (InputDir inputDir : fileCache.allDirs()) { String key = ((DefaultInputDir) inputDir).key(); json .beginObject() .prop("key", key) .prop("path", inputDir.relativePath()) .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) .endObject(); } json.endArray(); }
public static DefaultInputDir newDefaultInputDir(AbstractProjectOrModule module, String relativePath) throws IOException { Path basedir = module.getBaseDir().resolve(relativePath); Files.createDirectory(basedir); return new DefaultInputDir(module.key(), relativePath) .setModuleBaseDir(module.getBaseDir()); }
private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) { InputComponent component = location.inputComponent(); Optional<Path> dirOrModulePath = Optional.empty(); if (component instanceof DefaultInputDir) { DefaultInputDir dirComponent = (DefaultInputDir) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(dirComponent.path())); } else if (component instanceof DefaultInputModule && !Objects.equals(project.key(), component.key())) { DefaultInputModule moduleComponent = (DefaultInputModule) component; dirOrModulePath = Optional.of(project.getBaseDir().relativize(moduleComponent.getBaseDir())); } if (dirOrModulePath.isPresent()) { String path = PathUtils.sanitize(dirOrModulePath.get().toString()); DefaultIssueLocation fixedLocation = new DefaultIssueLocation(); fixedLocation.on(project); StringBuilder fullMessage = new StringBuilder(); if (!isNullOrEmpty(path)) { fullMessage.append("[").append(path).append("] "); } fullMessage.append(location.message()); fixedLocation.message(fullMessage.toString()); return fixedLocation; } else { return location; } } }
@Override public URI uri() { return path().toUri(); } }