From dcff9f6a5851dc8af816832b61a4fd580a24e9c0 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 7 Jan 2025 09:35:58 +0100 Subject: [PATCH] feat: #536 Add a property to limit plugin to a list of files usage: mvn license:check -Dlicense.filesToCheck="src/main/org/acme/MyClass.java,src/main/org/acme/MyEnum.java" --- .../invoker.properties | 1 + .../legacy-config-checkFiles/mock-license.txt | 2 + .../src/it/legacy-config-checkFiles/pom.xml | 34 +++++++++++ .../it/legacy-config-checkFiles/setup.groovy | 23 +++++++ .../java/com/mycilla/it/Unformatted1.java | 4 ++ .../java/com/mycilla/it/Unformatted2.java | 4 ++ .../it/legacy-config-checkFiles/verify.groovy | 61 +++++++++++++++++++ .../invoker.properties | 1 + .../mock-license-1.txt | 2 + .../mock-license-2.txt | 2 + .../mock-license-3.txt | 2 + .../src/it/tri-license-set-checkFiles/pom.xml | 60 ++++++++++++++++++ .../tri-license-set-checkFiles/setup.groovy | 23 +++++++ .../java/com/mycilla/it/Unformatted1.java | 4 ++ .../java/com/mycilla/it/Unformatted2.java | 4 ++ .../java/com/mycilla/it/Unformatted3.java | 4 ++ .../tri-license-set-checkFiles/verify.groovy | 53 ++++++++++++++++ .../plugin/license/AbstractLicenseMojo.java | 33 +++++++--- .../maven/plugin/license/util/Selection.java | 28 ++++++++- .../maven/plugin/license/ReportTest.java | 60 ++++++++++++++++-- .../plugin/license/util/SelectionTest.java | 22 +++++++ .../issues/issue-122/check_checkFiles.json | 15 +++++ .../issues/issue-122/check_checkFiles.xml | 7 +++ 23 files changed, 434 insertions(+), 15 deletions(-) create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/invoker.properties create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/mock-license.txt create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/pom.xml create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/setup.groovy create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java create mode 100644 license-maven-plugin/src/it/legacy-config-checkFiles/verify.groovy create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/invoker.properties create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-1.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-2.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-3.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/pom.xml create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/setup.groovy create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted3.java create mode 100644 license-maven-plugin/src/it/tri-license-set-checkFiles/verify.groovy create mode 100644 license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.json create mode 100644 license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.xml diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/invoker.properties b/license-maven-plugin/src/it/legacy-config-checkFiles/invoker.properties new file mode 100644 index 000000000..8fe714097 --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/invoker.properties @@ -0,0 +1 @@ +invoker.goals=license:format \ No newline at end of file diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/mock-license.txt b/license-maven-plugin/src/it/legacy-config-checkFiles/mock-license.txt new file mode 100644 index 000000000..dceaccb56 --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/mock-license.txt @@ -0,0 +1,2 @@ +This is a +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/pom.xml b/license-maven-plugin/src/it/legacy-config-checkFiles/pom.xml new file mode 100644 index 000000000..52156815f --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + com.mycila.license-maven-plugin.it + legacy-config + 1.0.0 + + Check Legacy Configuration with checkFiles + Integration Test for checking legacy configuration with checkFiles + + + + + com.mycila + license-maven-plugin + @project.version@ + +
mock-license.txt
+ + invoker.properties + pom.xml + *.groovy + **/*.bak + *.log + + src/main/java/com/mycilla/it/Unformatted2.java +
+
+
+
+ +
diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/setup.groovy b/license-maven-plugin/src/it/legacy-config-checkFiles/setup.groovy new file mode 100644 index 000000000..bf1ca7d27 --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/setup.groovy @@ -0,0 +1,23 @@ +import java.nio.file.Files +import java.nio.file.Path + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +final List ALL_FILES = Arrays.asList("Unformatted1.java", "Unformatted2.java") + +for (final String filename : ALL_FILES) { + final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename) + if (!Files.exists(unformattedJavaFile)) { + System.err.println(filename + " file is missing.") + return false + } + + Files.copy(unformattedJavaFile, unformattedJavaFile.resolveSibling(filename + ".bak")) +} + +return true; diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java b/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java new file mode 100644 index 000000000..53c628eba --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted1 { +} diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java b/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java new file mode 100644 index 000000000..2c4311d3c --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted2 { +} diff --git a/license-maven-plugin/src/it/legacy-config-checkFiles/verify.groovy b/license-maven-plugin/src/it/legacy-config-checkFiles/verify.groovy new file mode 100644 index 000000000..b935e249b --- /dev/null +++ b/license-maven-plugin/src/it/legacy-config-checkFiles/verify.groovy @@ -0,0 +1,61 @@ +import java.nio.file.Files +import java.nio.file.Path + +import static org.junit.jupiter.api.Assertions.assertEquals + + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +final Path license = base.resolve("mock-license.txt") +if (!Files.exists(license)) { + System.err.println("license file is missing.") + return false +} + +final Path unformattedJavaFile1 = base.resolve("src/main/java/com/mycilla/it/Unformatted1.java.bak") +if (!Files.exists(unformattedJavaFile1)) { + System.err.println(unformattedJavaFile1.getFileName() + " file is missing (should have been created by setup.groovy).") + return false +} + +final Path unformattedJavaFile2 = base.resolve("src/main/java/com/mycilla/it/Unformatted2.java.bak") +if (!Files.exists(unformattedJavaFile2)) { + System.err.println(unformattedJavaFile2.getFileName() + " file is missing (should have been created by setup.groovy).") + return false +} + +final Path formattedJavaFile1 = base.resolve("src/main/java/com/mycilla/it/Unformatted1.java") +if (!Files.exists(formattedJavaFile1)) { + System.err.println(formattedJavaFile1.getFileName() + " file is missing.") + return false +} + + +final Path formattedJavaFile2 = base.resolve("src/main/java/com/mycilla/it/Unformatted2.java") +if (!Files.exists(formattedJavaFile2)) { + System.err.println(formattedJavaFile2.getFileName() + " file is missing.") + return false +} + +assertEquals(new String(Files.readAllBytes(formattedJavaFile1)), new String(Files.readAllBytes(unformattedJavaFile1))) + +final StringBuilder expected = new StringBuilder(); +expected.append("/*\n"); +license.withReader { reader -> + while ((line = reader.readLine()) != null) { + expected.append(" * ").append(line).append('\n') + } +} +expected.append(" */\n") +expected.append(new String(Files.readAllBytes(unformattedJavaFile2))) + +final String actual = new String(Files.readAllBytes(formattedJavaFile2)) + +assertEquals(expected.toString(), actual) + +return true diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/invoker.properties b/license-maven-plugin/src/it/tri-license-set-checkFiles/invoker.properties new file mode 100644 index 000000000..8fe714097 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/invoker.properties @@ -0,0 +1 @@ +invoker.goals=license:format \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-1.txt b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-1.txt new file mode 100644 index 000000000..cd4a9d5f0 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-1.txt @@ -0,0 +1,2 @@ +This is the 1st +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-2.txt b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-2.txt new file mode 100644 index 000000000..891e9697e --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-2.txt @@ -0,0 +1,2 @@ +This is the 2nd +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-3.txt b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-3.txt new file mode 100644 index 000000000..4a01d0771 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/mock-license-3.txt @@ -0,0 +1,2 @@ +This is the 3rd +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/pom.xml b/license-maven-plugin/src/it/tri-license-set-checkFiles/pom.xml new file mode 100644 index 000000000..225c816ae --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/pom.xml @@ -0,0 +1,60 @@ + + 4.0.0 + + com.mycila.license-maven-plugin.it + tri-license-set + 1.0.0 + + Check a Triple License Set + Integration Test for checking a triple license set + + + + + com.mycila + license-maven-plugin + @project.version@ + + src/main/java/com/mycilla/it/Unformatted2.java + + +
mock-license-1.txt
+ + invoker.properties + pom.xml + *.groovy + **/*.bak + *.log + mock-license-* + **/Unformatted2.java + **/Unformatted3.java + +
+ +
mock-license-2.txt
+ + invoker.properties + pom.xml + *.groovy + **/*.bak + *.log + mock-license-* + **/Unformatted1.java + **/Unformatted3.java + +
+ +
mock-license-3.txt
+ + **/Unformatted3.java + +
+
+
+
+
+
+ +
diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/setup.groovy b/license-maven-plugin/src/it/tri-license-set-checkFiles/setup.groovy new file mode 100644 index 000000000..6f9d91e68 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/setup.groovy @@ -0,0 +1,23 @@ +import java.nio.file.Files +import java.nio.file.Path + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +final List ALL_FILES = Arrays.asList("Unformatted1.java", "Unformatted2.java", "Unformatted3.java") + +for (final String filename : ALL_FILES) { + final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename) + if (!Files.exists(unformattedJavaFile)) { + System.err.println(filename + " file is missing.") + return false + } + + Files.copy(unformattedJavaFile, unformattedJavaFile.resolveSibling(filename + ".bak")) +} + +return true; \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java new file mode 100644 index 000000000..53c628eba --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted1.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted1 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java new file mode 100644 index 000000000..2c4311d3c --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted2.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted2 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted3.java b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted3.java new file mode 100644 index 000000000..d24e2823e --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/src/main/java/com/mycilla/it/Unformatted3.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted3 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-checkFiles/verify.groovy b/license-maven-plugin/src/it/tri-license-set-checkFiles/verify.groovy new file mode 100644 index 000000000..7a816a93d --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-checkFiles/verify.groovy @@ -0,0 +1,53 @@ +import java.nio.file.Files +import java.nio.file.Path + +import static org.junit.jupiter.api.Assertions.assertEquals + + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +for (int i = 1; i <= 3; i++) { + + final Path license = base.resolve("mock-license-" + i + ".txt") + if (!Files.exists(license)) { + System.err.println(license.getFileName() + " file is missing.") + return false + } + + final String filename = "Unformatted" + i + ".java" + + final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename + ".bak") + if (!Files.exists(unformattedJavaFile)) { + System.err.println(unformattedJavaFile.getFileName() + " file is missing (should have been created by setup.groovy).") + return false + } + + final Path formattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename) + if (!Files.exists(formattedJavaFile)) { + System.err.println(formattedJavaFile.getFileName() + " file is missing.") + return false + } + + final StringBuilder expected = new StringBuilder(); + if (i == 2) { + expected.append("/*\n"); + license.withReader { reader -> + while ((line = reader.readLine()) != null) { + expected.append(" * ").append(line).append('\n') + } + } + expected.append(" */\n") + } + expected.append(new String(Files.readAllBytes(unformattedJavaFile))) + + final String actual = new String(Files.readAllBytes(formattedJavaFile)) + + assertEquals(expected.toString(), actual) +} + +return true diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java index f21825c2a..df6ca0572 100644 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java @@ -199,6 +199,15 @@ public abstract class AbstractLicenseMojo extends AbstractMojo { @Parameter(alias = "properties") public Map defaultProperties = new HashMap<>(); + /** + * Specifies files, which files are to check. By default, all files + * are included. + * + */ + @Parameter(alias = "filesToCheck", property = "license.filesToCheck") + public String[] filesToCheck = new String[0]; + + /** * Specifies files, which are included in the check. By default, all files * are included. @@ -786,14 +795,22 @@ private Map getDefaultProperties() { } 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, - getLog()); - debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir)); - debug("Including: %s", deepToString(selection.getIncluded())); - debug("Excluding: %s", deepToString(selection.getExcluded())); - return selection.getSelectedFiles(); + final boolean useDefaultExcludes = (licenseSet.useDefaultExcludes != null ? licenseSet.useDefaultExcludes + : defaultUseDefaultExcludes); + if (filesToCheck == null || filesToCheck.length == 0) { + final Selection selection = new Selection(firstNonNull(licenseSet.basedir, defaultBasedir), licenseSet.includes, buildExcludes(licenseSet), + useDefaultExcludes, getLog()); + debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir)); + debug("Including: %s", deepToString(selection.getIncluded())); + debug("Excluding: %s", deepToString(selection.getExcluded())); + return selection.getSelectedFiles(); + } else { + final Selection selection = new Selection(filesToCheck, licenseSet.includes, buildExcludes(licenseSet), + useDefaultExcludes, getLog()); + debug("Including: %s", deepToString(selection.getIncluded())); + debug("Excluding: %s", deepToString(selection.getExcluded())); + return selection.getSelectedFiles(); + } } private String[] buildExcludes(final LicenseSet licenseSet) { diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/util/Selection.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/util/Selection.java index b084e5298..51537eddc 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/util/Selection.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/util/Selection.java @@ -23,6 +23,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -35,22 +36,43 @@ public final class Selection { private final String[] excluded; private final Log log; private final String[] userExcluded; + private final String[] filesToCheck; private DirectoryScanner scanner; public Selection(File basedir, String[] included, String[] excluded, boolean useDefaultExcludes, - final Log log) { + final Log log) { this.basedir = basedir; this.log = log; String[] overrides = buildOverrideInclusions(useDefaultExcludes, included); this.included = buildInclusions(included, overrides); this.userExcluded = excluded; this.excluded = buildExclusions(useDefaultExcludes, excluded, overrides); + this.filesToCheck = null; + } + + public Selection(String[] filesToCheck, String[] included, String[] excluded, boolean useDefaultExcludes, + final Log log) { + this.basedir = null; + this.log = log; + String[] overrides = buildOverrideInclusions(useDefaultExcludes, included); + this.included = buildInclusions(included, overrides); + this.userExcluded = excluded; + this.excluded = buildExclusions(useDefaultExcludes, excluded, overrides); + this.filesToCheck = filesToCheck; } public String[] getSelectedFiles() { - scanIfneeded(); - return scanner.getIncludedFiles(); + if (filesToCheck == null) { + scanIfneeded(); + return scanner.getIncludedFiles(); + } else { + final MatchPatterns includePatterns = MatchPatterns.from(included); + final MatchPatterns excludePatterns = MatchPatterns.from(excluded); + return Arrays.stream(filesToCheck) + .filter(f -> includePatterns.matches(f, true) && !excludePatterns.matches(f, true)) + .toArray(String[]::new); + } } // for tests diff --git a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/ReportTest.java b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/ReportTest.java index eba8aca4e..fb04645fa 100755 --- a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/ReportTest.java +++ b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/ReportTest.java @@ -57,7 +57,32 @@ void test_check_xml() throws Exception { plugin.project = mavenProjectStub; plugin.defaultBasedir = tmp; plugin.legacyConfigHeader = "src/test/resources/issues/issue-122/header.txt"; - plugin.legacyConfigIncludes = new String[]{"file*.*"}; + plugin.legacyConfigIncludes = new String[] { "file*.*" }; + plugin.reportLocation = new File(tmp, "report/license-plugin-report.xml"); + + try { + plugin.execute(); + Assertions.fail(); + } catch (MojoExecutionException | MojoFailureException e) { + } + + String processed = unixify(FileUtils.read(plugin.reportLocation, Charset.defaultCharset())); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/check.xml"), + Charset.defaultCharset()); + assertThat(processed, is(equalTo(expected))); + } + + @Test + void test_check_xml_with_checkFiles() throws Exception { + File tmp = new File("target/test/issues/issue-122/test_check_xml_with_checkFiles"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-122"), tmp); + + AbstractLicenseMojo plugin = new LicenseCheckMojo(); + plugin.clock = Clock.fixed(Instant.ofEpochMilli(1631615047644L), ZoneId.systemDefault()); + plugin.project = mavenProjectStub; + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-122/header.txt"; + plugin.filesToCheck = new String[]{"file2.mv"}; plugin.reportLocation = new File(tmp, "report/license-plugin-report.xml"); try { @@ -67,10 +92,11 @@ void test_check_xml() throws Exception { } String processed = unixify(FileUtils.read(plugin.reportLocation, Charset.defaultCharset())); - String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/check.xml"), Charset.defaultCharset()); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/check_checkFiles.xml"), Charset.defaultCharset()); assertThat(processed, is(equalTo(expected))); } + @Test void test_check_json() throws Exception { File tmp = new File("target/test/issues/issue-122/test_check_json"); @@ -95,6 +121,31 @@ void test_check_json() throws Exception { assertThat(processed, is(equalTo(expected))); } + @Test + void test_check_json_with_checkFiles() throws Exception { + File tmp = new File("target/test/issues/issue-122/test_check_json_with_checkFiles"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-122"), tmp); + + AbstractLicenseMojo plugin = new LicenseCheckMojo(); + plugin.clock = Clock.fixed(Instant.ofEpochMilli(1631615047644L), ZoneId.systemDefault()); + plugin.project = mavenProjectStub; + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-122/header.txt"; + plugin.filesToCheck = new String[]{"file2.mv"}; + plugin.reportLocation = new File(tmp, "report/license-plugin-report.json"); + + try { + plugin.execute(); + Assertions.fail(); + } catch (MojoExecutionException | MojoFailureException e) { + } + + String processed = unixify(FileUtils.read(plugin.reportLocation, Charset.defaultCharset())); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/check_checkFiles.json"), Charset.defaultCharset()); + assertThat(processed, is(equalTo(expected))); + } + + @Test void test_format_xml() throws Exception { File tmp = new File("target/test/issues/issue-122/test_format_xml"); @@ -165,13 +216,14 @@ void test_remove_json() throws Exception { plugin.project = mavenProjectStub; plugin.defaultBasedir = tmp; plugin.legacyConfigHeader = "src/test/resources/issues/issue-122/header.txt"; - plugin.legacyConfigIncludes = new String[]{"file*.*"}; + plugin.legacyConfigIncludes = new String[] { "file*.*" }; plugin.reportLocation = new File(tmp, "report/license-plugin-report.json"); plugin.execute(); String processed = unixify(FileUtils.read(plugin.reportLocation, Charset.defaultCharset())); - String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/remove.json"), Charset.defaultCharset()); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-122/remove.json"), + Charset.defaultCharset()); assertThat(processed, is(equalTo(expected))); } diff --git a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/util/SelectionTest.java b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/util/SelectionTest.java index 49c0c288d..528c23d8b 100755 --- a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/util/SelectionTest.java +++ b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/util/SelectionTest.java @@ -134,4 +134,26 @@ private void touch(File newFile, Log log) throws IOException { } log.debug("Created '" + newFile.getAbsolutePath() + "'"); } + + @Test + void test_exclusions_respect_with_files_to_check() throws IOException { + SystemStreamLog log = new SystemStreamLog() { + @Override + public boolean isDebugEnabled() { + return true; + } + }; + File root = createAFakeProject(log); + Selection selection = new Selection( + new String[] {"module/target/ignored.txt", "module/src/main/java/not-ignored.txt"}, + new String[]{"**" + File.separator + "*.txt"}, + new String[]{"target" + File.separator + "**", "module" + File.separator + "**" + File.separator + "target" + File.separator + "**"}, false, + log); + + String[] selectedFiles = selection.getSelectedFiles(); // triggers scan and scanner build + org.assertj.core.api.Assertions.assertThat(selectedFiles) + .hasSize(1) + .allMatch(f -> f.equals("module/src/main/java/not-ignored.txt")); + } + } diff --git a/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.json b/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.json new file mode 100644 index 000000000..099485214 --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.json @@ -0,0 +1,15 @@ +{ + "timestamp": "1631615047644", + "goal": "CHECK", + "module": { + "groupId": "com.mycila", + "artifactId": "license-maven-plugin", + "version": "4.2-SNAPSHOT" + }, + "files": [ + { + "path": "target/test/issues/issue-122/test_check_json_with_checkFiles/file2.mv", + "result": "MISSING" + } + ] +} \ No newline at end of file diff --git a/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.xml b/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.xml new file mode 100644 index 000000000..0bf68418e --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-122/check_checkFiles.xml @@ -0,0 +1,7 @@ + + + + + + +