-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from rmannibucau/rmannibucau/enforce-exclusions
ensure exclusions win over inclusions for scanning
- Loading branch information
Showing
5 changed files
with
162 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,40 +16,127 @@ | |
package com.mycila.maven.plugin.license.util; | ||
|
||
import com.mycila.maven.plugin.license.Default; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.plugin.logging.SystemStreamLog; | ||
import org.apache.maven.shared.utils.io.DirectoryScanner; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author Mathieu Carbou ([email protected]) | ||
*/ | ||
public final class SelectionTest { | ||
private final Log log = new SystemStreamLog(); | ||
|
||
@Test | ||
public void test_default_select_all() { | ||
Selection selection = new Selection(new File("."), new String[0], new String[0], false); | ||
Selection selection = new Selection(new File("."), new String[0], new String[0], false, log); | ||
assertEquals(selection.getExcluded().length, 0); | ||
assertEquals(selection.getIncluded().length, 1); | ||
assertTrue(selection.getSelectedFiles().length > 0); | ||
} | ||
|
||
@Test | ||
public void test_limit_inclusion() { | ||
Selection selection = new Selection(new File("."), new String[]{"toto"}, new String[]{"tata"}, false); | ||
Selection selection = new Selection(new File("."), new String[]{"toto"}, new String[]{"tata"}, false, log); | ||
assertEquals(selection.getExcluded().length, 1); | ||
assertEquals(selection.getIncluded().length, 1); | ||
assertEquals(selection.getSelectedFiles().length, 0); | ||
} | ||
|
||
@Test | ||
public void test_limit_inclusion_and_check_default_excludes() { | ||
Selection selection = new Selection(new File("."), new String[]{"toto"}, new String[0], true); | ||
Selection selection = new Selection(new File("."), new String[]{"toto"}, new String[0], true, log); | ||
assertEquals(selection.getExcluded().length, Default.EXCLUDES.length); // default exludes from Scanner and Selection + toto | ||
assertEquals(selection.getIncluded().length, 1); | ||
assertEquals(selection.getSelectedFiles().length, 0); | ||
assertTrue(Arrays.asList(selection.getExcluded()).containsAll(Arrays.asList(Default.EXCLUDES))); | ||
} | ||
|
||
@Test | ||
public void test_exclusions_respect_with_fastScan() throws IOException { | ||
SystemStreamLog log = new SystemStreamLog() { | ||
@Override | ||
public boolean isDebugEnabled() { | ||
return true; | ||
} | ||
}; | ||
File root = createAFakeProject(log); | ||
Selection selection = new Selection(root, | ||
new String[] { "**" + File.separator + "*.txt" }, | ||
new String[] {"target" + File.separator + "**", "module/**/target" + File.separator + "**"}, false, | ||
log); | ||
|
||
selection.getSelectedFiles(); // triggers scan and scanner build | ||
String debugMessage = buildDebugMessage(selection.getScanner()); | ||
assertIncludedFilesInFakeProject(selection, debugMessage); | ||
assertEquals(debugMessage, 0, selection.getScanner().getExcludedFiles().length); | ||
} | ||
|
||
private String buildDebugMessage(DirectoryScanner scanner) { | ||
return "excludedDirs=" + asList(scanner.getExcludedDirectories()) + ",\n" + | ||
"excludedFiles=" + asList(scanner.getExcludedFiles()) + ",\n" + | ||
"includedDirsFiles=" + asList(scanner.getIncludedDirectories()) + ",\n" + | ||
"includedFiles=" + asList(scanner.getIncludedFiles()) + ",\n" + | ||
"notIncludedDirs=" + asList(scanner.getNotIncludedDirectories()) + ",\n" + | ||
"notIncludedFiles=" + asList(scanner.getNotIncludedFiles()) + ",\n" + | ||
"diskFiles=" + listFiles(scanner.getBasedir(), new ArrayList<File>()); | ||
} | ||
|
||
private Collection<File> listFiles(File basedir, Collection<File> files) { | ||
files.add(basedir); | ||
for (File f : basedir.listFiles()) { | ||
if (f.isDirectory()) { | ||
listFiles(f, files); | ||
} else { | ||
files.add(f); | ||
} | ||
} | ||
return files; | ||
} | ||
|
||
private void assertIncludedFilesInFakeProject(Selection selection, String debugMessage) { | ||
List<String> selected = new ArrayList<String>(asList(selection.getSelectedFiles())); | ||
Collections.sort(selected); | ||
assertEquals(debugMessage, asList("included.txt", "module/src/main/java/not-ignored.txt", "module/sub/subsub/src/main/java/not-ignored.txt"), selected); | ||
} | ||
|
||
private File createAFakeProject(Log log) throws IOException { | ||
File temp = new File("target/workdir_" + UUID.randomUUID().toString()); | ||
touch(new File(temp, "included.txt"), log); | ||
touch(new File(temp, "target/ignored.txt"), log); | ||
touch(new File(temp, "module/src/main/java/not-ignored.txt"), log); | ||
touch(new File(temp, "module/target/ignored.txt"), log); | ||
touch(new File(temp, "module/sub/subsub/src/main/java/not-ignored.txt"), log); | ||
touch(new File(temp, "module/sub/subsub/target/foo/not-ignored.txt"), log); | ||
return temp; | ||
} | ||
|
||
private void touch(File newFile, Log log) throws IOException { | ||
final File parentFile = newFile.getParentFile(); | ||
if (parentFile != null && !parentFile.isDirectory() && !parentFile.mkdirs()) { | ||
fail("Can't create '" + parentFile + "'"); | ||
} | ||
final FileWriter w = new FileWriter(newFile); | ||
w.write("touched"); | ||
w.close(); | ||
if (!newFile.exists()) { | ||
fail("Can't create " + newFile); | ||
} | ||
log.debug("Created '" + newFile.getAbsolutePath() + "'"); | ||
} | ||
} |