Replace MockXcode with Xcode.test in unit tests (#74777)

This commit is contained in:
Jenn Magder
2021-01-27 11:04:03 -08:00
committed by GitHub
parent c6a428d500
commit 675fd5540e
3 changed files with 141 additions and 121 deletions

View File

@@ -4,7 +4,6 @@
import 'dart:async';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/io.dart' show ProcessException, ProcessResult;
import 'package:flutter_tools/src/base/logger.dart';
@@ -43,10 +42,7 @@ void main() {
setUp(() {
mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
xcode = Xcode(
logger: logger,
platform: FakePlatform(operatingSystem: 'macos'),
fileSystem: MemoryFileSystem.test(),
xcode = Xcode.test(
processManager: processManager,
xcodeProjectInterpreter: mockXcodeProjectInterpreter,
);
@@ -78,25 +74,27 @@ void main() {
group('xcdevice', () {
XCDevice xcdevice;
MockXcode mockXcode;
Xcode xcode;
setUp(() {
mockXcode = MockXcode();
xcode = Xcode.test(
processManager: FakeProcessManager.any(),
xcodeProjectInterpreter: XcodeProjectInterpreter.test(
processManager: FakeProcessManager.any(),
),
);
xcdevice = XCDevice(
processManager: processManager,
logger: logger,
xcode: mockXcode,
xcode: xcode,
platform: null,
artifacts: Artifacts.test(),
cache: Cache.test(),
iproxy: IProxy.test(logger: logger, processManager: processManager),
);
when(mockXcode.xcrunCommand()).thenReturn(<String>['xcrun']);
});
testWithoutContext('available devices xcdevice fails', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
.thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
@@ -104,8 +102,6 @@ void main() {
});
testWithoutContext('diagnostics xcdevice fails', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
.thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
@@ -129,10 +125,8 @@ void main() {
});
testWithoutContext('isInstalledAndMeetsVersionCheck is false when not macOS', () {
final Xcode xcode = Xcode(
logger: logger,
final Xcode xcode = Xcode.test(
platform: FakePlatform(operatingSystem: 'windows'),
fileSystem: MemoryFileSystem.test(),
processManager: fakeProcessManager,
xcodeProjectInterpreter: mockXcodeProjectInterpreter,
);
@@ -151,10 +145,7 @@ void main() {
],
),
);
final Xcode xcode = Xcode(
logger: logger,
platform: FakePlatform(operatingSystem: 'macos'),
fileSystem: MemoryFileSystem.test(),
final Xcode xcode = Xcode.test(
processManager: fakeProcessManager,
xcodeProjectInterpreter: mockXcodeProjectInterpreter,
);
@@ -175,10 +166,7 @@ void main() {
exitCode: 1,
),
);
final Xcode xcode = Xcode(
logger: logger,
platform: FakePlatform(operatingSystem: 'macos'),
fileSystem: MemoryFileSystem.test(),
final Xcode xcode = Xcode.test(
processManager: fakeProcessManager,
xcodeProjectInterpreter: mockXcodeProjectInterpreter,
);
@@ -189,16 +177,11 @@ void main() {
group('macOS', () {
Xcode xcode;
FakePlatform platform;
setUp(() {
mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
when(mockXcodeProjectInterpreter.xcrunCommand()).thenReturn(<String>['xcrun']);
platform = FakePlatform(operatingSystem: 'macos');
xcode = Xcode(
logger: logger,
platform: platform,
fileSystem: MemoryFileSystem.test(),
xcode = Xcode.test(
processManager: fakeProcessManager,
xcodeProjectInterpreter: mockXcodeProjectInterpreter,
);
@@ -318,36 +301,13 @@ void main() {
});
testWithoutContext('isInstalledAndMeetsVersionCheck is false when not installed', () {
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['/usr/bin/xcode-select', '--print-path'],
stdout: '/Applications/Xcode8.0.app/Contents/Developer',
));
when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);
expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
expect(fakeProcessManager.hasRemainingExpectations, isFalse);
});
testWithoutContext('isInstalledAndMeetsVersionCheck is false when no xcode-select', () {
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['/usr/bin/xcode-select', '--print-path'],
exitCode: 127,
stderr: 'ERROR',
));
when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
expect(fakeProcessManager.hasRemainingExpectations, isFalse);
});
testWithoutContext('isInstalledAndMeetsVersionCheck is false when version not satisfied', () {
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['/usr/bin/xcode-select', '--print-path'],
stdout: '/Applications/Xcode8.0.app/Contents/Developer',
));
when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
when(mockXcodeProjectInterpreter.majorVersion).thenReturn(10);
when(mockXcodeProjectInterpreter.minorVersion).thenReturn(2);
@@ -358,10 +318,6 @@ void main() {
});
testWithoutContext('isInstalledAndMeetsVersionCheck is true when macOS and installed and version is satisfied', () {
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['/usr/bin/xcode-select', '--print-path'],
stdout: '/Applications/Xcode8.0.app/Contents/Developer',
));
when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
@@ -432,42 +388,59 @@ void main() {
});
});
group('xcdevice', () {
group('xcdevice not installed', () {
XCDevice xcdevice;
MockXcode mockXcode;
Xcode xcode;
setUp(() {
mockXcode = MockXcode();
xcode = Xcode.test(
processManager: FakeProcessManager.any(),
xcodeProjectInterpreter: XcodeProjectInterpreter.test(
processManager: FakeProcessManager.any(),
majorVersion: null, // Not installed.
),
);
xcdevice = XCDevice(
processManager: fakeProcessManager,
logger: logger,
xcode: mockXcode,
xcode: xcode,
platform: null,
artifacts: Artifacts.test(),
cache: Cache.test(),
iproxy: IProxy.test(logger: logger, processManager: fakeProcessManager),
);
when(mockXcode.xcrunCommand()).thenReturn(<String>['xcrun']);
});
group('installed', () {
testWithoutContext('Xcode not installed', () {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
expect(xcdevice.isInstalled, false);
});
testWithoutContext('Xcode not installed', () async {
expect(xcode.isInstalled, false);
expect(xcdevice.isInstalled, false);
expect(xcdevice.observedDeviceEvents(), isNull);
expect(logger.traceText, contains("Xcode not found. Run 'flutter doctor' for more information."));
expect(await xcdevice.getAvailableIOSDevices(), isEmpty);
expect(await xcdevice.getDiagnostics(), isEmpty);
});
});
group('xcdevice', () {
XCDevice xcdevice;
Xcode xcode;
setUp(() {
xcode = Xcode.test(processManager: FakeProcessManager.any());
xcdevice = XCDevice(
processManager: fakeProcessManager,
logger: logger,
xcode: xcode,
platform: null,
artifacts: Artifacts.test(),
cache: Cache.test(),
iproxy: IProxy.test(logger: logger, processManager: fakeProcessManager),
);
});
group('observe device events', () {
testWithoutContext('Xcode not installed', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
expect(xcdevice.observedDeviceEvents(), isNull);
expect(logger.traceText, contains("Xcode not found. Run 'flutter doctor' for more information."));
});
testUsingContext('relays events', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
fakeProcessManager.addCommand(const FakeCommand(
command: <String>[
'script',
@@ -516,16 +489,7 @@ void main() {
group('available devices', () {
final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
testWithoutContext('Xcode not installed', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
expect(await xcdevice.getAvailableIOSDevices(), isEmpty);
});
testUsingContext('returns devices', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
const String devicesOutput = '''
[
{
@@ -644,8 +608,6 @@ void main() {
});
testWithoutContext('uses timeout', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '20'],
stdout: '[]',
@@ -655,8 +617,6 @@ void main() {
});
testUsingContext('ignores "Preparing debugger support for iPhone" error', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
const String devicesOutput = '''
[
{
@@ -695,8 +655,6 @@ void main() {
});
testUsingContext('handles unknown architectures', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
const String devicesOutput = '''
[
{
@@ -742,16 +700,7 @@ void main() {
group('diagnostics', () {
final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
testWithoutContext('Xcode not installed', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
expect(await xcdevice.getDiagnostics(), isEmpty);
});
testUsingContext('uses cache', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
const String devicesOutput = '''
[
{
@@ -787,8 +736,6 @@ void main() {
});
testUsingContext('returns error message', () async {
when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
const String devicesOutput = '''
[
{
@@ -894,6 +841,5 @@ void main() {
});
}
class MockXcode extends Mock implements Xcode {}
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}