|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/diagnostic/diagnostic.dart'; |
| 6 | +import 'package:analyzer/error/error.dart'; |
| 7 | +import 'package:analyzer/error/listener.dart'; |
| 8 | +import 'package:analyzer/source/file_source.dart'; |
| 9 | +import 'package:analyzer/src/lint/linter.dart'; // ignore: implementation_imports |
| 10 | +import 'package:analyzer/src/lint/pub.dart'; // ignore: implementation_imports |
| 11 | +import 'package:analyzer/src/lint/registry.dart'; // ignore: implementation_imports |
| 12 | +import 'package:analyzer_testing/src/analysis_rule/pub_package_resolution.dart'; |
| 13 | +import 'package:analyzer_testing/utilities/utilities.dart'; |
| 14 | +import 'package:meta/meta.dart'; |
| 15 | + |
| 16 | +/// Returns an [ExpectedDiagnostic] with the given arguments. |
| 17 | +/// |
| 18 | +/// Just a short-named helper for use in the `assert*Diagnostics` methods. |
| 19 | +ExpectedDiagnostic error( |
| 20 | + DiagnosticCode code, |
| 21 | + int offset, |
| 22 | + int length, { |
| 23 | + Pattern? messageContains, |
| 24 | +}) => ExpectedError(code, offset, length, messageContains: messageContains); |
| 25 | + |
| 26 | +/// A base class for analysis rule tests that use test_reflective_loader. |
| 27 | +abstract class AnalysisRuleTest extends PubPackageResolutionTest { |
| 28 | + /// The name of the analysis rule which this test is concerned with. |
| 29 | + String get analysisRule; |
| 30 | + |
| 31 | + /// Asserts that no diagnostics are reported when resolving [content]. |
| 32 | + Future<void> assertNoPubspecDiagnostics(String content) async { |
| 33 | + newFile(testPackagePubspecPath, content); |
| 34 | + var errors = await _analyzePubspecFile(content); |
| 35 | + assertDiagnosticsIn(errors, []); |
| 36 | + } |
| 37 | + |
| 38 | + /// Asserts that [expectedDiagnostics] are reported when resolving [content]. |
| 39 | + Future<void> assertPubspecDiagnostics( |
| 40 | + String content, |
| 41 | + List<ExpectedDiagnostic> expectedDiagnostics, |
| 42 | + ) async { |
| 43 | + newFile(testPackagePubspecPath, content); |
| 44 | + var errors = await _analyzePubspecFile(content); |
| 45 | + assertDiagnosticsIn(errors, expectedDiagnostics); |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns an "expected diagnostic" for [analysisRule] (or [name], if given) |
| 49 | + /// at [offset] and [length]. |
| 50 | + /// |
| 51 | + /// If given, [messageContains] is used to match against a diagnostic's |
| 52 | + /// message, and [correctionContains] is used to match against a diagnostic's |
| 53 | + /// correction message. |
| 54 | + ExpectedDiagnostic lint( |
| 55 | + int offset, |
| 56 | + int length, { |
| 57 | + Pattern? messageContains, |
| 58 | + Pattern? correctionContains, |
| 59 | + String? name, |
| 60 | + }) => ExpectedLint( |
| 61 | + name ?? analysisRule, |
| 62 | + offset, |
| 63 | + length, |
| 64 | + messageContains: messageContains, |
| 65 | + correctionContains: correctionContains, |
| 66 | + ); |
| 67 | + |
| 68 | + @mustCallSuper |
| 69 | + @override |
| 70 | + void setUp() { |
| 71 | + if (!Registry.ruleRegistry.any((r) => r.name == analysisRule)) { |
| 72 | + throw Exception("Unrecognized rule: '$analysisRule'"); |
| 73 | + } |
| 74 | + super.setUp(); |
| 75 | + newAnalysisOptionsYamlFile( |
| 76 | + testPackageRootPath, |
| 77 | + analysisOptionsContent(experiments: experiments, rules: [analysisRule]), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + Future<List<Diagnostic>> _analyzePubspecFile(String content) async { |
| 82 | + var path = convertPath(testPackagePubspecPath); |
| 83 | + var pubspecRules = <AbstractAnalysisRule, PubspecVisitor<Object?>>{}; |
| 84 | + var rules = Registry.ruleRegistry.where((r) => analysisRule == r.name); |
| 85 | + for (var rule in rules) { |
| 86 | + var visitor = rule.pubspecVisitor; |
| 87 | + if (visitor != null) { |
| 88 | + pubspecRules[rule] = visitor; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (pubspecRules.isEmpty) { |
| 93 | + throw UnsupportedError( |
| 94 | + 'Resolving pubspec files only supported with rules with ' |
| 95 | + 'PubspecVisitors.', |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + var sourceUri = resourceProvider.pathContext.toUri(path); |
| 100 | + var pubspecAst = Pubspec.parse( |
| 101 | + content, |
| 102 | + sourceUrl: sourceUri, |
| 103 | + resourceProvider: resourceProvider, |
| 104 | + ); |
| 105 | + var listener = RecordingErrorListener(); |
| 106 | + var file = resourceProvider.getFile(path); |
| 107 | + var reporter = ErrorReporter(listener, FileSource(file, sourceUri)); |
| 108 | + for (var entry in pubspecRules.entries) { |
| 109 | + entry.key.reporter = reporter; |
| 110 | + pubspecAst.accept(entry.value); |
| 111 | + } |
| 112 | + return [...listener.errors]; |
| 113 | + } |
| 114 | +} |
0 commit comments