From 8dada4726c62efe30388dc8451e92b36d93e3534 Mon Sep 17 00:00:00 2001 From: lingjhf Date: Mon, 12 Oct 2026 04:30:00 +0800 Subject: [PATCH] test(ci): run generated code verification without widget contention --- test/generated_code_test.dart | 93 ++++++++++++++++++++++++++++------- tool/check.py | 7 ++- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/test/generated_code_test.dart b/test/generated_code_test.dart index ad94165..8983e83 100644 --- a/test/generated_code_test.dart +++ b/test/generated_code_test.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -14,29 +15,83 @@ const _generatedFiles = [ ]; void main() { - test('workspace generated code is up to date', () async { - await _expectNoUntrackedGeneratedFiles(); - final before = await _generatedContents(); - final dart = flutterTestDartExecutable(); - for (final directory in _generatorRoots()) { - final result = await Process.run(dart, [ - 'run', - 'build_runner', - 'build', - ], workingDirectory: directory); + test( + 'workspace generated code is up to date', + () async { + await _expectNoUntrackedGeneratedFiles(); + final before = await _generatedContents(); + final dart = flutterTestDartExecutable(); + final elapsed = Stopwatch()..start(); + for (final directory in _generatorRoots()) { + await _generate( + dart, + directory, + const Duration(minutes: 5) - + const Duration(seconds: 10) - + elapsed.elapsed, + ); + } expect( - result.exitCode, - 0, - reason: '$directory\n${result.stdout}\n${result.stderr}', + await _generatedContents(), + before, + reason: 'Generated files changed. Run dart run build_runner build and review the output.', ); + await _expectNoUntrackedGeneratedFiles(); + }, + timeout: const Timeout(Duration(minutes: 5)), + tags: ['codegen'], + ); +} + +Future _generate( + String dart, + String directory, + Duration remaining, +) async { + if (remaining <= Duration.zero) { + throw TimeoutException('Generated-code verification budget exhausted'); + } + final elapsed = Stopwatch()..start(); + stdout.writeln( + 'Generating $directory; remaining budget: ${remaining.inSeconds}s', + ); + final process = await Process.start(dart, [ + 'run', + 'build_runner', + 'build', + ], workingDirectory: directory); + final output = StringBuffer(); + Future read(Stream> stream) async { + await for (final line + in stream.transform(utf8.decoder).transform(const LineSplitter())) { + output.writeln(line); + stdout.writeln('[$directory] $line'); } - expect( - await _generatedContents(), - before, - reason: 'Generated files changed. Run dart run build_runner build and review the output.', + } + + final streams = Future.wait([read(process.stdout), read(process.stderr)]); + var exited = false; + try { + final exitCode = await process.exitCode.timeout(remaining); + exited = true; + await streams; + expect(exitCode, 0, reason: '$directory\n$output'); + } finally { + if (!exited) { + // Flutter's outer test timeout cannot terminate Process.run children. + // End the generator before leaving the test, including on Windows. + if (Platform.isWindows) { + await Process.run('taskkill', ['/PID', '${process.pid}', '/T', '/F']); + } else { + process.kill(ProcessSignal.sigkill); + } + await process.exitCode; + await streams; + } + stdout.writeln( + 'Generation $directory ended after ${elapsed.elapsed.inMilliseconds}ms', ); - await _expectNoUntrackedGeneratedFiles(); - }, timeout: const Timeout(Duration(minutes: 5))); + } } Future _expectNoUntrackedGeneratedFiles() async { diff --git a/tool/check.py b/tool/check.py index 551a6b5..00e23a4 100644 --- a/tool/check.py +++ b/tool/check.py @@ -45,7 +45,12 @@ def check_analyze(): def check_flutter(): package_tests = sorted(str(path.relative_to(ROOT)) for pattern in ('*/test', '*/example/test') for path in (ROOT / 'packages').glob(pattern)) - run(tool_command('flutter', ['test', '--reporter', 'expanded', 'test', *package_tests])) + run(tool_command('flutter', ['test', '--reporter', 'expanded', '--exclude-tags', 'codegen', + 'test', *package_tests])) + # build_runner compiles generators and performs repository-wide file I/O. + # Keep this gate serial with widget tests to avoid competing with their isolates. + run(tool_command('flutter', ['test', '--reporter', 'expanded', '--tags', 'codegen', + 'test/generated_code_test.dart'])) def main():