Skip to content

Commit

Permalink
fix(test): cleanup .test_runner.dart on failure (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Mar 30, 2022
1 parent 828fb51 commit 548e872
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lib/src/cli/flutter_cli.dart
Expand Up @@ -181,15 +181,16 @@ class Flutter {
],
stdout: stdout ?? noop,
stderr: stderr ?? noop,
);
).whenComplete(() {
if (optimizePerformance) {
File(p.join(cwd, 'test', '.test_runner.dart')).delete().ignore();
}
});
},
cwd: cwd,
recursive: recursive,
);

if (optimizePerformance) {
File(p.join(cwd, 'test', '.test_runner.dart')).delete().ignore();
}
if (collectCoverage) await lcovFile.ensureCreated();
if (minCoverage != null) {
final records = await Parser.parse(lcovPath);
Expand Down Expand Up @@ -268,7 +269,8 @@ Future<int> _flutterTest({
},
);

flutterTest(
final StreamSubscription<TestEvent> subscription;
subscription = flutterTest(
workingDirectory: cwd,
arguments: [
if (collectCoverage) '--coverage',
Expand Down Expand Up @@ -333,6 +335,7 @@ Future<int> _flutterTest({
: lightRed.wrap('Some tests failed.')!;

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

return completer.future;
return completer.future.whenComplete(subscription.cancel);
}

final int _lineLength = () {
Expand Down
21 changes: 15 additions & 6 deletions test/src/cli/flutter_cli_test.dart
Expand Up @@ -287,14 +287,23 @@ void main() {
);
});

test('throws when process fails', () {
test('throws when process fails (with cleanup)', () async {
final directory = Directory.systemTemp.createTempSync();
final testDirectory = Directory(p.join(directory.path, 'test'))
..createSync();
File(p.join(directory.path, 'pubspec.yaml'))
.writeAsStringSync(invalidPubspec);

expectLater(
Flutter.test(cwd: directory.path),
throwsA('Test directory "test" not found.'),
.writeAsStringSync(pubspecFlutter);
File(p.join(testDirectory.path, 'example_test.dart'))
.writeAsStringSync(testContents);
await expectLater(
Flutter.test(cwd: directory.path, optimizePerformance: true),
throwsA(
'''Error: Couldn't resolve the package 'test' in 'package:test/test.dart'.''',
),
);
expect(
File(p.join(testDirectory.path, '.test_runner.dart')).existsSync(),
isFalse,
);
});

Expand Down

0 comments on commit 548e872

Please sign in to comment.