Skip to content
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

Add exclusions support to cli #1581

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.codehaus.plexus.util.IOUtil;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;
import org.jacoco.report.FileFilter;

/**
* Performs offline instrumentation. Note that after execution of test you must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.jacoco.report.FileFilter;
import org.jacoco.report.IReportGroupVisitor;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.FileFilter;
import org.jacoco.report.IReportGroupVisitor;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
Expand Down
13 changes: 12 additions & 1 deletion org.jacoco.cli/src/org/jacoco/cli/internal/commands/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.FileFilter;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
Expand All @@ -48,6 +49,9 @@ public class Report extends Command {
@Argument(usage = "list of JaCoCo *.exec files to read", metaVar = "<execfiles>")
List<File> execfiles = new ArrayList<File>();

@Option(name = "--exclusions", usage = "pattern to excludes, e.g. **/*DTO.class", metaVar = "<pattern>")
List<String> exclusions = new ArrayList<String>();

@Option(name = "--classfiles", usage = "location of Java class files", metaVar = "<path>", required = true)
List<File> classfiles = new ArrayList<File>();

Expand Down Expand Up @@ -106,8 +110,15 @@ private IBundleCoverage analyze(final ExecutionDataStore data,
final PrintWriter out) throws IOException {
final CoverageBuilder builder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(data, builder);
final FileFilter fileFilter = new FileFilter(null, exclusions);
for (final File f : classfiles) {
analyzer.analyzeAll(f);
if (!f.isDirectory()) {
analyzer.analyzeAll(f);
continue;
}
for (final File filtered : fileFilter.getFiles(f)) {
analyzer.analyzeAll(filtered);
}
}
printNoMatchWarning(builder.getNoMatchClasses(), out);
return builder.getBundle(name);
Expand Down
111 changes: 111 additions & 0 deletions org.jacoco.core/src/org/jacoco/core/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright (c) 2009, 2024 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Stark X - initial API and implementation
*******************************************************************************/
package org.jacoco.core.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class FileUtils {
private FileUtils() {
}

public static List<File> getFiles(final File directory,
final Collection<String> includes,
final Collection<String> excludes, final boolean includeBaseDir)
throws IOException {
final List<PathMatcher> includeMatchers = buildPathMatchers(includes);
final List<PathMatcher> excludeMatchers = buildPathMatchers(excludes);

final List<File> files = new ArrayList<File>();
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
boolean matches(Path file, Collection<PathMatcher> patterns) {
for (PathMatcher pattern : patterns) {
if (pattern.matches(file)) {
return true;
}
}
return false;
}

@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
final Path relativePath = directory.toPath().relativize(file);
if (matches(relativePath, includeMatchers)
&& !matches(relativePath, excludeMatchers)) {
final Path path = includeBaseDir ? file : relativePath;
files.add(path.toFile());
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(directory.toPath(), visitor);

return files;
}

private static List<PathMatcher> buildPathMatchers(
Collection<String> includeOrExcludes) {
final List<PathMatcher> excludeMatchers = new ArrayList<PathMatcher>();
for (String xclude : includeOrExcludes) {
final PathMatcher matcher = FileUtils.buildPathMatcher(xclude);
if (matcher != null) {
excludeMatchers.add(matcher);
}
if (xclude.startsWith("**/")) {
final PathMatcher rootMatcher = FileUtils
.buildPathMatcher(xclude.substring(3));
if (rootMatcher != null) {
excludeMatchers.add(rootMatcher);
}
}
}
return excludeMatchers;
}

public static List<File> getFiles(final File directory,
final Collection<String> includes,
final Collection<String> excludes) throws IOException {
return getFiles(directory, includes, excludes, true);
}

public static List<String> getFileNames(File directory,
final Collection<String> includes,
final Collection<String> excludes, boolean includeBaseDir)
throws IOException {
final List<File> files = FileUtils.getFiles(directory, includes,
excludes, includeBaseDir);
final List<String> names = new ArrayList<String>();
for (File file : files) {
names.add(file.toString());
}
return names;
}

public static List<String> getFileNames(File directory,
final Collection<String> includes,
final Collection<String> excludes) throws IOException {
return getFileNames(directory, includes, excludes, true);
}

public static PathMatcher buildPathMatcher(String pattern) {
if (pattern == null || pattern.isEmpty()) {
return null;
}
return FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
* Kyle Lieber - implementation of CheckMojo
*
*******************************************************************************/
package org.jacoco.maven;
package org.jacoco.report;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import org.jacoco.core.utils.FileUtils;

/**
* A file filter using includes/excludes patterns.
Expand Down Expand Up @@ -77,7 +77,7 @@ public List<File> getFiles(final File directory) throws IOException {
*
* @return the pattern
*/
public String getIncludes() {
public List<String> getIncludes() {
return this.buildPattern(this.includes, DEFAULT_INCLUDES);
}

Expand All @@ -86,16 +86,15 @@ public String getIncludes() {
*
* @return the pattern
*/
public String getExcludes() {
public List<String> getExcludes() {
return this.buildPattern(this.excludes, DEFAULT_EXCLUDES);
}

private String buildPattern(final List<String> patterns,
private List<String> buildPattern(final List<String> patterns,
final String defaultPattern) {
String pattern = defaultPattern;
if (patterns != null && !patterns.isEmpty()) {
pattern = StringUtils.join(patterns.iterator(), ",");
if (patterns == null || patterns.isEmpty()) {
return Collections.singletonList(defaultPattern);
}
return pattern;
return patterns;
}
}
Loading