Skip to content

add exclusion path in the filter #36

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 5 commits into from
Dec 26, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 26 additions & 11 deletions src/main/java/fr/univartois/sonargo/coverage/CoverageSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -60,17 +59,31 @@ public void describe(SensorDescriptor descriptor) {
descriptor.name("Go Coverage").onlyOnFileType(InputFile.Type.MAIN).onlyOnLanguage(GoLanguage.KEY);
}

private Set<String> getExcludedPath(SensorContext context) {
private List<String> getExcludedPath(SensorContext context) {

String globalExcludedPath = context.settings().getString(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY);

Set<String> listExcludedPath = new TreeSet<>(Arrays.asList(StringUtils.split(globalExcludedPath, ",")));
String globalExcludedPath = context.settings().getString(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
List<String> listExcludedPath = Arrays.asList(StringUtils.split(globalExcludedPath, ","));

return listExcludedPath;
}

private boolean isNotAnExcludedPath(Path p, SensorContext context) {
return !getExcludedPath(context).contains(p.toFile().getPath());
private boolean isAnExcludedPath(Path candidatePath, SensorContext context) {
List<String> listExcludedPath = getExcludedPath(context);

for (String s : listExcludedPath) {
if (s.contains("*")) {
PathMatcher pathMatcher = java.nio.file.FileSystems.getDefault().getPathMatcher("glob:" + s);
if (pathMatcher.matches(candidatePath)) {
return true;
}
}

if ((candidatePath.isAbsolute() && candidatePath.endsWith(s)) || (candidatePath.getFileName().equals(s))) {
return true;
}
}
return false;

}

public Stream<Path> createStream(SensorContext context) throws IOException {
Expand All @@ -79,7 +92,7 @@ public Stream<Path> createStream(SensorContext context) throws IOException {
return Files.walk(Paths.get(fullPath))
.filter(p -> !p.getParent().toString().contains(".git") && !p.getParent().toString().contains(".sonar")
&& !p.getParent().toString().contains(".scannerwork")
&& !p.getFileName().toString().startsWith(".") && isNotAnExcludedPath(p, context));
&& !p.getFileName().toString().startsWith(".") && !isAnExcludedPath(p, context));

}

Expand All @@ -88,6 +101,9 @@ public void execute(SensorContext context) {

try (Stream<Path> paths = createStream(context)) {
paths.forEach(filePath -> {

LOGGER.debug("Path in stream" + filePath.toFile().getAbsolutePath());

if (Files.isDirectory(filePath)) {
final String reportPath = context.settings().getString(GoProperties.COVERAGE_REPORT_PATH_KEY);
final File f = new File(filePath + File.separator + reportPath);
Expand Down Expand Up @@ -119,8 +135,7 @@ public static void save(SensorContext context, Map<String, List<LineCoverage>> c
final List<LineCoverage> lines = entry.getValue();
final FileSystem fileSystem = context.fileSystem();
final FilePredicates predicates = fileSystem.predicates();
final InputFile inputFile = fileSystem.inputFile(
predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath)));
final InputFile inputFile = fileSystem.inputFile(predicates.hasPath(filePath));

if (inputFile == null) {
LOGGER.warn("unable to create InputFile object: " + filePath);
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/fr/univartois/sonargo/gotest/GoJunitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,55 @@ public void parse(String reportPath) throws ParserConfigurationException, SAXExc
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;

String path = eElement.getAttribute(NAME_TEST_ATTR);
String fileName = path.substring(path.lastIndexOf("/") + 1) + "_test.go";
listTestSuiteByPackage.add(groupTestCaseByFile(eElement, fileName));

listTestSuiteByPackage.add(groupTestCaseByFile(eElement));
}
}

}

private HashMap<String, GoTestFile> groupTestCaseByFile(Element testSuite, String fileName) {
private HashMap<String, GoTestFile> groupTestCaseByFile(Element testSuite) {
final NodeList testCaseList = testSuite.getElementsByTagName(TEST_CASE_TAG);
HashMap<String, GoTestFile> goTestFileMap = new HashMap<>();
HashMap<String, GoTestFile> mapResult = new HashMap<>();
for (int i = 0; i < testCaseList.getLength(); i++) {
final Node nNode = testCaseList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element testCase = (Element) nNode;
String functionName = testCase.getAttribute(NAME_TEST_ATTR);
String key = fileName + "#" + functionName;
String absolutPahtOfFile = functionFileName.get(key);
if (absolutPahtOfFile == null) {
LOGGER.warn(functionName + " not found in expected test file named " + fileName);
continue;
}
String fileName = functionFileName.get(functionName);
GoTestFile goTest = null;
if (goTestFileMap.containsKey(key)) {
goTest = goTestFileMap.get(key);
} else {
if (mapResult.containsKey(fileName)) {
goTest = mapResult.get(fileName);
} else if (fileName != null) {
goTest = new GoTestFile();
goTest.setFile(absolutPahtOfFile);
goTest.setFile(fileName);
} else {
LOGGER.warn("The key is null");
continue;
}

goTest.addTestCase(new GoTestCase(testCase.getElementsByTagName(FAILURE_TAG).getLength() > 0,
testCase.getElementsByTagName(TEST_SKIPPED_TAG).getLength() > 0,
Double.parseDouble(testCase.getAttribute(TIME_TEST_ATTR)), functionName));
goTestFileMap.put(key, goTest);
mapResult.put(fileName, goTest);
}

}
return goTestFileMap;
return mapResult;

}

// public List<HashMap<String, GoTestFile>> getListTestSuite() {
//
// String path = eElement.getAttribute(NAME_TEST_ATTR);
// String fileName = path.substring(path.lastIndexOf("/") + 1) + "_test.go";
// listTestSuiteByPackage.add(groupTestCaseByFile(eElement, fileName));
//
// }
// }
//
// }
//
//
public List<Map<String, GoTestFile>> getListTestSuite() {
return listTestSuiteByPackage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public static void save(SensorContext context, List<Map<String, GoTestFile>> lis
for (Map<String, GoTestFile> map : list) {
for (Map.Entry<String, GoTestFile> entry : map.entrySet()) {
GoTestFile value = entry.getValue();
InputFile file = context.fileSystem().inputFile(predicates.hasAbsolutePath(value.getFile()));
LOGGER.debug("file " + value.getFile());
InputFile file = context.fileSystem().inputFile(predicates.hasPath(value.getFile()));

if (file == null) {
LOGGER.warn("file not found " + value.getFile());
continue;
Expand Down