flutter_flutter/dev/devicelab/lib/tasks/dart_plugin_registry_tests.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

199 lines
6.5 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as path;
import '../framework/framework.dart';
import '../framework/task_result.dart';
import '../framework/utils.dart';
TaskFunction dartPluginRegistryTest({String? deviceIdOverride, Map<String, String>? environment}) {
final Directory tempDir = Directory.systemTemp.createTempSync(
'flutter_devicelab_dart_plugin_test.',
);
return () async {
try {
section('Create implementation plugin');
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=plugin',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'aplugin_platform_implementation',
],
environment: environment,
);
});
final pluginMain = File(
path.join(
tempDir.absolute.path,
'aplugin_platform_implementation',
'lib',
'aplugin_platform_implementation.dart',
),
);
if (!pluginMain.existsSync()) {
return TaskResult.failure('${pluginMain.path} does not exist');
}
// Patch plugin main dart file.
await pluginMain.writeAsString('''
class ApluginPlatformInterfaceMacOS {
static void registerWith() {
print('ApluginPlatformInterfaceMacOS.registerWith() was called');
}
}
''', flush: true);
// Patch plugin main pubspec file.
final pluginImplPubspec = File(
path.join(tempDir.absolute.path, 'aplugin_platform_implementation', 'pubspec.yaml'),
);
String pluginImplPubspecContent = await pluginImplPubspec.readAsString();
pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
' pluginClass: ApluginPlatformImplementationPlugin',
' pluginClass: ApluginPlatformImplementationPlugin\n'
' dartPluginClass: ApluginPlatformInterfaceMacOS\n',
);
pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
' platforms:\n',
' implements: aplugin_platform_interface\n'
' platforms:\n',
);
await pluginImplPubspec.writeAsString(pluginImplPubspecContent, flush: true);
section('Create interface plugin');
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=plugin',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'aplugin_platform_interface',
],
environment: environment,
);
});
final pluginInterfacePubspec = File(
path.join(tempDir.absolute.path, 'aplugin_platform_interface', 'pubspec.yaml'),
);
String pluginInterfacePubspecContent = await pluginInterfacePubspec.readAsString();
pluginInterfacePubspecContent = pluginInterfacePubspecContent.replaceFirst(
' pluginClass: ApluginPlatformInterfacePlugin',
' default_package: aplugin_platform_implementation\n',
);
pluginInterfacePubspecContent = pluginInterfacePubspecContent.replaceFirst(
'dependencies:',
'dependencies:\n'
' aplugin_platform_implementation:\n'
' path: ../aplugin_platform_implementation\n',
);
await pluginInterfacePubspec.writeAsString(pluginInterfacePubspecContent, flush: true);
section('Create app');
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=app',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'app',
],
environment: environment,
);
});
final appPubspec = File(path.join(tempDir.absolute.path, 'app', 'pubspec.yaml'));
String appPubspecContent = await appPubspec.readAsString();
appPubspecContent = appPubspecContent.replaceFirst(
'dependencies:',
'dependencies:\n'
' aplugin_platform_interface:\n'
' path: ../aplugin_platform_interface\n',
);
await appPubspec.writeAsString(appPubspecContent, flush: true);
section('Flutter run for macos');
late Process run;
await inDirectory(path.join(tempDir.path, 'app'), () async {
run = await startFlutter('run', options: <String>['-d', 'macos', '-v']);
});
var registryExecutedCompleter = Completer<void>();
final StreamSubscription<void> stdoutSub = run.stdout
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
if (line.contains('ApluginPlatformInterfaceMacOS.registerWith() was called')) {
registryExecutedCompleter.complete();
}
print('stdout: $line');
});
final StreamSubscription<void> stderrSub = run.stderr
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
print('stderr: $line');
});
final Future<void> stdoutDone = stdoutSub.asFuture<void>();
final Future<void> stderrDone = stderrSub.asFuture<void>();
Future<void> waitForStreams() {
return Future.wait<void>(<Future<void>>[stdoutDone, stderrDone]);
}
Future<void> waitOrExit(Future<void> future) async {
final dynamic result = await Future.any<dynamic>(<Future<dynamic>>[future, run.exitCode]);
if (result is int) {
await waitForStreams();
throw 'process exited with code $result';
}
}
section('Wait for registry execution');
await waitOrExit(registryExecutedCompleter.future);
// Hot restart.
run.stdin.write('R');
await run.stdin.flush();
await run.stdin.close();
registryExecutedCompleter = Completer<void>();
section('Wait for registry execution after hot restart');
await waitOrExit(registryExecutedCompleter.future);
run.kill();
section('Wait for stdout/stderr streams');
await waitForStreams();
unawaited(stdoutSub.cancel());
unawaited(stderrSub.cancel());
return TaskResult.success(null);
} finally {
rmTree(tempDir);
}
};
}