diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java index 9318dbca..31c109e4 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/files/RequireFilesExist.java @@ -21,7 +21,6 @@ import javax.inject.Named; import java.io.File; -import java.io.IOException; /** * The Class RequireFilesExist. @@ -31,35 +30,11 @@ public final class RequireFilesExist extends AbstractRequireFiles { @Override boolean checkFile(File file) { // if we get here and the handle is null, treat it as a success - return file == null ? true : file.exists() && osIndependentNameMatch(file, true); + return file == null || file.exists(); } @Override String getErrorMsg() { return "Some required files are missing:" + System.lineSeparator(); } - - /** - * OSes like Windows are case insensitive, so this method will compare the file path with the actual path. A simple - * {@link File#exists()} is not enough for such OS. - * - * @param file the file to verify - * @param defaultValue value to return in case an IO exception occurs, should never happen as the file already - * exists - * @return - */ - private boolean osIndependentNameMatch(File file, boolean defaultValue) { - try { - File absFile; - if (!file.isAbsolute()) { - absFile = new File(new File(".").getCanonicalFile(), file.getPath()); - } else { - absFile = file; - } - - return absFile.toURI().equals(absFile.getCanonicalFile().toURI()); - } catch (IOException e) { - return defaultValue; - } - } } diff --git a/enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm b/enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm index 53b8ca68..06e41424 100644 --- a/enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm +++ b/enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm @@ -26,7 +26,10 @@ Require Files Don't Exist This rule checks that the specified list of files do not exist. - + + The mounted filesystem(s) will dictate the case-sensitivity rules when checking files. If checks are required that + filesystem(s) do not support (e.g. a case-sensitive check on a case-insensitive filesystem) consider adding a custom + plugin that meets the specific needs. The following parameters are supported by this rule: diff --git a/enforcer-rules/src/site/apt/requireFilesExist.apt.vm b/enforcer-rules/src/site/apt/requireFilesExist.apt.vm index 5a65f9ee..e0f97c43 100644 --- a/enforcer-rules/src/site/apt/requireFilesExist.apt.vm +++ b/enforcer-rules/src/site/apt/requireFilesExist.apt.vm @@ -13,8 +13,8 @@ ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations -~~ under the License. - +~~ under the License. + ------ Require Files Exist ------ @@ -27,22 +27,25 @@ Require Files Exist This rule checks that the specified list of files exist. + The mounted filesystem(s) will dictate the case-sensitivity rules when checking files. If you require checks that your + filesystem(s) do not support (e.g. a case-sensitive check on a case-insensitive filesystem) then you should consider + adding your own custom plugin that meets your specific needs. The following parameters are supported by this rule: - + * <> - an optional message to the user if the rule fails. - + * <> - A list of files to check. - + * <> - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false. * <> - Allows that one of files can make the rule pass, instead of all the files. Default is false. - + [] - + Sample Plugin Configuration: - + +---+ [...] diff --git a/enforcer-rules/src/site/apt/requireFilesSize.apt.vm b/enforcer-rules/src/site/apt/requireFilesSize.apt.vm index ed4fa0d8..dd2cd101 100644 --- a/enforcer-rules/src/site/apt/requireFilesSize.apt.vm +++ b/enforcer-rules/src/site/apt/requireFilesSize.apt.vm @@ -13,8 +13,8 @@ ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations -~~ under the License. - +~~ under the License. + ------ Require File Size ------ @@ -27,26 +27,29 @@ Require File Size This rule checks that the specified list of files exist and are within the specified size range. + The mounted filesystem(s) will dictate the case-sensitivity rules when checking files. If you require checks that your + filesystem(s) do not support (e.g. a case-sensitive check on a case-insensitive filesystem) then you should consider + adding your own custom plugin that meets your specific needs. The following parameters are supported by this rule: - + * <> - an optional message to the user if the rule fails. - + * <> - A list of files to check. If this list is empty, the main project artifact will be checked. - + * <> - maximum size in bytes for this file. - + * <> - minimum size in bytes for this file. - + * <> - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false. * <> - Allows that one of files can make the rule pass, instead of all the files. Default is false. - + [] - + Sample Plugin Configuration: - + +---+ [...] diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java index 93a1a0e3..552c81e0 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesDontExist.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; @@ -29,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -107,12 +110,55 @@ void testEmptyFileListAllowNull() { @Test void testFileDoesNotExist() throws EnforcerRuleException, IOException { File f = File.createTempFile("junit", null, temporaryFolder); + rule.setFilesList(Collections.singletonList(f)); + + // Check the file is detected as being present + EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute); + assertNotNull(e.getMessage()); + f.delete(); assertFalse(f.exists()); - rule.setFilesList(Collections.singletonList(f)); + // Rule should now pass as the file was deleted + rule.execute(); + } + + @Test + void testSymbolicLinkDoesNotExist() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); + + rule.setFilesList(Collections.singletonList(linkFile)); + // Check the link is detected as being present + EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute); + assertNotNull(e.getMessage()); + + linkFile.delete(); + + // Rule should now pass as the link was deleted + rule.execute(); + } + + @Test + void testSymbolicLinkTargetDoesNotExist() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); + + // Check the target is detected as being present + EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute); + assertNotNull(e.getMessage()); + + canonicalFile.delete(); + rule.setFilesList(Collections.singletonList(linkFile)); + // Rule should now pass as the target was deleted rule.execute(); } diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java index 33c72615..04769ea3 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesExist.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; @@ -27,7 +29,11 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Test the "require files exist" rule. @@ -50,12 +56,34 @@ void testFileExists() throws Exception { } @Test - void testFileOsIndependentExists() { - rule.setFilesList(Collections.singletonList(new File("POM.xml"))); + void testSymbolicLinkExists() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); - EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute()); + rule.setFilesList(Collections.singletonList(linkFile)); - assertNotNull(e.getMessage()); + rule.execute(); + } + + @Test + void testSymbolicLinkTargetDoesNotExist() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); + canonicalFile.delete(); + rule.setFilesList(Collections.singletonList(linkFile)); + + try { + rule.execute(); + fail("Should have received an exception"); + } catch (Exception e) { + assertNotNull(e.getMessage()); + } } @Test diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java index f0a7a042..e6377095 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/files/TestRequireFilesSize.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; @@ -180,6 +182,46 @@ void testRequireFilesSizeSatisfyAny() throws EnforcerRuleException, IOException rule.execute(); } + @Test + void testSymbolicLinkTooSmall() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); + + rule.setFilesList(Arrays.asList(linkFile)); + rule.setMinsize(10); + try { + rule.execute(); + fail("Should get exception"); + } catch (EnforcerRuleException e) { + assertNotNull(e.getMessage()); + } + } + + @Test + void testSymbolicLinkTooBig() throws Exception { + File canonicalFile = File.createTempFile("canonical_", null, temporaryFolder); + try (BufferedWriter out = new BufferedWriter(new FileWriter(canonicalFile))) { + out.write("123456789101112131415"); + } + assertTrue(canonicalFile.length() > 10); + File linkFile = Files.createSymbolicLink( + Paths.get(temporaryFolder.getAbsolutePath(), "symbolic.link"), + Paths.get(canonicalFile.getAbsolutePath())) + .toFile(); + + rule.setFilesList(Arrays.asList(linkFile)); + rule.setMaxsize(10); + try { + rule.execute(); + fail("Should get exception"); + } catch (EnforcerRuleException e) { + assertNotNull(e.getMessage()); + } + } + /** * Test id. */ diff --git a/maven-enforcer-plugin/src/it/projects/require-files-exist/pom.xml b/maven-enforcer-plugin/src/it/projects/require-files-exist/pom.xml index ce4612ce..9c81f169 100644 --- a/maven-enforcer-plugin/src/it/projects/require-files-exist/pom.xml +++ b/maven-enforcer-plugin/src/it/projects/require-files-exist/pom.xml @@ -46,6 +46,8 @@ under the License. pom.xml + ../require-files-exist/pom.xml + ${project.basedir}/../require-files-exist/pom.xml