Skip to content

Find correct paths to process #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 12, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
No need for exclusion patterns as CLI already resolve paths to be inc…
…luded
  • Loading branch information
filipesperandio committed Oct 11, 2017
commit 3af52b51cb2db852de20618a410591836a1cf92e
9 changes: 2 additions & 7 deletions src/main/java/cc/files/Collector.java
Original file line number Diff line number Diff line change
@@ -10,12 +10,10 @@
import java.util.List;

public class Collector extends SimpleFileVisitor<Path> {
final Matcher matcher;
final List<Path> files;
final Path baseDir;

public Collector(Path baseDir, Matcher matcher) {
this.matcher = matcher;
public Collector(Path baseDir) {
this.baseDir = baseDir;
this.files = new ArrayList<>();
}
@@ -26,10 +24,7 @@ public List<Path> getFiles() {

@Override
public FileVisitResult visitFile(final Path file, BasicFileAttributes attrs) throws IOException {
boolean valid = matcher.validatePath(baseDir, file);
if (valid) {
files.add(file);
}
files.add(file);
return super.visitFile(file, attrs);
}

18 changes: 9 additions & 9 deletions src/main/java/cc/files/Finder.java
Original file line number Diff line number Diff line change
@@ -19,11 +19,11 @@ public class Finder extends InputFileFinder {
final Charset charset;
final Matcher matcher;

public Finder(List<String> includedPaths, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
super(null, testsGlobPattern, excludeGlobPattern, charset);
public Finder(List<String> includedPaths, String testsGlobPattern, Charset charset) {
super(null, testsGlobPattern, null, charset);
this.includedPaths = includedPaths;
this.charset = charset;
this.matcher = new Matcher(testsGlobPattern, excludeGlobPattern, charset);
this.matcher = new Matcher(testsGlobPattern, charset);
}

@Override
@@ -48,19 +48,19 @@ List<Path> findPaths(Path baseDir) throws IOException {
}

ClientInputFile toClientInputFile(Path baseDir, Path path) {
boolean valid = matcher.validatePath(baseDir, path);
if (valid) {
return createInputFile(path, matcher.isTest(baseDir, path));
}
return null;
return createInputFile(path, isTest(baseDir, path));
}

boolean isTest(Path baseDir, Path path) {
return matcher.isTest(baseDir, path);
}

ClientInputFile createInputFile(Path resolvedPath, boolean test) {
return new DefaultClientInputFile(resolvedPath, test, charset);
}

List<Path> collectDir(Path baseDir, Path dir) throws IOException {
Collector collector = new Collector(baseDir, matcher);
Collector collector = new Collector(baseDir);
Files.walkFileTree(dir, collector);
return collector.getFiles();
}
20 changes: 6 additions & 14 deletions src/main/java/cc/files/Matcher.java
Original file line number Diff line number Diff line change
@@ -6,35 +6,27 @@
import java.nio.file.PathMatcher;

public class Matcher {
private static final String GLOB_PREFIX = "glob:";
public static PathMatcher REFUSE_ALL = p -> false;
static final String GLOB_PREFIX = "glob:";
static PathMatcher REFUSE_ALL = p -> false;

final PathMatcher testsMatcher;
final PathMatcher excludeMatcher;
final Charset charset;

public Matcher(PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) {
public Matcher(PathMatcher testsMatcher, Charset charset) {
this.testsMatcher = testsMatcher;
this.excludeMatcher = excludeMatcher;
this.charset = charset;
}

public Matcher(String testsGlobPattern, String excludeGlobPattern, Charset charset) {
this(create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), charset);
}

public boolean validatePath(Path baseDir, Path absoluteFilePath) {
Path relativeFilePath = baseDir.relativize(absoluteFilePath);
boolean isExcluded = excludeMatcher.matches(absoluteFilePath) || excludeMatcher.matches(relativeFilePath);
return !isExcluded;
public Matcher(String testsGlobPattern, Charset charset) {
this(createPathMatcher(testsGlobPattern, REFUSE_ALL), charset);
}

public boolean isTest(Path baseDir, Path absoluteFilePath) {
Path relativeFilePath = baseDir.relativize(absoluteFilePath);
return testsMatcher.matches(absoluteFilePath) || testsMatcher.matches(relativeFilePath);
}

static PathMatcher create(String pattern, PathMatcher defaultMatcher) {
static PathMatcher createPathMatcher(String pattern, PathMatcher defaultMatcher) {
try {
if (pattern != null) {
return FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern);
2 changes: 1 addition & 1 deletion src/main/java/org/sonarlint/cli/CustomMain.java
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ public static void execute(String[] args, System2 system) {
}

Config config = Config.from(system.getProperty("config"));
InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.tests(), parsedOpts.exclusions(), charset);
InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.tests(), charset);
ReportFactory reportFactory = new CustomReportFactory(charset);
ConfigurationReader reader = new ConfigurationReader();
SonarLintFactory sonarLintFactory = new SonarLintFactory(reader);
34 changes: 4 additions & 30 deletions src/test/java/cc/files/FinderTest.java
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ public class FinderTest {

@Test
public void find_files_in_directory() throws Exception {
Finder finder = new Finder(Arrays.asList("src/included/"), null, null, Charset.defaultCharset());
Finder finder = new Finder(Arrays.asList("src/included/"), null, Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
@@ -29,7 +29,7 @@ public void find_files_in_directory() throws Exception {

@Test
public void find_specified_files() throws Exception {
Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, null, Charset.defaultCharset());
Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
@@ -42,7 +42,7 @@ public void find_specified_files() throws Exception {

@Test
public void find_from_multiple_locations() throws Exception {
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, Charset.defaultCharset());
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
@@ -54,35 +54,9 @@ public void find_from_multiple_locations() throws Exception {
);
}

@Test
public void keep_exclude_pattern_behaviour_on_directories() throws Exception {
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, "**/HasNoIssue.*", Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());

assertThat(paths).containsOnly(
"fixtures/multiple_paths/config.json",
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java"
);
}

@Test
public void keep_exclude_pattern_behaviour_on_files() throws Exception {
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, "**/*.json", Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());

assertThat(paths).containsOnly(
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
);
}

@Test
public void differentiate_src_and_test() throws Exception {
Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "{**/test/**}", null, Charset.defaultCharset());
Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "{**/test/**}", Charset.defaultCharset());

List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));