From 554657bea2eccc3848f4399325398074566a6a67 Mon Sep 17 00:00:00 2001 From: Dmitry Krutskikh Date: Sat, 29 Aug 2020 08:47:40 +0300 Subject: [PATCH] update code_climate report --- .../code_climate/code_climate_issue.dart | 17 ++-- .../code_climate/code_climate_reporter.dart | 18 ++-- .../reporters/code_climate_reporter_test.dart | 89 +++++++++++-------- 3 files changed, 65 insertions(+), 59 deletions(-) diff --git a/lib/src/reporters/code_climate/code_climate_issue.dart b/lib/src/reporters/code_climate/code_climate_issue.dart index 18652c3141..0433260d2d 100644 --- a/lib/src/reporters/code_climate/code_climate_issue.dart +++ b/lib/src/reporters/code_climate/code_climate_issue.dart @@ -5,6 +5,8 @@ import 'package:dart_code_metrics/src/models/code_issue.dart'; import 'package:dart_code_metrics/src/models/code_issue_severity.dart'; import 'package:meta/meta.dart'; +import '../../models/design_issue.dart'; + @immutable class CodeClimateLocationLines { final int begin; @@ -57,15 +59,6 @@ class CodeClimateIssue { return CodeClimateIssue._(name, desc, categories, location, fingerprint); } - factory CodeClimateIssue.linesOfExecutableCode(int startLine, int endLine, - int value, String fileName, String functionName, int threshold) { - final desc = - 'Function `$functionName` has $value executable code lines (exceeds $threshold allowed). Consider refactoring.'; - - return CodeClimateIssue._create( - 'linesOfExecutableCode', desc, startLine, endLine, fileName); - } - factory CodeClimateIssue.cyclomaticComplexity(int startLine, int endLine, int value, String fileName, String functionName, int threshold) { final desc = @@ -114,6 +107,12 @@ class CodeClimateIssue { categories: severityHumanReadable[issue.severity]); } + factory CodeClimateIssue.fromDesignIssue( + DesignIssue issue, String fileName) => + CodeClimateIssue._create(issue.patternId, issue.message, + issue.sourceSpan.start.line, issue.sourceSpan.start.line, fileName, + categories: const ['Complexity']); + Map toJson() => { 'type': type, 'check_name': checkName, diff --git a/lib/src/reporters/code_climate/code_climate_reporter.dart b/lib/src/reporters/code_climate/code_climate_reporter.dart index d93847f3e0..437f7a2953 100644 --- a/lib/src/reporters/code_climate/code_climate_reporter.dart +++ b/lib/src/reporters/code_climate/code_climate_reporter.dart @@ -53,17 +53,6 @@ class CodeClimateReporter implements Reporter { reportConfig.cyclomaticComplexityWarningLevel)); } - if (UtilitySelector.isIssueLevel( - report.linesOfExecutableCode.violationLevel)) { - result.add(CodeClimateIssue.linesOfExecutableCode( - func.firstLine, - func.lastLine, - report.linesOfExecutableCode.value, - record.relativePath, - key, - reportConfig.linesOfExecutableCodeWarningLevel)); - } - if (UtilitySelector.isIssueLevel( report.maintainabilityIndex.violationLevel)) { result.add(CodeClimateIssue.maintainabilityIndex( @@ -85,8 +74,11 @@ class CodeClimateReporter implements Reporter { } } - result.addAll(record.issues.map( - (issue) => CodeClimateIssue.fromCodeIssue(issue, record.relativePath))); + result + ..addAll(record.issues.map((issue) => + CodeClimateIssue.fromCodeIssue(issue, record.relativePath))) + ..addAll(record.designIssue.map((issue) => + CodeClimateIssue.fromDesignIssue(issue, record.relativePath))); return result; } diff --git a/test/reporters/code_climate_reporter_test.dart b/test/reporters/code_climate_reporter_test.dart index 90dbce3bc0..9d3a89baa1 100644 --- a/test/reporters/code_climate_reporter_test.dart +++ b/test/reporters/code_climate_reporter_test.dart @@ -5,6 +5,7 @@ import 'package:dart_code_metrics/src/models/code_issue.dart'; import 'package:dart_code_metrics/src/models/code_issue_severity.dart'; import 'package:dart_code_metrics/src/models/component_record.dart'; import 'package:dart_code_metrics/src/models/config.dart'; +import 'package:dart_code_metrics/src/models/design_issue.dart'; import 'package:dart_code_metrics/src/models/file_record.dart'; import 'package:dart_code_metrics/src/models/function_record.dart'; import 'package:dart_code_metrics/src/reporters/code_climate/code_climate_reporter.dart'; @@ -27,6 +28,57 @@ void main() { expect(_reporter.report([]), isEmpty); }); + test('file with design issues', () { + const _issuePatternId = 'patternId1'; + const _issuePatternDocumentation = 'https://docu.edu/patternId1.html'; + const _issueLine = 2; + const _issueMessage = 'first issue message'; + const _issueRecomendation = 'issue recomendation'; + + final records = [ + FileRecord( + fullPath: fullPath, + relativePath: 'example.dart', + components: Map.unmodifiable({}), + functions: Map.unmodifiable({}), + issues: const [], + designIssue: [ + DesignIssue( + patternId: _issuePatternId, + patternDocumentation: Uri.parse(_issuePatternDocumentation), + sourceSpan: SourceSpanBase( + SourceLocation(1, + sourceUrl: Uri.parse(fullPath), + line: _issueLine, + column: 3), + SourceLocation(6, sourceUrl: Uri.parse(fullPath)), + 'issue'), + message: 'first issue message', + recommendation: _issueRecomendation, + ), + ], + ), + ]; + + final report = + (json.decode(_reporter.report(records).first) as List).first + as Map; + + expect(report, containsPair('type', 'issue')); + expect(report, containsPair('check_name', _issuePatternId)); + expect(report, containsPair('description', _issueMessage)); + expect(report, containsPair('categories', ['Complexity'])); + expect( + report, + containsPair('location', { + 'path': 'example.dart', + 'lines': {'begin': _issueLine, 'end': _issueLine}, + })); + expect(report, containsPair('remediation_points', 50000)); + expect(report, + containsPair('fingerprint', '8842a666b8aee4f2eae51205e0114dae')); + }); + test('file with style severity issues', () { const _issueRuleId = 'ruleId1'; const _issueRuleDocumentation = 'https://docu.edu/ruleId1.html'; @@ -139,43 +191,6 @@ void main() { }); group('function', () { - test('with long body', () { - final records = [ - FileRecord( - fullPath: fullPath, - relativePath: 'example.dart', - components: Map.unmodifiable({}), - functions: Map.unmodifiable({ - 'function': buildFunctionRecordStub( - linesWithCode: List.generate(150, (index) => index)), - }), - issues: const [], - designIssue: const [], - ), - ]; - - final report = - (json.decode(_reporter.report(records).first) as List).first - as Map; - - expect(report, containsPair('type', 'issue')); - expect(report, containsPair('check_name', 'linesOfExecutableCode')); - expect( - report, - containsPair('description', - 'Function `function` has 150 executable code lines (exceeds 50 allowed). Consider refactoring.')); - expect(report, containsPair('categories', ['Complexity'])); - expect( - report, - containsPair('location', { - 'path': 'example.dart', - 'lines': {'begin': 0, 'end': 0}, - })); - expect(report, containsPair('remediation_points', 50000)); - expect(report, - containsPair('fingerprint', '01bdb88a1141bd18f91bd2c933953436')); - }); - test('with short body', () { final records = [ FileRecord(