From 4daa1ed20635582575bc2324143b97e904724400 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Fri, 24 Sep 2021 19:24:39 +0000 Subject: [PATCH] Add a test to ensure that a single diagnostic never has multiple bulk appliable fixes This includes an allow list for the diagnostics that we're already approved, and we can add more as needed, but I don't want us to have multiple bulk fixes without first confirming that they are mutually exclusive. This should help prevent an oversight. Change-Id: I73c4a52e36005f256b416372bfbf8bf7db38400e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214424 Reviewed-by: Phil Quitslund Commit-Queue: Brian Wilkerson --- .../fix/fix_processor_map_test.dart | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart b/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart index 6e27913c31a..83e66a2bfef 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart @@ -15,12 +15,20 @@ void main() { @reflectiveTest class FixProcessorMapTest { + static const List lintsAllowedToHaveMultipleBulkFixes = [ + 'avoid_types_on_closure_parameters', + 'empty_statements', + 'prefer_collection_literals', + 'prefer_inlined_adds', + ]; + void test_lintProducerMap() { - _testMap(FixProcessor.lintProducerMap.values); + _assertMap(FixProcessor.lintProducerMap.entries, + lintsAllowedToHaveMultipleBulkFixes); } void test_nonLintProducerMap() { - _testMap(FixProcessor.nonLintProducerMap.values); + _assertMap(FixProcessor.nonLintProducerMap.entries); } void test_registerFixForLint() { @@ -34,8 +42,36 @@ class FixProcessorMapTest { FixProcessor.lintProducerMap.remove(lintName); } - void _testGenerator(ProducerGenerator generator) { - var producer = generator(); + void _assertMap(Iterable>> entries, + [List keysAllowedToHaveMultipleBulkFixes = const []]) { + var list = []; + for (var entry in entries) { + var bulkCount = 0; + for (var generator in entry.value) { + var producer = generator(); + _assertValidProducer(producer); + if (producer.canBeAppliedInBulk) { + bulkCount++; + } + } + if (bulkCount > 1) { + var key = entry.key.toString(); + if (!keysAllowedToHaveMultipleBulkFixes.contains(key)) { + list.add(key); + } + } + } + if (list.isNotEmpty) { + var buffer = StringBuffer(); + buffer.writeln('Multiple bulk fixes for'); + for (var code in list) { + buffer.writeln('- $code'); + } + fail(buffer.toString()); + } + } + + void _assertValidProducer(CorrectionProducer producer) { var className = producer.runtimeType.toString(); expect(producer.fixKind, isNotNull, reason: '$className.fixKind'); if (producer.canBeAppliedToFile) { @@ -43,14 +79,6 @@ class FixProcessorMapTest { reason: '$className.multiFixKind'); } } - - void _testMap(Iterable> values) { - for (var generators in values) { - for (var generator in generators) { - _testGenerator(generator); - } - } - } } class MockCorrectionProducer implements CorrectionProducer {