Skip to content

Commit

Permalink
[MENFORCER-397] allow no rules
Browse files Browse the repository at this point in the history
  • Loading branch information
raupachz authored and slawekjaranowski committed Jul 31, 2022
1 parent b61b6b8 commit ae93fa8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public class EnforceMojo
@Parameter( property = "enforcer.failFast", defaultValue = "false" )
private boolean failFast = false;

/**
* Flag to fail the build if no rules are present
*
* @since 3.1.1
*/
@Parameter( property = "enforcer.failIfNoRules", defaultValue = "true" )
private boolean failIfNoRules = true;

/**
* Array of objects that implement the EnforcerRule interface to execute.
*/
Expand Down Expand Up @@ -153,9 +161,17 @@ public void execute()

if ( !havingRules() )
{
// CHECKSTYLE_OFF: LineLength
throw new MojoExecutionException( "No rules are configured. Use the skip flag if you want to disable execution." );
// CHECKSTYLE_ON: LineLength
if ( isFailIfNoRules() )
{
// CHECKSTYLE_OFF: LineLength
throw new MojoExecutionException( "No rules are configured. Use the skip flag if you want to disable execution." );
// CHECKSTYLE_ON: LineLength
}
else
{
log.warn( "No rules are configured." );
return;
}
}

// messages with warn/error flag
Expand Down Expand Up @@ -399,6 +415,22 @@ public void setSkip( boolean theSkip )
this.skip = theSkip;
}

/**
* @return the failIfNoRules
*/
public boolean isFailIfNoRules()
{
return this.failIfNoRules;
}

/**
* @param thefailIfNoRules the failIfNoRules to set
*/
public void setFailIfNoRules( boolean thefailIfNoRules )
{
this.failIfNoRules = thefailIfNoRules;
}

/**
* @return the project
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ public void testLoggingOnEnforcerRuleExceptionWithoutMessage()
Mockito.verify( logSpy ).warn( Mockito.matches( ".* failed with message:" + System.lineSeparator() + "null" ) );
}

@Test
public void testFailIfNoTests()
throws MojoExecutionException {
setupBasics( false );
mojo.setFailIfNoRules( false );

Log logSpy = setupLogSpy();

mojo.execute();

Mockito.verify( logSpy ).warn( Mockito.eq( "No rules are configured." ) );
Mockito.verifyNoMoreInteractions( logSpy );
}

private void setupBasics( boolean fail )
{
mojo.setFail( fail );
Expand Down

0 comments on commit ae93fa8

Please sign in to comment.