2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- import 'package:analysis_server/src/services/correction/dart/data_driven.dart' ;
6
- import 'package:analysis_server/src/services/correction/fix_internal.dart' ;
7
- import 'package:analyzer/error/error.dart' ;
8
- import 'package:analyzer/file_system/physical_file_system.dart' ;
9
- import 'package:analyzer/src/lint/registry.dart' ;
10
- import 'package:analyzer_utilities/package_root.dart' as package_root;
11
- import 'package:linter/src/rules.dart' ;
12
5
import 'package:test/test.dart' ;
13
6
import 'package:test_reflective_loader/test_reflective_loader.dart' ;
14
- import 'package:yaml/yaml.dart' ;
7
+
8
+ import '../tool/presubmit/verify_error_fix_status.dart' ;
15
9
16
10
void main () {
17
11
defineReflectiveSuite (() {
@@ -21,192 +15,10 @@ void main() {
21
15
22
16
@reflectiveTest
23
17
class VerifyErrorFixStatusTest {
24
- PhysicalResourceProvider resourceProvider = PhysicalResourceProvider .INSTANCE ;
25
-
26
18
void test_statusFile () {
27
- var statusInfo = _statusInfo ();
28
- var errorCodeNames = _errorCodeNames ();
29
- var lintRuleNames = _lintRuleNames ();
30
-
31
- var errorData = _ErrorData ();
32
- for (var code in errorCodeValues) {
33
- var name = code.uniqueName;
34
- if (name.startsWith ('TodoCode.' )) {
35
- // To-do codes are ignored.
36
- } else {
37
- var info = statusInfo.nodes[name];
38
- if (info == null ) {
39
- errorData.codesWithNoEntry.add (name);
40
- } else if (info is YamlMap ) {
41
- var markedAsHavingFix = info['status' ] == 'hasFix' ;
42
- var hasFix = _hasCodeFix (code);
43
- if (hasFix) {
44
- if (! markedAsHavingFix) {
45
- errorData.codesWithFixes.add (name);
46
- }
47
- } else {
48
- if (markedAsHavingFix) {
49
- errorData.codesWithoutFixes.add (name);
50
- }
51
- }
52
- }
53
- }
54
- }
55
- for (var name in lintRuleNames) {
56
- var info = statusInfo.nodes[name];
57
- if (info == null ) {
58
- errorData.codesWithNoEntry.add (name);
59
- } else if (info is YamlMap ) {
60
- var markedAsHavingFix = info['status' ] == 'hasFix' ;
61
- var hasFix = _hasLintFix (name);
62
- if (hasFix) {
63
- if (! markedAsHavingFix) {
64
- errorData.codesWithFixes.add (name);
65
- }
66
- } else {
67
- if (markedAsHavingFix) {
68
- errorData.codesWithoutFixes.add (name);
69
- }
70
- }
71
- }
72
- }
73
-
74
- for (var key in statusInfo.keys) {
75
- if (key is String ) {
76
- if (! errorCodeNames.contains (key) && ! lintRuleNames.contains (key)) {
77
- errorData.entriesWithNoCode.add (key);
78
- }
79
- }
80
- }
81
-
82
- if (errorData.isNotEmpty) {
83
- fail (_failureMessage (errorData));
84
- }
85
- }
86
-
87
- /// Return the unique names of the error codes.
88
- Set <String > _errorCodeNames () {
89
- var codes = errorCodeValues;
90
- var codeNames = < String > {};
91
- for (var code in codes) {
92
- codeNames.add (code.uniqueName);
93
- }
94
- return codeNames;
95
- }
96
-
97
- /// Return a failure message composed from the given lists.
98
- String _failureMessage (_ErrorData errorData) {
99
- var buffer = StringBuffer ('In ${_statusFilePath ()}:\n ' );
100
- var needsBlankLine = false ;
101
- if (errorData.codesWithNoEntry.isNotEmpty) {
102
- buffer.writeln ('Add the following entries:' );
103
- buffer.writeln ();
104
- for (var code in errorData.codesWithNoEntry) {
105
- buffer.writeln ('$code :' );
106
- buffer.writeln (' status: needsEvaluation' );
107
- }
108
- needsBlankLine = true ;
109
- }
110
- if (errorData.entriesWithNoCode.isNotEmpty) {
111
- if (needsBlankLine) {
112
- buffer.writeln ();
113
- }
114
- buffer.writeln ('Remove the following entries:' );
115
- for (var code in errorData.entriesWithNoCode) {
116
- buffer.writeln ('- $code ' );
117
- }
118
- needsBlankLine = true ;
119
- }
120
- if (errorData.codesWithFixes.isNotEmpty) {
121
- if (needsBlankLine) {
122
- buffer.writeln ();
123
- }
124
- buffer.writeln ('Mark the following entries as having fixes:' );
125
- for (var code in errorData.codesWithFixes) {
126
- buffer.writeln ('- $code ' );
127
- }
128
- needsBlankLine = true ;
129
- }
130
- if (errorData.codesWithoutFixes.isNotEmpty) {
131
- if (needsBlankLine) {
132
- buffer.writeln ();
133
- }
134
- buffer.writeln ('Mark the following entries as not having fixes:' );
135
- for (var code in errorData.codesWithoutFixes) {
136
- buffer.writeln ('- $code ' );
137
- }
138
- needsBlankLine = true ;
19
+ var errors = verifyErrorFixStatus ();
20
+ if (errors != null ) {
21
+ fail (errors);
139
22
}
140
- return buffer.toString ();
141
23
}
142
-
143
- /// Return `true` if the given error [code] has a fix associated with it.
144
- bool _hasCodeFix (ErrorCode code) {
145
- var producers = FixProcessor .nonLintProducerMap[code];
146
- if (producers != null ) {
147
- return true ;
148
- }
149
- var multiProducers = FixProcessor .nonLintMultiProducerMap[code];
150
- if (multiProducers != null ) {
151
- for (var producer in multiProducers) {
152
- if (producer is ! DataDriven ) {
153
- return true ;
154
- }
155
- }
156
- }
157
- return false ;
158
- }
159
-
160
- /// Return `true` if the lint with the given name has a fix associated with
161
- /// it.
162
- bool _hasLintFix (String codeName) {
163
- var name = codeName.substring ('LintCode.' .length);
164
- var producers = FixProcessor .lintProducerMap[name];
165
- return producers != null ;
166
- }
167
-
168
- /// Return the unique names of the lint rules.
169
- Set <String > _lintRuleNames () {
170
- registerLintRules ();
171
- var ruleNames = < String > {};
172
- for (var rule in Registry .ruleRegistry.rules) {
173
- for (var code in rule.lintCodes) {
174
- ruleNames.add (code.uniqueName);
175
- }
176
- }
177
- return ruleNames;
178
- }
179
-
180
- /// Return the path to the file containing the status information.
181
- String _statusFilePath () {
182
- var pathContext = resourceProvider.pathContext;
183
- var packageRoot = pathContext.normalize (package_root.packageRoot);
184
- return pathContext.join (packageRoot, 'analysis_server' , 'lib' , 'src' ,
185
- 'services' , 'correction' , 'error_fix_status.yaml' );
186
- }
187
-
188
- /// Return the content of the file containing the status information, parsed
189
- /// as a YAML map.
190
- YamlMap _statusInfo () {
191
- var statusFile = resourceProvider.getFile (_statusFilePath ());
192
- var document = loadYamlDocument (statusFile.readAsStringSync ());
193
- var statusInfo = document.contents;
194
- if (statusInfo is ! YamlMap ) {
195
- fail ('Expected a YamlMap, found ${statusInfo .runtimeType }' );
196
- }
197
- return statusInfo;
198
- }
199
- }
200
-
201
- class _ErrorData {
202
- final List <String > codesWithFixes = [];
203
- final List <String > codesWithNoEntry = [];
204
- final List <String > codesWithoutFixes = [];
205
- final List <String > entriesWithNoCode = [];
206
-
207
- bool get isNotEmpty =>
208
- codesWithFixes.isNotEmpty ||
209
- codesWithNoEntry.isNotEmpty ||
210
- codesWithoutFixes.isNotEmpty ||
211
- entriesWithNoCode.isNotEmpty;
212
24
}
0 commit comments