Unverified Commit e11a1a83 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Remove mocks from fucshia_pm_test, reduce createMockProcess usage (#80321)

parent 7e24faf0
...@@ -13,6 +13,7 @@ import 'package:flutter_tools/src/base/file_system.dart'; ...@@ -13,6 +13,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build_apk.dart'; import 'package:flutter_tools/src/commands/build_apk.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
...@@ -22,7 +23,6 @@ import 'package:process/process.dart'; ...@@ -22,7 +23,6 @@ import 'package:process/process.dart';
import '../../src/android_common.dart'; import '../../src/android_common.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
import '../../src/mocks.dart' hide MockAndroidSdk;
import '../../src/test_flutter_command_runner.dart'; import '../../src/test_flutter_command_runner.dart';
void main() { void main() {
...@@ -537,3 +537,21 @@ class FakeAndroidSdk extends Fake implements AndroidSdk { ...@@ -537,3 +537,21 @@ class FakeAndroidSdk extends Fake implements AndroidSdk {
} }
class MockProcessManager extends Mock implements ProcessManager {} class MockProcessManager extends Mock implements ProcessManager {}
/// Creates a mock process that returns with the given [exitCode], [stdout] and [stderr].
Process createMockProcess({ int exitCode = 0, String stdout = '', String stderr = '' }) {
final Stream<List<int>> stdoutStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stdout),
]);
final Stream<List<int>> stderrStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stderr),
]);
final Process process = _MockBasicProcess();
when(process.stdout).thenAnswer((_) => stdoutStream);
when(process.stderr).thenAnswer((_) => stderrStream);
when(process.exitCode).thenAnswer((_) => Future<int>.value(exitCode));
return process;
}
class _MockBasicProcess extends Mock implements Process {}
...@@ -6,71 +6,66 @@ ...@@ -6,71 +6,66 @@
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/fuchsia/fuchsia_pm.dart'; import 'package:flutter_tools/src/fuchsia/fuchsia_pm.dart';
import 'package:flutter_tools/src/fuchsia/fuchsia_sdk.dart'; import 'package:flutter_tools/src/fuchsia/fuchsia_sdk.dart';
import 'package:mockito/mockito.dart'; import 'package:test/fake.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
import '../../src/mocks.dart'; import '../../src/fake_process_manager.dart';
void main() { void main() {
group('FuchsiaPM', () { group('FuchsiaPM', () {
File pm; File pm;
MockProcessManager mockProcessManager; FakeProcessManager fakeProcessManager;
MockFuchsiaArtifacts mockFuchsiaArtifacts; FakeFuchsiaArtifacts fakeFuchsiaArtifacts;
setUp(() { setUp(() {
pm = MemoryFileSystem.test().file('pm'); pm = MemoryFileSystem.test().file('pm');
mockFuchsiaArtifacts = MockFuchsiaArtifacts(); fakeFuchsiaArtifacts = FakeFuchsiaArtifacts(pm);
when(mockFuchsiaArtifacts.pm).thenReturn(pm); fakeProcessManager = FakeProcessManager.empty();
mockProcessManager = MockProcessManager();
}); });
testUsingContext('serve - IPv4 address', () async { testUsingContext('serve - IPv4 address', () async {
when(mockProcessManager.start(any)).thenAnswer((_) { fakeProcessManager.addCommand(const FakeCommand(command: <String>[
return Future<Process>.value(createMockProcess());
});
await FuchsiaPM().serve('<repo>', '127.0.0.1', 43819);
verify(mockProcessManager.start(<String>[
'pm', 'pm',
'serve', 'serve',
'-repo', '-repo',
'<repo>', '<repo>',
'-l', '-l',
'127.0.0.1:43819', '127.0.0.1:43819',
])).called(1); ]));
await FuchsiaPM().serve('<repo>', '127.0.0.1', 43819);
expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FuchsiaArtifacts: () => mockFuchsiaArtifacts, FuchsiaArtifacts: () => fakeFuchsiaArtifacts,
ProcessManager: () => mockProcessManager, ProcessManager: () => fakeProcessManager,
}); });
testUsingContext('serve - IPv6 address', () async { testUsingContext('serve - IPv6 address', () async {
when(mockProcessManager.start(any)).thenAnswer((_) { fakeProcessManager.addCommand(const FakeCommand(command: <String>[
return Future<Process>.value(createMockProcess());
});
await FuchsiaPM().serve('<repo>', 'fe80::ec4:7aff:fecc:ea8f%eno2', 43819);
verify(mockProcessManager.start(<String>[
'pm', 'pm',
'serve', 'serve',
'-repo', '-repo',
'<repo>', '<repo>',
'-l', '-l',
'[fe80::ec4:7aff:fecc:ea8f%eno2]:43819', '[fe80::ec4:7aff:fecc:ea8f%eno2]:43819',
])).called(1); ]));
await FuchsiaPM().serve('<repo>', 'fe80::ec4:7aff:fecc:ea8f%eno2', 43819);
expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FuchsiaArtifacts: () => mockFuchsiaArtifacts, FuchsiaArtifacts: () => fakeFuchsiaArtifacts,
ProcessManager: () => mockProcessManager, ProcessManager: () => fakeProcessManager,
}); });
}); });
} }
class MockFuchsiaArtifacts extends Mock implements FuchsiaArtifacts {} class FakeFuchsiaArtifacts extends Fake implements FuchsiaArtifacts {
class MockProcessManager extends Mock implements ProcessManager {} FakeFuchsiaArtifacts(this.pm);
@override
final File pm;
}
...@@ -191,6 +191,7 @@ abstract class FakeProcessManager implements ProcessManager { ...@@ -191,6 +191,7 @@ abstract class FakeProcessManager implements ProcessManager {
/// last command and verify its execution is successful, to ensure that all /// last command and verify its execution is successful, to ensure that all
/// the specified commands are actually called. /// the specified commands are actually called.
factory FakeProcessManager.list(List<FakeCommand> commands) = _SequenceProcessManager; factory FakeProcessManager.list(List<FakeCommand> commands) = _SequenceProcessManager;
factory FakeProcessManager.empty() => _SequenceProcessManager(<FakeCommand>[]);
FakeProcessManager._(); FakeProcessManager._();
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
// @dart = 2.8 // @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'package:flutter_tools/src/android/android_device.dart'; import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart' show AndroidSdk; import 'package:flutter_tools/src/android/android_sdk.dart' show AndroidSdk;
...@@ -156,24 +155,6 @@ _ProcessFactory flakyProcessFactory({ ...@@ -156,24 +155,6 @@ _ProcessFactory flakyProcessFactory({
}; };
} }
/// Creates a mock process that returns with the given [exitCode], [stdout] and [stderr].
Process createMockProcess({ int exitCode = 0, String stdout = '', String stderr = '' }) {
final Stream<List<int>> stdoutStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stdout),
]);
final Stream<List<int>> stderrStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stderr),
]);
final Process process = _MockBasicProcess();
when(process.stdout).thenAnswer((_) => stdoutStream);
when(process.stderr).thenAnswer((_) => stderrStream);
when(process.exitCode).thenAnswer((_) => Future<int>.value(exitCode));
return process;
}
class _MockBasicProcess extends Mock implements Process {}
class MockAndroidDevice extends Mock implements AndroidDevice { class MockAndroidDevice extends Mock implements AndroidDevice {
@override @override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm; Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment