-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MENFORCER-390] "requireFilesExist" no longer handles non-canonical paths #297
base: master
Are you sure you want to change the base?
Changes from all commits
4568135
c7ca861
81b8027
0b45664
326ff7f
b82306d
01570d4
e6cd78c
11dbb95
e0d8be0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,10 @@ | |
Require Files Exist | ||
|
||
This rule checks that the specified list of files exist. | ||
|
||
|
||
The mounted filesystem(s) will dictate the case-sensitive rules when checking files. If you require checks that your | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case-sensitivity |
||
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: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case-sensitivity |
||
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: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,15 +110,69 @@ void testEmptyFileListAllowNull() { | |
@Test | ||
void testFileDoesNotExist() throws EnforcerRuleException, IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There is also "testFileDoesNotExistSatisfyAny", which is original code. |
||
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(); | ||
|
||
try { | ||
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(); | ||
} finally { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use try with resources for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don;'t think so, isn't that just for autoclosables? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the temporary folder rule should take care of that |
||
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(); | ||
|
||
// 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)); | ||
|
||
try { | ||
// Rule should now pass as the target was deleted | ||
rule.execute(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no exception is a pass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. This whole class it basically the inverse of |
||
} finally { | ||
linkFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
void testFileDoesNotExistSatisfyAny() throws EnforcerRuleException, IOException { | ||
File f = File.createTempFile("junit", null, temporaryFolder); | ||
|
There was a problem hiding this comment.
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