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
Next Next commit
Find correct paths to process
  • Loading branch information
filipesperandio committed Oct 10, 2017
commit 723e86f11a5c96104891b8caade01767b47688e7
9 changes: 9 additions & 0 deletions fixtures/multiple_paths/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pkg1;

public class Main {
public void main(String[] args) {
for (int k = 0; k < 20; i++) { // cause issue
System.out.println(new Class1());
}
}
}
2 changes: 1 addition & 1 deletion fixtures/multiple_paths/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"enabled": true,
"include_paths": [
"src/main/java/",
"src/included/",
"Main.java"
]
}
11 changes: 11 additions & 0 deletions fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg1;

public class HasIssue {
public void method() {
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 20; i++) {
System.out.println("Hello");
}
}
}
}
11 changes: 11 additions & 0 deletions fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg1;

class HasIssue {
public void method() {
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 20; i++) {
System.out.println("Hello");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pkg1;

public class HasNoIssue {
}
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions fixtures/multiple_paths/src/test/java/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Test {
}
7 changes: 6 additions & 1 deletion src/main/java/cc/App.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cc;

import org.sonarlint.cli.CustomMain;
import org.sonarlint.cli.util.System2;

public class App {
public static void main(String[] args) {
CustomMain.main(args);
execute(args, System2.INSTANCE);
}

public static void execute(String[] args, System2 system) {
CustomMain.execute(args, system);
}
}
67 changes: 67 additions & 0 deletions src/main/java/cc/CustomInputFileFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cc;

import cc.files.FileCollector;
import cc.files.FileMatcher;
import org.sonarlint.cli.InputFileFinder;
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CustomInputFileFinder extends InputFileFinder {
final List<String> includedPaths;
final Charset charset;
final FileMatcher fileMatcher;


public CustomInputFileFinder(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
this(new ArrayList<>(), srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
}

public CustomInputFileFinder(List<String> includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
super(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
this.includedPaths = includedPaths;
this.charset = charset;
this.fileMatcher = new FileMatcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
}

@Override
public List<ClientInputFile> collect(Path baseDir) throws IOException {
List<Path> paths = new ArrayList<>();
for (String path : includedPaths) {
Path resolvedPath = baseDir.resolve(path);
if (path.endsWith("/")) {
paths.addAll(collectDir(baseDir, resolvedPath));
} else {
paths.add(resolvedPath);
}
}
return paths.stream()
.map(p -> toFile(baseDir, p))
.filter(f -> f != null)
.collect(Collectors.toList());
}

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

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

List<Path> collectDir(Path baseDir, Path dir) throws IOException {
FileCollector collector = new FileCollector(baseDir, fileMatcher);
Files.walkFileTree(dir, collector);
return collector.getFiles();
}
}
47 changes: 47 additions & 0 deletions src/main/java/cc/files/FileCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cc.files;

import cc.CustomInputFileFinder;
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class FileCollector extends SimpleFileVisitor<Path> {
final FileMatcher fileMatcher;
final List<Path> files;
final Path baseDir;

public FileCollector(Path baseDir, FileMatcher fileMatcher) {
this.fileMatcher = fileMatcher;
this.baseDir = baseDir;
this.files = new ArrayList<>();
}

public List<Path> getFiles() {
return files;
}

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

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (Files.isHidden(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}

return super.preVisitDirectory(dir, attrs);
}
}
55 changes: 55 additions & 0 deletions src/main/java/cc/files/FileMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cc.files;

import org.sonarlint.cli.InputFileFinder;
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;

import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;

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

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

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

public FileMatcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
this(create(srcGlobPattern, ACCEPT_ALL), create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), charset);
}

public boolean validatePath(Path baseDir, Path absoluteFilePath) {
Path relativeFilePath = baseDir.relativize(absoluteFilePath);
boolean isSrc = srcMatcher.matches(absoluteFilePath) || srcMatcher.matches(relativeFilePath);
boolean isExcluded = excludeMatcher.matches(absoluteFilePath) || excludeMatcher.matches(relativeFilePath);
return isSrc && !isExcluded;
}

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) {
try {
if (pattern != null) {
return FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern);
} else {
return defaultMatcher;
}
} catch (Exception e) {
throw new RuntimeException("Error creating create with pattern: " + pattern, e);
}
}
}
6 changes: 1 addition & 5 deletions src/main/java/org/sonarlint/cli/CustomMain.java
Original file line number Diff line number Diff line change
@@ -46,11 +46,7 @@ public CustomMain(Options opts, SonarLintFactory sonarLintFactory, ReportFactory
super(opts, sonarLintFactory, reportFactory, fileFinder, projectHome);
}

public static void main(String[] args) {
execute(args, System2.INSTANCE);
}

static void execute(String[] args, System2 system) {
public static void execute(String[] args, System2 system) {
Options parsedOpts;
try {
parsedOpts = Options.parse(args);
4 changes: 2 additions & 2 deletions src/test/java/cc/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,6 @@ public class ConfigTest {
@Test
public void include_paths() throws Exception {
Config config = Config.from("fixtures/multiple_paths/config.json");
assertThat(config.includePaths).containsOnly("Main.java", "src/main/java/");
assertThat(config.includePaths).containsOnly("Main.java", "src/included/");
}
}
}
99 changes: 99 additions & 0 deletions src/test/java/cc/CustomInputFileFinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cc;

import org.junit.Test;
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;


public class CustomInputFileFinderTest {

@Test
public void find_files_in_directory() throws Exception {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/"), null, null, null, 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 find_specified_files() throws Exception {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "Main.java"), null, null, null, 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/Main.java",
"fixtures/multiple_paths/config.json"
);
}

@Test
public void find_from_multiple_locations() throws Exception {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, null, null, 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",
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
);
}

@Test
public void keep_exclude_pattern_behaviour_on_directories() throws Exception {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, 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 {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, 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 {
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset());

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

assertThat(files.stream().filter(f -> !f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly(
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
);
assertThat(files.stream().filter(f -> f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly(
"fixtures/multiple_paths/src/test/java/Test.java"
);
}


}
Loading
Oops, something went wrong.