Skip to content

Introduce parameter failOnMismatchedAllowedViolationsCount #165

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/it/check-fail-with-mismatch-violation-count/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.goals=verify
invoker.buildResult=failure
57 changes: 57 additions & 0 deletions src/it/check-fail-with-mismatch-violation-count/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.checkstyle</groupId>
<artifactId>check-fail</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<url>http://maven.apache.org/</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<checkstyle.violation.ignore>NewlineAtEndOfFile</checkstyle.violation.ignore>
<checkstyle.maxAllowedViolations>314</checkstyle.maxAllowedViolations>
<checkstyle.failOnMismatchedAllowedViolationsCount>true</checkstyle.failOnMismatchedAllowedViolationsCount>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* My class.
*/
public class MyClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Nothing very important here, only a file for checkstyle rule PackageInfo.
*/
package org;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
22 changes: 22 additions & 0 deletions src/it/check-fail-with-mismatch-violation-count/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
def buildLog = new File( basedir, 'build.log' )

assert buildLog.text.contains( "The number of violations (1) is not equal to the maximum number of allowed violations (314)" )
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
@Parameter(property = "checkstyle.failOnViolation", defaultValue = "true")
private boolean failOnViolation;

/**
* Fail the build when the number of violations is not equal to {@link #maxAllowedViolations}.
* <p>This prevents the following scenario:
* <ul>
* <li>some changes reduce the checkstyle count but maxAllowedViolations is not updated</li>
* <li>some other changes introduces new violations but the build does not fail, as the count left some margin for error</li>
* </ul>
*
* * @since 3.6.1
*/
@Parameter(property = "checkstyle.failOnMismatchedAllowedViolationsCount", defaultValue = "false")
private boolean failOnMismatchedAllowedViolationsCount;

/**
* The maximum number of allowed violations. The execution fails only if the
* number of violations is above this limit.
Expand Down Expand Up @@ -599,6 +612,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

getLog().warn("checkstyle:check violations detected but failOnViolation set to false");
} else if (failOnMismatchedAllowedViolationsCount && violationCount != maxAllowedViolations) {
throw new MojoFailureException("The number of violations (" + violationCount
+ ") is not equal to the maximum number of allowed violations (" + maxAllowedViolations
+ ").");
}
if (logViolationCountToConsole) {
if (maxAllowedViolations > 0) {
Expand Down