Skip to content

Commit c87b185

Browse files
pqCommit Queue
authored andcommitted
[CQ] rationalize/unify test lint registration
Eliminates duplication and promotes hermetic tests (some were registering and failing to unregister test lints). Bug: #60036 Change-Id: I9d0fe5fd610b51ce9000decbf75da2338ed87737 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408320 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent a78fb5e commit c87b185

File tree

11 files changed

+115
-59
lines changed

11 files changed

+115
-59
lines changed

pkg/analysis_server/test/src/services/completion/yaml/analysis_options_generator_test.dart

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import 'package:analysis_server/src/services/completion/yaml/analysis_options_generator.dart';
66
import 'package:analyzer/src/dart/error/lint_codes.dart';
77
import 'package:analyzer/src/lint/linter.dart';
8-
import 'package:analyzer/src/lint/registry.dart';
98
import 'package:analyzer/src/task/options.dart';
9+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
1010
import 'package:linter/src/rules.dart';
1111
import 'package:test/test.dart';
1212
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -20,30 +20,21 @@ void main() {
2020
}
2121

2222
@reflectiveTest
23-
class AnalysisOptionsGeneratorTest extends YamlGeneratorTest {
24-
// Keep track of any added rules so they can be unregistered in `tearDown`.
25-
var addedRules = <LintRule>[];
26-
23+
class AnalysisOptionsGeneratorTest extends YamlGeneratorTest
24+
with LintRegistrationMixin {
2725
@override
2826
String get fileName => 'analysis_options.yaml';
2927

3028
@override
3129
AnalysisOptionsGenerator get generator =>
3230
AnalysisOptionsGenerator(resourceProvider);
3331

34-
void registerRule(LintRule rule) {
35-
addedRules.add(rule);
36-
Registry.ruleRegistry.registerLintRule(rule);
37-
}
38-
3932
void setUp() {
4033
registerLintRules();
4134
}
4235

4336
void tearDown() {
44-
for (var rule in addedRules) {
45-
Registry.ruleRegistry.unregisterLintRule(rule);
46-
}
37+
unregisterLintRules();
4738
}
4839

4940
void test_analyzer() {
@@ -161,7 +152,7 @@ linter:
161152
}
162153

163154
void test_linter_rules_internal() {
164-
registerRule(InternalRule());
155+
registerLintRule(InternalRule());
165156

166157
getCompletions('''
167158
linter:
@@ -236,7 +227,7 @@ linter:
236227
}
237228

238229
void test_linter_rules_removed() {
239-
registerRule(_RemovedLint());
230+
registerLintRule(_RemovedLint());
240231

241232
getCompletions('''
242233
linter:

pkg/analysis_server/test/src/services/correction/fix/analysis_options/remove_lint_test.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/src/dart/error/lint_codes.dart';
66
import 'package:analyzer/src/lint/linter.dart';
7-
import 'package:analyzer/src/lint/registry.dart';
7+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
88
import 'package:linter/src/rules.dart';
99
import 'package:test_reflective_loader/test_reflective_loader.dart';
1010

@@ -49,20 +49,19 @@ class RemovedRule extends LintRule {
4949
}
5050

5151
@reflectiveTest
52-
class RemoveLintTest extends AnalysisOptionsFixTest {
52+
class RemoveLintTest extends AnalysisOptionsFixTest with LintRegistrationMixin {
5353
// Keep track of these rules so they can be unregistered in `tearDown`.
5454
var deprecatedRule = DeprecatedRule();
5555
var removedRule = RemovedRule();
5656

5757
void setUp() {
5858
registerLintRules();
59-
Registry.ruleRegistry.registerLintRule(deprecatedRule);
60-
Registry.ruleRegistry.registerLintRule(removedRule);
59+
registerLintRule(deprecatedRule);
60+
registerLintRule(removedRule);
6161
}
6262

6363
void tearDown() {
64-
Registry.ruleRegistry.unregisterLintRule(deprecatedRule);
65-
Registry.ruleRegistry.unregisterLintRule(removedRule);
64+
unregisterLintRules();
6665
}
6766

6867
Future<void> test_deprecated() async {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2025, rights reserved. Use of this source code is governed by a
2+
// BSD-style license that can be found in the LICENSE file.
3+
4+
import 'package:analyzer/src/lint/linter.dart';
5+
import 'package:analyzer/src/lint/registry.dart';
6+
7+
/// A mixin for test classes that interact with the global lint
8+
/// [Registry.ruleRegistry].
9+
mixin LintRegistrationMixin {
10+
final _registeredRules = <LintRule>{};
11+
12+
/// Register the given [rule] with the global lint [Registry.ruleRegistry],
13+
/// caching it so that it can be unregistered by a call to [unregisterLintRules].
14+
void registerLintRule(LintRule rule) {
15+
_registeredRules.add(rule);
16+
Registry.ruleRegistry.registerLintRule(rule);
17+
}
18+
19+
/// Register the given [rules] with the global lint [Registry.ruleRegistry],
20+
/// caching each so that it can be unregistered by a call to [unregisterLintRules].
21+
void registerLintRules(List<LintRule> rules) {
22+
rules.forEach(registerLintRule);
23+
}
24+
25+
/// Unregister all rules added by [registerLintRule].
26+
void unregisterLintRules() {
27+
_registeredRules.forEach(Registry.ruleRegistry.unregisterLintRule);
28+
_registeredRules.clear();
29+
}
30+
}

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'package:analyzer/src/dart/analysis/file_state.dart';
1717
import 'package:analyzer/src/dart/analysis/status.dart';
1818
import 'package:analyzer/src/error/codes.dart';
1919
import 'package:analyzer/src/lint/linter.dart';
20-
import 'package:analyzer/src/lint/registry.dart';
20+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
2121
import 'package:analyzer/src/utilities/extensions/async.dart';
2222
import 'package:analyzer_utilities/testing/tree_string_sink.dart';
2323
import 'package:linter/src/rules.dart';
@@ -91,21 +91,22 @@ import '$innerUri';
9191
}
9292

9393
@reflectiveTest
94-
class AnalysisDriver_LintTest extends PubPackageResolutionTest {
94+
class AnalysisDriver_LintTest extends PubPackageResolutionTest
95+
with LintRegistrationMixin {
9596
@override
9697
void setUp() {
9798
super.setUp();
9899

99100
useEmptyByteStore();
100-
Registry.ruleRegistry.registerLintRule(_AlwaysReportedLint.instance);
101+
registerLintRule(_AlwaysReportedLint.instance);
101102
writeTestPackageAnalysisOptionsFile(analysisOptionsContent(
102103
rules: [_AlwaysReportedLint.code.name],
103104
));
104105
}
105106

106107
@override
107108
Future<void> tearDown() {
108-
Registry.ruleRegistry.unregisterLintRule(_AlwaysReportedLint.instance);
109+
unregisterLintRules();
109110
return super.tearDown();
110111
}
111112

pkg/analyzer/test/src/diagnostics/analysis_options/analysis_options_test_support.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import 'package:analyzer/src/context/source.dart';
77
import 'package:analyzer/src/file_system/file_system.dart';
88
import 'package:analyzer/src/generated/source.dart';
99
import 'package:analyzer/src/task/options.dart';
10+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
1011
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
12+
import 'package:meta/meta.dart';
1113
import 'package:pub_semver/pub_semver.dart';
1214

1315
import '../../../generated/test_support.dart';
1416

15-
abstract class AbstractAnalysisOptionsTest with ResourceProviderMixin {
17+
abstract class AbstractAnalysisOptionsTest
18+
with ResourceProviderMixin, LintRegistrationMixin {
1619
late SourceFactory sourceFactory;
1720

1821
VersionConstraint? get sdkVersionConstraint => null;
@@ -49,4 +52,9 @@ abstract class AbstractAnalysisOptionsTest with ResourceProviderMixin {
4952
var resolvers = [ResourceUriResolver(resourceProvider)];
5053
sourceFactory = SourceFactoryImpl(resolvers);
5154
}
55+
56+
@mustCallSuper
57+
void tearDown() {
58+
unregisterLintRules();
59+
}
5260
}

pkg/analyzer/test/src/diagnostics/removed_lint_use_test.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/src/error/codes.g.dart';
66
import 'package:analyzer/src/lint/linter.dart';
7-
import 'package:analyzer/src/lint/registry.dart';
7+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
88
import 'package:test_reflective_loader/test_reflective_loader.dart';
99

1010
import '../dart/resolution/context_collection_resolution.dart';
@@ -25,11 +25,18 @@ class RemovedLint extends LintRule {
2525
}
2626

2727
@reflectiveTest
28-
class ReplacedLintUseTest extends PubPackageResolutionTest {
28+
class ReplacedLintUseTest extends PubPackageResolutionTest
29+
with LintRegistrationMixin {
2930
@override
3031
void setUp() {
3132
super.setUp();
32-
Registry.ruleRegistry.registerLintRule(RemovedLint());
33+
registerLintRule(RemovedLint());
34+
}
35+
36+
@override
37+
Future<void> tearDown() {
38+
unregisterLintRules();
39+
return super.tearDown();
3340
}
3441

3542
@FailingTest(

pkg/analyzer/test/src/diagnostics/replaced_lint_use_test.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/src/error/codes.g.dart';
66
import 'package:analyzer/src/lint/linter.dart';
7-
import 'package:analyzer/src/lint/registry.dart';
7+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
88
import 'package:test_reflective_loader/test_reflective_loader.dart';
99

1010
import '../dart/resolution/context_collection_resolution.dart';
@@ -25,12 +25,19 @@ class RemovedLint extends LintRule {
2525
}
2626

2727
@reflectiveTest
28-
class ReplacedLintUseTest extends PubPackageResolutionTest {
28+
class ReplacedLintUseTest extends PubPackageResolutionTest
29+
with LintRegistrationMixin {
2930
@override
3031
void setUp() {
3132
super.setUp();
32-
Registry.ruleRegistry.registerLintRule(RemovedLint());
33-
Registry.ruleRegistry.registerLintRule(ReplacingLint());
33+
registerLintRule(RemovedLint());
34+
registerLintRule(ReplacingLint());
35+
}
36+
37+
@override
38+
Future<void> tearDown() {
39+
unregisterLintRules();
40+
return super.tearDown();
3441
}
3542

3643
@FailingTest(

pkg/analyzer/test/src/diagnostics/unignorable_ignore_test.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/src/error/codes.dart';
88
import 'package:analyzer/src/lint/linter.dart';
9-
import 'package:analyzer/src/lint/registry.dart';
9+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
1010
import 'package:test_reflective_loader/test_reflective_loader.dart';
1111

1212
import '../dart/resolution/context_collection_resolution.dart';
@@ -18,7 +18,14 @@ main() {
1818
}
1919

2020
@reflectiveTest
21-
class UnignorableIgnoreTest extends PubPackageResolutionTest {
21+
class UnignorableIgnoreTest extends PubPackageResolutionTest
22+
with LintRegistrationMixin {
23+
@override
24+
Future<void> tearDown() {
25+
unregisterLintRules();
26+
return super.tearDown();
27+
}
28+
2229
test_file_lowerCase() async {
2330
writeTestPackageAnalysisOptionsFile(
2431
analysisOptionsContent(unignorableNames: ['undefined_annotation']),
@@ -63,7 +70,7 @@ class UnignorableIgnoreTest extends PubPackageResolutionTest {
6370
),
6471
);
6572
var avoidIntRule = _AvoidIntRule();
66-
Registry.ruleRegistry.registerLintRule(avoidIntRule);
73+
registerLintRule(avoidIntRule);
6774
await assertErrorsInCode(r'''
6875
// ignore: avoid_int
6976
int a = 0;

pkg/analyzer/test/src/options/options_provider_test.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:analyzer/src/dart/analysis/analysis_options.dart';
1010
import 'package:analyzer/src/file_system/file_system.dart';
1111
import 'package:analyzer/src/generated/source.dart';
1212
import 'package:analyzer/src/lint/linter.dart';
13-
import 'package:analyzer/src/lint/registry.dart';
13+
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
1414
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1515
import 'package:test/test.dart';
1616
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -43,7 +43,7 @@ class ErrorProcessorMatcher extends Matcher {
4343
}
4444

4545
@reflectiveTest
46-
class OptionsProviderTest with ResourceProviderMixin {
46+
class OptionsProviderTest with ResourceProviderMixin, LintRegistrationMixin {
4747
late final SourceFactory sourceFactory;
4848

4949
late final AnalysisOptionsProvider provider;
@@ -55,6 +55,10 @@ class OptionsProviderTest with ResourceProviderMixin {
5555
provider = AnalysisOptionsProvider(sourceFactory);
5656
}
5757

58+
void tearDown() {
59+
unregisterLintRules();
60+
}
61+
5862
test_chooseFirstLegacyPlugin() {
5963
newFile('/more_options.yaml', '''
6064
analyzer:
@@ -298,8 +302,7 @@ linter:
298302

299303
var lowLevelLint = TestRule.withName('low_level_lint');
300304
var topLevelLint = TestRule.withName('top_level_lint');
301-
Registry.ruleRegistry.registerLintRule(lowLevelLint);
302-
Registry.ruleRegistry.registerLintRule(topLevelLint);
305+
registerLintRules([lowLevelLint, topLevelLint]);
303306
var options = _getOptionsObject('/');
304307

305308
expect(options.lintRules, unorderedEquals([topLevelLint, lowLevelLint]));
@@ -320,8 +323,7 @@ linter:
320323

321324
var lowLevelLint = TestRule.withName('low_level_lint');
322325
var topLevelLint = TestRule.withName('top_level_lint');
323-
Registry.ruleRegistry.registerLintRule(lowLevelLint);
324-
Registry.ruleRegistry.registerLintRule(topLevelLint);
326+
registerLintRules([lowLevelLint, topLevelLint]);
325327
var options = _getOptionsObject('/');
326328

327329
expect(options.lintRules, unorderedEquals([topLevelLint, lowLevelLint]));
@@ -341,7 +343,7 @@ linter:
341343
''');
342344

343345
var topLevelLint = TestRule.withName('top_level_lint');
344-
Registry.ruleRegistry.registerLintRule(topLevelLint);
346+
registerLintRule(topLevelLint);
345347
var options = _getOptionsObject('/');
346348

347349
expect(options.lintRules, isNot(contains(topLevelLint)));

pkg/analyzer/test/src/options/options_rule_validator_test.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analyzer/error/listener.dart';
77
import 'package:analyzer/src/analysis_options/error/option_codes.dart';
88
import 'package:analyzer/src/lint/linter.dart';
99
import 'package:analyzer/src/lint/options_rule_validator.dart';
10-
import 'package:analyzer/src/lint/registry.dart';
1110
import 'package:analyzer/src/string_source.dart';
1211
import 'package:pub_semver/pub_semver.dart';
1312
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -262,16 +261,17 @@ mixin OptionsRuleValidatorTestMixin on AbstractAnalysisOptionsTest {
262261

263262
@override
264263
void setUp() {
265-
Registry.ruleRegistry
266-
..registerLintRule(DeprecatedLint())
267-
..registerLintRule(DeprecatedSince3Lint())
268-
..registerLintRule(DeprecatedLintWithReplacement())
269-
..registerLintRule(StableLint())
270-
..registerLintRule(RuleNeg())
271-
..registerLintRule(RulePos())
272-
..registerLintRule(RemovedIn2_12Lint())
273-
..registerLintRule(ReplacedLint())
274-
..registerLintRule(ReplacingLint());
264+
registerLintRules([
265+
DeprecatedLint(),
266+
DeprecatedSince3Lint(),
267+
DeprecatedLintWithReplacement(),
268+
StableLint(),
269+
RuleNeg(),
270+
RulePos(),
271+
RemovedIn2_12Lint(),
272+
ReplacedLint(),
273+
ReplacingLint()
274+
]);
275275
super.setUp();
276276
}
277277
}

0 commit comments

Comments
 (0)