Skip to content

Commit 548e872

Browse files
authored
fix(test): cleanup .test_runner.dart on failure (#348)
1 parent 828fb51 commit 548e872

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

lib/src/cli/flutter_cli.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,16 @@ class Flutter {
181181
],
182182
stdout: stdout ?? noop,
183183
stderr: stderr ?? noop,
184-
);
184+
).whenComplete(() {
185+
if (optimizePerformance) {
186+
File(p.join(cwd, 'test', '.test_runner.dart')).delete().ignore();
187+
}
188+
});
185189
},
186190
cwd: cwd,
187191
recursive: recursive,
188192
);
189193

190-
if (optimizePerformance) {
191-
File(p.join(cwd, 'test', '.test_runner.dart')).delete().ignore();
192-
}
193194
if (collectCoverage) await lcovFile.ensureCreated();
194195
if (minCoverage != null) {
195196
final records = await Parser.parse(lcovPath);
@@ -268,7 +269,8 @@ Future<int> _flutterTest({
268269
},
269270
);
270271

271-
flutterTest(
272+
final StreamSubscription<TestEvent> subscription;
273+
subscription = flutterTest(
272274
workingDirectory: cwd,
273275
arguments: [
274276
if (collectCoverage) '--coverage',
@@ -333,6 +335,7 @@ Future<int> _flutterTest({
333335
: lightRed.wrap('Some tests failed.')!;
334336

335337
stdout('$clearLine${darkGray.wrap(timeElapsed)} $stats: $summary\n');
338+
if (completer.isCompleted) return;
336339
completer.complete(
337340
event.success == true
338341
? ExitCode.success.code
@@ -343,7 +346,7 @@ Future<int> _flutterTest({
343346
onError: completer.completeError,
344347
);
345348

346-
return completer.future;
349+
return completer.future.whenComplete(subscription.cancel);
347350
}
348351

349352
final int _lineLength = () {

test/src/cli/flutter_cli_test.dart

+15-6
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,23 @@ void main() {
287287
);
288288
});
289289

290-
test('throws when process fails', () {
290+
test('throws when process fails (with cleanup)', () async {
291291
final directory = Directory.systemTemp.createTempSync();
292+
final testDirectory = Directory(p.join(directory.path, 'test'))
293+
..createSync();
292294
File(p.join(directory.path, 'pubspec.yaml'))
293-
.writeAsStringSync(invalidPubspec);
294-
295-
expectLater(
296-
Flutter.test(cwd: directory.path),
297-
throwsA('Test directory "test" not found.'),
295+
.writeAsStringSync(pubspecFlutter);
296+
File(p.join(testDirectory.path, 'example_test.dart'))
297+
.writeAsStringSync(testContents);
298+
await expectLater(
299+
Flutter.test(cwd: directory.path, optimizePerformance: true),
300+
throwsA(
301+
'''Error: Couldn't resolve the package 'test' in 'package:test/test.dart'.''',
302+
),
303+
);
304+
expect(
305+
File(p.join(testDirectory.path, '.test_runner.dart')).existsSync(),
306+
isFalse,
298307
);
299308
});
300309

0 commit comments

Comments
 (0)