Files
cutdex/test/generated_code_test.dart
lingjhf 8dada4726c
Gitea CI / Check code and tests (pull_request) Successful in 14m15s
Gitea CI / Build Windows x64 app (pull_request) Successful in 4m19s
Gitea CI / Gitea CI passed (pull_request) Successful in 1s
Gitea CI / Build Android APK (pull_request) Successful in 5m25s
Gitea CI / Audit and package Windows x64 (pull_request) Successful in 1m39s
Gitea CI / Assemble and validate CI artifacts (pull_request) Successful in 57s
test(ci): run generated code verification without widget contention
2026-10-12 04:30:00 +08:00

149 lines
4.1 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:crypto/crypto.dart';
import 'support/dart_executable.dart';
const _generatedFiles = [
':(glob)lib/**/*.g.dart',
':(glob)lib/**/*.freezed.dart',
':(glob)test/**/*.g.dart',
':(glob)test/**/*.freezed.dart',
];
void main() {
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(
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<void> _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<void> read(Stream<List<int>> stream) async {
await for (final line
in stream.transform(utf8.decoder).transform(const LineSplitter())) {
output.writeln(line);
stdout.writeln('[$directory] $line');
}
}
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',
);
}
}
Future<void> _expectNoUntrackedGeneratedFiles() async {
final result = await Process.run('git', [
'ls-files',
'--others',
'--',
..._generatedFiles,
]);
expect(result.exitCode, 0, reason: '${result.stderr}');
expect(
(result.stdout as String).trim(),
isEmpty,
reason: 'Generate and track the .g.dart and .freezed.dart files in Git.',
);
}
Future<Map<String, String>> _generatedContents() async {
final contents = <String, String>{};
for (final root in [
for (final directory in _generatorRoots()) ...[
'$directory/lib',
'$directory/test',
],
]) {
if (!Directory(root).existsSync()) continue;
await for (final entity in Directory(root).list(recursive: true)) {
if (entity is File &&
(entity.path.endsWith('.g.dart') ||
entity.path.endsWith('.freezed.dart'))) {
contents[entity.path] = sha256
.convert(
utf8.encode(
(await entity.readAsString()).replaceAll('\r\n', '\n'),
),
)
.toString();
}
}
}
return contents;
}
Iterable<String> _generatorRoots() sync* {
yield '.';
for (final directory in Directory(
'packages',
).listSync().whereType<Directory>()) {
final manifest = File('${directory.path}/pubspec.yaml').readAsStringSync();
if (RegExp(r'^ build_runner:', multiLine: true).hasMatch(manifest)) {
yield directory.path;
}
}
}