@@ -88,10 +88,12 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
88
88
if (! dir.existsSync ()) {
89
89
usageException ("Directory doesn't exist: ${dir .path }" );
90
90
}
91
+ dir = io.Directory (path.canonicalize (path.normalize (dir.absolute.path)));
92
+ var dirPath = dir.path;
91
93
92
94
var modeText = dryRun ? ' (dry run)' : '' ;
93
95
94
- final projectName = path.basename (path. canonicalize (dir.path) );
96
+ final projectName = path.basename (dirPath );
95
97
var progress = log.progress (
96
98
'Computing fixes in ${log .ansi .emphasized (projectName )}$modeText ' );
97
99
@@ -111,24 +113,24 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
111
113
}
112
114
});
113
115
114
- fixes = await server.requestBulkFixes (dir.absolute.path );
116
+ fixes = await server.requestBulkFixes (dirPath );
115
117
final List <SourceFileEdit > edits = fixes.edits;
116
118
117
119
await server.shutdown ();
118
120
119
121
progress.finish (showTiming: true );
120
122
121
123
if (testMode) {
122
- if ( _compareFixes ( edits)) {
123
- return 1 ;
124
- }
124
+ var result = _compareFixesInDirectory (dir, edits);
125
+ log. stdout ( 'Passed: ${ result . passCount }, Failed: ${ result . failCount }' ) ;
126
+ return result.failCount > 0 ? 1 : 0 ;
125
127
} else if (edits.isEmpty) {
126
128
log.stdout ('Nothing to fix!' );
127
129
} else {
128
130
var details = fixes.details;
129
131
details.sort ((f1, f2) => path
130
- .relative (f1.path, from: dir.path )
131
- .compareTo (path.relative (f2.path, from: dir.path )));
132
+ .relative (f1.path, from: dirPath )
133
+ .compareTo (path.relative (f2.path, from: dirPath )));
132
134
133
135
var fileCount = 0 ;
134
136
var fixCount = 0 ;
@@ -170,32 +172,83 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
170
172
171
173
/// Return `true` if any of the fixes fail to create the same content as is
172
174
/// found in the golden file.
173
- bool _compareFixes (List <SourceFileEdit > edits) {
174
- var passCount = 0 ;
175
- var failCount = 0 ;
175
+ _TestResult _compareFixesInDirectory (
176
+ io.Directory directory, List <SourceFileEdit > edits) {
177
+ var result = _TestResult ();
178
+ //
179
+ // Gather the files of interest in this directory and process
180
+ // subdirectories.
181
+ //
182
+ var dartFiles = < io.File > [];
183
+ var expectFileMap = < String , io.File > {};
184
+ for (var child in directory.listSync ()) {
185
+ if (child is io.Directory ) {
186
+ var childResult = _compareFixesInDirectory (child, edits);
187
+ result.passCount += childResult.passCount;
188
+ result.failCount += childResult.failCount;
189
+ } else if (child is io.File ) {
190
+ var name = child.name;
191
+ if (name.endsWith ('.dart' )) {
192
+ dartFiles.add (child);
193
+ } else if (name.endsWith ('.expect' )) {
194
+ expectFileMap[child.path] = child;
195
+ }
196
+ }
197
+ }
198
+ var editMap = < String , SourceFileEdit > {};
176
199
for (var edit in edits) {
177
- var filePath = edit.file;
200
+ editMap[edit.file] = edit;
201
+ }
202
+ for (var originalFile in dartFiles) {
203
+ var filePath = originalFile.path;
178
204
var baseName = path.basename (filePath);
179
205
var expectFileName = baseName + '.expect' ;
180
206
var expectFilePath = path.join (path.dirname (filePath), expectFileName);
207
+ var expectFile = expectFileMap.remove (expectFilePath);
208
+ if (expectFile == null ) {
209
+ result.failCount++ ;
210
+ log.stdout (
211
+ 'No corresponding expect file for the Dart file at "$filePath ".' );
212
+ continue ;
213
+ }
214
+ var edit = editMap[filePath];
181
215
try {
182
- var originalCode = io.File (filePath).readAsStringSync ();
183
- var expectedCode = io.File (expectFilePath).readAsStringSync ();
184
- var actualCode = SourceEdit .applySequence (originalCode, edit.edits);
185
- if (actualCode != expectedCode) {
186
- failCount++ ;
216
+ var originalCode = originalFile.readAsStringSync ();
217
+ var expectedCode = expectFile.readAsStringSync ();
218
+ var actualCode = edit == null
219
+ ? originalCode
220
+ : SourceEdit .applySequence (originalCode, edit.edits);
221
+ // Use a whitespace insensitive comparison.
222
+ if (_compressWhitespace (actualCode) !=
223
+ _compressWhitespace (expectedCode)) {
224
+ result.failCount++ ;
187
225
_reportFailure (filePath, actualCode, expectedCode);
226
+ _printEdits (edits);
188
227
} else {
189
- passCount++ ;
228
+ result. passCount++ ;
190
229
}
191
230
} on io.FileSystemException {
192
- // Ignored for now.
231
+ result.failCount++ ;
232
+ log.stdout ('Failed to process "$filePath ".' );
233
+ log.stdout (
234
+ ' Ensure that the file and its expect file are both readable.' );
193
235
}
194
236
}
195
- log.stdout ('Passed: $passCount , Failed: $failCount ' );
196
- return failCount > 0 ;
237
+ //
238
+ // Report any `.expect` files that have no corresponding `.dart` file.
239
+ //
240
+ for (var unmatchedExpectPath in expectFileMap.keys) {
241
+ result.failCount++ ;
242
+ log.stdout (
243
+ 'No corresponding Dart file for the expect file at "$unmatchedExpectPath ".' );
244
+ }
245
+ return result;
197
246
}
198
247
248
+ /// Compress sequences of whitespace characters into a single space.
249
+ String _compressWhitespace (String code) =>
250
+ code.replaceAll (RegExp (r'\s*' ), ' ' );
251
+
199
252
String _pluralFix (int count) => count == 1 ? 'fix' : 'fixes' ;
200
253
201
254
void _printDetails (List <BulkFix > details, io.Directory workingDir) {
@@ -215,6 +268,16 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
215
268
}
216
269
}
217
270
271
+ void _printEdits (List <SourceFileEdit > edits) {
272
+ log.stdout ('Edits returned from server:' );
273
+ for (var fileEdit in edits) {
274
+ log.stdout (' ${fileEdit .file }' );
275
+ for (var edit in fileEdit.edits) {
276
+ log.stdout (" ${edit .offset } - ${edit .end }, '${edit .replacement }'" );
277
+ }
278
+ }
279
+ }
280
+
218
281
/// Report that the [actualCode] produced by applying fixes to the content of
219
282
/// [filePath] did not match the [expectedCode] .
220
283
void _reportFailure (String filePath, String actualCode, String expectedCode) {
@@ -228,3 +291,15 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
228
291
229
292
static String _format (int value) => _numberFormat.format (value);
230
293
}
294
+
295
+ /// The result of running tests in a given directory.
296
+ class _TestResult {
297
+ /// The number of tests that passed.
298
+ int passCount = 0 ;
299
+
300
+ /// The number of tests that failed.
301
+ int failCount = 0 ;
302
+
303
+ /// Initialize a newly created result object.
304
+ _TestResult ();
305
+ }
0 commit comments