-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f7e753
Add testing files
filipesperandio 723e86f
Find correct paths to process
filipesperandio f2cbbc6
Integrate new Finder to honor which files to be analyzed
filipesperandio 1d51a6d
Rollback uneeded change
filipesperandio 22ef723
Review adjustments
filipesperandio c353b93
Simplify how to differentiate src and test files
filipesperandio 3af52b5
No need for exclusion patterns as CLI already resolve paths to be inc…
filipesperandio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package pkg1; | ||
|
|
||
| public class HasNoIssue { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| public class Test { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package cc.files; | ||
|
|
||
| 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 Collector extends SimpleFileVisitor<Path> { | ||
| final Matcher matcher; | ||
| final List<Path> files; | ||
| final Path baseDir; | ||
|
|
||
| public Collector(Path baseDir, Matcher matcher) { | ||
| this.matcher = matcher; | ||
| 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 = matcher.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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package cc.files; | ||
|
|
||
| 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; | ||
|
|
||
| import static java.nio.file.Files.isDirectory; | ||
|
|
||
| public class Finder extends InputFileFinder { | ||
| final List<String> includedPaths; | ||
| final Charset charset; | ||
| final Matcher matcher; | ||
|
|
||
| public Finder(List<String> includedPaths, String srcGlobPattern, String testsGlobPattern, String excludeGlobPattern, Charset charset) { | ||
| super(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); | ||
| this.includedPaths = includedPaths; | ||
| this.charset = charset; | ||
| this.matcher = new Matcher(srcGlobPattern, testsGlobPattern, excludeGlobPattern, charset); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ClientInputFile> collect(Path baseDir) throws IOException { | ||
| return findPaths(baseDir).stream() | ||
| .map(p -> toClientInputFile(baseDir, p)) | ||
| .filter(f -> f != null) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| List<Path> findPaths(Path baseDir) throws IOException { | ||
| List<Path> paths = new ArrayList<>(); | ||
| for (String path : includedPaths) { | ||
| Path resolvedPath = baseDir.resolve(path); | ||
| if (isDirectory(resolvedPath)) { | ||
| paths.addAll(collectDir(baseDir, resolvedPath)); | ||
| } else { | ||
| paths.add(resolvedPath); | ||
| } | ||
| } | ||
| return paths; | ||
| } | ||
|
|
||
| ClientInputFile toClientInputFile(Path baseDir, Path path) { | ||
| boolean valid = matcher.validatePath(baseDir, path); | ||
| if (valid) { | ||
| return createInputFile(path, matcher.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 { | ||
| Collector collector = new Collector(baseDir, matcher); | ||
| Files.walkFileTree(dir, collector); | ||
| return collector.getFiles(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package cc.files; | ||
|
|
||
| import java.nio.charset.Charset; | ||
| import java.nio.file.FileSystems; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.PathMatcher; | ||
|
|
||
| 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; | ||
| 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 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); | ||
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of expanding these single letter variables out? Not sure what's
pstands for. Isffor file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dblandin
pexpanded topath.f != nullreplaced withObjects::nonull;)Ready for another shot?