Make sure to finish the suite if all tests are skipped (flutter/engine#49339)

Fixes https://github.com/flutter/flutter/issues/140481

Before this fix, if all tests are skipped, the `onDone` callback never fires, which means the recieve port never closes, which means the process just hangs indefinitely (and the success message is never printed).
This commit is contained in:
Dan Field
2024-01-02 10:34:04 -08:00
committed by GitHub
parent ba2b335b99
commit 76fc29312f
2 changed files with 39 additions and 7 deletions

View File

@@ -40,12 +40,22 @@ class TestSuite {
);
}
if (skip) {
_logger.writeln('Test $name: Skipped');
_logger.writeln('Test "$name": Skipped');
_primeQueue();
return;
}
_pushTest(name, body);
}
void _primeQueue() {
if (!_testQueuePrimed) {
// All tests() must be added synchronously with main, so we can enqueue an
// event to start all tests to run after main() is done.
Timer.run(_startAllTests);
_testQueuePrimed = true;
}
}
void _pushTest(
String name,
dynamic Function() body,
@@ -53,12 +63,7 @@ class TestSuite {
final Test newTest = Test(name, body, logger: _logger);
_testQueue.add(newTest);
newTest.state = TestState.queued;
if (!_testQueuePrimed) {
// All tests() must be added synchronously with main, so we can enqueue an
// event to start all tests to run after main() is done.
Timer.run(_startAllTests);
_testQueuePrimed = true;
}
_primeQueue();
}
void _startAllTests() {
@@ -72,6 +77,10 @@ class TestSuite {
});
}
_lifecycle.onStart();
if (_testQueue.isEmpty) {
_logger.writeln('All tests skipped.');
_lifecycle.onDone(_testQueue);
}
}
}

View File

@@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:collection';
import 'dart:isolate';
import 'package:async_helper/async_helper.dart';
import 'package:async_helper/async_minitest.dart';
@@ -13,6 +14,25 @@ import 'package:litetest/src/test_suite.dart';
Future<void> main() async {
asyncStart();
test('skip', () async {
final StringBuffer buffer = StringBuffer();
final TestLifecycle lifecycle = TestLifecycle();
final TestSuite ts = TestSuite(
logger: buffer,
lifecycle: lifecycle,
);
ts.test('Test', () {
expect(1, equals(1));
}, skip: true);
final bool result = await lifecycle.result;
expect(result, true);
expect(buffer.toString(), equals(
'Test "Test": Skipped\nAll tests skipped.\n',
));
});
test('test', () async {
final StringBuffer buffer = StringBuffer();
final TestLifecycle lifecycle = TestLifecycle();
@@ -211,6 +231,8 @@ Test "Test3": Passed
}
class TestLifecycle implements Lifecycle {
final ReceivePort port = ReceivePort();
final Completer<bool> _testCompleter = Completer<bool>();
Future<bool> get result => _testCompleter.future;
@@ -225,5 +247,6 @@ class TestLifecycle implements Lifecycle {
testsSucceeded = testsSucceeded && (t.state == TestState.succeeded);
}
_testCompleter.complete(testsSucceeded);
port.close();
}
}