Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/codeclimate-sonar
Original file line number Diff line number Diff line change
Expand Up @@ -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" $@
9 changes: 9 additions & 0 deletions fixtures/multiple_paths/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pkg1;

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

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

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

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

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

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

public static void execute(String[] args, System2 system) {
CustomMain.execute(args, system);
}
}
13 changes: 10 additions & 3 deletions src/main/java/cc/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> includePaths;
public List<String> 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() {
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/cc/files/Collector.java
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);
}
}
66 changes: 66 additions & 0 deletions src/main/java/cc/files/Finder.java
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)

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 p stands for. Is f for file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dblandin p expanded to path.
f != null replaced with Objects::nonull ;)

Ready for another shot?

.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();
}
}
52 changes: 52 additions & 0 deletions src/main/java/cc/files/Matcher.java
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Error creating matcher?

}
}
}
13 changes: 6 additions & 7 deletions src/main/java/org/sonarlint/cli/CustomMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,11 +48,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);
Expand All @@ -74,7 +72,8 @@ 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);
Expand Down Expand Up @@ -102,4 +101,4 @@ public List<Reporter> createReporters(Path basePath) {
return Arrays.asList(new JsonReport());
}
}
}
}
10 changes: 8 additions & 2 deletions src/test/java/cc/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ 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/");
}
}

@Test
public void default_config_path_include_base_dir() throws Exception {
Config config = new Config();
assertThat(config.includePaths).containsOnly("");
}
}
Loading