Skip to content

Issue #13109: Kill mutation for CustomImport#13210

Merged
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:cic2
Jun 24, 2023
Merged

Issue #13109: Kill mutation for CustomImport#13210
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:cic2

Conversation

@Kevin222004
Copy link
Copy Markdown
Contributor

@Kevin222004 Kevin222004 commented Jun 13, 2023

Issue #13109: Kill mutation for CustomImport


Check:-

https://checkstyle.org/config_imports.html#CustomImportOrder


Mutation

<mutation unstable="false">
<sourceFile>CustomImportOrderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck</mutatedClass>
<mutatedMethod>&lt;init&gt;</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator</mutator>
<description>Removed assignment to member variable samePackageMatchingDepth</description>
<lineContent>private int samePackageMatchingDepth = 2;</lineContent>
</mutation>
<mutation unstable="false">
<sourceFile>CustomImportOrderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck</mutatedClass>
<mutatedMethod>getFirstDomainsFromIdent</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to java/lang/StringBuilder::append</description>
<lineContent>builder.append(tokens.nextToken()).append(&apos;.&apos;);</lineContent>
</mutation>


Explaination

ok so there are 4 places where samePackageMatchingDepth is used let's talk about first in method addRulesToList

else if (ruleStr.startsWith(SAME_PACKAGE_RULE_GROUP)) {
final String rule = ruleStr.substring(ruleStr.indexOf('(') + 1,
ruleStr.indexOf(')'));
samePackageMatchingDepth = Integer.parseInt(rule);
if (samePackageMatchingDepth <= 0) {
throw new IllegalArgumentException(
"SAME_PACKAGE rule parameter should be positive integer: " + ruleStr);
}
customOrderRules.add(SAME_PACKAGE_RULE_GROUP);
}

know it will actually call by only a setter method which is setCustomImportOrderRules
public final void setCustomImportOrderRules(final String inputCustomImportOrder) {
if (!customImportOrderRules.equals(inputCustomImportOrder)) {
for (String currentState : GROUP_SEPARATOR_PATTERN.split(inputCustomImportOrder)) {
addRulesToList(currentState);
}
customOrderRules.add(NON_GROUP_RULE_GROUP);
}
customImportOrderRules = inputCustomImportOrder;
}

know else if part only executes when condition ruleStr.startsWith(SAME_PACKAGE_RULE_GROUP) meet hence in this method user always defined such like SAME_PACKAGE(3)
know here we are reassigning samePackageMatchingDepth
with the value inside the bracket in SAME_PACKAGE hence whatever value of samePackageMatchingDepth doesn't matter because the further operation will be performed based on the new value.

append('.')

ok so basically the removal of append('.') is not creating an issue the reason is this method basically used in 2 methods 1) createSamePackageRegexp
https://github.com/checkstyle/checkstyle/blob/c7f3688f4380144bb712bee7ad6808cd075b327c/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java#L1249C1-L1252
which is actually called visitToken when

public void visitToken(DetailAST ast) {
if (ast.getType() == TokenTypes.PACKAGE_DEF) {
samePackageDomainsRegExp = createSamePackageRegexp(
samePackageMatchingDepth, ast);
}
else {

ast is equal to PACKAGE_DEF.
so logically here samePackageDomainsRegExp will assign to the String which will be return by
our method getFirstDomainsFromIdent here if we remove
append('.') so let's suppose if
String with append('.') look like 'java.awt.button.'
look like javaawtbutton.

know the samePackageDomainsRegExp has a use case only in
getImportGroup

else if (customOrderRules.contains(SAME_PACKAGE_RULE_GROUP)) {
final String importPathTrimmedToSamePackageDepth =
getFirstDomainsFromIdent(samePackageMatchingDepth, importPath);
if (samePackageDomainsRegExp.equals(importPathTrimmedToSamePackageDepth)) {
bestMatch.group = SAME_PACKAGE_RULE_GROUP;
bestMatch.matchLength = importPath.length();
}

at

final String importPathTrimmedToSamePackageDepth =
getFirstDomainsFromIdent(samePackageMatchingDepth, importPath);
if (samePackageDomainsRegExp.equals(importPathTrimmedToSamePackageDepth)) {

in if statement know we are equaling samePackageDomainsRegExp with importPathTrimmedToSamePackageDepth and importPathTrimmedToSamePackageDepth will also call
getFirstDomainsFromIdent to get string so here if we remove append it will affect both
samePackageDomainsRegExp and importPathTrimmedToSamePackageDepth and remove doty from both so their will no issue appear here while comparing. apart from this, there is no usage of samePackageDomainsRegExp which will cause output/logging violation hence the test for append('.') is not possible.

and know rest two usage of samePackageMatchingDepth are here. while calling getFirstDomainsFromIdent
they always pass samePackageMatchingDepth with it. and that value of samePackageMatchingDepth will be updated
addRulesToList so it never be zero and update by the value provided by user in SAME_PACKAGE_RULE_GROUP
so no issue is their


Regression

Report-1: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/1d4f30f_2023083444/reports/diff/index.html

Report-2: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/1d4f30f_2023105216/reports/diff/index.html

Report-3: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/1d4f30f_2023223348/reports/diff/index.html

Report-4: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/1d4f30f_2023055327/reports/diff/index.html


Diff Regression config: https://gist.githubusercontent.com/Kevin222004/c77cb85c86c9676985cf8fb916591a94/raw/713375f8427445ec0297b481413a185f4afbe8dc/cic.xml
Diff Regression projects: https://gist.githubusercontent.com/Kevin222004/9600f179b602d4c971bdb0a050099005/raw/360a95ed7bb60d7a0956e531199d484c4d6f6617/test-projects.properties
Report label: New Report-2(2)

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@romani I have tried to explain the whole thing, I hope i have covered all case, if you still have any doubt about it please ask me

@Kevin222004 Kevin222004 marked this pull request as ready for review June 15, 2023 07:16
@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Vyom-Yadav
Copy link
Copy Markdown
Member

@Kevin222004 Please put the final reports in the PR description.

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@Vyom-Yadav done

@Vyom-Yadav
Copy link
Copy Markdown
Member

@kevin Please generate a report with regex .*, I haven't seen the logic but regex matching everything is really valuable for checks using regular expressions as properties.

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Github, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@Vyom-Yadav Reports are clean please go through the explanation

Copy link
Copy Markdown
Member

@rdiachenko rdiachenko left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Copy Markdown
Member

@Vyom-Yadav Vyom-Yadav left a comment

Choose a reason for hiding this comment

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

LGTM!

@Vyom-Yadav Vyom-Yadav assigned romani and unassigned Vyom-Yadav Jun 23, 2023
@romani romani merged commit dcedae6 into checkstyle:master Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants