Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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 @@ -21,7 +21,6 @@
import javax.inject.Named;

import java.io.File;
import java.io.IOException;

/**
* The Class RequireFilesExist.
Expand All @@ -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 ? true : file.exists();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO

return file == null || file.exists()

is clearer, but up to you

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old habits die hard. :-)

}

@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;
}
}
}
5 changes: 4 additions & 1 deletion enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
Require Files Don't Exist

This rule checks that the specified list of files do not exist.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete "list of"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



The mounted filesystem(s) will dictate the case-sensitive rules when checking files. If you require checks that your

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case-sensitivity

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

filesystem(s) do not support (e.g. a case-sensitive check on a case-insensitive filesystem) then you should consider

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete "then you should"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

adding your own custom plugin that meets your specific needs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your own --> a

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


The following parameters are supported by this rule:

Expand Down
5 changes: 4 additions & 1 deletion enforcer-rules/src/site/apt/requireFilesExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
Require Files Exist

This rule checks that the specified list of files exist.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



The mounted filesystem(s) will dictate the case-sensitive rules when checking files. If you require checks that your

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case-sensitivity

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Apols.

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:

Expand Down
5 changes: 4 additions & 1 deletion enforcer-rules/src/site/apt/requireFilesSize.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
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-sensitive rules when checking files. If you require checks that your

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case-sensitivity

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Apols.

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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

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;

import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project style does not use wildcard imports

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, and in TestRequireFilesExist as well


/**
* Test the "require files don't exist" rule.
Expand Down Expand Up @@ -107,15 +106,58 @@ void testEmptyFileListAllowNull() {
@Test
void testFileDoesNotExist() throws EnforcerRuleException, IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test. It checks that we create a file, delete it, and then that the file doesn't exist? Consider renamingi

@roadSurfer roadSurfer Jan 9, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to just be a minor variation on the original test code, but I can change the name.
What do you think "testDeletedFileDetected"?
I'll update the symbolic link test to follow a similar format.

There is also "testFileDoesNotExistSatisfyAny", which is original code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, renaming is a good idea

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give me a format of the new method name you'd prefer and I will ensure that all test...Exists and test...DoesNotExist methods are updated in all test classes.
Such a change will affect TestRequireFilesSize, TestRequireFileChecksum TestRequireFilesDontExist, and TestRequireFilesExist; this means touching classes that are unrelated to the bug fix in order to ensure naming consistency.

To be clear, I disagree with this but it is not my project and so I will follow your direction.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change other methods not related to this PR. keep the PR focused.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that means this TestRequireFilesDontExist class will be following a different naming convention to the other classes. I don't follow why that is a good idea, I merely followed the convention that was already in place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better 9 wrong and 1 right than 10 wrong. That's the fundamental principle.

@roadSurfer roadSurfer Jan 29, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the new names be?

  • testFileDoesNotExist -> testDeletedFileDetected
  • testSymbolicLinkDoesNotExist -> testSymbolicLinDeletedDetected
  • testSymbolicLinkTargetDoesNotExist -> testSymbolicLinkTargetDeletedDetected
  • testFileDoesNotExistSatisfyAny -> testDeletedFileDetectedSatisfyAny

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testSymbolicLinkDeletedDetected (spelling) but otherwise sounds good

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

File f = File.createTempFile("junit", null, temporaryFolder);
rule.setFilesList(Collections.singletonList(f));

EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
assertNotNull(e.getMessage());

f.delete();

assertFalse(f.exists());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my confusion is that this is the only assert. It should be changed to an assumeFalse or removed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a quick negative check to ensure the file is detected and the test does not pass by fluke. It is consistent with checks in the other tests (throws an exception with a message).

And I am sorry, but I do no understand how assumeFalse is helpful here. If the file is not detected as being present and an exception thrown with a message, then that should be a hard failure as the rule is not working according to spec.

I can see a mix of checkin/not-checking for an unexpected exception, I can add and explicit check for one and add a fail if you would like? I don't think it would be appropriate to update every test though, just one directly required by this fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test setup failure does not mean the test failed. It means the test wasn't run. That's the difference between assume and assert. JUnit reports these two cases differently.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrong one was changed. Line 122 should be changed or better yet deleted. The point of the test is not whether f.delete() succeeds. You're not testing the delete method in java.io.File.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


rule.setFilesList(Collections.singletonList(f));

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();

try {
rule.setFilesList(Collections.singletonList(linkFile));
EnforcerRuleException e = assertThrows(EnforcerRuleException.class, rule::execute);
assertNotNull(e.getMessage());

linkFile.delete();
rule.execute();
} finally {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use try with resources for this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don;'t think so, isn't that just for autoclosables?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at other tests, it does not seem to be project standard to ensure tests delete files they create; I could just remove the try block entirely in that case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the temporary folder rule should take care of that

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

if (linkFile.exists()) {
linkFile.delete();
}
canonicalFile.delete();
}
}

@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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no exception is a pass?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. This whole class it basically the inverse of TestRequireFilesExist.

} finally {
linkFile.delete();
}
}

@Test
void testFileDoesNotExistSatisfyAny() throws EnforcerRuleException, IOException {
File f = File.createTempFile("junit", null, temporaryFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,12 +52,40 @@ void testFileExists() throws Exception {
}

@Test
void testFileOsIndependentExists() {
rule.setFilesList(Collections.singletonList(new File("POM.xml")));

EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
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();
rule.setFilesList(Collections.singletonList(linkFile));

try {
rule.execute();
} finally {
linkFile.delete();
canonicalFile.delete();
}
}

assertNotNull(e.getMessage());
@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 get an exception");
} catch (Exception e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which exception should be thrown here? Be specific.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assertNotNull(e.getMessage());
} finally {
linkFile.delete();
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -180,6 +182,52 @@ 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());
} finally {
linkFile.delete();
canonicalFile.delete();
}
}

@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());
} finally {
linkFile.delete();
canonicalFile.delete();
}
}

/**
* Test id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ under the License.
<requireFilesExist>
<files>
<file>pom.xml</file>
<file>../require-files-exist/pom.xml</file>
<file>${project.basedir}/../require-files-exist/pom.xml</file>
</files>
</requireFilesExist>
</rules>
Expand Down