Dependency injection Attach command (#113227)
This commit is contained in:
@@ -13,13 +13,14 @@ import 'package:flutter_tools/src/base/dds.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/signals.dart';
|
||||
import 'package:flutter_tools/src/base/terminal.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/attach.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:flutter_tools/src/device_port_forwarder.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/ios/application_package.dart';
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/macos/macos_ipad_device.dart';
|
||||
@@ -38,6 +39,16 @@ import '../../src/context.dart';
|
||||
import '../../src/fake_devices.dart';
|
||||
import '../../src/test_flutter_command_runner.dart';
|
||||
|
||||
class FakeStdio extends Fake implements Stdio {
|
||||
@override
|
||||
bool stdinHasTerminal = false;
|
||||
}
|
||||
|
||||
class FakeProcessInfo extends Fake implements ProcessInfo {
|
||||
@override
|
||||
int maxRss = 0;
|
||||
}
|
||||
|
||||
void main() {
|
||||
tearDown(() {
|
||||
MacOSDesignedForIPadDevices.allowDiscovery = false;
|
||||
@@ -47,17 +58,25 @@ void main() {
|
||||
late StreamLogger logger;
|
||||
late FileSystem testFileSystem;
|
||||
late TestDeviceManager testDeviceManager;
|
||||
late Artifacts artifacts;
|
||||
late Stdio stdio;
|
||||
late Terminal terminal;
|
||||
late Signals signals;
|
||||
late Platform platform;
|
||||
late ProcessInfo processInfo;
|
||||
|
||||
setUp(() {
|
||||
Cache.disableLocking();
|
||||
logger = StreamLogger();
|
||||
testFileSystem = MemoryFileSystem(
|
||||
style: globals.platform.isWindows
|
||||
? FileSystemStyle.windows
|
||||
: FileSystemStyle.posix,
|
||||
);
|
||||
platform = FakePlatform();
|
||||
testFileSystem = MemoryFileSystem.test();
|
||||
testFileSystem.directory('lib').createSync();
|
||||
testFileSystem.file(testFileSystem.path.join('lib', 'main.dart')).createSync();
|
||||
artifacts = Artifacts.test();
|
||||
stdio = FakeStdio();
|
||||
terminal = FakeTerminal();
|
||||
signals = Signals.test();
|
||||
processInfo = FakeProcessInfo();
|
||||
testDeviceManager = TestDeviceManager(logger: BufferLogger.test());
|
||||
});
|
||||
|
||||
@@ -102,7 +121,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand()).run(<String>['attach']);
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(<String>['attach']);
|
||||
await completer.future;
|
||||
|
||||
expect(portForwarder.devicePort, devicePort);
|
||||
@@ -137,7 +165,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand()).run(<String>['attach']);
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(<String>['attach']);
|
||||
await completer.future;
|
||||
|
||||
expect(portForwarder.devicePort, devicePort);
|
||||
@@ -161,7 +198,16 @@ void main() {
|
||||
return fakeLogReader;
|
||||
};
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
expect(() => createTestCommandRunner(AttachCommand()).run(<String>['attach']), throwsToolExit());
|
||||
expect(() => createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(<String>['attach']), throwsToolExit());
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => testFileSystem,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
@@ -197,6 +243,14 @@ void main() {
|
||||
|
||||
final AttachCommand command = AttachCommand(
|
||||
hotRunnerFactory: hotRunnerFactory,
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await createTestCommandRunner(command).run(<String>[
|
||||
'attach',
|
||||
@@ -232,7 +286,16 @@ void main() {
|
||||
testUsingContext('exits when ipv6 is specified and debug-port is not', () async {
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
|
||||
final AttachCommand command = AttachCommand();
|
||||
final AttachCommand command = AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await expectLater(
|
||||
createTestCommandRunner(command).run(<String>['attach', '--ipv6']),
|
||||
throwsToolExit(
|
||||
@@ -254,7 +317,16 @@ void main() {
|
||||
};
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
|
||||
final AttachCommand command = AttachCommand();
|
||||
final AttachCommand command = AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await expectLater(
|
||||
createTestCommandRunner(command).run(<String>['attach', '--observatory-port', '100']),
|
||||
throwsToolExit(
|
||||
@@ -294,7 +366,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand())
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
))
|
||||
.run(<String>['attach', '--debug-port', '$devicePort']);
|
||||
await completer.future;
|
||||
expect(portForwarder.devicePort, devicePort);
|
||||
@@ -320,7 +401,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand())
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
))
|
||||
.run(<String>['attach', '--debug-port', '$devicePort', '--ipv6']);
|
||||
await completer.future;
|
||||
|
||||
@@ -347,7 +437,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand()).run(
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(
|
||||
<String>[
|
||||
'attach',
|
||||
'--debug-port',
|
||||
@@ -383,7 +482,16 @@ void main() {
|
||||
completer.complete();
|
||||
}
|
||||
});
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand()).run(
|
||||
final Future<void> task = createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(
|
||||
<String>[
|
||||
'attach',
|
||||
'--debug-port',
|
||||
@@ -411,7 +519,16 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('exits when no device connected', () async {
|
||||
final AttachCommand command = AttachCommand();
|
||||
final AttachCommand command = AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await expectLater(
|
||||
createTestCommandRunner(command).run(<String>['attach']),
|
||||
throwsToolExit(),
|
||||
@@ -426,7 +543,16 @@ void main() {
|
||||
testUsingContext('fails when targeted device is not Android with --device-user', () async {
|
||||
final FakeIOSDevice device = FakeIOSDevice();
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
expect(createTestCommandRunner(AttachCommand()).run(<String>[
|
||||
expect(createTestCommandRunner(AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
)).run(<String>[
|
||||
'attach',
|
||||
'--device-user',
|
||||
'10',
|
||||
@@ -438,7 +564,16 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('exits when multiple devices connected', () async {
|
||||
final AttachCommand command = AttachCommand();
|
||||
final AttachCommand command = AttachCommand(
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
testDeviceManager.devices = <Device>[
|
||||
FakeAndroidDevice(id: 'xx1'),
|
||||
FakeAndroidDevice(id: 'yy2'),
|
||||
@@ -478,7 +613,17 @@ void main() {
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
testFileSystem.file('lib/main.dart').createSync();
|
||||
|
||||
final AttachCommand command = AttachCommand(hotRunnerFactory: hotRunnerFactory);
|
||||
final AttachCommand command = AttachCommand(
|
||||
hotRunnerFactory: hotRunnerFactory,
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await expectLater(createTestCommandRunner(command).run(<String>[
|
||||
'attach',
|
||||
]), throwsToolExit(message: 'Lost connection to device.'));
|
||||
@@ -509,7 +654,17 @@ void main() {
|
||||
testDeviceManager.devices = <Device>[device];
|
||||
testFileSystem.file('lib/main.dart').createSync();
|
||||
|
||||
final AttachCommand command = AttachCommand(hotRunnerFactory: hotRunnerFactory);
|
||||
final AttachCommand command = AttachCommand(
|
||||
hotRunnerFactory: hotRunnerFactory,
|
||||
artifacts: artifacts,
|
||||
stdio: stdio,
|
||||
logger: logger,
|
||||
terminal: terminal,
|
||||
signals: signals,
|
||||
platform: platform,
|
||||
processInfo: processInfo,
|
||||
fileSystem: testFileSystem,
|
||||
);
|
||||
await expectLater(createTestCommandRunner(command).run(<String>[
|
||||
'attach',
|
||||
]), throwsA(isA<vm_service.RPCError>()));
|
||||
@@ -709,7 +864,7 @@ class StreamLogger extends Logger {
|
||||
bool get hasTerminal => false;
|
||||
|
||||
@override
|
||||
void clear() => _log('[stdout] ${globals.terminal.clearScreen()}\n');
|
||||
void clear() => _log('[stdout] ${terminal.clearScreen()}\n');
|
||||
|
||||
@override
|
||||
Terminal get terminal => Terminal.test();
|
||||
@@ -936,4 +1091,7 @@ class FakeTerminal extends Fake implements AnsiTerminal {
|
||||
|
||||
@override
|
||||
final bool stdinHasTerminal;
|
||||
|
||||
@override
|
||||
bool usesTerminalUi = false;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,16 @@
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/artifacts.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/signals.dart';
|
||||
import 'package:flutter_tools/src/base/terminal.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/build_system/build_system.dart';
|
||||
import 'package:flutter_tools/src/commands/attach.dart';
|
||||
import 'package:flutter_tools/src/commands/build.dart';
|
||||
import 'package:flutter_tools/src/commands/build_aar.dart';
|
||||
@@ -16,17 +25,33 @@ import 'package:flutter_tools/src/commands/build_linux.dart';
|
||||
import 'package:flutter_tools/src/commands/build_macos.dart';
|
||||
import 'package:flutter_tools/src/commands/build_web.dart';
|
||||
import 'package:flutter_tools/src/commands/build_windows.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:test/fake.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/context.dart';
|
||||
import '../../src/fakes.dart';
|
||||
|
||||
class FakeTerminal extends Fake implements AnsiTerminal {
|
||||
FakeTerminal({this.stdinHasTerminal = true});
|
||||
|
||||
@override
|
||||
final bool stdinHasTerminal;
|
||||
}
|
||||
|
||||
class FakeProcessInfo extends Fake implements ProcessInfo {
|
||||
@override
|
||||
int maxRss = 0;
|
||||
}
|
||||
|
||||
void main() {
|
||||
testUsingContext('All build commands support null safety options', () {
|
||||
final FileSystem fileSystem = MemoryFileSystem.test();
|
||||
final Platform platform = FakePlatform();
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final List<FlutterCommand> commands = <FlutterCommand>[
|
||||
BuildWindowsCommand(),
|
||||
BuildLinuxCommand(operatingSystemUtils: globals.os),
|
||||
BuildLinuxCommand(operatingSystemUtils: FakeOperatingSystemUtils()),
|
||||
BuildMacosCommand(verboseHelp: false),
|
||||
BuildWebCommand(verboseHelp: false),
|
||||
BuildApkCommand(),
|
||||
@@ -36,9 +61,22 @@ void main() {
|
||||
BuildAarCommand(verboseHelp: false),
|
||||
BuildIOSFrameworkCommand(
|
||||
verboseHelp: false,
|
||||
buildSystem: globals.buildSystem,
|
||||
buildSystem: FlutterBuildSystem(
|
||||
fileSystem: fileSystem,
|
||||
platform: platform,
|
||||
logger: logger,
|
||||
),
|
||||
),
|
||||
AttachCommand(
|
||||
artifacts: Artifacts.test(),
|
||||
stdio: FakeStdio(),
|
||||
logger: logger,
|
||||
terminal: FakeTerminal(),
|
||||
signals: Signals.test(),
|
||||
platform: platform,
|
||||
processInfo: FakeProcessInfo(),
|
||||
fileSystem: MemoryFileSystem.test(),
|
||||
),
|
||||
AttachCommand(),
|
||||
];
|
||||
|
||||
for (final FlutterCommand command in commands) {
|
||||
|
||||
Reference in New Issue
Block a user