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

Fix #143: Add ability to override a default exclusion to include a pattern from the latter #147

Merged
merged 1 commit into from
Feb 6, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.*;
import static java.util.Arrays.asList;

/**
Expand All @@ -38,8 +40,9 @@ public final class Selection {

public Selection(File basedir, String[] included, String[] excluded, boolean useDefaultExcludes) {
this.basedir = basedir;
this.included = buildInclusions(included);
this.excluded = buildExclusions(useDefaultExcludes, excluded);
String[] overrides = buildOverrideInclusions(useDefaultExcludes, included);
this.included = buildInclusions(included, overrides);
this.excluded = buildExclusions(useDefaultExcludes, excluded, overrides);
}

public String[] getSelectedFiles() {
Expand Down Expand Up @@ -69,17 +72,39 @@ private void scanIfneeded() {
}
}

private static String[] buildExclusions(boolean useDefaultExcludes, String... excludes) {
private static String[] buildExclusions(boolean useDefaultExcludes, String[] excludes, String[] overrides) {
List<String> exclusions = new ArrayList<String>();
if (useDefaultExcludes)
if (useDefaultExcludes) {
exclusions.addAll(asList(Default.EXCLUDES));
if (excludes != null && excludes.length > 0)
}
// Remove from the default exclusion list the patterns that have been explicitly included
for (String override : overrides) {
exclusions.remove(override);
}
if (excludes != null && excludes.length > 0) {
exclusions.addAll(asList(excludes));
}
return exclusions.toArray(new String[exclusions.size()]);
}

private static String[] buildInclusions(String... includes) {
return includes != null && includes.length > 0 ? includes : Default.INCLUDE;
private static String[] buildInclusions(String[] includes, String[] overrides) {
// if we use the default exclusion list, we just remove
List<String> inclusions = new ArrayList<String>(asList(includes != null && includes.length > 0 ? includes : Default.INCLUDE));
inclusions.removeAll(asList(overrides));
if (inclusions.isEmpty()) {
inclusions.addAll(asList(Default.INCLUDE));
}
return inclusions.toArray(new String[inclusions.size()]);
}

private static String[] buildOverrideInclusions(boolean useDefaultExcludes, String[] includes) {
// return the list of patterns that we have explicitly included when using default exclude list
if (!useDefaultExcludes || includes == null || includes.length == 0) {
return new String[0];
}
List<String> overrides = new ArrayList<String>(asList(Default.EXCLUDES));
overrides.retainAll(asList(includes));
return overrides.toArray(new String[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -62,7 +63,12 @@ public void setPluginClassPath(ClassLoader classLoader) {
*/
public URL findResource(String resource) throws MojoFailureException {
// first search relatively to the base directory
URL res = toURL(new File(basedir, resource));
URL res ;
try {
res = toURL(new File(basedir, resource).getCanonicalFile());
} catch (IOException e) {
throw new MojoFailureException("Resource " + resource + " not found in file system, classpath or URL: " + e.getMessage(), e);
}
if (res != null) {
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public void test_include_and_fail() throws Exception {
check.execute();
}

@Test(expected = MojoExecutionException.class)
public void test_include_overrides_default_exclusion() throws Exception {
LicenseCheckMojo check = new LicenseCheckMojo();
check.basedir = new File("src/test/resources/issues/issue-71");
check.header = "../../check/header.txt";
check.project = new MavenProjectStub();
check.includes = new String[]{"**/.travis.yml"};
check.execute();
}

}
Empty file.