Skip to content

Commit

Permalink
ensure exclusions win over inclusions for scanning
Browse files Browse the repository at this point in the history
using maven shared utils and adding a flag to have a fast scan
  • Loading branch information
rmannibucau committed Jun 9, 2020
1 parent 91b730a commit 65877c4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 7 deletions.
7 changes: 6 additions & 1 deletion license-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.2.1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ private boolean hasHeader(final LicenseSet licenseSet) {

private int getNumberOfExecutorThreads() {
return nThreads > 0 ?
nThreads :
Math.max(1, (int) (Runtime.getRuntime().availableProcessors() * concurrencyFactor));
nThreads :
Math.max(1, (int) (Runtime.getRuntime().availableProcessors() * concurrencyFactor));
}

private Map<String, String> mergeProperties(final LicenseSet licenseSet, final Document document) {
Expand Down Expand Up @@ -602,6 +602,7 @@ private Map<String, String> mergeProperties(final LicenseSet licenseSet, final D
private String[] listSelectedFiles(final LicenseSet licenseSet) {
final boolean useDefaultExcludes = (licenseSet.useDefaultExcludes != null ? licenseSet.useDefaultExcludes : defaultUseDefaultExcludes);
final Selection selection = new Selection(firstNonNull(licenseSet.basedir, defaultBasedir), licenseSet.includes, buildExcludes(licenseSet), useDefaultExcludes);
selection.setFastScan(licenseSet.fastScan);
debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir));
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public class LicenseSet {
@Parameter
public String[] excludes = new String[0];

/**
* If true, excluded folders will not be visited for included files
* even if includes use wildcard. This is generally what you aim at
* but for compatibility reason default matches the existing behavior.
*/
@Parameter(property = "license.fastScan", defaultValue = "false")
public boolean fastScan;

/**
* Specify the list of keywords to use to detect a header. A header must
* include all keywords to be valid. By default, the word 'copyright' is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
package com.mycila.maven.plugin.license.util;

import com.mycila.maven.plugin.license.Default;
import org.codehaus.plexus.util.DirectoryScanner;
import org.apache.maven.shared.utils.io.DirectoryScanner;
import org.apache.maven.shared.utils.io.MatchPatterns;
import org.apache.maven.shared.utils.io.ScanConductor;

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 @@ -37,6 +37,7 @@ public final class Selection {
private final String[] excluded;

private DirectoryScanner scanner;
private boolean fastScan;

public Selection(File basedir, String[] included, String[] excluded, boolean useDefaultExcludes) {
this.basedir = basedir;
Expand All @@ -50,6 +51,11 @@ public String[] getSelectedFiles() {
return scanner.getIncludedFiles();
}

// for tests
String[] getFilesExcluded() {
return scanner.getExcludedFiles();
}

public File getBasedir() {
return basedir;
}
Expand All @@ -62,9 +68,34 @@ public String[] getExcluded() {
return excluded;
}

public void setFastScan(boolean fastScan) {
this.fastScan = fastScan;
}

public boolean isFastScan() {
return fastScan;
}

private void scanIfneeded() {
if (scanner == null) {
final MatchPatterns excludePatterns = MatchPatterns.from(excluded);
scanner = new DirectoryScanner();
if (fastScan) {
scanner.setScanConductor(new ScanConductor() {
@Override
public ScanAction visitDirectory(final String name, final File directory) {
if (excludePatterns.matches(name, true)) {
return ScanAction.ABORT_DIRECTORY;
}
return ScanAction.CONTINUE;
}

@Override
public ScanAction visitFile(final String name, final File file) {
return ScanAction.CONTINUE;
}
});
}
scanner.setBasedir(basedir);
scanner.setIncludes(included);
scanner.setExcludes(excluded);
Expand Down Expand Up @@ -106,5 +137,4 @@ private static String[] buildOverrideInclusions(boolean useDefaultExcludes, Stri
overrides.retainAll(asList(includes));
return overrides.toArray(new String[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@
package com.mycila.maven.plugin.license.util;

import com.mycila.maven.plugin.license.Default;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Mathieu Carbou ([email protected])
*/
public final class SelectionTest {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();

@Test
public void test_default_select_all() {
Selection selection = new Selection(new File("."), new String[0], new String[0], false);
Expand All @@ -52,4 +63,46 @@ public void test_limit_inclusion_and_check_default_excludes() {
assertEquals(selection.getSelectedFiles().length, 0);
assertTrue(Arrays.asList(selection.getExcluded()).containsAll(Arrays.asList(Default.EXCLUDES)));
}

@Test
public void test_exclusions_violated_by_default() throws IOException {
createAFakeProject();

Selection selection = new Selection(temp.getRoot(), new String[]{"**/*.txt"}, new String[] {"**/target/**"}, false);

assertIncludedFilesInFakeProject(selection);
assertEquals(3, selection.getFilesExcluded().length);
}

@Test
public void test_exclusions_respect_with_fastScan() throws IOException {
createAFakeProject();

Selection selection = new Selection(temp.getRoot(), new String[]{"**/*.txt"}, new String[] {"**/target/**"}, false);
selection.setFastScan(true);

assertIncludedFilesInFakeProject(selection);
assertEquals(0, selection.getFilesExcluded().length);
}

private void assertIncludedFilesInFakeProject(Selection selection) {
List<String> selected = new ArrayList<String>(asList(selection.getSelectedFiles()));
Collections.sort(selected);
assertEquals(asList("included.txt", "module/src/main/java/not-ignored.txt", "module/sub/subsub/src/main/java/not-ignored.txt"), selected);
}

private void createAFakeProject() throws IOException {
touch(temp.newFile("included.txt"));
touch(new File(temp.newFolder("target"), "ignored.txt"));
touch(new File(temp.getRoot(), "module/src/main/java/not-ignored.txt"));
touch(new File(temp.getRoot(), "module/target/ignored.txt"));
touch(new File(temp.getRoot(), "module/sub/subsub/src/main/java/not-ignored.txt"));
touch(new File(temp.getRoot(), "module/sub/subsub/target/foo/not-ignored.txt"));
}

private void touch(final File newFile) throws IOException {
newFile.getParentFile().mkdirs();
final FileWriter w = new FileWriter(newFile);
w.close();
}
}

0 comments on commit 65877c4

Please sign in to comment.