From e4c8498795d20ca674d8b89f296863d595226409 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Tue, 26 Jan 2021 13:44:03 -0800 Subject: [PATCH] no more mockito for fuchsia remote debug protocol (#74755) --- .../pubspec.yaml | 3 +- .../test/fuchsia_remote_connection_test.dart | 199 +++++++++++------- .../test/src/dart/dart_vm_test.dart | 115 +++++----- .../src/runners/ssh_command_runner_test.dart | 94 +++++---- 4 files changed, 239 insertions(+), 172 deletions(-) diff --git a/packages/fuchsia_remote_debug_protocol/pubspec.yaml b/packages/fuchsia_remote_debug_protocol/pubspec.yaml index c6f65e78ff..66d62a54f0 100644 --- a/packages/fuchsia_remote_debug_protocol/pubspec.yaml +++ b/packages/fuchsia_remote_debug_protocol/pubspec.yaml @@ -17,7 +17,6 @@ dependencies: platform: 3.0.0-nullsafety.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" dev_dependencies: - mockito: 4.1.1 test: 1.16.0-nullsafety.16 _fe_analyzer_shared: 14.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" @@ -65,4 +64,4 @@ dev_dependencies: webkit_inspection_protocol: 0.7.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" yaml: 2.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" -# PUBSPEC CHECKSUM: 0795 +# PUBSPEC CHECKSUM: 384f diff --git a/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart b/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart index 0e1a3387e2..cc9072b794 100644 --- a/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart +++ b/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:mockito/mockito.dart'; import 'package:vm_service/vm_service.dart' as vms; +import 'package:test/fake.dart'; import 'package:fuchsia_remote_debug_protocol/fuchsia_remote_debug_protocol.dart'; @@ -11,8 +11,8 @@ import 'common.dart'; void main() { group('FuchsiaRemoteConnection.connect', () { - List forwardedPorts; - List mockVmServices; + List forwardedPorts; + List fakeVmServices; List uriConnections; setUp(() { @@ -58,31 +58,27 @@ void main() { }, ]; - forwardedPorts = []; - mockVmServices = []; + forwardedPorts = []; + fakeVmServices = []; uriConnections = []; - Future mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { return Future(() async { - final MockVmService service = MockVmService(); - mockVmServices.add(service); + final FakeVmService service = FakeVmService(); + fakeVmServices.add(service); uriConnections.add(uri); - when(service.callMethod('_flutter.listViews')) - // The local ports match the desired indices for now, so get the - // canned response from the URI port. - .thenAnswer((_) => Future( - () => vms.Response.parse(flutterViewCannedResponses[uri.port]))); + service.flutterListViews = vms.Response.parse(flutterViewCannedResponses[uri.port]); return service; }); } - fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction; + fuchsiaVmServiceConnectionFunction = fakeVmConnectionFunction; }); tearDown(() { - /// Most tests will mock out the port forwarding and connection + /// Most tests will fake out the port forwarding and connection /// functions. restoreFuchsiaPortForwardingFunction(); restoreVmServiceConnectionFunction(); @@ -90,37 +86,33 @@ void main() { test('end-to-end with three vm connections and flutter view query', () async { int port = 0; - Future mockPortForwardingFunction( + Future fakePortForwardingFunction( String address, int remotePort, [ String interface = '', String configFile, ]) { return Future(() { - final MockPortForwarder pf = MockPortForwarder(); + final FakePortForwarder pf = FakePortForwarder(); forwardedPorts.add(pf); - when(pf.port).thenReturn(port++); - when(pf.remotePort).thenReturn(remotePort); + pf.port = port++; + pf.remotePort = remotePort; return pf; }); } - fuchsiaPortForwardingFunction = mockPortForwardingFunction; - final MockSshCommandRunner mockRunner = MockSshCommandRunner(); + fuchsiaPortForwardingFunction = fakePortForwardingFunction; + final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner(); // Adds some extra junk to make sure the strings will be cleaned up. - when(mockRunner.run(argThat(startsWith('/bin/find')))).thenAnswer( - (_) => Future>.value( - ['/hub/blah/blah/blah/vmservice-port\n'])); - when(mockRunner.run(argThat(startsWith('/bin/ls')))).thenAnswer( - (_) => Future>.value( - ['123\n\n\n', '456 ', '789'])); - when(mockRunner.address).thenReturn('fe80::8eae:4cff:fef4:9247'); - when(mockRunner.interface).thenReturn('eno1'); + fakeRunner.findResponse = ['/hub/blah/blah/blah/vmservice-port\n']; + fakeRunner.lsResponse = ['123\n\n\n', '456 ', '789']; + fakeRunner.address = 'fe80::8eae:4cff:fef4:9247'; + fakeRunner.interface = 'eno1'; final FuchsiaRemoteConnection connection = - await FuchsiaRemoteConnection.connectWithSshCommandRunner(mockRunner); + await FuchsiaRemoteConnection.connectWithSshCommandRunner(fakeRunner); - // [mockPortForwardingFunction] will have returned three different + // [fakePortForwardingFunction] will have returned three different // forwarded ports, incrementing the port each time by one. (Just a sanity // check that the forwarding port was called). expect(forwardedPorts.length, 3); @@ -132,7 +124,7 @@ void main() { expect(forwardedPorts[2].port, 2); // VMs should be accessed via localhost ports given by - // [mockPortForwardingFunction]. + // [fakePortForwardingFunction]. expect(uriConnections[0], Uri(scheme:'ws', host:'[::1]', port:0, path:'/ws')); expect(uriConnections[1], @@ -154,44 +146,40 @@ void main() { // Ensure the ports are all closed after stop was called. await connection.stop(); - verify(forwardedPorts[0].stop()); - verify(forwardedPorts[1].stop()); - verify(forwardedPorts[2].stop()); + expect(forwardedPorts[0].stopped, true); + expect(forwardedPorts[1].stopped, true); + expect(forwardedPorts[2].stopped, true); }); test('end-to-end with three vms and remote open port', () async { int port = 0; - Future mockPortForwardingFunction( + Future fakePortForwardingFunction( String address, int remotePort, [ String interface = '', String configFile, ]) { return Future(() { - final MockPortForwarder pf = MockPortForwarder(); + final FakePortForwarder pf = FakePortForwarder(); forwardedPorts.add(pf); - when(pf.port).thenReturn(port++); - when(pf.remotePort).thenReturn(remotePort); - when(pf.openPortAddress).thenReturn('fe80::1:2%eno2'); + pf.port = port++; + pf.remotePort = remotePort; + pf.openPortAddress = 'fe80::1:2%eno2'; return pf; }); } - fuchsiaPortForwardingFunction = mockPortForwardingFunction; - final MockSshCommandRunner mockRunner = MockSshCommandRunner(); + fuchsiaPortForwardingFunction = fakePortForwardingFunction; + final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner(); // Adds some extra junk to make sure the strings will be cleaned up. - when(mockRunner.run(argThat(startsWith('/bin/find')))).thenAnswer( - (_) => Future>.value( - ['/hub/blah/blah/blah/vmservice-port\n'])); - when(mockRunner.run(argThat(startsWith('/bin/ls')))).thenAnswer( - (_) => Future>.value( - ['123\n\n\n', '456 ', '789'])); - when(mockRunner.address).thenReturn('fe80::8eae:4cff:fef4:9247'); - when(mockRunner.interface).thenReturn('eno1'); + fakeRunner.findResponse = ['/hub/blah/blah/blah/vmservice-port\n']; + fakeRunner.lsResponse = ['123\n\n\n', '456 ', '789']; + fakeRunner.address = 'fe80::8eae:4cff:fef4:9247'; + fakeRunner.interface = 'eno1'; final FuchsiaRemoteConnection connection = - await FuchsiaRemoteConnection.connectWithSshCommandRunner(mockRunner); + await FuchsiaRemoteConnection.connectWithSshCommandRunner(fakeRunner); - // [mockPortForwardingFunction] will have returned three different + // [fakePortForwardingFunction] will have returned three different // forwarded ports, incrementing the port each time by one. (Just a sanity // check that the forwarding port was called). expect(forwardedPorts.length, 3); @@ -203,7 +191,7 @@ void main() { expect(forwardedPorts[2].port, 2); // VMs should be accessed via the alternate adddress given by - // [mockPortForwardingFunction]. + // [fakePortForwardingFunction]. expect(uriConnections[0], Uri(scheme:'ws', host:'[fe80::1:2%25eno2]', port:0, path:'/ws')); expect(uriConnections[1], @@ -225,43 +213,39 @@ void main() { // Ensure the ports are all closed after stop was called. await connection.stop(); - verify(forwardedPorts[0].stop()); - verify(forwardedPorts[1].stop()); - verify(forwardedPorts[2].stop()); + expect(forwardedPorts[0].stopped, true); + expect(forwardedPorts[1].stopped, true); + expect(forwardedPorts[2].stopped, true); }); test('end-to-end with three vms and ipv4', () async { int port = 0; - Future mockPortForwardingFunction( + Future fakePortForwardingFunction( String address, int remotePort, [ String interface = '', String configFile, ]) { return Future(() { - final MockPortForwarder pf = MockPortForwarder(); + final FakePortForwarder pf = FakePortForwarder(); forwardedPorts.add(pf); - when(pf.port).thenReturn(port++); - when(pf.remotePort).thenReturn(remotePort); + pf.port = port++; + pf.remotePort = remotePort; return pf; }); } - fuchsiaPortForwardingFunction = mockPortForwardingFunction; - final MockSshCommandRunner mockRunner = MockSshCommandRunner(); + fuchsiaPortForwardingFunction = fakePortForwardingFunction; + final FakeSshCommandRunner fakeRunner = FakeSshCommandRunner(); // Adds some extra junk to make sure the strings will be cleaned up. - when(mockRunner.run(argThat(startsWith('/bin/find')))).thenAnswer( - (_) => Future>.value( - ['/hub/blah/blah/blah/vmservice-port\n'])); - when(mockRunner.run(argThat(startsWith('/bin/ls')))).thenAnswer( - (_) => Future>.value( - ['123\n\n\n', '456 ', '789'])); - when(mockRunner.address).thenReturn('196.168.1.4'); + fakeRunner.findResponse = ['/hub/blah/blah/blah/vmservice-port\n']; + fakeRunner.lsResponse = ['123\n\n\n', '456 ', '789']; + fakeRunner.address = '196.168.1.4'; final FuchsiaRemoteConnection connection = - await FuchsiaRemoteConnection.connectWithSshCommandRunner(mockRunner); + await FuchsiaRemoteConnection.connectWithSshCommandRunner(fakeRunner); - // [mockPortForwardingFunction] will have returned three different + // [fakePortForwardingFunction] will have returned three different // forwarded ports, incrementing the port each time by one. (Just a sanity // check that the forwarding port was called). expect(forwardedPorts.length, 3); @@ -294,9 +278,9 @@ void main() { // Ensure the ports are all closed after stop was called. await connection.stop(); - verify(forwardedPorts[0].stop()); - verify(forwardedPorts[1].stop()); - verify(forwardedPorts[2].stop()); + expect(forwardedPorts[0].stopped, true); + expect(forwardedPorts[1].stopped, true); + expect(forwardedPorts[2].stopped, true); }); test('env variable test without remote addr', () async { @@ -311,8 +295,69 @@ void main() { }); } -class MockSshCommandRunner extends Mock implements SshCommandRunner {} +class FakeSshCommandRunner extends Fake implements SshCommandRunner { + List findResponse; + List lsResponse; + @override + Future> run(String command) async { + if (command.startsWith('/bin/find')) { + return findResponse; + } + if (command.startsWith('/bin/ls')) { + return lsResponse; + } + throw UnimplementedError(command); + } -class MockPortForwarder extends Mock implements PortForwarder {} + @override + String interface; -class MockVmService extends Mock implements vms.VmService {} + @override + String address; + + @override + String get sshConfigPath => '~/.ssh'; +} + +class FakePortForwarder extends Fake implements PortForwarder { + @override + int port; + + @override + int remotePort; + + @override + String openPortAddress; + + bool stopped = false; + @override + Future stop() async { + stopped = true; + } +} + +class FakeVmService extends Fake implements vms.VmService { + bool disposed = false; + vms.Response flutterListViews; + + @override + Future dispose() async { + disposed = true; + } + + @override + Future callMethod(String method, {String isolateId, Map args}) async { + if (method == '_flutter.listViews') { + return flutterListViews; + } + throw UnimplementedError(method); + } + + @override + Future onDone; + + @override + Future getVersion() async { + return vms.Version(major: -1, minor: -1); + } +} diff --git a/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart b/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart index 358513666c..d5b95252a0 100644 --- a/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart +++ b/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart @@ -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 mockServiceFunction( + Future fakeServiceFunction( Uri uri, { Duration timeout, }) { return Future(() => 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 mockServiceFunction( + final FakeVmService service = FakeVmService(); + Future fakeServiceFunction( Uri uri, { Duration timeout, }) { return Future(() => 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 mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { - when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async => - vms.Response.parse(flutterViewCannedResponses)); - return Future(() => mockService); + fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponses); + return Future(() => 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 views = await vm.getAllFlutterViews(); expect(views.length, 3); @@ -148,18 +145,16 @@ void main() { ], }; - Future mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { - when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async => - vms.Response.parse(flutterViewCannedResponses)); - return Future(() => mockService); + fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponses); + return Future(() => 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 views = await vm.getAllFlutterViews(); expect(views.length, 3); @@ -175,8 +170,7 @@ void main() { }); test('invalid flutter view missing ID', () async { - final Map flutterViewCannedResponseMissingId = - { + final Map flutterViewCannedResponseMissingId = { 'views': >[ // Valid flutter view. { @@ -198,18 +192,16 @@ void main() { ], }; - Future mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { - when(mockService.callMethod('_flutter.listViews')).thenAnswer((_) async => - vms.Response.parse(flutterViewCannedResponseMissingId)); - return Future(() => mockService); + fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponseMissingId); + return Future(() => 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 failingFunction() async { await vm.getAllFlutterViews(); @@ -251,29 +243,25 @@ void main() { }), ]; - Future mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { - when(mockService.getVM()).thenAnswer((_) async => - FakeVM(isolates: isolates)); - return Future(() => mockService); + fakeService.vm = FakeVM(isolates: isolates); + return Future(() => 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 matchingFlutterIsolates = - await vm.getMainIsolatesByPattern('flutterBinary.cmx'); + final List matchingFlutterIsolates = await vm.getMainIsolatesByPattern('flutterBinary.cmx'); expect(matchingFlutterIsolates.length, 1); final List allIsolates = await vm.getMainIsolatesByPattern(''); expect(allIsolates.length, 4); }); test('invalid flutter view missing ID', () async { - final Map flutterViewCannedResponseMissingIsolateName = - { + final Map flutterViewCannedResponseMissingIsolateName = { 'views': >[ // Missing isolate name. { @@ -289,18 +277,16 @@ void main() { ], }; - Future mockVmConnectionFunction( + Future fakeVmConnectionFunction( Uri uri, { Duration timeout, }) { - when(mockService.callMethod(any)).thenAnswer((_) async => - vms.Response.parse(flutterViewCannedResponseMissingIsolateName)); - return Future(() => mockService); + fakeService.flutterListViews = vms.Response.parse(flutterViewCannedResponseMissingIsolateName); + return Future(() => 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 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 getVM() async => vm; + + @override + Future dispose() async { + disposed = true; + } + + @override + Future callMethod(String method, {String isolateId, Map args}) async { + if (method == '_flutter.listViews') { + return flutterListViews; + } + throw UnimplementedError(method); + } + + @override + Future onDone; +} class FakeVM extends Fake implements vms.VM { FakeVM({ diff --git a/packages/fuchsia_remote_debug_protocol/test/src/runners/ssh_command_runner_test.dart b/packages/fuchsia_remote_debug_protocol/test/src/runners/ssh_command_runner_test.dart index 59dd79471d..a0b5d5c218 100644 --- a/packages/fuchsia_remote_debug_protocol/test/src/runners/ssh_command_runner_test.dart +++ b/packages/fuchsia_remote_debug_protocol/test/src/runners/ssh_command_runner_test.dart @@ -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.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(mockProcessResult.stdout).thenReturn('somestuff'); - when(mockProcessResult.exitCode).thenReturn(0); - await runner.run('ls /whatever'); - final List passedCommand = - verify(mockProcessManager.run(captureAny)).captured.single as List; - 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(mockProcessResult.stdout).thenReturn('somestuff'); - when(mockProcessResult.exitCode).thenReturn(0); + fakeProcessResult.stdout = 'somestuff'; await runner.run('ls /whatever'); - final List passedCommand = - verify(mockProcessManager.run(captureAny)).captured.single as List; - 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(mockProcessResult.stdout).thenReturn(''' + fakeProcessResult.stdout = ''' this has four - lines'''); - when(mockProcessResult.exitCode).thenReturn(0); + lines'''; final List 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(mockProcessResult.stdout).thenReturn('whatever'); - when(mockProcessResult.exitCode).thenReturn(1); + fakeProcessResult.stdout = 'whatever'; + fakeProcessResult.exitCode = 1; Future 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(mockProcessResult.stdout).thenReturn('somestuff'); - when(mockProcessResult.exitCode).thenReturn(0); + fakeProcessResult.stdout = 'somestuff'; await runner.run('ls /whatever'); - final List passedCommand = - verify(mockProcessManager.run(captureAny)).captured.single as List; + final List passedCommand = fakeProcessManager.runCommands.single as List; 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(mockProcessResult.stdout).thenReturn('somestuff'); - when(mockProcessResult.exitCode).thenReturn(0); + fakeProcessResult.stdout = 'somestuff'; await runner.run('ls /whatever'); - final List passedCommand = - verify(mockProcessManager.run(captureAny)).captured.single as List; + final List passedCommand = fakeProcessManager.runCommands.single as List; 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> runCommands = >[]; + + @override + Future run(List command, { + String workingDirectory, + Map 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; +}