Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
feat: add cli options for fatal exit if unused files or l10n are found
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrutskikh committed Dec 4, 2021
1 parent f35888e commit db4df25
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* feat: add cli options for fatal exit if unused files or l10n are found

## 4.8.0

* feat: add alphabetical sorting by type for `member-ordering-extended` rule.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/analyzers/lint_analyzer/lint_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class LintAnalyzer {
violations:
metricViolations(records, SourceLinesOfCodeMetric.metricId),
),
SummaryLintReportRecord<String>(
title: 'Total tech debt',
value: totalTechDebt(records),
),
];

LintFileReport? _analyzeFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class TechnicalDebtMetric extends FileMetric<double> {
_deprecatedAnnotationsCost = readConfigValue<double>(
config,
metricId,
'deprecated-annotations',
'deprecated-annotations-cost',
) ??
0.0,
_fileNullSafetyMigrationCost = readConfigValue<double>(
config,
metricId,
'file-nullsafety-migration',
'file-nullsafety-migration-cost',
) ??
0.0,
_unitType = readConfigValue<String>(config, metricId, 'unit-type'),
Expand Down
22 changes: 22 additions & 0 deletions lib/src/analyzers/lint_analyzer/utils/report_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:path/path.dart' as p;
import '../metrics/metric_utils.dart';
import '../metrics/metrics_list/cyclomatic_complexity/cyclomatic_complexity_metric.dart';
import '../metrics/metrics_list/source_lines_of_code/source_lines_of_code_metric.dart';
import '../metrics/metrics_list/technical_debt/technical_debt_metric.dart';
import '../metrics/models/metric_value_level.dart';
import '../models/lint_file_report.dart';
import '../models/severity.dart';
Expand Down Expand Up @@ -47,6 +48,27 @@ int totalSLOC(Iterable<LintFileReport> records) => records.fold(
),
);

String totalTechDebt(Iterable<LintFileReport> records) {
final debtValue = records.fold<num>(
0,
(prevValue, fileReport) =>
prevValue +
(fileReport.file.metric(TechnicalDebtMetric.metricId)?.value ?? 0),
);

final debtUnitType = records
.firstWhereOrNull(
(record) =>
record.file.metric(TechnicalDebtMetric.metricId) != null,
)
?.file
.metric(TechnicalDebtMetric.metricId)
?.unitType ??
'';

return debtValue > 0 ? '$debtValue $debtUnitType'.trim() : 'not found';
}

int totalClasses(Iterable<LintFileReport> records) => records.fold(
0,
(prevValue, fileReport) => prevValue + fileReport.classes.keys.length,
Expand Down
17 changes: 17 additions & 0 deletions lib/src/cli/commands/check_unused_files_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ class CheckUnusedFilesCommand extends BaseCommand {
output: stdout,
)
?.report(unusedFilesResult);

if (unusedFilesResult.isNotEmpty &&
(argResults[FlagNames.fatalOnUnused] as bool)) {
exit(1);
}
}

void _addFlags() {
_usesReporterOption();
addCommonFlags();
_usesExitOption();
}

void _usesReporterOption() {
Expand All @@ -68,4 +74,15 @@ class CheckUnusedFilesCommand extends BaseCommand {
defaultsTo: FlagNames.consoleReporter,
);
}

void _usesExitOption() {
argParser
..addSeparator('')
..addFlag(
FlagNames.fatalOnUnused,
help: 'Treat find unused file as fatal.',
// TODO(dkrutrkikh): activate on next major version
// defaultsTo: true,
);
}
}
19 changes: 18 additions & 1 deletion lib/src/cli/commands/check_unused_l10n_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ class CheckUnusedL10nCommand extends BaseCommand {
sdkPath: findSdkPath(),
);

return _analyzer
await _analyzer
.getReporter(
name: reporterName,
output: stdout,
)
?.report(unusedL10nResult);

if (unusedL10nResult.isNotEmpty &&
(argResults[FlagNames.fatalOnUnused] as bool)) {
exit(1);
}
}

void _addFlags() {
_usesL10nClassPatternOption();
_usesReporterOption();
addCommonFlags();
_usesExitOption();
}

void _usesReporterOption() {
Expand Down Expand Up @@ -86,4 +92,15 @@ class CheckUnusedL10nCommand extends BaseCommand {
defaultsTo: r'I18n$',
);
}

void _usesExitOption() {
argParser
..addSeparator('')
..addFlag(
FlagNames.fatalOnUnused,
help: 'Treat find unused l10n as fatal.',
// TODO(dkrutrkikh): activate on next major version
// defaultsTo: true,
);
}
}
2 changes: 2 additions & 0 deletions lib/src/cli/models/flag_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ class FlagNames {
static const fatalWarnings = 'fatal-warnings';

static const l10nClassPattern = 'class-pattern';

static const fatalOnUnused = 'fatal-unused';
}
17 changes: 16 additions & 1 deletion test/src/analyzers/lint_analyzer/lint_analyzer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,25 @@ void main() {
result.firstWhere((r) => r.title == 'Total scanned files').value,
isZero,
);

expect(
result.firstWhere((r) => r.title == 'Total tech debt').value,
equals('not found'),
);
});

test('collect summary for passed report', () {
final result = analyzer.getSummary([
LintFileReport(
path: '/home/dev/project/bin/example.dart',
relativePath: 'bin/example.dart',
file: buildReportStub(),
file: buildReportStub(metrics: [
buildMetricValueStub(
id: 'technical-debt',
value: 10,
unitType: 'USD',
),
]),
classes: Map.unmodifiable(<String, Report>{}),
functions: Map.unmodifiable(<String, Report>{}),
issues: const [],
Expand All @@ -197,6 +208,10 @@ void main() {
result.firstWhere((r) => r.title == 'Total scanned files').value,
equals(2),
);
expect(
result.firstWhere((r) => r.title == 'Total tech debt').value,
equals('10 USD'),
);
});
},
testOn: 'posix',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Future<void> main() async {
'ignore-cost': 320,
'ignore-for-file-cost': 396,
'as-dynamic-cost': 322,
'deprecated-annotations': 37,
'file-nullsafety-migration': 41,
'deprecated-annotations-cost': 37,
'file-nullsafety-migration-cost': 41,
},
},
);
Expand Down
3 changes: 3 additions & 0 deletions test/src/cli/commands/check_unused_files_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const _usage = 'Check unused *.dart files.\n'
' --exclude=<{/**.g.dart,/**.template.dart}> File paths in Glob syntax to be exclude.\n'
' (defaults to "{/**.g.dart,/**.template.dart}")\n'
'\n'
'\n'
' --[no-]fatal-unused Treat find unused file as fatal.\n'
'\n'
'Run "metrics help" to see global options.';

void main() {
Expand Down
3 changes: 3 additions & 0 deletions test/src/cli/commands/check_unused_l10n_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const _usage = 'Check unused localization in *.dart files.\n'
' --exclude=<{/**.g.dart,/**.template.dart}> File paths in Glob syntax to be exclude.\n'
' (defaults to "{/**.g.dart,/**.template.dart}")\n'
'\n'
'\n'
' --[no-]fatal-unused Treat find unused l10n as fatal.\n'
'\n'
'Run "metrics help" to see global options.';

void main() {
Expand Down

0 comments on commit db4df25

Please sign in to comment.