From d20242b703ebcdfbe1f60259403c5233c06a4122 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 19 Mar 2020 14:53:14 -0700 Subject: [PATCH] [flutter_tools] refactor ios install/uninstall to new file (#52891) --- .../test/general.shard/ios/devices_test.dart | 210 ------------------ .../ios/ios_device_install_test.dart | 186 ++++++++++++++++ 2 files changed, 186 insertions(+), 210 deletions(-) create mode 100644 packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index e454554b4a..fa1698092f 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -215,90 +215,6 @@ void main() { final bool result = await device.isAppInstalled(mockApp); expect(result, false); }); - - testWithoutContext('installApp() catches ProcessException from ios-deploy', () async { - const String bundlePath = '/path/to/bundle'; - final MockIOSApp mockApp = MockIOSApp(); - when(mockApp.id).thenReturn(appId); - when(mockApp.deviceBundlePath).thenReturn(bundlePath); - final MockDirectory mockDirectory = MockDirectory(); - when(mockFileSystem.directory(bundlePath)).thenReturn(mockDirectory); - when(mockDirectory.existsSync()).thenReturn(true); - when(mockDirectory.path).thenReturn(bundlePath); - fakeProcessManager = FakeProcessManager.list([ - FakeCommand( - command: const [ - iosDeployPath, - '--id', - deviceId, - '--bundle', - bundlePath, - '--no-wifi', - ], - onRun: () => throw const ProcessException('ios-deploy', []), - ) - ]); - iosDeploy = IOSDeploy( - artifacts: mockArtifacts, - cache: mockCache, - logger: logger, - platform: macPlatform, - processManager: fakeProcessManager, - ); - device = IOSDevice( - deviceId, - artifacts: mockArtifacts, - fileSystem: mockFileSystem, - logger: logger, - platform: macPlatform, - iosDeploy: iosDeploy, - name: 'iPhone 1', - sdkVersion: '13.3', - cpuArchitecture: DarwinArch.arm64, - ); - - final bool result = await device.installApp(mockApp); - expect(result, false); - }); - - testWithoutContext('uninstallApp() catches ProcessException from ios-deploy', () async { - final MockIOSApp mockApp = MockIOSApp(); - when(mockApp.id).thenReturn(appId); - fakeProcessManager = FakeProcessManager.list([ - FakeCommand( - command: const [ - iosDeployPath, - '--id', - deviceId, - '--uninstall_only', - '--bundle_id', - appId, - ], - onRun: () => throw const ProcessException('ios-deploy', []), - ) - ]); - iosDeploy = IOSDeploy( - artifacts: mockArtifacts, - cache: mockCache, - logger: logger, - platform: macPlatform, - processManager: fakeProcessManager, - ); - device = IOSDevice( - deviceId, - artifacts: mockArtifacts, - fileSystem: mockFileSystem, - logger: logger, - platform: macPlatform, - iosDeploy: iosDeploy, - name: 'iPhone 1', - sdkVersion: '13.3', - cpuArchitecture: DarwinArch.arm64, - ); - - final bool result = await device.uninstallApp(mockApp); - expect(result, false); - }); }); group('.dispose()', () { @@ -645,132 +561,6 @@ void main() { }, ); }); - - group('Process calls', () { - const String bundlePath = '/path/to/bundle'; - FileSystem fs; - MockDirectory directory; - MockIOSApp mockApp; - MockArtifacts mockArtifacts; - MockCache mockCache; - MockFileSystem mockFileSystem; - MockProcessManager mockProcessManager; - Logger logger; - MockPlatform mockPlatform; - const String iosDeployPath = '/path/to/ios-deploy'; - const String appId = '789'; - const String deviceId = '123'; - const MapEntry libraryEntry = MapEntry( - 'DYLD_LIBRARY_PATH', - '/path/to/libraries', - ); - IOSDeploy iosDeploy; - - setUp(() { - mockFileSystem = MockFileSystem(); - directory = MockDirectory(); - when(mockFileSystem.directory(bundlePath)).thenReturn(directory); - - mockApp = MockIOSApp(); - when(mockApp.id).thenReturn(appId); - when(mockApp.deviceBundlePath).thenReturn(bundlePath); - when(directory.existsSync()).thenReturn(true); - when(directory.path).thenReturn(bundlePath); - - mockArtifacts = MockArtifacts(); - mockCache = MockCache(); - logger = BufferLogger.test(); - mockPlatform = MockPlatform(); - when(mockPlatform.environment).thenReturn({}); - when(mockPlatform.isMacOS).thenReturn(true); - when( - mockArtifacts.getArtifactPath( - Artifact.iosDeploy, - platform: anyNamed('platform'), - ), - ).thenReturn(iosDeployPath); - mockProcessManager = MockProcessManager(); - iosDeploy = IOSDeploy( - artifacts: mockArtifacts, - cache: mockCache, - logger: logger, - platform: mockPlatform, - processManager: mockProcessManager, - ); - when(mockCache.dyLdLibEntry).thenReturn(libraryEntry); - mockFileSystem = MockFileSystem(); - final MemoryFileSystem memoryFileSystem = MemoryFileSystem(); - when(mockFileSystem.currentDirectory) - .thenReturn(memoryFileSystem.currentDirectory); - }); - - testWithoutContext('installApp() calls ios-deploy', () async { - final FakeProcessManager fakeProcessManager = FakeProcessManager.list([ - const FakeCommand(command: [ - iosDeployPath, - '--id', - deviceId, - '--bundle', - bundlePath, - '--no-wifi', - ]), - ]); - iosDeploy = IOSDeploy( - artifacts: mockArtifacts, - cache: mockCache, - logger: logger, - platform: mockPlatform, - processManager: fakeProcessManager, - ); - when(mockFileSystem.directory(bundlePath)).thenReturn(directory); - final IOSDevice device = IOSDevice( - deviceId, - name: 'iPhone 1', - fileSystem: mockFileSystem, - sdkVersion: '13.3', - cpuArchitecture: DarwinArch.arm64, - logger: logger, - platform: mockPlatform, - artifacts: mockArtifacts, - iosDeploy: iosDeploy, - ); - - await device.installApp(mockApp); - }); - - testWithoutContext('uninstallApp() calls ios-deploy', () async { - final FakeProcessManager fakeProcessManager = FakeProcessManager.list([ - const FakeCommand(command: [ - iosDeployPath, - '--id', - deviceId, - '--uninstall_only', - '--bundle_id', - appId, - ]), - ]); - iosDeploy = IOSDeploy( - artifacts: mockArtifacts, - cache: mockCache, - logger: logger, - platform: mockPlatform, - processManager: fakeProcessManager, - ); - final IOSDevice device = IOSDevice( - deviceId, - name: 'iPhone 1', - fileSystem: fs, - sdkVersion: '13.3', - cpuArchitecture: DarwinArch.arm64, - logger: logger, - platform: mockPlatform, - artifacts: mockArtifacts, - iosDeploy: iosDeploy, - ); - - await device.uninstallApp(mockApp); - }); - }); }); group('pollingGetDevices', () { diff --git a/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart b/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart new file mode 100644 index 0000000000..a79b0f75f1 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/ios/ios_device_install_test.dart @@ -0,0 +1,186 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:file/memory.dart'; +import 'package:flutter_tools/src/application_package.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/build_info.dart'; +import 'package:flutter_tools/src/cache.dart'; +import 'package:flutter_tools/src/ios/devices.dart'; +import 'package:flutter_tools/src/ios/ios_deploy.dart'; +import 'package:meta/meta.dart'; +import 'package:mockito/mockito.dart'; +import 'package:platform/platform.dart'; + +import '../../src/common.dart'; +import '../../src/context.dart'; + +const Map kDyLdLibEntry = { + 'DYLD_LIBRARY_PATH': '/path/to/libs', +}; + +void main() { + testWithoutContext('IOSDevice.installApp calls ios-deploy correctly', () async { + final FileSystem fileSystem = MemoryFileSystem.test(); + final IOSApp iosApp = PrebuiltIOSApp( + projectBundleId: 'app', + bundleDir: fileSystem.currentDirectory, + ); + final FakeProcessManager processManager = FakeProcessManager.list([ + const FakeCommand(command: [ + 'ios-deploy', + '--id', + '1234', + '--bundle', + '/', + '--no-wifi', + ], environment: { + 'PATH': '/usr/bin:null', + ...kDyLdLibEntry, + }) + ]); + final IOSDevice device = setUpIOSDevice( + processManager: processManager, + fileSystem: fileSystem, + ); + final bool wasInstalled = await device.installApp(iosApp); + + expect(wasInstalled, true); + expect(processManager.hasRemainingExpectations, false); + }); + + testWithoutContext('IOSDevice.uninstallApp calls ios-deploy correctly', () async { + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final FakeProcessManager processManager = FakeProcessManager.list([ + const FakeCommand(command: [ + 'ios-deploy', + '--id', + '1234', + '--uninstall_only', + '--bundle_id', + 'app', + ], environment: { + 'PATH': '/usr/bin:null', + ...kDyLdLibEntry, + }) + ]); + final IOSDevice device = setUpIOSDevice(processManager: processManager); + final bool wasUninstalled = await device.uninstallApp(iosApp); + + expect(wasUninstalled, true); + expect(processManager.hasRemainingExpectations, false); + }); + + testWithoutContext('IOSDevice.isAppInstalled catches ProcessException from ios-deploy', () async { + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand(command: const [ + 'ios-deploy', + '--id', + '1234', + '--exists', + '--bundle_id', + 'app', + ], environment: const { + 'PATH': '/usr/bin:null', + ...kDyLdLibEntry, + }, onRun: () { + throw const ProcessException('ios-deploy', []); + }) + ]); + final IOSDevice device = setUpIOSDevice(processManager: processManager); + final bool isAppInstalled = await device.isAppInstalled(iosApp); + + expect(isAppInstalled, false); + expect(processManager.hasRemainingExpectations, false); + }); + + testWithoutContext('IOSDevice.installApp catches ProcessException from ios-deploy', () async { + final FileSystem fileSystem = MemoryFileSystem.test(); + final IOSApp iosApp = PrebuiltIOSApp( + projectBundleId: 'app', + bundleDir: fileSystem.currentDirectory, + ); + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand(command: const [ + 'ios-deploy', + '--id', + '1234', + '--bundle', + '/', + '--no-wifi', + ], environment: const { + 'PATH': '/usr/bin:null', + ...kDyLdLibEntry, + }, onRun: () { + throw const ProcessException('ios-deploy', []); + }) + ]); + final IOSDevice device = setUpIOSDevice(processManager: processManager); + final bool wasAppInstalled = await device.installApp(iosApp); + + expect(wasAppInstalled, false); + }); + + testWithoutContext('IOSDevice.uninstallApp catches ProcessException from ios-deploy', () async { + final IOSApp iosApp = PrebuiltIOSApp(projectBundleId: 'app'); + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand(command: const [ + 'ios-deploy', + '--id', + '1234', + '--uninstall_only', + '--bundle_id', + 'app', + ], environment: const { + 'PATH': '/usr/bin:null', + ...kDyLdLibEntry, + }, onRun: () { + throw const ProcessException('ios-deploy', []); + }) + ]); + final IOSDevice device = setUpIOSDevice(processManager: processManager); + final bool wasAppUninstalled = await device.uninstallApp(iosApp); + + expect(wasAppUninstalled, false); + }); +} + +IOSDevice setUpIOSDevice({ + @required ProcessManager processManager, + FileSystem fileSystem, +}) { + final FakePlatform platform = FakePlatform( + operatingSystem: 'macos', + environment: {}, + ); + final MockArtifacts artifacts = MockArtifacts(); + final MockCache cache = MockCache(); + when(cache.dyLdLibEntry).thenReturn(kDyLdLibEntry.entries.first); + when(artifacts.getArtifactPath(Artifact.iosDeploy, platform: anyNamed('platform'))) + .thenReturn('ios-deploy'); + return IOSDevice( + '1234', + name: 'iPhone 1', + logger: BufferLogger.test(), + fileSystem: fileSystem ?? MemoryFileSystem.test(), + sdkVersion: '13.3', + cpuArchitecture: DarwinArch.arm64, + platform: platform, + iosDeploy: IOSDeploy( + logger: BufferLogger.test(), + platform: platform, + processManager: processManager, + artifacts: artifacts, + cache: cache, + ), + artifacts: artifacts, + ); +} + +class MockArtifacts extends Mock implements Artifacts {} +class MockCache extends Mock implements Cache {}