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-397] allow no rules #178

Merged
merged 1 commit into from
Jul 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" )
Copy link
Member

Choose a reason for hiding this comment

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

Pleas add @since

Yes - I see that other parameters also has missing it ... we can fix for rest in separate PR

It is used for documentation: https://maven.apache.org/enforcer/maven-enforcer-plugin/enforce-mojo.html

Maybe documentation - similar to other, eg:

Flag to allow executions without rules.

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