Skip to content

Commit 5e10240

Browse files
Integrate new Finder to honor which files to be analyzed
1 parent 2df638d commit 5e10240

File tree

10 files changed

+73
-54
lines changed

10 files changed

+73
-54
lines changed

bin/codeclimate-sonar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ java \
1212
-Djava.awt.headless=true \
1313
-Dsonarlint.home="${BUILD_DIR}" \
1414
-Dproject.home="${CODE_DIR}" \
15+
-Dconfig="/config.json" \
1516
-Dorg.freemarker.loggerLibrary=none \
1617
cc.App --src "**/*.java" $@

src/main/java/cc/Config.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
import com.google.gson.GsonBuilder;
66
import com.google.gson.stream.JsonReader;
77

8+
import java.io.File;
89
import java.io.FileNotFoundException;
910
import java.io.FileReader;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
1013
import java.util.List;
1114

1215
public class Config {
13-
List<String> includePaths;
16+
public List<String> includePaths = Arrays.asList("");
1417

15-
public static Config from(String file) throws FileNotFoundException {
16-
return gson().fromJson(new JsonReader(new FileReader(file)), Config.class);
18+
public static Config from(String file) {
19+
try {
20+
return gson().fromJson(new JsonReader(new FileReader(file)), Config.class);
21+
} catch (Exception e) {
22+
return new Config();
23+
}
1724
}
1825

1926
private static Gson gson() {

src/main/java/cc/files/FileCollector.java renamed to src/main/java/cc/files/Collector.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package cc.files;
22

3-
import cc.CustomInputFileFinder;
4-
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
5-
63
import java.io.IOException;
74
import java.nio.file.FileVisitResult;
85
import java.nio.file.Files;
@@ -12,13 +9,13 @@
129
import java.util.ArrayList;
1310
import java.util.List;
1411

15-
public class FileCollector extends SimpleFileVisitor<Path> {
16-
final FileMatcher fileMatcher;
12+
public class Collector extends SimpleFileVisitor<Path> {
13+
final Matcher matcher;
1714
final List<Path> files;
1815
final Path baseDir;
1916

20-
public FileCollector(Path baseDir, FileMatcher fileMatcher) {
21-
this.fileMatcher = fileMatcher;
17+
public Collector(Path baseDir, Matcher matcher) {
18+
this.matcher = matcher;
2219
this.baseDir = baseDir;
2320
this.files = new ArrayList<>();
2421
}
@@ -29,7 +26,7 @@ public List<Path> getFiles() {
2926

3027
@Override
3128
public FileVisitResult visitFile(final Path file, BasicFileAttributes attrs) throws IOException {
32-
boolean valid = fileMatcher.validatePath(baseDir, file);
29+
boolean valid = matcher.validatePath(baseDir, file);
3330
if (valid) {
3431
files.add(file);
3532
}
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package cc;
1+
package cc.files;
22

3-
import cc.files.FileCollector;
4-
import cc.files.FileMatcher;
53
import org.sonarlint.cli.InputFileFinder;
64
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
75

@@ -13,44 +11,45 @@
1311
import java.util.List;
1412
import java.util.stream.Collectors;
1513

16-
public class CustomInputFileFinder extends InputFileFinder {
14+
import static java.nio.file.Files.isDirectory;
15+
16+
public class Finder extends InputFileFinder {
1717
final List<String> includedPaths;
1818
final Charset charset;
19-
final FileMatcher fileMatcher;
20-
19+
final Matcher matcher;
2120

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) {
21+
public Finder(List<String> includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
2722
super(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
2823
this.includedPaths = includedPaths;
2924
this.charset = charset;
30-
this.fileMatcher = new FileMatcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
25+
this.matcher = new Matcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset);
3126
}
3227

3328
@Override
3429
public List<ClientInputFile> collect(Path baseDir) throws IOException {
30+
return findPaths(baseDir).stream()
31+
.map(p -> toClientInputFile(baseDir, p))
32+
.filter(f -> f != null)
33+
.collect(Collectors.toList());
34+
}
35+
36+
List<Path> findPaths(Path baseDir) throws IOException {
3537
List<Path> paths = new ArrayList<>();
3638
for (String path : includedPaths) {
3739
Path resolvedPath = baseDir.resolve(path);
38-
if (path.endsWith("/")) {
40+
if (isDirectory(resolvedPath)) {
3941
paths.addAll(collectDir(baseDir, resolvedPath));
4042
} else {
4143
paths.add(resolvedPath);
4244
}
4345
}
44-
return paths.stream()
45-
.map(p -> toFile(baseDir, p))
46-
.filter(f -> f != null)
47-
.collect(Collectors.toList());
46+
return paths;
4847
}
4948

50-
ClientInputFile toFile(Path baseDir, Path path) {
51-
boolean valid = fileMatcher.validatePath(baseDir, path);
49+
ClientInputFile toClientInputFile(Path baseDir, Path path) {
50+
boolean valid = matcher.validatePath(baseDir, path);
5251
if (valid) {
53-
return createInputFile(path, fileMatcher.isTest(baseDir, path));
52+
return createInputFile(path, matcher.isTest(baseDir, path));
5453
}
5554
return null;
5655
}
@@ -60,8 +59,8 @@ ClientInputFile createInputFile(Path resolvedPath, boolean test) {
6059
}
6160

6261
List<Path> collectDir(Path baseDir, Path dir) throws IOException {
63-
FileCollector collector = new FileCollector(baseDir, fileMatcher);
62+
Collector collector = new Collector(baseDir, matcher);
6463
Files.walkFileTree(dir, collector);
6564
return collector.getFiles();
6665
}
67-
}
66+
}

src/main/java/cc/files/FileMatcher.java renamed to src/main/java/cc/files/Matcher.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package cc.files;
22

3-
import org.sonarlint.cli.InputFileFinder;
4-
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
5-
63
import java.nio.charset.Charset;
74
import java.nio.file.FileSystems;
85
import java.nio.file.Path;
96
import java.nio.file.PathMatcher;
107

11-
public class FileMatcher {
8+
public class Matcher {
129
private static final String GLOB_PREFIX = "glob:";
1310
public static PathMatcher ACCEPT_ALL = p -> true;
1411
public static PathMatcher REFUSE_ALL = p -> false;
@@ -18,14 +15,14 @@ public class FileMatcher {
1815
final PathMatcher excludeMatcher;
1916
final Charset charset;
2017

21-
public FileMatcher(PathMatcher srcMatcher, PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) {
18+
public Matcher(PathMatcher srcMatcher, PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) {
2219
this.srcMatcher = srcMatcher;
2320
this.testsMatcher = testsMatcher;
2421
this.excludeMatcher = excludeMatcher;
2522
this.charset = charset;
2623
}
2724

28-
public FileMatcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
25+
public Matcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) {
2926
this(create(srcGlobPattern, ACCEPT_ALL), create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), charset);
3027
}
3128

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
package org.sonarlint.cli;
2121

22+
import cc.Config;
23+
import cc.files.Finder;
2224
import cc.JsonReport;
2325
import org.sonarlint.cli.analysis.SonarLintFactory;
2426
import org.sonarlint.cli.config.ConfigurationReader;
@@ -70,7 +72,8 @@ public static void execute(String[] args, System2 system) {
7072
return;
7173
}
7274

73-
InputFileFinder fileFinder = new InputFileFinder(parsedOpts.src(), parsedOpts.tests(), parsedOpts.exclusions(), charset);
75+
Config config = Config.from(system.getProperty("config"));
76+
InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.src(), parsedOpts.tests(), parsedOpts.exclusions(), charset);
7477
ReportFactory reportFactory = new CustomReportFactory(charset);
7578
ConfigurationReader reader = new ConfigurationReader();
7679
SonarLintFactory sonarLintFactory = new SonarLintFactory(reader);
@@ -98,4 +101,4 @@ public List<Reporter> createReporters(Path basePath) {
98101
return Arrays.asList(new JsonReport());
99102
}
100103
}
101-
}
104+
}

src/test/java/cc/ConfigTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ public void include_paths() throws Exception {
1212
Config config = Config.from("fixtures/multiple_paths/config.json");
1313
assertThat(config.includePaths).containsOnly("Main.java", "src/included/");
1414
}
15+
16+
@Test
17+
public void default_config_path_include_base_dir() throws Exception {
18+
Config config = new Config();
19+
assertThat(config.includePaths).containsOnly("");
20+
}
1521
}

src/test/java/cc/CustomInputFileFinderTest.java renamed to src/test/java/cc/files/FinderTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc;
1+
package cc.files;
22

33
import org.junit.Test;
44
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
@@ -12,11 +12,11 @@
1212
import static org.assertj.core.api.Assertions.assertThat;
1313

1414

15-
public class CustomInputFileFinderTest {
15+
public class FinderTest {
1616

1717
@Test
1818
public void find_files_in_directory() throws Exception {
19-
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/"), null, null, null, Charset.defaultCharset());
19+
Finder finder = new Finder(Arrays.asList("src/included/"), null, null, null, Charset.defaultCharset());
2020

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

3030
@Test
3131
public void find_specified_files() throws Exception {
32-
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "Main.java"), null, null, null, Charset.defaultCharset());
32+
Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, null, null, Charset.defaultCharset());
3333

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

4343
@Test
4444
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());
45+
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, null, Charset.defaultCharset());
4646

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

5757
@Test
5858
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());
59+
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/HasNoIssue.*", Charset.defaultCharset());
6060

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

7070
@Test
7171
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());
72+
Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/*.json", Charset.defaultCharset());
7373

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

8383
@Test
8484
public void differentiate_src_and_test() throws Exception {
85-
CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset());
85+
Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset());
8686

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

@@ -94,6 +94,4 @@ public void differentiate_src_and_test() throws Exception {
9494
"fixtures/multiple_paths/src/test/java/Test.java"
9595
);
9696
}
97-
98-
9997
}

src/test/java/integration/ConfigurationOptionsTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@ public void setUp() throws Exception {
3737

3838
@Test
3939
public void limit_path_included_within_analysis() throws Exception {
40-
App.execute(new String[]{}, system);
40+
App.execute(new String[]{"-Dconfig=fixtures/multiple_paths/config.json"}, system);
4141

4242
String output = stdout.toString();
4343
assertThat(output).contains("issue", "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java");
4444
assertThat(output).doesNotContain("fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java");
4545
}
4646

47+
@Test
48+
public void include_all_files_by_default() throws Exception {
49+
App.execute(new String[]{}, system);
50+
51+
String output = stdout.toString();
52+
assertThat(output).contains(
53+
"issue",
54+
"fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java",
55+
"fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java"
56+
);
57+
}
58+
4759
}

src/test/java/integration/SanityCheckTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public void executeJavaLibFixture() throws Exception {
4848

4949
Shell.Process process = Shell.execute("build/codeclimate-sonar fixtures/java_lib");
5050

51-
assertThat(process.exitCode).isEqualTo(0);
52-
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>." + process.stdout);
5351
assertThat(process.stdout).contains(expectedOutput);
52+
assertThat(process.exitCode).isEqualTo(0);
5453
}
5554
}

0 commit comments

Comments
 (0)