no more mockito for fuchsia remote debug protocol (#74755)
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fuchsia_remote_debug_protocol/src/dart/dart_vm.dart';
|
||||
import 'package:test/fake.dart';
|
||||
import 'package:vm_service/vm_service.dart' as vms;
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import '../../common.dart';
|
||||
|
||||
@@ -17,41 +17,40 @@ void main() {
|
||||
});
|
||||
|
||||
test('null connector', () async {
|
||||
Future<vms.VmService> mockServiceFunction(
|
||||
Future<vms.VmService> fakeServiceFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
return Future<vms.VmService>(() => null);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockServiceFunction;
|
||||
fuchsiaVmServiceConnectionFunction = fakeServiceFunction;
|
||||
expect(await DartVm.connect(Uri.parse('http://this.whatever/ws')),
|
||||
equals(null));
|
||||
});
|
||||
|
||||
test('disconnect closes peer', () async {
|
||||
final MockVmService service = MockVmService();
|
||||
Future<vms.VmService> mockServiceFunction(
|
||||
final FakeVmService service = FakeVmService();
|
||||
Future<vms.VmService> fakeServiceFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
return Future<vms.VmService>(() => service);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockServiceFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://this.whatever/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeServiceFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://this.whatever/ws'));
|
||||
expect(vm, isNot(null));
|
||||
await vm.stop();
|
||||
verify(service.dispose());
|
||||
expect(service.disposed, true);
|
||||
});
|
||||
});
|
||||
|
||||
group('DartVm.getAllFlutterViews', () {
|
||||
MockVmService mockService;
|
||||
FakeVmService fakeService;
|
||||
|
||||
setUp(() {
|
||||
mockService = MockVmService();
|
||||
fakeService = FakeVmService();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
@@ -90,18 +89,16 @@ void main() {
|
||||
],
|
||||
};
|
||||
|
||||
Future<vms.VmService> mockVmConnectionFunction(
|
||||
Future<vms.VmService> fakeVmConnectionFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async =>
|
||||
vms.Response.parse(flutterViewCannedResponses));
|
||||
return Future<vms.VmService>(() => mockService);
|
||||
fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponses);
|
||||
return Future<vms.VmService>(() => fakeService);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
final List<FlutterView> views = await vm.getAllFlutterViews();
|
||||
expect(views.length, 3);
|
||||
@@ -148,18 +145,16 @@ void main() {
|
||||
],
|
||||
};
|
||||
|
||||
Future<vms.VmService> mockVmConnectionFunction(
|
||||
Future<vms.VmService> fakeVmConnectionFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async =>
|
||||
vms.Response.parse(flutterViewCannedResponses));
|
||||
return Future<vms.VmService>(() => mockService);
|
||||
fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponses);
|
||||
return Future<vms.VmService>(() => fakeService);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
final List<FlutterView> views = await vm.getAllFlutterViews();
|
||||
expect(views.length, 3);
|
||||
@@ -175,8 +170,7 @@ void main() {
|
||||
});
|
||||
|
||||
test('invalid flutter view missing ID', () async {
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingId =
|
||||
<String, dynamic>{
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingId = <String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
// Valid flutter view.
|
||||
<String, dynamic>{
|
||||
@@ -198,18 +192,16 @@ void main() {
|
||||
],
|
||||
};
|
||||
|
||||
Future<vms.VmService> mockVmConnectionFunction(
|
||||
Future<vms.VmService> fakeVmConnectionFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async =>
|
||||
vms.Response.parse(flutterViewCannedResponseMissingId));
|
||||
return Future<vms.VmService>(() => mockService);
|
||||
fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponseMissingId);
|
||||
return Future<vms.VmService>(() => fakeService);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
Future<void> failingFunction() async {
|
||||
await vm.getAllFlutterViews();
|
||||
@@ -251,29 +243,25 @@ void main() {
|
||||
}),
|
||||
];
|
||||
|
||||
Future<vms.VmService> mockVmConnectionFunction(
|
||||
Future<vms.VmService> fakeVmConnectionFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
when(mockService.getVM()).thenAnswer((_) async =>
|
||||
FakeVM(isolates: isolates));
|
||||
return Future<vms.VmService>(() => mockService);
|
||||
fakeService.vm = FakeVM(isolates: isolates);
|
||||
return Future<vms.VmService>(() => fakeService);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
final List<IsolateRef> matchingFlutterIsolates =
|
||||
await vm.getMainIsolatesByPattern('flutterBinary.cmx');
|
||||
final List<IsolateRef> matchingFlutterIsolates = await vm.getMainIsolatesByPattern('flutterBinary.cmx');
|
||||
expect(matchingFlutterIsolates.length, 1);
|
||||
final List<IsolateRef> allIsolates = await vm.getMainIsolatesByPattern('');
|
||||
expect(allIsolates.length, 4);
|
||||
});
|
||||
|
||||
test('invalid flutter view missing ID', () async {
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingIsolateName =
|
||||
<String, dynamic>{
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingIsolateName = <String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
// Missing isolate name.
|
||||
<String, dynamic>{
|
||||
@@ -289,18 +277,16 @@ void main() {
|
||||
],
|
||||
};
|
||||
|
||||
Future<vms.VmService> mockVmConnectionFunction(
|
||||
Future<vms.VmService> fakeVmConnectionFunction(
|
||||
Uri uri, {
|
||||
Duration timeout,
|
||||
}) {
|
||||
when(mockService.callMethod(any)).thenAnswer((_) async =>
|
||||
vms.Response.parse(flutterViewCannedResponseMissingIsolateName));
|
||||
return Future<vms.VmService>(() => mockService);
|
||||
fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponseMissingIsolateName);
|
||||
return Future<vms.VmService>(() => fakeService);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction;
|
||||
final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
Future<void> failingFunction() async {
|
||||
await vm.getAllFlutterViews();
|
||||
@@ -312,7 +298,30 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
class MockVmService extends Mock implements vms.VmService {}
|
||||
class FakeVmService extends Fake implements vms.VmService {
|
||||
bool disposed = false;
|
||||
vms.Response flutterListViews;
|
||||
vms.VM vm;
|
||||
|
||||
@override
|
||||
Future<vms.VM> getVM() async => vm;
|
||||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<vms.Response> callMethod(String method, {String isolateId, Map<String, dynamic> args}) async {
|
||||
if (method == '_flutter.listViews') {
|
||||
return flutterListViews;
|
||||
}
|
||||
throw UnimplementedError(method);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onDone;
|
||||
}
|
||||
|
||||
class FakeVM extends Fake implements vms.VM {
|
||||
FakeVM({
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io' show ProcessResult;
|
||||
import 'dart:convert';
|
||||
import 'dart:io' show ProcessResult, systemEncoding;
|
||||
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:fuchsia_remote_debug_protocol/src/runners/ssh_command_runner.dart';
|
||||
import 'package:test/fake.dart';
|
||||
|
||||
import '../../common.dart';
|
||||
|
||||
@@ -32,68 +33,59 @@ void main() {
|
||||
});
|
||||
|
||||
group('SshCommandRunner.run', () {
|
||||
MockProcessManager mockProcessManager;
|
||||
MockProcessResult mockProcessResult;
|
||||
FakeProcessManager fakeProcessManager;
|
||||
FakeProcessResult fakeProcessResult;
|
||||
SshCommandRunner runner;
|
||||
|
||||
setUp(() {
|
||||
mockProcessManager = MockProcessManager();
|
||||
mockProcessResult = MockProcessResult();
|
||||
when(mockProcessManager.run(any)).thenAnswer(
|
||||
(_) => Future<MockProcessResult>.value(mockProcessResult));
|
||||
fakeProcessResult = FakeProcessResult();
|
||||
fakeProcessManager = FakeProcessManager()..fakeResult = fakeProcessResult;
|
||||
});
|
||||
|
||||
test('verify interface is appended to ipv6 address', () async {
|
||||
const String ipV6Addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
const String interface = 'eno1';
|
||||
runner = SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
fakeProcessManager,
|
||||
address: ipV6Addr,
|
||||
interface: interface,
|
||||
sshConfigPath: '/whatever',
|
||||
);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(captureAny)).captured.single as List<String>;
|
||||
expect(passedCommand, contains('$ipV6Addr%$interface'));
|
||||
fakeProcessResult.stdout = 'somestuff';
|
||||
await runner.run('ls /whatever');
|
||||
expect(fakeProcessManager.runCommands.single, contains('$ipV6Addr%$interface'));
|
||||
});
|
||||
|
||||
test('verify no percentage symbol is added when no ipv6 interface', () async {
|
||||
const String ipV6Addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
runner = SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
fakeProcessManager,
|
||||
address: ipV6Addr,
|
||||
);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
fakeProcessResult.stdout = 'somestuff';
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(captureAny)).captured.single as List<String>;
|
||||
expect(passedCommand, contains(ipV6Addr));
|
||||
expect(fakeProcessManager.runCommands.single, contains(ipV6Addr));
|
||||
});
|
||||
|
||||
test('verify commands are split into multiple lines', () async {
|
||||
const String addr = '192.168.1.1';
|
||||
runner = SshCommandRunner.withProcessManager(mockProcessManager,
|
||||
runner = SshCommandRunner.withProcessManager(fakeProcessManager,
|
||||
address: addr);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('''
|
||||
fakeProcessResult.stdout = '''
|
||||
this
|
||||
has
|
||||
four
|
||||
lines''');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
lines''';
|
||||
final List<String> result = await runner.run('oihaw');
|
||||
expect(result, hasLength(4));
|
||||
});
|
||||
|
||||
test('verify exception on nonzero process result exit code', () async {
|
||||
const String addr = '192.168.1.1';
|
||||
runner = SshCommandRunner.withProcessManager(mockProcessManager,
|
||||
runner = SshCommandRunner.withProcessManager(fakeProcessManager,
|
||||
address: addr);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('whatever');
|
||||
when(mockProcessResult.exitCode).thenReturn(1);
|
||||
fakeProcessResult.stdout = 'whatever';
|
||||
fakeProcessResult.exitCode = 1;
|
||||
Future<void> failingFunction() async {
|
||||
await runner.run('oihaw');
|
||||
}
|
||||
@@ -105,15 +97,13 @@ void main() {
|
||||
const String addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
const String config = '/this/that/this/and/uh';
|
||||
runner = SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
fakeProcessManager,
|
||||
address: addr,
|
||||
sshConfigPath: config,
|
||||
);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
fakeProcessResult.stdout = 'somestuff';
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(captureAny)).captured.single as List<String>;
|
||||
final List<String> passedCommand = fakeProcessManager.runCommands.single as List<String>;
|
||||
expect(passedCommand, contains('-F'));
|
||||
final int indexOfFlag = passedCommand.indexOf('-F');
|
||||
final String passedConfig = passedCommand[indexOfFlag + 1];
|
||||
@@ -123,20 +113,44 @@ void main() {
|
||||
test('verify config is excluded correctly', () async {
|
||||
const String addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
runner = SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
fakeProcessManager,
|
||||
address: addr,
|
||||
);
|
||||
when<dynamic>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
fakeProcessResult.stdout = 'somestuff';
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(captureAny)).captured.single as List<String>;
|
||||
final List<String> passedCommand = fakeProcessManager.runCommands.single as List<String>;
|
||||
final int indexOfFlag = passedCommand.indexOf('-F');
|
||||
expect(indexOfFlag, equals(-1));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
class FakeProcessManager extends Fake implements ProcessManager {
|
||||
FakeProcessResult fakeResult;
|
||||
|
||||
class MockProcessResult extends Mock implements ProcessResult {}
|
||||
List<List<dynamic>> runCommands = <List<dynamic>>[];
|
||||
|
||||
@override
|
||||
Future<ProcessResult> run(List<dynamic> command, {
|
||||
String workingDirectory,
|
||||
Map<String, String> environment,
|
||||
bool includeParentEnvironment = true,
|
||||
bool runInShell = false,
|
||||
Encoding stdoutEncoding = systemEncoding,
|
||||
Encoding stderrEncoding = systemEncoding,
|
||||
}) async {
|
||||
runCommands.add(command);
|
||||
return fakeResult;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeProcessResult extends Fake implements ProcessResult {
|
||||
@override
|
||||
int exitCode = 0;
|
||||
|
||||
@override
|
||||
dynamic stdout;
|
||||
|
||||
@override
|
||||
dynamic stderr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user