Migrate test/src/common to null safety (#79907)
This commit is contained in:
@@ -2,40 +2,22 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:fake_async/fake_async.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/base/context.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/base/user_messages.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/create.dart';
|
||||
import 'package:flutter_tools/src/convert.dart';
|
||||
import 'package:flutter_tools/src/doctor_validator.dart';
|
||||
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
|
||||
import 'package:flutter_tools/src/vmservice.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:path/path.dart' as path; // flutter_ignore: package_path_import
|
||||
import 'package:test_api/test_api.dart' as test_package show test; // ignore: deprecated_member_use
|
||||
import 'package:test_api/test_api.dart' hide test; // ignore: deprecated_member_use
|
||||
import 'package:vm_service/vm_service.dart' as vm_service;
|
||||
|
||||
export 'package:test_api/test_api.dart' hide test, isInstanceOf; // ignore: deprecated_member_use
|
||||
|
||||
/// A matcher that compares the type of the actual value to the type argument T.
|
||||
TypeMatcher<T> isInstanceOf<T>() => isA<T>();
|
||||
|
||||
void tryToDelete(Directory directory) {
|
||||
// This should not be necessary, but it turns out that
|
||||
// on Windows it's common for deletions to fail due to
|
||||
@@ -57,7 +39,7 @@ void tryToDelete(Directory directory) {
|
||||
String getFlutterRoot() {
|
||||
const Platform platform = LocalPlatform();
|
||||
if (platform.environment.containsKey('FLUTTER_ROOT')) {
|
||||
return platform.environment['FLUTTER_ROOT'];
|
||||
return platform.environment['FLUTTER_ROOT']!;
|
||||
}
|
||||
|
||||
Error invalidScript() => StateError('Could not determine flutter_tools/ path from script URL (${globals.platform.script}); consider setting FLUTTER_ROOT explicitly.');
|
||||
@@ -69,11 +51,11 @@ String getFlutterRoot() {
|
||||
break;
|
||||
case 'data':
|
||||
final RegExp flutterTools = RegExp(r'(file://[^"]*[/\\]flutter_tools[/\\][^"]+\.dart)', multiLine: true);
|
||||
final Match match = flutterTools.firstMatch(Uri.decodeFull(platform.script.path));
|
||||
final Match? match = flutterTools.firstMatch(Uri.decodeFull(platform.script.path));
|
||||
if (match == null) {
|
||||
throw invalidScript();
|
||||
}
|
||||
scriptUri = Uri.parse(match.group(1));
|
||||
scriptUri = Uri.parse(match.group(1)!);
|
||||
break;
|
||||
default:
|
||||
throw invalidScript();
|
||||
@@ -88,23 +70,6 @@ String getFlutterRoot() {
|
||||
return path.normalize(path.join(toolsPath, '..', '..'));
|
||||
}
|
||||
|
||||
/// Gets the path to the root of the Android SDK from the environment variable.
|
||||
String getAndroidSdkRoot() {
|
||||
const Platform platform = LocalPlatform();
|
||||
if (platform.environment.containsKey('ANDROID_SDK_ROOT')) {
|
||||
return platform.environment['ANDROID_SDK_ROOT'];
|
||||
}
|
||||
throw StateError('ANDROID_SDK_ROOT environment varible not set');
|
||||
}
|
||||
|
||||
CommandRunner<void> createTestCommandRunner([ FlutterCommand command ]) {
|
||||
final FlutterCommandRunner runner = TestFlutterCommandRunner();
|
||||
if (command != null) {
|
||||
runner.addCommand(command);
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
|
||||
/// Capture console print events into a string buffer.
|
||||
Future<StringBuffer> capturedConsolePrint(Future<void> Function() body) async {
|
||||
final StringBuffer buffer = StringBuffer();
|
||||
@@ -121,8 +86,8 @@ Future<StringBuffer> capturedConsolePrint(Future<void> Function() body) async {
|
||||
final Matcher throwsAssertionError = throwsA(isA<AssertionError>());
|
||||
|
||||
/// Matcher for functions that throw [ToolExit].
|
||||
Matcher throwsToolExit({ int exitCode, Pattern message }) {
|
||||
Matcher matcher = isToolExit;
|
||||
Matcher throwsToolExit({ int? exitCode, Pattern? message }) {
|
||||
Matcher matcher = _isToolExit;
|
||||
if (exitCode != null) {
|
||||
matcher = allOf(matcher, (ToolExit e) => e.exitCode == exitCode);
|
||||
}
|
||||
@@ -133,33 +98,19 @@ Matcher throwsToolExit({ int exitCode, Pattern message }) {
|
||||
}
|
||||
|
||||
/// Matcher for [ToolExit]s.
|
||||
final TypeMatcher<ToolExit> isToolExit = isA<ToolExit>();
|
||||
final TypeMatcher<ToolExit> _isToolExit = isA<ToolExit>();
|
||||
|
||||
/// Matcher for functions that throw [ProcessException].
|
||||
Matcher throwsProcessException({ Pattern message }) {
|
||||
Matcher matcher = isProcessException;
|
||||
Matcher throwsProcessException({ Pattern? message }) {
|
||||
Matcher matcher = _isProcessException;
|
||||
if (message != null) {
|
||||
matcher = allOf(matcher, (ProcessException e) => e.message?.contains(message));
|
||||
matcher = allOf(matcher, (ProcessException e) => e.message.contains(message));
|
||||
}
|
||||
return throwsA(matcher);
|
||||
}
|
||||
|
||||
/// Matcher for [ProcessException]s.
|
||||
final TypeMatcher<ProcessException> isProcessException = isA<ProcessException>();
|
||||
|
||||
/// Creates a flutter project in the [temp] directory using the
|
||||
/// [arguments] list if specified, or `--no-pub` if not.
|
||||
/// Returns the path to the flutter project.
|
||||
Future<String> createProject(Directory temp, { List<String> arguments }) async {
|
||||
arguments ??= <String>['--no-pub'];
|
||||
final String projectPath = globals.fs.path.join(temp.path, 'flutter_project');
|
||||
final CreateCommand command = CreateCommand();
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
await runner.run(<String>['create', ...arguments, projectPath]);
|
||||
// Created `.packages` since it's not created when the flag `--no-pub` is passed.
|
||||
globals.fs.file(globals.fs.path.join(projectPath, '.packages')).createSync();
|
||||
return projectPath;
|
||||
}
|
||||
final TypeMatcher<ProcessException> _isProcessException = isA<ProcessException>();
|
||||
|
||||
Future<void> expectToolExitLater(Future<dynamic> future, Matcher messageMatcher) async {
|
||||
try {
|
||||
@@ -187,12 +138,12 @@ Matcher containsIgnoringWhitespace(String toSearch) {
|
||||
/// `LocalFileSystem.dispose()`.
|
||||
@isTest
|
||||
void test(String description, FutureOr<void> Function() body, {
|
||||
String testOn,
|
||||
Timeout timeout,
|
||||
String? testOn,
|
||||
Timeout? timeout,
|
||||
dynamic skip,
|
||||
List<String> tags,
|
||||
Map<String, dynamic> onPlatform,
|
||||
int retry,
|
||||
List<String>? tags,
|
||||
Map<String, dynamic>? onPlatform,
|
||||
int? retry,
|
||||
}) {
|
||||
test_package.test(
|
||||
description,
|
||||
@@ -221,17 +172,17 @@ void test(String description, FutureOr<void> Function() body, {
|
||||
/// For more information, see https://github.com/flutter/flutter/issues/47161
|
||||
@isTest
|
||||
void testWithoutContext(String description, FutureOr<void> Function() body, {
|
||||
String testOn,
|
||||
Timeout timeout,
|
||||
String? testOn,
|
||||
Timeout? timeout,
|
||||
dynamic skip,
|
||||
List<String> tags,
|
||||
Map<String, dynamic> onPlatform,
|
||||
int retry,
|
||||
List<String>? tags,
|
||||
Map<String, dynamic>? onPlatform,
|
||||
int? retry,
|
||||
}) {
|
||||
return test(
|
||||
description, () async {
|
||||
return runZoned(body, zoneValues: <Object, Object>{
|
||||
contextKey: const NoContext(),
|
||||
contextKey: const _NoContext(),
|
||||
});
|
||||
},
|
||||
timeout: timeout,
|
||||
@@ -243,28 +194,13 @@ void testWithoutContext(String description, FutureOr<void> Function() body, {
|
||||
);
|
||||
}
|
||||
|
||||
/// Runs a callback using FakeAsync.run while continually pumping the
|
||||
/// microtask queue. This avoids a deadlock when tests `await` a Future
|
||||
/// which queues a microtask that will not be processed unless the queue
|
||||
/// is flushed.
|
||||
Future<T> runFakeAsync<T>(Future<T> Function(FakeAsync time) f) async {
|
||||
return FakeAsync().run((FakeAsync time) async {
|
||||
bool pump = true;
|
||||
final Future<T> future = f(time).whenComplete(() => pump = false);
|
||||
while (pump) {
|
||||
time.flushMicrotasks();
|
||||
}
|
||||
return future;
|
||||
});
|
||||
}
|
||||
|
||||
/// An implementation of [AppContext] that throws if context.get is called in the test.
|
||||
///
|
||||
/// The intention of the class is to ensure we do not accidentally regress when
|
||||
/// moving towards more explicit dependency injection by accidentally using
|
||||
/// a Zone value in place of a constructor parameter.
|
||||
class NoContext implements AppContext {
|
||||
const NoContext();
|
||||
class _NoContext implements AppContext {
|
||||
const _NoContext();
|
||||
|
||||
@override
|
||||
T get<T>() {
|
||||
@@ -280,169 +216,16 @@ class NoContext implements AppContext {
|
||||
|
||||
@override
|
||||
Future<V> run<V>({
|
||||
FutureOr<V> Function() body,
|
||||
String name,
|
||||
Map<Type, Generator> overrides,
|
||||
Map<Type, Generator> fallbacks,
|
||||
ZoneSpecification zoneSpecification,
|
||||
required FutureOr<V> Function() body,
|
||||
String? name,
|
||||
Map<Type, Generator>? overrides,
|
||||
Map<Type, Generator>? fallbacks,
|
||||
ZoneSpecification? zoneSpecification,
|
||||
}) async {
|
||||
return body();
|
||||
}
|
||||
}
|
||||
|
||||
/// A fake implementation of a vm_service that mocks the JSON-RPC request
|
||||
/// and response structure.
|
||||
class FakeVmServiceHost {
|
||||
FakeVmServiceHost({
|
||||
@required List<VmServiceExpectation> requests,
|
||||
Uri httpAddress,
|
||||
Uri wsAddress,
|
||||
}) : _requests = requests {
|
||||
_vmService = FlutterVmService(vm_service.VmService(
|
||||
_input.stream,
|
||||
_output.add,
|
||||
), httpAddress: httpAddress, wsAddress: wsAddress);
|
||||
_applyStreamListen();
|
||||
_output.stream.listen((String data) {
|
||||
final Map<String, Object> request = json.decode(data) as Map<String, Object>;
|
||||
if (_requests.isEmpty) {
|
||||
throw Exception('Unexpected request: $request');
|
||||
}
|
||||
final FakeVmServiceRequest fakeRequest = _requests.removeAt(0) as FakeVmServiceRequest;
|
||||
expect(request, isA<Map<String, Object>>()
|
||||
.having((Map<String, Object> request) => request['method'], 'method', fakeRequest.method)
|
||||
.having((Map<String, Object> request) => request['params'], 'args', fakeRequest.args)
|
||||
);
|
||||
if (fakeRequest.close) {
|
||||
unawaited(_vmService.dispose());
|
||||
expect(_requests, isEmpty);
|
||||
return;
|
||||
}
|
||||
if (fakeRequest.errorCode == null) {
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'id': request['id'],
|
||||
'result': fakeRequest.jsonResponse ?? <String, Object>{'type': 'Success'},
|
||||
}));
|
||||
} else {
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'id': request['id'],
|
||||
'error': <String, Object>{
|
||||
'code': fakeRequest.errorCode,
|
||||
}
|
||||
}));
|
||||
}
|
||||
_applyStreamListen();
|
||||
});
|
||||
}
|
||||
|
||||
final List<VmServiceExpectation> _requests;
|
||||
final StreamController<String> _input = StreamController<String>();
|
||||
final StreamController<String> _output = StreamController<String>();
|
||||
|
||||
FlutterVmService get vmService => _vmService;
|
||||
FlutterVmService _vmService;
|
||||
|
||||
|
||||
bool get hasRemainingExpectations => _requests.isNotEmpty;
|
||||
|
||||
// remove FakeStreamResponse objects from _requests until it is empty
|
||||
// or until we hit a FakeRequest
|
||||
void _applyStreamListen() {
|
||||
while (_requests.isNotEmpty && !_requests.first.isRequest) {
|
||||
final FakeVmServiceStreamResponse response = _requests.removeAt(0) as FakeVmServiceStreamResponse;
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'streamNotify',
|
||||
'params': <String, Object>{
|
||||
'streamId': response.streamId,
|
||||
'event': response.event.toJson(),
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class VmServiceExpectation {
|
||||
bool get isRequest;
|
||||
}
|
||||
|
||||
class FakeVmServiceRequest implements VmServiceExpectation {
|
||||
const FakeVmServiceRequest({
|
||||
@required this.method,
|
||||
this.args = const <String, Object>{},
|
||||
this.jsonResponse,
|
||||
this.errorCode,
|
||||
this.close = false,
|
||||
});
|
||||
|
||||
final String method;
|
||||
|
||||
/// When true, the vm service is automatically closed.
|
||||
final bool close;
|
||||
|
||||
/// If non-null, the error code for a [vm_service.RPCError] in place of a
|
||||
/// standard response.
|
||||
final int errorCode;
|
||||
final Map<String, Object> args;
|
||||
final Map<String, Object> jsonResponse;
|
||||
|
||||
@override
|
||||
bool get isRequest => true;
|
||||
}
|
||||
|
||||
class FakeVmServiceStreamResponse implements VmServiceExpectation {
|
||||
const FakeVmServiceStreamResponse({
|
||||
@required this.event,
|
||||
@required this.streamId,
|
||||
});
|
||||
|
||||
final vm_service.Event event;
|
||||
final String streamId;
|
||||
|
||||
@override
|
||||
bool get isRequest => false;
|
||||
}
|
||||
|
||||
class TestFlutterCommandRunner extends FlutterCommandRunner {
|
||||
@override
|
||||
Future<void> runCommand(ArgResults topLevelResults) async {
|
||||
final Logger topLevelLogger = globals.logger;
|
||||
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{
|
||||
if (topLevelResults['verbose'] as bool)
|
||||
Logger: VerboseLogger(topLevelLogger),
|
||||
};
|
||||
return context.run<void>(
|
||||
overrides: contextOverrides.map<Type, Generator>((Type type, dynamic value) {
|
||||
return MapEntry<Type, Generator>(type, () => value);
|
||||
}),
|
||||
body: () {
|
||||
Cache.flutterRoot ??= Cache.defaultFlutterRoot(
|
||||
platform: globals.platform,
|
||||
fileSystem: globals.fs,
|
||||
userMessages: UserMessages(),
|
||||
);
|
||||
// For compatibility with tests that set this to a relative path.
|
||||
Cache.flutterRoot = globals.fs.path.normalize(globals.fs.path.absolute(Cache.flutterRoot));
|
||||
return super.runCommand(topLevelResults);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches a doctor validation result.
|
||||
Matcher matchDoctorValidation({
|
||||
ValidationType validationType,
|
||||
String statusInfo,
|
||||
dynamic messages
|
||||
}) {
|
||||
return const TypeMatcher<ValidationResult>()
|
||||
.having((ValidationResult result) => result.type, 'type', validationType)
|
||||
.having((ValidationResult result) => result.statusInfo, 'statusInfo', statusInfo)
|
||||
.having((ValidationResult result) => result.messages, 'messages', messages);
|
||||
}
|
||||
|
||||
/// Allows inserting file system exceptions into certain
|
||||
/// [MemoryFileSystem] operations by tagging path/op combinations.
|
||||
///
|
||||
@@ -467,16 +250,16 @@ class FileExceptionHandler {
|
||||
void addError(FileSystemEntity entity, FileSystemOp operation, FileSystemException exception) {
|
||||
final String path = entity.path;
|
||||
_contextErrors[path] ??= <FileSystemOp, FileSystemException>{};
|
||||
_contextErrors[path][operation] = exception;
|
||||
_contextErrors[path]![operation] = exception;
|
||||
}
|
||||
|
||||
// Tear-off this method and pass it to the memory filesystem `opHandle` parameter.
|
||||
void opHandle(String path, FileSystemOp operation) {
|
||||
final Map<FileSystemOp, FileSystemException> exceptions = _contextErrors[path];
|
||||
final Map<FileSystemOp, FileSystemException>? exceptions = _contextErrors[path];
|
||||
if (exceptions == null) {
|
||||
return;
|
||||
}
|
||||
final FileSystemException exception = exceptions[operation];
|
||||
final FileSystemException? exception = exceptions[operation];
|
||||
if (exception == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
131
packages/flutter_tools/test/src/fake_vm_services.dart
Normal file
131
packages/flutter_tools/test/src/fake_vm_services.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
// 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 'dart:async';
|
||||
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/convert.dart';
|
||||
import 'package:flutter_tools/src/vmservice.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:test_api/test_api.dart' hide test; // ignore: deprecated_member_use
|
||||
import 'package:vm_service/vm_service.dart' as vm_service;
|
||||
|
||||
export 'package:test_api/test_api.dart' hide test, isInstanceOf; // ignore: deprecated_member_use
|
||||
|
||||
/// A fake implementation of a vm_service that mocks the JSON-RPC request
|
||||
/// and response structure.
|
||||
class FakeVmServiceHost {
|
||||
FakeVmServiceHost({
|
||||
@required List<VmServiceExpectation> requests,
|
||||
Uri httpAddress,
|
||||
Uri wsAddress,
|
||||
}) : _requests = requests {
|
||||
_vmService = FlutterVmService(vm_service.VmService(
|
||||
_input.stream,
|
||||
_output.add,
|
||||
), httpAddress: httpAddress, wsAddress: wsAddress);
|
||||
_applyStreamListen();
|
||||
_output.stream.listen((String data) {
|
||||
final Map<String, Object> request = json.decode(data) as Map<String, Object>;
|
||||
if (_requests.isEmpty) {
|
||||
throw Exception('Unexpected request: $request');
|
||||
}
|
||||
final FakeVmServiceRequest fakeRequest = _requests.removeAt(0) as FakeVmServiceRequest;
|
||||
expect(request, isA<Map<String, Object>>()
|
||||
.having((Map<String, Object> request) => request['method'], 'method', fakeRequest.method)
|
||||
.having((Map<String, Object> request) => request['params'], 'args', fakeRequest.args)
|
||||
);
|
||||
if (fakeRequest.close) {
|
||||
unawaited(_vmService.dispose());
|
||||
expect(_requests, isEmpty);
|
||||
return;
|
||||
}
|
||||
if (fakeRequest.errorCode == null) {
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'id': request['id'],
|
||||
'result': fakeRequest.jsonResponse ?? <String, Object>{'type': 'Success'},
|
||||
}));
|
||||
} else {
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'id': request['id'],
|
||||
'error': <String, Object>{
|
||||
'code': fakeRequest.errorCode,
|
||||
}
|
||||
}));
|
||||
}
|
||||
_applyStreamListen();
|
||||
});
|
||||
}
|
||||
|
||||
final List<VmServiceExpectation> _requests;
|
||||
final StreamController<String> _input = StreamController<String>();
|
||||
final StreamController<String> _output = StreamController<String>();
|
||||
|
||||
FlutterVmService get vmService => _vmService;
|
||||
FlutterVmService _vmService;
|
||||
|
||||
|
||||
bool get hasRemainingExpectations => _requests.isNotEmpty;
|
||||
|
||||
// remove FakeStreamResponse objects from _requests until it is empty
|
||||
// or until we hit a FakeRequest
|
||||
void _applyStreamListen() {
|
||||
while (_requests.isNotEmpty && !_requests.first.isRequest) {
|
||||
final FakeVmServiceStreamResponse response = _requests.removeAt(0) as FakeVmServiceStreamResponse;
|
||||
_input.add(json.encode(<String, Object>{
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'streamNotify',
|
||||
'params': <String, Object>{
|
||||
'streamId': response.streamId,
|
||||
'event': response.event.toJson(),
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class VmServiceExpectation {
|
||||
bool get isRequest;
|
||||
}
|
||||
|
||||
class FakeVmServiceRequest implements VmServiceExpectation {
|
||||
const FakeVmServiceRequest({
|
||||
@required this.method,
|
||||
this.args = const <String, Object>{},
|
||||
this.jsonResponse,
|
||||
this.errorCode,
|
||||
this.close = false,
|
||||
});
|
||||
|
||||
final String method;
|
||||
|
||||
/// When true, the vm service is automatically closed.
|
||||
final bool close;
|
||||
|
||||
/// If non-null, the error code for a [vm_service.RPCError] in place of a
|
||||
/// standard response.
|
||||
final int errorCode;
|
||||
final Map<String, Object> args;
|
||||
final Map<String, Object> jsonResponse;
|
||||
|
||||
@override
|
||||
bool get isRequest => true;
|
||||
}
|
||||
|
||||
class FakeVmServiceStreamResponse implements VmServiceExpectation {
|
||||
const FakeVmServiceStreamResponse({
|
||||
@required this.event,
|
||||
@required this.streamId,
|
||||
});
|
||||
|
||||
final vm_service.Event event;
|
||||
final String streamId;
|
||||
|
||||
@override
|
||||
bool get isRequest => false;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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 'dart:async';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:flutter_tools/src/base/context.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/base/user_messages.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/create.dart';
|
||||
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
|
||||
|
||||
export 'package:test_api/test_api.dart' hide test, isInstanceOf; // ignore: deprecated_member_use
|
||||
|
||||
CommandRunner<void> createTestCommandRunner([ FlutterCommand command ]) {
|
||||
final FlutterCommandRunner runner = TestFlutterCommandRunner();
|
||||
if (command != null) {
|
||||
runner.addCommand(command);
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
|
||||
/// Creates a flutter project in the [temp] directory using the
|
||||
/// [arguments] list if specified, or `--no-pub` if not.
|
||||
/// Returns the path to the flutter project.
|
||||
Future<String> createProject(Directory temp, { List<String> arguments }) async {
|
||||
arguments ??= <String>['--no-pub'];
|
||||
final String projectPath = globals.fs.path.join(temp.path, 'flutter_project');
|
||||
final CreateCommand command = CreateCommand();
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
await runner.run(<String>['create', ...arguments, projectPath]);
|
||||
// Created `.packages` since it's not created when the flag `--no-pub` is passed.
|
||||
globals.fs.file(globals.fs.path.join(projectPath, '.packages')).createSync();
|
||||
return projectPath;
|
||||
}
|
||||
|
||||
class TestFlutterCommandRunner extends FlutterCommandRunner {
|
||||
@override
|
||||
Future<void> runCommand(ArgResults topLevelResults) async {
|
||||
final Logger topLevelLogger = globals.logger;
|
||||
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{
|
||||
if (topLevelResults['verbose'] as bool)
|
||||
Logger: VerboseLogger(topLevelLogger),
|
||||
};
|
||||
return context.run<void>(
|
||||
overrides: contextOverrides.map<Type, Generator>((Type type, dynamic value) {
|
||||
return MapEntry<Type, Generator>(type, () => value);
|
||||
}),
|
||||
body: () {
|
||||
Cache.flutterRoot ??= Cache.defaultFlutterRoot(
|
||||
platform: globals.platform,
|
||||
fileSystem: globals.fs,
|
||||
userMessages: UserMessages(),
|
||||
);
|
||||
// For compatibility with tests that set this to a relative path.
|
||||
Cache.flutterRoot = globals.fs.path.normalize(globals.fs.path.absolute(Cache.flutterRoot));
|
||||
return super.runCommand(topLevelResults);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user