flutter/packages/flutter_tools/test/integration.shard/test_utils.dart
Jonah Williams 8b6baae44c
[flutter_tools] move process manager into tool (#75350)
Our current top crasher is an unclear error when ProcessManager fails to resolve an executable path. To fix this, we'd like to being adjusting the process resolution logic and adding more instrumentation to track failures. In order to begin the process, the ProcessManager has been folded back into the flutter tool
2021-02-04 13:19:11 -08:00

106 lines
3.4 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.
// @dart = 2.8
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/process.dart';
import 'package:meta/meta.dart';
import 'package:vm_service/vm_service.dart';
import '../src/common.dart';
import 'test_driver.dart';
/// The [FileSystem] for the integration test environment.
const FileSystem fileSystem = LocalFileSystem();
/// The [Platform] for the integration test environment.
const Platform platform = LocalPlatform();
/// The [ProcessManager] for the integration test environment.
const ProcessManager processManager = LocalProcessManager(
platform: platform,
fileSystem: fileSystem,
);
/// Creates a temporary directory but resolves any symlinks to return the real
/// underlying path to avoid issues with breakpoints/hot reload.
/// https://github.com/flutter/flutter/pull/21741
Directory createResolvedTempDirectorySync(String prefix) {
assert(prefix.endsWith('.'));
final Directory tempDirectory = fileSystem.systemTempDirectory.createTempSync('flutter_$prefix');
return fileSystem.directory(tempDirectory.resolveSymbolicLinksSync());
}
void writeFile(String path, String content) {
fileSystem.file(path)
..createSync(recursive: true)
..writeAsStringSync(content)
..setLastModifiedSync(DateTime.now().add(const Duration(seconds: 10)));
}
void writePackages(String folder) {
writeFile(fileSystem.path.join(folder, '.packages'), '''
test:${fileSystem.path.join(fileSystem.currentDirectory.path, 'lib')}/
''');
}
void writePubspec(String folder) {
writeFile(fileSystem.path.join(folder, 'pubspec.yaml'), '''
name: test
dependencies:
flutter:
sdk: flutter
''');
}
Future<void> getPackages(String folder) async {
final List<String> command = <String>[
fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'),
'pub',
'get',
];
final ProcessResult result = await processManager.run(command, workingDirectory: folder);
if (result.exitCode != 0) {
throw Exception('flutter pub get failed: ${result.stderr}\n${result.stdout}');
}
}
const String kLocalEngineEnvironment = 'FLUTTER_LOCAL_ENGINE';
const String kLocalEngineLocation = 'FLUTTER_LOCAL_ENGINE_SRC_PATH';
List<String> getLocalEngineArguments() {
return <String>[
if (platform.environment.containsKey(kLocalEngineEnvironment))
'--local-engine=${platform.environment[kLocalEngineEnvironment]}',
if (platform.environment.containsKey(kLocalEngineLocation))
'--local-engine-src-path=${platform.environment[kLocalEngineLocation]}',
];
}
Future<void> pollForServiceExtensionValue<T>({
@required FlutterTestDriver testDriver,
@required String extension,
@required T continuePollingValue,
@required Matcher matches,
String valueKey = 'value',
}) async {
for (int i = 0; i < 10; i++) {
final Response response = await testDriver.callServiceExtension(extension);
if (response.json[valueKey] as T == continuePollingValue) {
await Future<void>.delayed(const Duration(seconds: 1));
} else {
expect(response.json[valueKey] as T, matches);
return;
}
}
fail(
'Did not find expected value for service extension \'$extension\'. All call'
' attempts responded with \'$continuePollingValue\'.',
);
}