Skip to content
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-430] Allow one of many files in RequireFiles rules to pass #183

Merged
merged 2 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -42,6 +42,9 @@ public abstract class AbstractRequireFiles
/** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
private boolean allowNulls = false;

/** Allow that a single one of the files can make the rule to pass. */
private boolean satisfyAny;

// check the file for the specific condition
/**
* Check one file.
Expand Down Expand Up @@ -82,32 +85,46 @@ else if ( !checkFile( file ) )
}
}

// if anything was found, log it with the optional message.
if ( !failures.isEmpty() )
if ( satisfyAny )
{
String message = getMessage();

StringBuilder buf = new StringBuilder();
if ( message != null )
int passed = files.length - failures.size();
if ( passed == 0 )
{
buf.append( message + System.lineSeparator() );
fail( failures );
}
buf.append( getErrorMsg() );
}
// if anything was found, log it with the optional message.
else if ( !failures.isEmpty() )
{
fail( failures );
}
}

private void fail( List<File> failures )
throws EnforcerRuleException
{
String message = getMessage();

StringBuilder buf = new StringBuilder();
if ( message != null )
{
buf.append( message + System.lineSeparator() );
}
buf.append( getErrorMsg() );

for ( File file : failures )
for ( File file : failures )
{
if ( file != null )
{
if ( file != null )
{
buf.append( file.getAbsolutePath() + System.lineSeparator() );
}
else
{
buf.append( "(an empty filename was given and allowNulls is false)" + System.lineSeparator() );
}
buf.append( file.getAbsolutePath() + System.lineSeparator() );
}
else
{
buf.append( "(an empty filename was given and allowNulls is false)" + System.lineSeparator() );
}

throw new EnforcerRuleException( buf.toString() );
}

throw new EnforcerRuleException( buf.toString() );
}

@Override
Expand Down Expand Up @@ -168,4 +185,14 @@ public void setAllowNulls( boolean allowNulls )
{
this.allowNulls = allowNulls;
}

public boolean isSatisfyAny()
{
return satisfyAny;
}

public void setSatisfyAny( boolean satisfyAny )
{
this.satisfyAny = satisfyAny;
}
}
2 changes: 2 additions & 0 deletions enforcer-rules/src/site/apt/requireFilesDontExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Require Files Don't Exist
* files - A list of files to check.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do not exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
2 changes: 2 additions & 0 deletions enforcer-rules/src/site/apt/requireFilesExist.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Require Files Exist
* files - A list of files to check.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
2 changes: 2 additions & 0 deletions enforcer-rules/src/site/apt/requireFilesSize.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Require File Size
* minsize - minimum size in bytes for this file.

* allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false.

* satisfyAny - Allows that one of files can make the rule pass, instead of all the files. Default is false.

[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ public void testFileDoesNotExist()
rule.execute( EnforcerTestUtils.getHelper() );
}

@Test
public void testFileDoesNotExistSatisfyAny()
throws EnforcerRuleException, IOException
{
File f = File.createTempFile( "junit", null, temporaryFolder );
f.delete();
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved

assertFalse( f.exists() );

File g = File.createTempFile( "junit", null, temporaryFolder );

assertTrue( g.exists() );

rule.setFiles( new File[] { f, g.getCanonicalFile() } );
rule.setSatisfyAny(true);

rule.execute( EnforcerTestUtils.getHelper() );
}

/**
* Test id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.*;

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

import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -121,6 +122,25 @@ public void testFileDoesNotExist()

}

@Test
public void testFileExistsSatisfyAny()
throws EnforcerRuleException, IOException
{
File f = File.createTempFile( "junit", null, temporaryFolder );
f.delete();

assertFalse( f.exists() );

File g = File.createTempFile( "junit", null, temporaryFolder );

assertTrue( g.exists() );

rule.setFiles( new File[] { f, g.getCanonicalFile() } );
rule.setSatisfyAny(true);

rule.execute( EnforcerTestUtils.getHelper() );
}

/**
* Test id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ public void testFileTooBig()
}
}

@Test
public void testRequireFilesSizeSatisfyAny()
throws EnforcerRuleException, IOException
{
File f = File.createTempFile( "junit", null, temporaryFolder );
try ( BufferedWriter out = new BufferedWriter( new FileWriter( f ) ) )
{
out.write( "123456789101112131415" );
}
assertTrue( f.length() > 10 );

File g = File.createTempFile( "junit", null, temporaryFolder );

rule.setFiles( new File[] { f, g } );
rule.setMaxsize( 10 );
rule.setSatisfyAny(true);

rule.execute( EnforcerTestUtils.getHelper() );
}

/**
* Test id.
*/
Expand Down