From 9f7e753fcc30dd13b0af222f5676d5d01de7d57f Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Tue, 10 Oct 2017 12:53:05 -0300 Subject: [PATCH 1/7] Add testing files --- fixtures/multiple_paths/Main.java | 0 fixtures/multiple_paths/src/main/java/Class1.java | 0 fixtures/multiple_paths/src/main/java/Class2.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 fixtures/multiple_paths/Main.java create mode 100644 fixtures/multiple_paths/src/main/java/Class1.java create mode 100644 fixtures/multiple_paths/src/main/java/Class2.java diff --git a/fixtures/multiple_paths/Main.java b/fixtures/multiple_paths/Main.java new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/multiple_paths/src/main/java/Class1.java b/fixtures/multiple_paths/src/main/java/Class1.java new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/multiple_paths/src/main/java/Class2.java b/fixtures/multiple_paths/src/main/java/Class2.java new file mode 100644 index 0000000..e69de29 From 723e86f11a5c96104891b8caade01767b47688e7 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Thu, 5 Oct 2017 19:06:42 -0300 Subject: [PATCH 2/7] Find correct paths to process --- fixtures/multiple_paths/Main.java | 9 ++ fixtures/multiple_paths/config.json | 2 +- .../src/excluded/java/pkg1/HasIssue.java | 11 +++ .../src/included/java/pkg1/HasIssue.java | 11 +++ .../src/included/java/pkg1/HasNoIssue.java | 4 + .../multiple_paths/src/main/java/Class1.java | 0 .../multiple_paths/src/main/java/Class2.java | 0 .../multiple_paths/src/test/java/Test.java | 2 + src/main/java/cc/App.java | 7 +- src/main/java/cc/CustomInputFileFinder.java | 67 +++++++++++++ src/main/java/cc/files/FileCollector.java | 47 +++++++++ src/main/java/cc/files/FileMatcher.java | 55 +++++++++++ .../java/org/sonarlint/cli/CustomMain.java | 6 +- src/test/java/cc/ConfigTest.java | 4 +- .../java/cc/CustomInputFileFinderTest.java | 99 +++++++++++++++++++ .../integration/ConfigurationOptionsTest.java | 47 +++++++++ .../java/integration/SanityCheckTest.java | 1 + src/test/java/support/TestSystem.java | 12 +++ 18 files changed, 375 insertions(+), 9 deletions(-) create mode 100644 fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java create mode 100644 fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java create mode 100644 fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java delete mode 100644 fixtures/multiple_paths/src/main/java/Class1.java delete mode 100644 fixtures/multiple_paths/src/main/java/Class2.java create mode 100644 fixtures/multiple_paths/src/test/java/Test.java create mode 100644 src/main/java/cc/CustomInputFileFinder.java create mode 100644 src/main/java/cc/files/FileCollector.java create mode 100644 src/main/java/cc/files/FileMatcher.java create mode 100644 src/test/java/cc/CustomInputFileFinderTest.java create mode 100644 src/test/java/integration/ConfigurationOptionsTest.java create mode 100644 src/test/java/support/TestSystem.java diff --git a/fixtures/multiple_paths/Main.java b/fixtures/multiple_paths/Main.java index e69de29..ebb7893 100644 --- a/fixtures/multiple_paths/Main.java +++ b/fixtures/multiple_paths/Main.java @@ -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()); + } + } +} \ No newline at end of file diff --git a/fixtures/multiple_paths/config.json b/fixtures/multiple_paths/config.json index 48e7a98..a3c992c 100644 --- a/fixtures/multiple_paths/config.json +++ b/fixtures/multiple_paths/config.json @@ -1,7 +1,7 @@ { "enabled": true, "include_paths": [ - "src/main/java/", + "src/included/", "Main.java" ] } diff --git a/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java b/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java new file mode 100644 index 0000000..f45794e --- /dev/null +++ b/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java @@ -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"); + } + } + } +} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java b/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java new file mode 100644 index 0000000..b85a861 --- /dev/null +++ b/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java @@ -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"); + } + } + } +} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java b/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java new file mode 100644 index 0000000..281d5df --- /dev/null +++ b/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java @@ -0,0 +1,4 @@ +package pkg1; + +public class HasNoIssue { +} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/main/java/Class1.java b/fixtures/multiple_paths/src/main/java/Class1.java deleted file mode 100644 index e69de29..0000000 diff --git a/fixtures/multiple_paths/src/main/java/Class2.java b/fixtures/multiple_paths/src/main/java/Class2.java deleted file mode 100644 index e69de29..0000000 diff --git a/fixtures/multiple_paths/src/test/java/Test.java b/fixtures/multiple_paths/src/test/java/Test.java new file mode 100644 index 0000000..0b8bc08 --- /dev/null +++ b/fixtures/multiple_paths/src/test/java/Test.java @@ -0,0 +1,2 @@ +public class Test { +} \ No newline at end of file diff --git a/src/main/java/cc/App.java b/src/main/java/cc/App.java index 5ebb316..fedb4c3 100644 --- a/src/main/java/cc/App.java +++ b/src/main/java/cc/App.java @@ -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); } } diff --git a/src/main/java/cc/CustomInputFileFinder.java b/src/main/java/cc/CustomInputFileFinder.java new file mode 100644 index 0000000..1a9630a --- /dev/null +++ b/src/main/java/cc/CustomInputFileFinder.java @@ -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 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 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 collect(Path baseDir) throws IOException { + List 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 collectDir(Path baseDir, Path dir) throws IOException { + FileCollector collector = new FileCollector(baseDir, fileMatcher); + Files.walkFileTree(dir, collector); + return collector.getFiles(); + } +} diff --git a/src/main/java/cc/files/FileCollector.java b/src/main/java/cc/files/FileCollector.java new file mode 100644 index 0000000..35f88f1 --- /dev/null +++ b/src/main/java/cc/files/FileCollector.java @@ -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 { + final FileMatcher fileMatcher; + final List files; + final Path baseDir; + + public FileCollector(Path baseDir, FileMatcher fileMatcher) { + this.fileMatcher = fileMatcher; + this.baseDir = baseDir; + this.files = new ArrayList<>(); + } + + public List 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); + } +} diff --git a/src/main/java/cc/files/FileMatcher.java b/src/main/java/cc/files/FileMatcher.java new file mode 100644 index 0000000..72006e2 --- /dev/null +++ b/src/main/java/cc/files/FileMatcher.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/sonarlint/cli/CustomMain.java b/src/main/java/org/sonarlint/cli/CustomMain.java index d1f2e2e..53cc9b4 100644 --- a/src/main/java/org/sonarlint/cli/CustomMain.java +++ b/src/main/java/org/sonarlint/cli/CustomMain.java @@ -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); diff --git a/src/test/java/cc/ConfigTest.java b/src/test/java/cc/ConfigTest.java index cce177c..8b29449 100644 --- a/src/test/java/cc/ConfigTest.java +++ b/src/test/java/cc/ConfigTest.java @@ -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/"); } -} +} \ No newline at end of file diff --git a/src/test/java/cc/CustomInputFileFinderTest.java b/src/test/java/cc/CustomInputFileFinderTest.java new file mode 100644 index 0000000..daa3e8c --- /dev/null +++ b/src/test/java/cc/CustomInputFileFinderTest.java @@ -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 files = finder.collect(Paths.get("fixtures/multiple_paths")); + List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); + List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); + List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); + List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); + List 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 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" + ); + } + + +} \ No newline at end of file diff --git a/src/test/java/integration/ConfigurationOptionsTest.java b/src/test/java/integration/ConfigurationOptionsTest.java new file mode 100644 index 0000000..f0f86bd --- /dev/null +++ b/src/test/java/integration/ConfigurationOptionsTest.java @@ -0,0 +1,47 @@ +package integration; + +import cc.App; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonarlint.cli.SonarProperties; +import org.sonarlint.cli.util.System2; +import support.TestSystem; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ConfigurationOptionsTest { + ByteArrayOutputStream stdout; + ByteArrayOutputStream stderr; + + System2 system; + + @BeforeClass + public static void beforeAll() { + System.setProperty(SonarProperties.PROJECT_HOME, "fixtures/multiple_paths"); + System.setProperty(SonarProperties.SONARLINT_HOME, "build"); + } + + @Before + public void setUp() throws Exception { + system = new TestSystem(); + stdout = new ByteArrayOutputStream(); + stderr = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(stdout)); + System.setErr(new PrintStream(stderr)); + } + + @Test + public void limit_path_included_within_analysis() throws Exception { + App.execute(new String[]{}, system); + + String output = stdout.toString(); + assertThat(output).contains("issue", "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java"); + assertThat(output).doesNotContain("fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java"); + } + +} diff --git a/src/test/java/integration/SanityCheckTest.java b/src/test/java/integration/SanityCheckTest.java index 62d979a..e679f33 100644 --- a/src/test/java/integration/SanityCheckTest.java +++ b/src/test/java/integration/SanityCheckTest.java @@ -49,6 +49,7 @@ public void executeJavaLibFixture() throws Exception { Shell.Process process = Shell.execute("build/codeclimate-sonar fixtures/java_lib"); assertThat(process.exitCode).isEqualTo(0); + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>." + process.stdout); assertThat(process.stdout).contains(expectedOutput); } } diff --git a/src/test/java/support/TestSystem.java b/src/test/java/support/TestSystem.java new file mode 100644 index 0000000..2ff9718 --- /dev/null +++ b/src/test/java/support/TestSystem.java @@ -0,0 +1,12 @@ +package support; + +import org.sonarlint.cli.util.System2; + +public class TestSystem extends System2 { + public int exitCode; + + @Override + public void exit(int exitCode) { + this.exitCode = exitCode; + } +} From f2cbbc65a4471e9f676788d64101a0813a7b8c24 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Fri, 6 Oct 2017 10:34:19 -0300 Subject: [PATCH 3/7] Integrate new Finder to honor which files to be analyzed --- bin/codeclimate-sonar | 1 + src/main/java/cc/Config.java | 13 ++++-- .../{FileCollector.java => Collector.java} | 13 +++--- .../Finder.java} | 43 +++++++++---------- .../files/{FileMatcher.java => Matcher.java} | 9 ++-- .../java/org/sonarlint/cli/CustomMain.java | 7 ++- src/test/java/cc/ConfigTest.java | 6 +++ .../FinderTest.java} | 18 ++++---- .../integration/ConfigurationOptionsTest.java | 19 ++++++-- .../java/integration/SanityCheckTest.java | 3 +- src/test/java/support/TestSystem.java | 22 ++++++++++ 11 files changed, 98 insertions(+), 56 deletions(-) rename src/main/java/cc/files/{FileCollector.java => Collector.java} (71%) rename src/main/java/cc/{CustomInputFileFinder.java => files/Finder.java} (57%) rename src/main/java/cc/files/{FileMatcher.java => Matcher.java} (82%) rename src/test/java/cc/{CustomInputFileFinderTest.java => files/FinderTest.java} (77%) diff --git a/bin/codeclimate-sonar b/bin/codeclimate-sonar index 53d1fda..2450080 100755 --- a/bin/codeclimate-sonar +++ b/bin/codeclimate-sonar @@ -12,5 +12,6 @@ java \ -Djava.awt.headless=true \ -Dsonarlint.home="${BUILD_DIR}" \ -Dproject.home="${CODE_DIR}" \ + -Dconfig="/config.json" \ -Dorg.freemarker.loggerLibrary=none \ cc.App --src "**/*.java" $@ diff --git a/src/main/java/cc/Config.java b/src/main/java/cc/Config.java index 16f7095..5b9a48d 100644 --- a/src/main/java/cc/Config.java +++ b/src/main/java/cc/Config.java @@ -5,15 +5,22 @@ import com.google.gson.GsonBuilder; import com.google.gson.stream.JsonReader; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class Config { - List includePaths; + public List includePaths = Arrays.asList(""); - public static Config from(String file) throws FileNotFoundException { - return gson().fromJson(new JsonReader(new FileReader(file)), Config.class); + public static Config from(String file) { + try { + return gson().fromJson(new JsonReader(new FileReader(file)), Config.class); + } catch (Exception e) { + return new Config(); + } } private static Gson gson() { diff --git a/src/main/java/cc/files/FileCollector.java b/src/main/java/cc/files/Collector.java similarity index 71% rename from src/main/java/cc/files/FileCollector.java rename to src/main/java/cc/files/Collector.java index 35f88f1..699407d 100644 --- a/src/main/java/cc/files/FileCollector.java +++ b/src/main/java/cc/files/Collector.java @@ -1,8 +1,5 @@ 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; @@ -12,13 +9,13 @@ import java.util.ArrayList; import java.util.List; -public class FileCollector extends SimpleFileVisitor { - final FileMatcher fileMatcher; +public class Collector extends SimpleFileVisitor { + final Matcher matcher; final List files; final Path baseDir; - public FileCollector(Path baseDir, FileMatcher fileMatcher) { - this.fileMatcher = fileMatcher; + public Collector(Path baseDir, Matcher matcher) { + this.matcher = matcher; this.baseDir = baseDir; this.files = new ArrayList<>(); } @@ -29,7 +26,7 @@ public List getFiles() { @Override public FileVisitResult visitFile(final Path file, BasicFileAttributes attrs) throws IOException { - boolean valid = fileMatcher.validatePath(baseDir, file); + boolean valid = matcher.validatePath(baseDir, file); if (valid) { files.add(file); } diff --git a/src/main/java/cc/CustomInputFileFinder.java b/src/main/java/cc/files/Finder.java similarity index 57% rename from src/main/java/cc/CustomInputFileFinder.java rename to src/main/java/cc/files/Finder.java index 1a9630a..481dd03 100644 --- a/src/main/java/cc/CustomInputFileFinder.java +++ b/src/main/java/cc/files/Finder.java @@ -1,7 +1,5 @@ -package cc; +package cc.files; -import cc.files.FileCollector; -import cc.files.FileMatcher; import org.sonarlint.cli.InputFileFinder; import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; @@ -13,44 +11,45 @@ import java.util.List; import java.util.stream.Collectors; -public class CustomInputFileFinder extends InputFileFinder { +import static java.nio.file.Files.isDirectory; + +public class Finder extends InputFileFinder { final List includedPaths; final Charset charset; - final FileMatcher fileMatcher; - + final Matcher matcher; - public CustomInputFileFinder(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { - this(new ArrayList<>(), srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); - } - - public CustomInputFileFinder(List includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { + public Finder(List 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); + this.matcher = new Matcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); } @Override public List collect(Path baseDir) throws IOException { + return findPaths(baseDir).stream() + .map(p -> toClientInputFile(baseDir, p)) + .filter(f -> f != null) + .collect(Collectors.toList()); + } + + List findPaths(Path baseDir) throws IOException { List paths = new ArrayList<>(); for (String path : includedPaths) { Path resolvedPath = baseDir.resolve(path); - if (path.endsWith("/")) { + if (isDirectory(resolvedPath)) { paths.addAll(collectDir(baseDir, resolvedPath)); } else { paths.add(resolvedPath); } } - return paths.stream() - .map(p -> toFile(baseDir, p)) - .filter(f -> f != null) - .collect(Collectors.toList()); + return paths; } - ClientInputFile toFile(Path baseDir, Path path) { - boolean valid = fileMatcher.validatePath(baseDir, path); + ClientInputFile toClientInputFile(Path baseDir, Path path) { + boolean valid = matcher.validatePath(baseDir, path); if (valid) { - return createInputFile(path, fileMatcher.isTest(baseDir, path)); + return createInputFile(path, matcher.isTest(baseDir, path)); } return null; } @@ -60,8 +59,8 @@ ClientInputFile createInputFile(Path resolvedPath, boolean test) { } List collectDir(Path baseDir, Path dir) throws IOException { - FileCollector collector = new FileCollector(baseDir, fileMatcher); + Collector collector = new Collector(baseDir, matcher); Files.walkFileTree(dir, collector); return collector.getFiles(); } -} +} \ No newline at end of file diff --git a/src/main/java/cc/files/FileMatcher.java b/src/main/java/cc/files/Matcher.java similarity index 82% rename from src/main/java/cc/files/FileMatcher.java rename to src/main/java/cc/files/Matcher.java index 72006e2..dad1c8e 100644 --- a/src/main/java/cc/files/FileMatcher.java +++ b/src/main/java/cc/files/Matcher.java @@ -1,14 +1,11 @@ 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 { +public class Matcher { private static final String GLOB_PREFIX = "glob:"; public static PathMatcher ACCEPT_ALL = p -> true; public static PathMatcher REFUSE_ALL = p -> false; @@ -18,14 +15,14 @@ public class FileMatcher { final PathMatcher excludeMatcher; final Charset charset; - public FileMatcher(PathMatcher srcMatcher, PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) { + public Matcher(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) { + public Matcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { this(create(srcGlobPattern, ACCEPT_ALL), create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), charset); } diff --git a/src/main/java/org/sonarlint/cli/CustomMain.java b/src/main/java/org/sonarlint/cli/CustomMain.java index 53cc9b4..052d0f9 100644 --- a/src/main/java/org/sonarlint/cli/CustomMain.java +++ b/src/main/java/org/sonarlint/cli/CustomMain.java @@ -19,6 +19,8 @@ */ package org.sonarlint.cli; +import cc.Config; +import cc.files.Finder; import cc.JsonReport; import org.sonarlint.cli.analysis.SonarLintFactory; import org.sonarlint.cli.config.ConfigurationReader; @@ -70,7 +72,8 @@ public static void execute(String[] args, System2 system) { return; } - InputFileFinder fileFinder = new InputFileFinder(parsedOpts.src(), parsedOpts.tests(), parsedOpts.exclusions(), charset); + Config config = Config.from(system.getProperty("config")); + InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.src(), parsedOpts.tests(), parsedOpts.exclusions(), charset); ReportFactory reportFactory = new CustomReportFactory(charset); ConfigurationReader reader = new ConfigurationReader(); SonarLintFactory sonarLintFactory = new SonarLintFactory(reader); @@ -98,4 +101,4 @@ public List createReporters(Path basePath) { return Arrays.asList(new JsonReport()); } } -} +} \ No newline at end of file diff --git a/src/test/java/cc/ConfigTest.java b/src/test/java/cc/ConfigTest.java index 8b29449..402d4d5 100644 --- a/src/test/java/cc/ConfigTest.java +++ b/src/test/java/cc/ConfigTest.java @@ -11,4 +11,10 @@ public void include_paths() throws Exception { Config config = Config.from("fixtures/multiple_paths/config.json"); assertThat(config.includePaths).containsOnly("Main.java", "src/included/"); } + + @Test + public void default_config_path_include_base_dir() throws Exception { + Config config = new Config(); + assertThat(config.includePaths).containsOnly(""); + } } \ No newline at end of file diff --git a/src/test/java/cc/CustomInputFileFinderTest.java b/src/test/java/cc/files/FinderTest.java similarity index 77% rename from src/test/java/cc/CustomInputFileFinderTest.java rename to src/test/java/cc/files/FinderTest.java index daa3e8c..5880b30 100644 --- a/src/test/java/cc/CustomInputFileFinderTest.java +++ b/src/test/java/cc/files/FinderTest.java @@ -1,4 +1,4 @@ -package cc; +package cc.files; import org.junit.Test; import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; @@ -12,11 +12,11 @@ import static org.assertj.core.api.Assertions.assertThat; -public class CustomInputFileFinderTest { +public class FinderTest { @Test public void find_files_in_directory() throws Exception { - CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/"), null, null, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("src/included/"), null, null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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 { - CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "Main.java"), null, null, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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 { - CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("config.json", "src/included/java/"), null, null, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -56,7 +56,7 @@ public void find_from_multiple_locations() throws Exception { @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()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/HasNoIssue.*", Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -69,7 +69,7 @@ public void keep_exclude_pattern_behaviour_on_directories() throws Exception { @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()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/*.json", Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -82,7 +82,7 @@ public void keep_exclude_pattern_behaviour_on_files() throws Exception { @Test public void differentiate_src_and_test() throws Exception { - CustomInputFileFinder finder = new CustomInputFileFinder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); @@ -94,6 +94,4 @@ public void differentiate_src_and_test() throws Exception { "fixtures/multiple_paths/src/test/java/Test.java" ); } - - } \ No newline at end of file diff --git a/src/test/java/integration/ConfigurationOptionsTest.java b/src/test/java/integration/ConfigurationOptionsTest.java index f0f86bd..f898e92 100644 --- a/src/test/java/integration/ConfigurationOptionsTest.java +++ b/src/test/java/integration/ConfigurationOptionsTest.java @@ -5,7 +5,6 @@ import org.junit.BeforeClass; import org.junit.Test; import org.sonarlint.cli.SonarProperties; -import org.sonarlint.cli.util.System2; import support.TestSystem; import java.io.ByteArrayOutputStream; @@ -17,11 +16,10 @@ public class ConfigurationOptionsTest { ByteArrayOutputStream stdout; ByteArrayOutputStream stderr; - System2 system; + TestSystem system; @BeforeClass public static void beforeAll() { - System.setProperty(SonarProperties.PROJECT_HOME, "fixtures/multiple_paths"); System.setProperty(SonarProperties.SONARLINT_HOME, "build"); } @@ -33,10 +31,14 @@ public void setUp() throws Exception { System.setOut(new PrintStream(stdout)); System.setErr(new PrintStream(stderr)); + + system.setProperty(SonarProperties.PROJECT_HOME, "fixtures/multiple_paths"); } @Test public void limit_path_included_within_analysis() throws Exception { + system.setProperty("config", "fixtures/multiple_paths/config.json"); + App.execute(new String[]{}, system); String output = stdout.toString(); @@ -44,4 +46,15 @@ public void limit_path_included_within_analysis() throws Exception { assertThat(output).doesNotContain("fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java"); } + @Test + public void include_all_files_by_default() throws Exception { + App.execute(new String[]{}, system); + + String output = stdout.toString(); + assertThat(output).contains( + "issue", + "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java", + "fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java" + ); + } } diff --git a/src/test/java/integration/SanityCheckTest.java b/src/test/java/integration/SanityCheckTest.java index e679f33..7add561 100644 --- a/src/test/java/integration/SanityCheckTest.java +++ b/src/test/java/integration/SanityCheckTest.java @@ -48,8 +48,7 @@ public void executeJavaLibFixture() throws Exception { Shell.Process process = Shell.execute("build/codeclimate-sonar fixtures/java_lib"); - assertThat(process.exitCode).isEqualTo(0); - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>." + process.stdout); assertThat(process.stdout).contains(expectedOutput); + assertThat(process.exitCode).isEqualTo(0); } } diff --git a/src/test/java/support/TestSystem.java b/src/test/java/support/TestSystem.java index 2ff9718..687030e 100644 --- a/src/test/java/support/TestSystem.java +++ b/src/test/java/support/TestSystem.java @@ -2,11 +2,33 @@ import org.sonarlint.cli.util.System2; +import java.util.Properties; + public class TestSystem extends System2 { public int exitCode; + final Properties properties = new Properties(); @Override public void exit(int exitCode) { this.exitCode = exitCode; } + + @Override + public Properties properties() { + return properties; + } + + @Override + public String property(String key) { + return properties.getProperty(key); + } + + @Override + public String getProperty(String key) { + return properties.getProperty(key); + } + + public void setProperty(String key, String value) { + properties.setProperty(key, value); + } } From 1d51a6df5c9eed2a2fcba463acddc2f14769ab53 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Tue, 10 Oct 2017 13:09:47 -0300 Subject: [PATCH 4/7] Rollback uneeded change --- src/test/java/integration/SanityCheckTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/integration/SanityCheckTest.java b/src/test/java/integration/SanityCheckTest.java index 7add561..62d979a 100644 --- a/src/test/java/integration/SanityCheckTest.java +++ b/src/test/java/integration/SanityCheckTest.java @@ -48,7 +48,7 @@ public void executeJavaLibFixture() throws Exception { Shell.Process process = Shell.execute("build/codeclimate-sonar fixtures/java_lib"); - assertThat(process.stdout).contains(expectedOutput); assertThat(process.exitCode).isEqualTo(0); + assertThat(process.stdout).contains(expectedOutput); } } From 22ef7239896b9c3763072015f0fab4f71292b4a8 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Tue, 10 Oct 2017 16:47:19 -0300 Subject: [PATCH 5/7] Review adjustments --- src/main/java/cc/files/Finder.java | 5 +++-- src/main/java/cc/files/Matcher.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/cc/files/Finder.java b/src/main/java/cc/files/Finder.java index 481dd03..1a24e49 100644 --- a/src/main/java/cc/files/Finder.java +++ b/src/main/java/cc/files/Finder.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import static java.nio.file.Files.isDirectory; @@ -28,8 +29,8 @@ public Finder(List includedPaths, String srcGlobPattern, String testsGlo @Override public List collect(Path baseDir) throws IOException { return findPaths(baseDir).stream() - .map(p -> toClientInputFile(baseDir, p)) - .filter(f -> f != null) + .map(path -> toClientInputFile(baseDir, path)) + .filter(Objects::nonNull) .collect(Collectors.toList()); } diff --git a/src/main/java/cc/files/Matcher.java b/src/main/java/cc/files/Matcher.java index dad1c8e..a197d6a 100644 --- a/src/main/java/cc/files/Matcher.java +++ b/src/main/java/cc/files/Matcher.java @@ -46,7 +46,7 @@ static PathMatcher create(String pattern, PathMatcher defaultMatcher) { return defaultMatcher; } } catch (Exception e) { - throw new RuntimeException("Error creating create with pattern: " + pattern, e); + throw new RuntimeException("Error creating matcher with pattern: " + pattern, e); } } } \ No newline at end of file From c353b93c04f2d5a25cc0abd609ca6a494f495b8f Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Tue, 10 Oct 2017 17:31:24 -0300 Subject: [PATCH 6/7] Simplify how to differentiate src and test files --- src/main/java/cc/files/Finder.java | 6 +++--- src/main/java/cc/files/Matcher.java | 12 ++++-------- src/main/java/org/sonarlint/cli/CustomMain.java | 2 +- src/test/java/cc/files/FinderTest.java | 12 ++++++------ 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/cc/files/Finder.java b/src/main/java/cc/files/Finder.java index 1a24e49..b624b27 100644 --- a/src/main/java/cc/files/Finder.java +++ b/src/main/java/cc/files/Finder.java @@ -19,11 +19,11 @@ public class Finder extends InputFileFinder { final Charset charset; final Matcher matcher; - public Finder(List includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { - super(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); + public Finder(List includedPaths, String testsGlobPattern, String excludeGlobPattern, Charset charset) { + super(null, testsGlobPattern, excludeGlobPattern, charset); this.includedPaths = includedPaths; this.charset = charset; - this.matcher = new Matcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); + this.matcher = new Matcher(testsGlobPattern, excludeGlobPattern, charset); } @Override diff --git a/src/main/java/cc/files/Matcher.java b/src/main/java/cc/files/Matcher.java index a197d6a..d377a1a 100644 --- a/src/main/java/cc/files/Matcher.java +++ b/src/main/java/cc/files/Matcher.java @@ -7,30 +7,26 @@ public class Matcher { 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 Matcher(PathMatcher srcMatcher, PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) { - this.srcMatcher = srcMatcher; + public Matcher(PathMatcher testsMatcher, PathMatcher excludeMatcher, Charset charset) { this.testsMatcher = testsMatcher; this.excludeMatcher = excludeMatcher; this.charset = charset; } - public Matcher(String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { - this(create(srcGlobPattern, ACCEPT_ALL), create(testsGlobPattern, REFUSE_ALL), create(excludeGlobPattern, REFUSE_ALL), 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 isSrc = srcMatcher.matches(absoluteFilePath) || srcMatcher.matches(relativeFilePath); boolean isExcluded = excludeMatcher.matches(absoluteFilePath) || excludeMatcher.matches(relativeFilePath); - return isSrc && !isExcluded; + return !isExcluded; } public boolean isTest(Path baseDir, Path absoluteFilePath) { diff --git a/src/main/java/org/sonarlint/cli/CustomMain.java b/src/main/java/org/sonarlint/cli/CustomMain.java index 052d0f9..a68d48f 100644 --- a/src/main/java/org/sonarlint/cli/CustomMain.java +++ b/src/main/java/org/sonarlint/cli/CustomMain.java @@ -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.src(), parsedOpts.tests(), parsedOpts.exclusions(), charset); + InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.tests(), parsedOpts.exclusions(), charset); ReportFactory reportFactory = new CustomReportFactory(charset); ConfigurationReader reader = new ConfigurationReader(); SonarLintFactory sonarLintFactory = new SonarLintFactory(reader); diff --git a/src/test/java/cc/files/FinderTest.java b/src/test/java/cc/files/FinderTest.java index 5880b30..947bd4f 100644 --- a/src/test/java/cc/files/FinderTest.java +++ b/src/test/java/cc/files/FinderTest.java @@ -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, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("src/included/"), null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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, null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -56,7 +56,7 @@ 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, null, "**/HasNoIssue.*", Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, "**/HasNoIssue.*", Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -69,7 +69,7 @@ public void keep_exclude_pattern_behaviour_on_directories() throws Exception { @Test public void keep_exclude_pattern_behaviour_on_files() throws Exception { - Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, null, "**/*.json", Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, "**/*.json", Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); @@ -82,7 +82,7 @@ public void keep_exclude_pattern_behaviour_on_files() throws Exception { @Test public void differentiate_src_and_test() throws Exception { - Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "src/**", "**/test/**", null, Charset.defaultCharset()); + Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "{**/test/**}", null, Charset.defaultCharset()); List files = finder.collect(Paths.get("fixtures/multiple_paths")); From 3af52b51cb2db852de20618a410591836a1cf92e Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Wed, 11 Oct 2017 19:13:07 -0300 Subject: [PATCH 7/7] No need for exclusion patterns as CLI already resolve paths to be included --- src/main/java/cc/files/Collector.java | 9 ++--- src/main/java/cc/files/Finder.java | 18 +++++----- src/main/java/cc/files/Matcher.java | 20 ++++------- .../java/org/sonarlint/cli/CustomMain.java | 2 +- src/test/java/cc/files/FinderTest.java | 34 +++---------------- 5 files changed, 22 insertions(+), 61 deletions(-) diff --git a/src/main/java/cc/files/Collector.java b/src/main/java/cc/files/Collector.java index 699407d..2429da5 100644 --- a/src/main/java/cc/files/Collector.java +++ b/src/main/java/cc/files/Collector.java @@ -10,12 +10,10 @@ import java.util.List; public class Collector extends SimpleFileVisitor { - final Matcher matcher; final List 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 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); } diff --git a/src/main/java/cc/files/Finder.java b/src/main/java/cc/files/Finder.java index b624b27..6481ce4 100644 --- a/src/main/java/cc/files/Finder.java +++ b/src/main/java/cc/files/Finder.java @@ -19,11 +19,11 @@ public class Finder extends InputFileFinder { final Charset charset; final Matcher matcher; - public Finder(List includedPaths, String testsGlobPattern, String excludeGlobPattern, Charset charset) { - super(null, testsGlobPattern, excludeGlobPattern, charset); + public Finder(List 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,11 +48,11 @@ List 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) { @@ -60,7 +60,7 @@ ClientInputFile createInputFile(Path resolvedPath, boolean test) { } List 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(); } diff --git a/src/main/java/cc/files/Matcher.java b/src/main/java/cc/files/Matcher.java index d377a1a..99b3e2b 100644 --- a/src/main/java/cc/files/Matcher.java +++ b/src/main/java/cc/files/Matcher.java @@ -6,27 +6,19 @@ 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) { @@ -34,7 +26,7 @@ public boolean isTest(Path baseDir, Path 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); diff --git a/src/main/java/org/sonarlint/cli/CustomMain.java b/src/main/java/org/sonarlint/cli/CustomMain.java index a68d48f..fa4b029 100644 --- a/src/main/java/org/sonarlint/cli/CustomMain.java +++ b/src/main/java/org/sonarlint/cli/CustomMain.java @@ -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); diff --git a/src/test/java/cc/files/FinderTest.java b/src/test/java/cc/files/FinderTest.java index 947bd4f..d726224 100644 --- a/src/test/java/cc/files/FinderTest.java +++ b/src/test/java/cc/files/FinderTest.java @@ -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 files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); - List 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 files = finder.collect(Paths.get("fixtures/multiple_paths")); - List 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 files = finder.collect(Paths.get("fixtures/multiple_paths"));