test: add change risk anti-pattern report #2913
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Description of change
Add CRAP Score Analysis Task for Test Coverage Prioritization
This PR adds a Gradle task that generates prioritized reports identifying which code poses the highest risk when modified. CRAP (Change Risk Anti-Patterns) combines cyclomatic complexity and test coverage to quantify modification risk using the formula CRAP = complexity² × (1 - coverage)³ + complexity. Methods with CRAP > 30 are high-risk and should either have comprehensive tests added or be refactored to reduce complexity before modification. Methods with complexity > 30 cannot achieve CRAP < 30 even with 100% test coverage and must be refactored. Traditional coverage metrics treat all uncovered code equally, but a simple getter with 0% coverage carries far less risk than complex business logic with 0% coverage.
The task parses existing JaCoCo XML reports and generates an interactive HTML report showing methods ranked by CRAP score, package-level statistics for broad coverage planning, and color-coded risk indicators. It exports CSV for custom analysis and prints a console summary showing the top 10 highest-CRAP methods and packages with the most untested complexity. Before modifying legacy code, run the analysis to check CRAP scores. CRAP < 10 indicates relatively safe code to modify. CRAP 10-30 requires adding tests before major changes. CRAP > 30 requires either comprehensive tests to bring the score below 30, or refactoring if complexity exceeds 30. The HTML report includes a usage guide and the Packages Overview tab identifies entire packages with high uncovered complexity for systematic coverage expansion.
Run ./gradlew crapAnalysis to generate reports in build/reports/crap-analysis/. The task depends on jacocoAggregateReport and will run tests and generate coverage automatically if needed. No additional dependencies or configuration required, fully backward compatible, and only runs when explicitly called. This identifies which code needs attention before refactoring using existing JaCoCo data.