flutter/packages/flutter_tools/test/integration.shard/daemon_mode_test.dart
Danny Tuppeny 4c8b0a3873
[flutter_tool] Change the startup message for the "flutter daemon" command (#160444)
There have been some reports of the Flutter daemon not starting up
properly (for ex. https://github.com/flutter/flutter/issues/143625), but
it turns out that when starting successfully, it just prints "Starting
device daemon..." and then nothing more. This is confusing and can lead
users to think other issues are because the daemon hasn't started
properly.

There's not a great hook to print that this "finished" starting up
(because it does async polling in the background), so I just updated the
message of the text and moved it to after the creation of the daemon.

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

## Pre-launch Checklist

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

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

<!-- 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

---------

Co-authored-by: Andrew Kolos <andrewrkolos@gmail.com>
2024-12-22 02:32:02 +00:00

117 lines
4.1 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' hide Directory;
import 'package:file/file.dart';
import 'package:process/process.dart';
import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';
void main() {
late Directory tempDir;
late Process daemonProcess;
setUp(() async {
tempDir = createResolvedTempDirectorySync('daemon_mode_test.');
});
tearDown(() async {
tryToDelete(tempDir);
daemonProcess.kill();
});
testWithoutContext('startup events', () async {
final BasicProject project = BasicProject();
await project.setUpIn(tempDir);
const ProcessManager processManager = LocalProcessManager();
daemonProcess = await processManager.start(
<String>[flutterBin, ...getLocalEngineArguments(), '--show-test-device', 'daemon'],
workingDirectory: tempDir.path,
);
final StreamController<String> stdout = StreamController<String>.broadcast();
transformToLines(daemonProcess.stdout).listen((String line) => stdout.add(line));
final Stream<Map<String, Object?>> stream =
stdout.stream
.map<Map<String, Object?>?>(parseFlutterResponse)
.where((Map<String, Object?>? value) => value != null)
.cast<Map<String, Object?>>();
final [
Map<String, Object?> connectedEvent,
Map<String, Object?> logMessage,
] = await Future.wait(<Future<Map<String, Object?>>>[
stream.firstWhere((Map<String, Object?> e) => e['event'] == 'daemon.connected'),
stream.firstWhere((Map<String, Object?> e) => e['event'] == 'daemon.logMessage'),
]);
// Check the connected message has a version.
final Map<String, Object?> connectedParams = connectedEvent['params']! as Map<String, Object?>;
expect(connectedParams['version'], isNotNull);
// Check we got the startup message.
final Map<String, Object?> logParams = logMessage['params']! as Map<String, Object?>;
expect(logParams['level'], 'status');
expect(logParams['message'], 'Device daemon started.');
});
testWithoutContext('device.getDevices', () async {
final BasicProject project = BasicProject();
await project.setUpIn(tempDir);
const ProcessManager processManager = LocalProcessManager();
daemonProcess = await processManager.start(<String>[
flutterBin,
...getLocalEngineArguments(),
'--show-test-device',
'daemon',
], workingDirectory: tempDir.path);
final StreamController<String> stdout = StreamController<String>.broadcast();
transformToLines(daemonProcess.stdout).listen((String line) => stdout.add(line));
final Stream<Map<String, Object?>?> stream = stdout.stream
.map<Map<String, Object?>?>(parseFlutterResponse)
.where((Map<String, Object?>? value) => value != null);
Map<String, Object?> response = (await stream.first)!;
expect(response['event'], 'daemon.connected');
// start listening for devices
daemonProcess.stdin.writeln(
'[${jsonEncode(<String, dynamic>{'id': 1, 'method': 'device.enable'})}]',
);
response = (await stream.firstWhere((Map<String, Object?>? json) => json!['id'] == 1))!;
expect(response['id'], 1);
expect(response['error'], isNull);
// [{"event":"device.added","params":{"id":"flutter-tester","name":
// "Flutter test device","platform":"flutter-tester","emulator":false}}]
response = (await stream.first)!;
expect(response['event'], 'device.added');
// get the list of all devices
daemonProcess.stdin.writeln(
'[${jsonEncode(<String, dynamic>{'id': 2, 'method': 'device.getDevices'})}]',
);
// Skip other device.added events that may fire (desktop/web devices).
response =
(await stream.firstWhere(
(Map<String, Object?>? response) => response!['event'] != 'device.added',
))!;
expect(response['id'], 2);
expect(response['error'], isNull);
final dynamic result = response['result'];
expect(result, isList);
expect(result, isNotEmpty);
});
}