Skip to content

Commit 2df638d

Browse files
Find correct paths to process
1 parent 39fb0c1 commit 2df638d

File tree

19 files changed

+376
-8
lines changed

19 files changed

+376
-8
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ dependencies {
4141
// Plugins
4242
compile("org.sonarsource.java:sonar-java-plugin:4.3.0.7717")
4343

44+
testCompile("org.mockito:mockito-core:2.+")
4445
testCompile("org.assertj:assertj-core:2.8.0")
4546
testCompile("junit:junit:4.12")
4647
}
4748

4849
test {
4950
outputs.upToDateWhen { false }
5051
testLogging {
52+
events "passed", "skipped", "failed", "standardOut", "standardError"
5153
exceptionFormat "full"
5254
}
5355
}

fixtures/multiple_paths/Main.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pkg1;
2+
3+
public class Main {
4+
public void main(String[] args) {
5+
for (int k = 0; k < 20; i++) { // cause issue
6+
System.out.println(new Class1());
7+
}
8+
}
9+
}

fixtures/multiple_paths/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"enabled": true,
33
"include_paths": [
4-
"src/main/java/",
4+
"src/included/",
55
"Main.java"
66
]
77
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg1;
2+
3+
public class HasIssue {
4+
public void method() {
5+
for (int i = 0; i < 10; i++) {
6+
for (int k = 0; k < 20; i++) {
7+
System.out.println("Hello");
8+
}
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg1;
2+
3+
class HasIssue {
4+
public void method() {
5+
for (int i = 0; i < 10; i++) {
6+
for (int k = 0; k < 20; i++) {
7+
System.out.println("Hello");
8+
}
9+
}
10+
}
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package pkg1;
2+
3+
public class HasNoIssue {
4+
}

fixtures/multiple_paths/src/main/java/Class1.java

Whitespace-only changes.

fixtures/multiple_paths/src/main/java/Class2.java

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public class Test {
2+
}

src/main/java/cc/App.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package cc;
22

33
import org.sonarlint.cli.CustomMain;
4+
import org.sonarlint.cli.util.System2;
45

56
public class App {
67
public static void main(String[] args) {
7-
CustomMain.main(args);
8+
execute(args, System2.INSTANCE);
9+
}
10+
11+
public static void execute(String[] args, System2 system) {
12+
CustomMain.execute(args, system);
813
}
914
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cc;
2+
3+
import cc.files.FileCollector;
4+
import cc.files.FileMatcher;
5+
import org.sonarlint.cli.InputFileFinder;
6+
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
7+
8+
import java.io.IOException;
9+
import java.nio.charset.Charset;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
public class CustomInputFileFinder extends InputFileFinder {
17+
final List<String> includedPaths;
18+
final Charset charset;
19+
final FileMatcher fileMatcher;
20+
21+
22+
public CustomInputFileFinder(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
23+
this(new ArrayList<>(), srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
24+
}
25+
26+
public CustomInputFileFinder(List<String> includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
27+
super(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
28+
this.includedPaths = includedPaths;
29+
this.charset = charset;
30+
this.fileMatcher = new FileMatcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
31+
}
32+
33+
@Override
34+
public List<ClientInputFile> collect(Path baseDir) throws IOException {
35+
List<Path> paths = new ArrayList<>();
36+
for (String path : includedPaths) {
37+
Path resolvedPath = baseDir.resolve(path);
38+
if (path.endsWith("/")) {
39+
paths.addAll(collectDir(baseDir, resolvedPath));
40+
} else {
41+
paths.add(resolvedPath);
42+
}
43+
}
44+
return paths.stream()
45+
.map(p -> toFile(baseDir, p))
46+
.filter(f -> f != null)
47+
.collect(Collectors.toList());
48+
}
49+
50+
ClientInputFile toFile(Path baseDir, Path path) {
51+
boolean valid = fileMatcher.validatePath(baseDir, path);
52+
if (valid) {
53+
return createInputFile(path, fileMatcher.isTest(baseDir, path));
54+
}
55+
return null;
56+
}
57+
58+
ClientInputFile createInputFile(Path resolvedPath, boolean test) {
59+
return new DefaultClientInputFile(resolvedPath, test, charset);
60+
}
61+
62+
List<Path> collectDir(Path baseDir, Path dir) throws IOException {
63+
FileCollector collector = new FileCollector(baseDir, fileMatcher);
64+
Files.walkFileTree(dir, collector);
65+
return collector.getFiles();
66+
}
67+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cc.files;
2+
3+
import cc.CustomInputFileFinder;
4+
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
5+
6+
import java.io.IOException;
7+
import java.nio.file.FileVisitResult;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.SimpleFileVisitor;
11+
import java.nio.file.attribute.BasicFileAttributes;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class FileCollector extends SimpleFileVisitor<Path> {
16+
final FileMatcher fileMatcher;
17+
final List<Path> files;
18+
final Path baseDir;
19+
20+
public FileCollector(Path baseDir, FileMatcher fileMatcher) {
21+
this.fileMatcher = fileMatcher;
22+
this.baseDir = baseDir;
23+
this.files = new ArrayList<>();
24+
}
25+
26+
public List<Path> getFiles() {
27+
return files;
28+
}
29+
30+
@Override
31+
public FileVisitResult visitFile(final Path file, BasicFileAttributes attrs) throws IOException {
32+
boolean valid = fileMatcher.validatePath(baseDir, file);
33+
if (valid) {
34+
files.add(file);
35+
}
36+
return super.visitFile(file, attrs);
37+
}
38+
39+
@Override
40+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
41+
if (Files.isHidden(dir)) {
42+
return FileVisitResult.SKIP_SUBTREE;
43+
}
44+
45+
return super.preVisitDirectory(dir, attrs);
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cc.files;
2+
3+
import org.sonarlint.cli.InputFileFinder;
4+
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
5+
6+
import java.nio.charset.Charset;
7+
import java.nio.file.FileSystems;
8+
import java.nio.file.Path;
9+
import java.nio.file.PathMatcher;
10+
11+
public class FileMatcher {
12+
private static final String GLOB_PREFIX = "glob:";
13+
public static PathMatcher ACCEPT_ALL = p -> true;
14+
public static PathMatcher REFUSE_ALL = p -> false;
15+
16+
final PathMatcher srcMatcher;
17+
final PathMatcher testsMatcher;
18+
final PathMatcher excludeMatcher;
19+
final Charset charset;
20+
21+
public FileMatcher(PathMatcher srcMatcher, PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) {
22+
this.srcMatcher = srcMatcher;
23+
this.testsMatcher = testsMatcher;
24+
this.excludeMatcher = excludeMatcher;
25+
this.charset = charset;
26+
}
27+
28+
public FileMatcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
29+
this(create(srcGlobPattern, ACCEPT_ALL), create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), charset);
30+
}
31+
32+
public boolean validatePath(Path baseDir, Path absoluteFilePath) {
33+
Path relativeFilePath = baseDir.relativize(absoluteFilePath);
34+
boolean isSrc = srcMatcher.matches(absoluteFilePath) || srcMatcher.matches(relativeFilePath);
35+
boolean isExcluded = excludeMatcher.matches(absoluteFilePath) || excludeMatcher.matches(relativeFilePath);
36+
return isSrc && !isExcluded;
37+
}
38+
39+
public boolean isTest(Path baseDir, Path absoluteFilePath) {
40+
Path relativeFilePath = baseDir.relativize(absoluteFilePath);
41+
return testsMatcher.matches(absoluteFilePath) || testsMatcher.matches(relativeFilePath);
42+
}
43+
44+
static PathMatcher create(String pattern, PathMatcher defaultMatcher) {
45+
try {
46+
if (pattern != null) {
47+
return FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern);
48+
} else {
49+
return defaultMatcher;
50+
}
51+
} catch (Exception e) {
52+
throw new RuntimeException("Error creating create with pattern: " + pattern, e);
53+
}
54+
}
55+
}

src/main/java/org/sonarlint/cli/CustomMain.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ public CustomMain(Options opts, SonarLintFactory sonarLintFactory, ReportFactory
4646
super(opts, sonarLintFactory, reportFactory, fileFinder, projectHome);
4747
}
4848

49-
public static void main(String[] args) {
50-
execute(args, System2.INSTANCE);
51-
}
52-
53-
static void execute(String[] args, System2 system) {
49+
public static void execute(String[] args, System2 system) {
5450
Options parsedOpts;
5551
try {
5652
parsedOpts = Options.parse(args);

src/test/java/cc/ConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public class ConfigTest {
1010
@Test
1111
public void include_paths() throws Exception {
1212
Config config = Config.from("fixtures/multiple_paths/config.json");
13-
assertThat(config.includePaths).containsOnly("Main.java", "src/main/java/");
13+
assertThat(config.includePaths).containsOnly("Main.java", "src/included/");
1414
}
1515
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package cc;
2+
3+
import org.junit.Test;
4+
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
5+
6+
import java.nio.charset.Charset;
7+
import java.nio.file.Paths;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
15+
public class CustomInputFileFinderTest {
16+
17+
@Test
18+
public void find_files_in_directory() throws Exception {
19+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/"), null, null, null, Charset.defaultCharset());
20+
21+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
22+
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
23+
24+
assertThat(paths).containsOnly(
25+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
26+
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
27+
);
28+
}
29+
30+
@Test
31+
public void find_specified_files() throws Exception {
32+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "Main.java"), null, null, null, Charset.defaultCharset());
33+
34+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
35+
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
36+
37+
assertThat(paths).containsOnly(
38+
"fixtures/multiple_paths/Main.java",
39+
"fixtures/multiple_paths/config.json"
40+
);
41+
}
42+
43+
@Test
44+
public void find_from_multiple_locations() throws Exception {
45+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, null, null, Charset.defaultCharset());
46+
47+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
48+
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
49+
50+
assertThat(paths).containsOnly(
51+
"fixtures/multiple_paths/config.json",
52+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
53+
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
54+
);
55+
}
56+
57+
@Test
58+
public void keep_exclude_pattern_behaviour_on_directories() throws Exception {
59+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/HasNoIssue.*", Charset.defaultCharset());
60+
61+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
62+
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
63+
64+
assertThat(paths).containsOnly(
65+
"fixtures/multiple_paths/config.json",
66+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java"
67+
);
68+
}
69+
70+
@Test
71+
public void keep_exclude_pattern_behaviour_on_files() throws Exception {
72+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/*.json", Charset.defaultCharset());
73+
74+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
75+
List<String> paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList());
76+
77+
assertThat(paths).containsOnly(
78+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
79+
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
80+
);
81+
}
82+
83+
@Test
84+
public void differentiate_src_and_test() throws Exception {
85+
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset());
86+
87+
List<ClientInputFile> files = finder.collect(Paths.get("fixtures/multiple_paths"));
88+
89+
assertThat(files.stream().filter(f -> !f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly(
90+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
91+
"fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java"
92+
);
93+
assertThat(files.stream().filter(f -> f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly(
94+
"fixtures/multiple_paths/src/test/java/Test.java"
95+
);
96+
}
97+
98+
99+
}

0 commit comments

Comments
 (0)