Skip to content

Commit

Permalink
Issue checkstyle#13809: Kill mutation in SuppressionCommentFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Nov 7, 2023
1 parent abed5ad commit ec54435
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
9 changes: 0 additions & 9 deletions config/pitest-suppressions/pitest-filters-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,6 @@
<lineContent>eventMessageRegexp = null;</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>SuppressionCommentFilter.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter</mutatedClass>
<mutatedMethod>accept</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter::getFileContents</description>
<lineContent>if (getFileContents() != currentContents) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>SuppressionCommentFilter.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter$Tag</mutatedClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@

import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.JavaParser;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import nl.jqno.equalsverifier.EqualsVerifier;
Expand Down Expand Up @@ -649,4 +658,51 @@ private static List<Comparable<Object>> getTagsAfterExecution(SuppressionComment
return TestUtil.getInternalState(filter, "tags");
}

@Test
public void testCachingByFileContentsInstance() throws Exception {

File file = new File(getPath("InputSuppressionCommentFilterSuppressById6.java"));
DetailAST rootast = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS);
String[] lines = {"//CSOFF", "//CSON"};
final FileContents fileContents = new FileContents(
new FileText(file, Arrays.asList(lines)));
for (int lineNo = 0; lineNo < lines.length; lineNo++) {
final int colNo = lines[lineNo].indexOf("//");
if (colNo >= 0) {
fileContents.reportSingleLineComment(lineNo + 1, colNo);}
}


final FileContents mockedContents = mock(FileContents.class);
Map<Integer, TextBlock> returnValue = fileContents.getSingleLineComments();
when(mockedContents.getSingleLineComments())
.thenReturn(returnValue)
.thenThrow(new IllegalStateException("Second call is not allowed"));


String[] args = {"line_length", "^[a-z][a-zA-Z0-9]*$"};
Violation violation = new Violation(27, 9, 58,
"com.puppycrawl.tools.checkstyle.checks.naming.messages",
"name.invalidPattern",
args,
SeverityLevel.ERROR,
"ignore",
MemberNameCheck.class,
null);

TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(
mockedContents, file.getAbsolutePath(), violation, rootast);

SuppressionCommentFilter filter = new SuppressionCommentFilter();
filter.setOffCommentFormat(Pattern.compile("CSOFF"));
filter.setOnCommentFormat(Pattern.compile("CSON"));
filter.setCheckFormat("MemberNameCheck");
filter.finishLocalSetup();

assertWithMessage("should accept").that(filter.accept(event)).isTrue();
// due to caching in filter second execution should not do parsing of comments in file
// so exception is not expected
assertWithMessage("should accept").that(filter.accept(event)).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
SuppressionCommentFilter
offCommentFormat = CSOFF
onCommentFormat = CSON
checkFormat = MemberNameCheck
messageFormat = (default)(null)
idFormat = (default)(null)
checkCPP = (default)true
checkC = (default)true
com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck
id = ignore
format = (default)^[a-z][a-zA-Z0-9]*$
applyToPublic = (default)true
applyToProtected = (default)true
applyToPackage = (default)true
applyToPrivate = (default)true
*/

package com.puppycrawl.tools.checkstyle.filters.suppressioncommentfilter;

public class InputSuppressionCommentFilterSuppressById6 {

// CSOFF
int line_length = 100; // filtered violation
//CSON
}

0 comments on commit ec54435

Please sign in to comment.